As I wrote in my previous posts, the ESP, the STM and the server were working together to receive images or animations depending of what the user wants. Now it’s time for the STM to become the sender.
We want the devices to be able to received images from the server but we also want to establish a connection between Touchs to send images from one to the other. On top of that at the end we want to have a communication between both devices in both ways. But as you may know chi va piano va sano. So first things firts, we implemented the emitting part for the first device. When the user button of the test board is hold, we want the STM to be able to communicate with another device through MQTT.
Don’t listen anymore:
The first thing we will have to do is that once we will become the sender we don’t want to receive the data from the server anymore. To do so, once the ESP will be notify of the choice of the user (we will get to that in a few lines), it will unsubscribe to its current topic (animation or image).
To notify the ESP, we added a new signal in our communication protocol: BECOMING_SENDER. When the ESP receive this signal, it will unsubscibe and reply with a corresponding acquittal.
How to specify the target:
This is obviously a big issue, we want to communicate with the Touch the user chose as receiver not any Touch. To decide which Touch we want, we first needed to give each one of them an ID. Each ID will have a corresponding image engrave on the device wood. It will be a line of marble. SO if you want to be pared with your friend’s Touch, you will just need to draw his line. To do so we decided that the server will be the one to make the connection drawing->ID.
So in the end what happens ?
The user holds the button, it sends to the ESP the notification and the address of the target. The ESP respond and transmit the address to the server though its private topic, the server then reply with the private topic of the target. Then the ESP will be free to post messages directly there.
Obviously for our test the address is hard-coded as we don’t have the marbles yet.
What’s next ?
We will implement how the images will be sent once the target is acquired during the weekend and then next week we will start implementing the receiving part. Hopefully, next Friday, we will be able to connect two couples of ESP, STM with each other with one couple being the target and the other the sender.
Issues we faced:
First : if you are using clang format for the first time be careful of the “IncludeBlocks” setting. We lost half an hour before understanding that the includes had been reordered and that it caused a few dozens of errors.
We also faced a couple of bugs. The first one appeared because we re implemented the ESP part of the communication to use a state machine which allowed us to use less semaphores (instead of 3 flags we only have the state that need to be protected). But the result was that when the button was hold while a transaction was in progress, the STM and the ESP were ready to send but the ESP was also trying over and over again to do the transaction once again. We fixed this by adding a check when a transaction failed to see if we didn’t become sender in the middle of it.