Mesh network node
|
The purpose of this project is to deploy classrooms with environmental sensors that measure metrics like temperature and humidity. Because of the large area that has to be covered, it is impossible to set up a conventional solution. The Wifi infrastructure of the school cannot be used for security reasons.
The solution to these problems is the deployment of a mesh network. The nodes in a mesh network don't only act as clients but also as repeaters. Every node will expand the network.
Centralized architecture | Mesh architecture |
---|---|
The painless mesh library enables us to set up this network without needing to worry about how the network is structured or managed. We only need to worry about our root node. The use of a root node is highly recommended and will improve the stability of the network.
The root node can be used as a bridge to another network. This functionality is important so we can send our data to Home Assistant. We will use the REST API of Home Assistant to transfer the sensor data. Our root has to be selected based on the connection strength to the access point.
Every node needs its unique identifier. It is important that this can be configured by the users of the sensors. To be able to set an identifier we set up a small access point and webserver on the ESP. When you connect to this access point you can enter a name in the captive portal. The configuration will be saved in the EEPROM so it is not lost when there is a loss of power.
Steps we execute:
ESPAsyncWebServer: We use this to be able to set up the webserver.
DNSServer: Used to make a captive portal. When you connect to the access point a webpage will pop up.
SPIFFS: Used for reading and writing data in EEPROM.
The Received Signal Strength Index (RSSI) is used to determine the best root node. The RSSI is the measurement of how strong the signal of a router or access point is to a device. The RSSI is a value between -100 and 0. An RSSI closer to 0 means a better connection.
Steps we execute:
WiFi: To scan for networks and retrieve the RSSI.
The mesh network uses its SSID and password. Every node that uses the same credentials will join the same mesh network. The use of delay() should be avoided. Using delay() will cause the mesh to fall apart.
The mesh.update() function should be executed as much as possible. When a message from the mesh network is received a callback will be executed.
painlessMesh: Mesh library.
When a mesh network is set up, a root has to be selected that will act as a bridge. All the nodes in the network will start broadcasting their RSSI values to each other. When a node receives a message with a better RSSI than its RSSI it will stop broadcasting its RSSI. Eventually, there will be only one node that is still broadcasting its RSSI. This node will be selected to be the root.
Steps we execute:
painlessMesh: Mesh library.
Only if the node is root!
When a node is declared as root it will connect to the access point. A REST API will also be set up so Home Assistant can send messages to the mesh network.
Connecting to the access point can be tricky because the mesh network is still running. Sometimes the connection fails, the node will be reset when this happens.
The root will start broadcasting a message telling all the nodes to send measurements to his address. This is also a message telling the nodes that the root is still alive.
WiFi: For the connection to the access point.
WebServer: Used for the REST API server.
ArduinoJson: To deserialize the JSON message of the REST API.
All the nodes will read the sensor data and start sending their messages to the root node. The messages are specifically formatted to ease deserialization. When the root receives these messages they will be posted to Home Assistant.
The JSON model we use is based on SenML which is designed for sending sensor data. This structure allows us to expand this Json without needing to change the deserialization function. Every part of the JSON is used for the POST request to Home Assistant.
If the root loses its connection to Home Assistant it will stop broadcasting. In this case, all the nodes that stop receiving these messages will start selecting a new root.
Zanshin_BME680: Read the sensor data if the BME680 is used.
Adafruit_BME280: Read the sensor data if the BME6280 is used.
ArduinoJson: To deserialize the JSON message of the mesh network.
HTTPClient: Used for the POST request to Home Assistant.