My working MQTT / Thingspeak project
Posted: Fri Jan 07, 2022 1:34 am
This is a description of how I've got the Flowcode 9 MQTT component to work with Thingspeak.
I hope that this can help people to start working with Thingspeak in an easy way.
ThingSpeak is an IoT analytics platform service that allows you to aggregate, visualize, and analyze live data streams in the cloud. You can send data to ThingSpeak from your devices, create instant visualization of live data, and send alerts.
It took me a while, much testing, much reading before I got my first working results.
Many things can be found in tutorials on the website of Thingspeak: https://thingspeak.com
But the way the data is processed is not always transparent.
When "publishing" data to the Thingspeak broker, the response from the server isn't that transparent to interpret. I got return values like "45", "53" and so on, but I couldn't find what these meant. So this was sometimes working blind.
I will use an example that I've made and that seems to work. This is a simple setup, a very simple one. But this contains everything to get started and I hope that this "tutorial" can get people to improve the way I've used it. Or just use it copy/paste to get their project up and running.
Feel free to comment and/or add improvements. Please do (and share).
Get started:
- My setup: ESP32wroom32 with a DS18S20 as my temperature sensor.
- First (off course): you need to register an account with Thingspeak (https://thingspeak.com/ ).
Thingspeak offers 3 free channels (with I think 48 fields of data). As a free subscriber you can (if I'm not wrong), publish 3.000.000 times a year data to store on their servers. That is every 11 seconds.
I think this is for each channel of 48 "fields".
- Help can be found on their documentation page: https://nl.mathworks.com/help/thingspeak/
- Create a "channel": https://nl.mathworks.com/help/thingspea ... annel.html
In my example I've created a channel with only one field (temperature). A field is in a channel 1 container where data is stored and can be visualized in charts (or so).
When creating a channel, the API keys are needed later on in Flowcode. Please store the channel name too at this point. It looks a bit like this: " 1955401"
- Now add a new device: you have created a channel? Then add a device via: https://thingspeak.com/devices/mqtt This step creates credentials that you can use in the Flowcode MQTT component to identify the used device. Make sure to store the "Client ID", the " Username" and the "Password". They all look like this: " Cxhrgt5zy4zy7471urteyh5 " (I won't use my own credentials for obvious reasons). In the Flowchart I will use Client identifier: Cxhrgt5zy4zy7471urteyh5, Name = "Cxhrgt5zy4zy7471urteyh5" and password: " kegh57FDhe277rw4Cfekad83"
My Flowcode project is attached, please open it (and be amazed of the ugly way I've used Flowcode )
In this example I've used a UART component to read back the response of the process. This component can easily be removed (with all the macro calls).
In the 2D panel there are the following components: DS18S20 attached to port 23 from my ESP32Wroom32 in my example.
You can use any other sensor as long as you can read a value that can be turned into a string to be send to Thingspeak.
The MQTT client component: make sure the host is set to: mqtt3.thingspeak.com
The Client Identifier: use the value that was generated in the generated MQTT device (in this example "Cxhrgt5zy4zy7471urteyh5"
Authentication: choose "name and password", use the values that were generated in the generated MQTT device.
Attach the network component to the NetworkComms1 (or whatever it is called in your setup).
The two components are attached and this is shown with a red line between them in the dashboard.
The Network component doesn't need any change of properties except the connection to the ESP component. Choose TCP/IP component WLAN_ESP32.
Again, the connection is shown by a red line.
As a sensor I use the DS18S20, with the One-wire component, so again, connect the sensor with the One-wire component and choose the pin on which the sensor is attached (in my case: portA.23)
In my hardware, the sensor is connected to the 3.3V with a 4k7 resistor between the +3.3V and the data pin.
In the Flowcode chart I start with initializing all the components.
The WLAN component is connected to an wireless network using the credentials of that SSID.
There are UART calls, but they all can be removed, I'm too lazy to do that myself right now
The MQTT component is connected to the Thingspeak broker and the result is stored in variable "connected" to be used to check the status. You can add a piece of code that returns to the connect macro if the result fails.
Next the MQTT component subscribes to a topic. In this case by using the code " (replace 1234567 with the number of your channel).
If subscribe returns another response then 0, go further.
If success: go to the actual sampling the sensor and publish it to the broker.
Sample the sensor and convert the value to a stringvalue.
I have chosen to add all parameters for the payload in 1 variable, the string "Payload".
This is generated by adding the nessesarry code together with the sampled value.
This variable is published to the channel by: " and payload is the variable "Payload".
After that I wait 60 seconds and start a new temperature sample... publish....
--------------------------------------------------------------------------------------------------------------------------------------------
I hope this example can be used to start with Thingspeak/Flowcode.
Note that there are other ways to publish data to your channel like the http equivalent that looks like this:". But that worked for me in a browser, but I couldn't yet get it to work in Flowcode.
Please use, update, modify this project, but please share your results/insights
I hope that this can help people to start working with Thingspeak in an easy way.
ThingSpeak is an IoT analytics platform service that allows you to aggregate, visualize, and analyze live data streams in the cloud. You can send data to ThingSpeak from your devices, create instant visualization of live data, and send alerts.
It took me a while, much testing, much reading before I got my first working results.
Many things can be found in tutorials on the website of Thingspeak: https://thingspeak.com
But the way the data is processed is not always transparent.
When "publishing" data to the Thingspeak broker, the response from the server isn't that transparent to interpret. I got return values like "45", "53" and so on, but I couldn't find what these meant. So this was sometimes working blind.
I will use an example that I've made and that seems to work. This is a simple setup, a very simple one. But this contains everything to get started and I hope that this "tutorial" can get people to improve the way I've used it. Or just use it copy/paste to get their project up and running.
Feel free to comment and/or add improvements. Please do (and share).
Get started:
- My setup: ESP32wroom32 with a DS18S20 as my temperature sensor.
- First (off course): you need to register an account with Thingspeak (https://thingspeak.com/ ).
Thingspeak offers 3 free channels (with I think 48 fields of data). As a free subscriber you can (if I'm not wrong), publish 3.000.000 times a year data to store on their servers. That is every 11 seconds.
I think this is for each channel of 48 "fields".
- Help can be found on their documentation page: https://nl.mathworks.com/help/thingspeak/
- Create a "channel": https://nl.mathworks.com/help/thingspea ... annel.html
In my example I've created a channel with only one field (temperature). A field is in a channel 1 container where data is stored and can be visualized in charts (or so).
When creating a channel, the API keys are needed later on in Flowcode. Please store the channel name too at this point. It looks a bit like this: " 1955401"
- Now add a new device: you have created a channel? Then add a device via: https://thingspeak.com/devices/mqtt This step creates credentials that you can use in the Flowcode MQTT component to identify the used device. Make sure to store the "Client ID", the " Username" and the "Password". They all look like this: " Cxhrgt5zy4zy7471urteyh5 " (I won't use my own credentials for obvious reasons). In the Flowchart I will use Client identifier: Cxhrgt5zy4zy7471urteyh5, Name = "Cxhrgt5zy4zy7471urteyh5" and password: " kegh57FDhe277rw4Cfekad83"
My Flowcode project is attached, please open it (and be amazed of the ugly way I've used Flowcode )
In this example I've used a UART component to read back the response of the process. This component can easily be removed (with all the macro calls).
In the 2D panel there are the following components: DS18S20 attached to port 23 from my ESP32Wroom32 in my example.
You can use any other sensor as long as you can read a value that can be turned into a string to be send to Thingspeak.
The MQTT client component: make sure the host is set to: mqtt3.thingspeak.com
The Client Identifier: use the value that was generated in the generated MQTT device (in this example "Cxhrgt5zy4zy7471urteyh5"
Authentication: choose "name and password", use the values that were generated in the generated MQTT device.
Attach the network component to the NetworkComms1 (or whatever it is called in your setup).
The two components are attached and this is shown with a red line between them in the dashboard.
The Network component doesn't need any change of properties except the connection to the ESP component. Choose TCP/IP component WLAN_ESP32.
Again, the connection is shown by a red line.
As a sensor I use the DS18S20, with the One-wire component, so again, connect the sensor with the One-wire component and choose the pin on which the sensor is attached (in my case: portA.23)
In my hardware, the sensor is connected to the 3.3V with a 4k7 resistor between the +3.3V and the data pin.
In the Flowcode chart I start with initializing all the components.
The WLAN component is connected to an wireless network using the credentials of that SSID.
There are UART calls, but they all can be removed, I'm too lazy to do that myself right now
The MQTT component is connected to the Thingspeak broker and the result is stored in variable "connected" to be used to check the status. You can add a piece of code that returns to the connect macro if the result fails.
Next the MQTT component subscribes to a topic. In this case by using the code "
Code: Select all
"channels/1234567/subscribe"
If subscribe returns another response then 0, go further.
If success: go to the actual sampling the sensor and publish it to the broker.
Sample the sensor and convert the value to a stringvalue.
I have chosen to add all parameters for the payload in 1 variable, the string "Payload".
This is generated by adding the nessesarry code together with the sampled value.
Code: Select all
"Payload = "field1=" + Tempstring + "&status=MQTTPUBLISH"
Code: Select all
""channels/1234567/publish"
After that I wait 60 seconds and start a new temperature sample... publish....
--------------------------------------------------------------------------------------------------------------------------------------------
I hope this example can be used to start with Thingspeak/Flowcode.
Note that there are other ways to publish data to your channel like the http equivalent that looks like this:
Code: Select all
"https://api.thingspeak.com/update?api_key=CUSFQINNUhgefeUZ&field1=17
Please use, update, modify this project, but please share your results/insights