1

I have one central ESP32 as an access point which hosts a webserver. The goal is to display live sensor data from three more ESP32s on this webserver.

These other ESP32s are implemented as stations. The central ESP32 should be able to control the other ones. What is the best approach to manage the communication over WiFi?

I thought of using websockets and get variables, but how to set this up? Or are there better ways? Apart from the WiFi-communication, there are no restrictions, e.g. changing accesspoint to station or the webserver destination is possible.

ocrdu
  • 8,705
  • 21
  • 30
  • 42
Curious99
  • 113
  • 3
  • Welcome! Can I ask how quickly you need the responses? "live" data might be a millisecond or an hour or anything, dependong in what the sensor is measuring. Also: do you have a particualr noisy environment with lots of wifi traffic? – jonathanjo Nov 21 '22 at 10:13
  • 1
    Q2: And what kind of control do you need on the sensors? I'm wondering what work the central ESP does. – jonathanjo Nov 21 '22 at 10:46
  • Something like a 10 sec delay would be ok. The environment is not that noisy. The "control ESP32" should host the website. On the website are three buttons which control a led of each ESP32. Is there a better approach? Like do I even need the "control ESP32"? – Curious99 Nov 21 '22 at 15:10

3 Answers3

1

UDP is a bit easier to implement than TCP (websocket) as you don't need to care about implementing the connection and various error states.

You also don't need to implement the server aspect of it. The downside is that you have no confirmation the packets arrive, so it needs to be kept in mind while writing the code.

I'd probably go for some raw UDP packets to keep things simple and lightweight and if you are in a local network, you can also look at broadcasting.

Damien
  • 7,827
  • 1
  • 12
  • 29
  • Ok I got this. So basically my stations (and UDP clients) send the data to my ap. The ap then displays the data on the Webserver. And how does the "control" aspect work with this communication. Like how can I send data from ap to a station on a button press? – Curious99 Nov 21 '22 at 15:16
  • https://www.google.com/search?q=esp32+udp+communication – Damien Nov 22 '22 at 05:36
1

There are plenty of libraries out there for setting up raw TCP sockets on all your ESP32s; even the Arduino environment comes with example code for this. You can send messages, commands, and data using these sockets from one ESP32 to another. The rest of your code would have to parse what comes out of a socket and process the result.

You could also use UDP sockets in a similar way, but then arrival of messages isn't guaranteed and you would have to add a mechanism for that (checks and resends) yourself. UDP is easier to implement, but that doesn't matter much when the TCP socket's heavy lifting is done by a library anyway.

Websockets are best left to bidirectional communication between a webpage and a server.

If you really want a web interface (instead of buttons and a display, or a touchscreen), you could use websockets for communication between a web page and your access point/webserver ESP32, and use raw TCP for communication between that and the other ESP32s.

I put some example code for a webinterface that uses websockets on GitHub, but it is for the Arduino Nano 33 IoT (and similar ones). If it helps, feel free to re-use parts of the code.

ocrdu
  • 8,705
  • 21
  • 30
  • 42
1

If you are getting the data into a Web server, you presumably want to display that data on a Web browser, and to do that you'll probably be running a Javascript application on the browser.

One possibility is to use that browser code to fetch and decode the data directly from the remotes, if it is available in (say) JSON file format on the HTTP server on your remote ESP32s.

You can also use the same connection for control, so a data request to a remote unit might look like:

GET /data.json?rate=10000&samples=100&command=1

..where data.json is a file with live data, that can be generated on-the-fly by the remote ESP32 Web server, using the parameters given.

This concentrates the analysis & display code in one place (the browser Javascript) which minimises the complexity of code on the remote units, simplifies error handling, and makes the units easily testable, since you can just point a browser at their Web servers, to view the raw JSON data.

I have implemented a networked logic analyser system using this method, with ESP32 remotes, details are here

jayben
  • 1,247
  • 2
  • 5