How to run an OPC UA server on ESP32?

In an earlier post, i have briefly mentioned about OPC UA and what it is being used for. Thanks to open source projects and developers, it is possible to build an industrial communication network without any cost except the hardware.

If you want to download and use immediately then please move on this post. But if you are ok with building things from source then proceed 🙂 .

On this post, i will explain how i have compiled an OPC UA Server and make it run on ESP32 embedded development board. You can find the source here, or if you are interested in a ready to use one then please contact me over here. There are also other platforms that you can port the OPC UA server code but i chose ESP32, because of its dynamic development environment, called esp-idf and it has a built-in Wi-Fi.

OPC UA server function has been developed by using open62541 open source C stack, which lets you compile embedded servers as small as 100 kB.

Here are the steps to compile OPC UA Server on ESP32;

1 – Get the latest esp-idf:

2 – Get the opcua-esp32 under esp-idf examples:

3 – Plug the ESP32 development board you are using. I have used ESP32-EVB so if are also using it you will be able to control the on board relays without making any changes in the code.

4 – Configure the ESP32 with “menuconfig” command.

  • (python) idf.py menuconfig

Navigate to “Connection Configuration” to set the type of connection (Wi-Fi or Ethernet), Wi-Fi SSID, password and static IP configuration. Currently static IP can only be set for ethernet.

ESP-IDF menuconfig

You can see the config options you can set for your OPC UA server under “Connection Configuration” as;

OPCUA-ESP32 Connection Configuration

5 – Flash the build to the board.

  • (python) idf.py flash

6 – Run monitor to check if the board got IP and started the OPC UA Server. This is required if you have not set an IP address to ESP32 board via DHCP or not connecting over ethernet with a static IP address.

  • (python) idf.py monitor

7 – Run an OPC UA Client to connect to the server. I have been using UAExpert from Unified Automation.

After connecting properly, you will be able to browse/subscribe/control GPIO 32 (Relay-1 on ESP32-EVB) and GPIO 33 (Relay-2 on ESP32-EVB).

You can set the GPIO’s from “components/open62541lib/model.h”;

  • #define BLINK_GPIO 2
  • #define DHT22_GPIO 4
  • #define RELAY_0_GPIO 32
  • #define RELAY_1_GPIO 33

DHT22 temperature sensor and led blink gpio’s are not functional but can be used after few modifications.

So, now you got the cheapest opc ua server in your hand. It can even work on battery, which i haven’t tried yet. Enjoy controlling devices around, over an industrial grade communication…

If you are interested in a ready to use one, please contact me over here.

Useful Links and references:

10 thoughts on “How to run an OPC UA server on ESP32?

  1. Thanks, this is very good tutorial. Have you ever try to connect to this ESP32 OPC UA Server using PLC S7-1500 OPC UA client??

    Like

    1. Hello Tran,

      Thank you very much for your feedback.

      Yes, i have tried to connect over an OPC Client working on S7-1500 and it worked. But, it depends on your network configuration. For instance if there is a DHCP server with specified MAC Addresses
      ESP32 will not be able to get an IP Address, so OPC UA server will not start.

      If you try and could not connect please let me know by openning an issue in github. https://github.com/cmbahadir/opcua-esp32/issues

      Like

  2. Hello
    while trying to work on OPC UA, encountered the following error on idf.py build command,
    ‘idf.py’ is not recognized as an internal or external command,
    operable program or batch file

    Can you help me out what am i doing wrong ?

    Like

  3. Hello !
    Thanks for this great tutorial. One question : Do you think its possible to use this on a Lilygo T-Call 1.3 ESP ?
    It´s an ESP32 with GSM on board. I am trying to send an analog value from the ESP32 to WinCC via OPC UA.

    Like

    1. Hello Jens,

      Thank you for the positive feedback.

      I expect it to be compiled and flashed successfully on Lilygo T-Call board and connect over Wi-Fi. But ofcourse you need to configure the analog or digital peripherals according to your setup with Lilygo. And if you want to use OPC UA over GSM, it won’t work, to be honest i do not know if its even possible. 🙂

      Like

  4. Hello

    This is a Great tutorial and Very helpful to new user.
    I am now planning is to create an OPC UA server and I think i can do it with the help of this tutorial.
    My project including below.
    1) Want to read multiple sensors analog values and want to send the sensors data to the UaExpert client.
    2) Want to update the sensor values in every second.
    I want to confirm, do I need any additional board to read the sensor value?
    If I want to change the port number where do I need to change?

    Thank you for any kind of help.

    Regards,
    Rony

    Like

    1. Hello,

      I will try to answer your questions, but it is not possible to say that its going to work or not without calculating the required resources of your setup.

      —- Want to read multiple sensors analog values and want to send the sensors data to the UaExpert client.
      —- Want to update the sensor values in every second.
      Well actually it depends on the type of sensors and server board you are using. There are many examples for analog read and other protocols(I2C, SPI etc.) for ESP32. You can directly assign them as a data source in open62541.

      Analog read depends on two things one is the sampling rate and other is the quantization so depending on the desired specs of your setup, i guess ESP32 is pretty capable in terms of meeting your needs.

      —- Do I need any additional board to read the sensor value?
      So my opinion is, you should not need an extra board if its just a standard(commonly used) sensor.

      —- If I want to change the port number where do I need to change?
      Currently its not possible to change the port number(default 4840) without making the change and compiling the opcua-esp32 code. That would be a nice feature request maybe you should open it as an issue here;
      https://github.com/cmbahadir/opcua-esp32

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s