My working MQTT / Thingspeak project

For general Flowcode discussion that does not belong in the other sections.
Post Reply
MJU20
Posts: 238
http://meble-kuchenne.info.pl
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

My working MQTT / Thingspeak project

Post by MJU20 »

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 :-) )
DS18B20 met MQTT voor MM.fcfx
(24.84 KiB) Downloaded 325 times

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"
(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.

Code: Select all

"Payload = "field1=" + Tempstring + "&status=MQTTPUBLISH"
This variable is published to the channel by:

Code: Select all

""channels/1234567/publish"
" 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:

Code: Select all

"https://api.thingspeak.com/update?api_key=CUSFQINNUhgefeUZ&field1=17
". 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
DS18B20 met MQTT voor MM.fcfx
(24.84 KiB) Downloaded 325 times

lucibel
Posts: 172
Joined: Thu Sep 23, 2021 3:44 pm
Location: France
Has thanked: 29 times
Been thanked: 22 times

Re: My working MQTT / Thingspeak project

Post by lucibel »

hi,
thx for your template !
I did a test without "subscribe component macro" and it work well too.
No need to subscribe before to publish data.
no need.jpg
no need.jpg (67.44 KiB) Viewed 9899 times
second point, I add initialise MQTT before to connect
init.jpg
init.jpg (20.82 KiB) Viewed 9899 times
Delay can be adjusted
Seb

MJU20
Posts: 238
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Re: My working MQTT / Thingspeak project

Post by MJU20 »

Hey lucibel,

Thanks for testing this and adding it to the "quick start guide".
Let's make this better :-)

george_b
Posts: 18
Joined: Fri Jul 01, 2022 5:00 pm
Has thanked: 9 times
Been thanked: 2 times

Re: My working MQTT / Thingspeak project

Post by george_b »

Thanks for your post. This is the first complete tutorial of how to send data to MQTT using flowcode!


I am wondering if anyone knows how to change the client ID limitation from 20 characters to a larger number?

Regards
George

chipfryer27
Valued Contributor
Posts: 1110
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 278 times
Been thanked: 397 times

Re: My working MQTT / Thingspeak project

Post by chipfryer27 »

Hi

If I remember correctly this limitation is already removed, although it still says 20.

Regards

george_b
Posts: 18
Joined: Fri Jul 01, 2022 5:00 pm
Has thanked: 9 times
Been thanked: 2 times

Re: My working MQTT / Thingspeak project

Post by george_b »

I am strugling to connect to pubnub server spending hours on the PC.

I was hopping that the issue is this length limitation!

chipfryer27
Valued Contributor
Posts: 1110
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 278 times
Been thanked: 397 times

Re: My working MQTT / Thingspeak project

Post by chipfryer27 »

Hi

See this


https://www.matrixtsl.com/mmforums/view ... 22#p107722


Ben says the name is 32 bytes in length but I think Leigh also increased it (if I remember correctly). If I find the post I'll send you a link.

Regards

chipfryer27
Valued Contributor
Posts: 1110
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 278 times
Been thanked: 397 times

Re: My working MQTT / Thingspeak project

Post by chipfryer27 »

Hi

I remember trying to connect with PubNub a while back and having issues too. I can't remember if I ever managed it or if I just ended up using ThingSpeak. I think PubNub allowed me to host the server myself, so that probably threw some additional spanners into the works.

I don't think I was best impressed with the documentation as it seemed to be a pain to actually get the information I was after, such as the correct address to send the message to.

Regards

Post Reply