I swear I could connect

Now that the Hall effect sensor is working and while the coils are still being tested with the marbles, we started working on our WiFi module. As explained in our project’s presentation, we want our devices to be able to communicate through MQTT with a backend and with other Touch.

The WiFi module :

To do our test we took an ESP32 Wroom-32 Dev kit C. The main purpose of this module will be to connect to the WiFi, receive through MQTT the images that the box will display and then transmit them to the STM32. In some cases, the STM32 will send to the ESP the state of the box and it will have to transfert those data through MQTT.

First step the WiFi connexion :

The ultimate goal will be to get the WiFi connexion in two steps :
_If the device can’t reach the last Wifi it was connected to, it present itself as an access point.
_In this case, the user will have to connect a smartphone or a computer to the device. It will then through a web page ask for the SSID and the password of the network it should use.

Yet, for now it isn’t the main priority so we will directly hardcode the SSID and password. But as we want to have a code that will be compatible with the procedure above, we still made a first step towards it.

So what we did is that, at first the ESP will present itself as an access point and will also have a wrong SSID and password for the network we want it to connect to. Then, when any device connects to the ESP, it will use the right SSID and password (which are also hardcoded). That way, if we get enough time by the end of the project, we will just need to add a Web server and to ask for the SSID and the password through it.

Second step the MQTT protocol :

Now that we have a WiFi connexion, we want to be able to reach and communicate with the broker we already installed. The issue had is the fact that the school network doesn’t seem to allow connexion on 1883 port (the port reserved for MQTT). We had two options to avoid this.The first one was to use MQTT through the 80 port. While this was completly supported by the ESP and some online broker, it was much harder to make it work with our backend (that use the paho-mqtt library) and our broker. The second option, and the one we finaly chose was to connect the ESP and the broker to the same subnetwork so we could use the 1883 port.
Obviously, this is not the situation the device is meant to be use, but usually networks don’t block the MQTT protocol.
Doing so we managed to have the ESP and our server being connected through the MQTT protocol.

Here is what we get when running the program. On the left side we see what the ESP does and on the right what the broker receive .
First the server goes online and post “Server Online” as a retained message. Then the ESP connects to the broker and sends “Lets sing !”. Once the backend receive this it post the first part of the song. When the ESP hear the first part he can’t help but continue !

Conclusion :

At this point we have an ESP that can receive data from our backend and also send them to it (or to any other device). In the upcoming days, we will implement a communication protocol to connect the ESP and the STM.

Leave a Reply

Your email address will not be published. Required fields are marked *