guild icon
Toit
#MQTT Service
Thread channel in help
RobvanLopik
RobvanLopik 05/08/2023 11:30 AM
I wanted to use the Datacake package as an example for writing a general MQTT service. It turns out the Datacake code is much more complicated than I expected, especially the Networkmodule part. Could someone (@kasperl ?) explain to me the rationale for this. Second question: how would you implement subscribing to topics and receiving answers?
bitphlipphar
bitphlipphar 05/08/2023 03:48 PM
Yeah, the complexity in the implementation is a valid point. I wanted to build a service that would connect on demand and the NetworkModule handles that well. We use it for net.open/close.
bitphlipphar
bitphlipphar 05/08/2023 03:48 PM
So if two separate clients start at the same time, the share one connection to Datacake.
bitphlipphar
bitphlipphar 05/08/2023 03:49 PM
Once the last one goes away, we close down the connection.(edited)
bitphlipphar
bitphlipphar 05/08/2023 03:53 PM
There are multiple options for subscribing to topics and receiving answers. One is a polling approach and one is a pushing approach. It is probably nice to subscribe on the client and then maintain a simple map in the service (topic -> resource) and use the notification mechanism to push data to the clients that want the data.
bitphlipphar
bitphlipphar 05/08/2023 03:55 PM
So https://github.com/kasperl/toit-datacake/blob/main/src/service.toit#L164 is the resource you can call notify_ on, possibly through a new public method on that class.(edited)
Datacake connector for the ESP32 based on the Toit language. - toit-datacake/service.toit at main · kasperl/toit-datacake
RobvanLopik
RobvanLopik 05/09/2023 08:29 AM
Thanks for the info. The resource/notify is what I also came up with. For the complexity I will have to decide whether to try to understand it, or think of something simpler.
RobvanLopikOPRobvanLopik
I wanted to use the Datacake package as an example for writing a general MQTT service. It turns out the Datacake code is much more complicated than I expected, especially the Netwo...
floitsch
floitsch 05/09/2023 08:44 AM
The Datacake package is a bit more sophisticated (as Kasper explained for several reasons).
If you just want to use MQTT, or have a simple example for using a different broker, then have a look at the Adafruit example: https://github.com/toitware/mqtt/blob/main/examples/adafruit.toit
(edited)
Contribute to toitware/mqtt development by creating an account on GitHub.
floitschfloitsch
The Datacake package is a bit more sophisticated (as Kasper explained for several reasons). If you just want to use MQTT, or have a simple example for using a different broker, the...(edited)
RobvanLopik
RobvanLopik 05/09/2023 09:14 AM
That stage I already passed; see: https://github.com/robvanlopik/toit-pharo
Contribute to robvanlopik/toit-pharo development by creating an account on GitHub.
floitsch
floitsch 05/09/2023 09:17 AM
I see.
Nice.
RobvanLopik
RobvanLopik 05/09/2023 09:19 AM
Now I specifically want to have a base container to do the MQTT stuff and load different containers with services that talk to my Pharo systems through MQTT. In toit-pharo I expose for example I2C to the Pharo side, where I have code to run different I2C devices, but it makes more sense to move that over to the ESP32 side.
floitsch
floitsch 05/09/2023 09:20 AM
Sounds good.
12 messages in total