WebSocket with ESp32 w/ Node-RED and InfluxDB

ESP32 websocket

WebSocket is a communication protocol that provides full-duplex, bidirectional communication over a single, long-lived connection between clients and servers. In the context of IoT, WebSocket can be used for real-time data  By using WebSocket with ESP32, Node-RED, and InfluxDB, it is possible to create a real-time data streaming and visualization system. The ESP32 can be programmed to send data to the server using WebSocket protocol, which can then be processed and stored in InfluxDB using Node-RED. The data can then be visualized in real-time using InfluxDB’s powerful data visualization tools. Overall, using WebSocket with ESP32, Node-RED, and InfluxDB provides a powerful platform for building real-time IoT applications that require fast, efficient communication and data processing.

1. WebSocket with Node-RED:

ESP32 websocket

Node-RED comes with Out of the Box support for WebSocket. You get WS in and Out nodes. In node is something that gives you incoming messages and out node, as you can assume, is used to write to the socket.

The nodes can either be used as server or client. In this case we are going to use it as server. Both the cases you need to configure the nodes. To know more about how to use WS in Node-RED checkout the following video.

2. Send data from ESP32 [Arduino] to Node-RED over WebSocket:

To use WebSocket in Arduino, I have used ArduinoWebsockets(http://bit.ly/3LeAGVl) library. Also to parse JSON objects I have used ArduinoJson(http://bit.ly/3ZHI5Rs) library.  You can find the complete code here (https://github.com/WGLabz/Arduino-Node-RED-Websocket).  As of now the code simulates a Temperature and humidity sensor and send random values through WebSocket every x seconds, which is configurable, actually you can changes the code to set it from node-RED and persist it in Arduino. It also receives messages sent by Node-RED and if the message which is a JSON string ({data: 1 | 0}) with a data key which can have value 1 or 0, has data key then it changes the on-board LED status accordingly.

3. Storing data to InfluxDB from Node-RED

For timeseries data InfluxDB is an appropriate DB to use. Its also free to use. Node-RED by default does not support InfluxDB OOB. You need to install required libraries for that. I have used this one https://flows.nodered.org/node/node-red-contrib-influxdb. The data received from ESP32 is written to the InfluxDB database and can be queried and displayed in tools like Grafana or Node-RED dashboard. Here i am just storing the randomly generated Temperature and Humidity data to InfluxDB.

I hope this help you in someway or other. You can find links to everything that I have used in the description of respective videos in YouTube. There are lot of links so am not posting here.

Related posts

Leave a Comment