My working MQTT Thingspeak subscribe...

For general Flowcode discussion that does not belong in the other sections.
Post Reply
MJU20
Posts: 244
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 subscribe...

Post by MJU20 »

Maybe not the right moment to share this file..

This was promised to share in this topic: viewtopic.php?f=3&t=1312

It's a remake of a file that was found in the Wiki (https://www.flowcode.co.uk/wiki/images/ ... P8266.fcfx) but altered for use with Thingspeak.

I'm using a ESP32 Wroom and a SSD1306 display.

I've got another module that publishes data every 5 minutes onto a Thingspeak channel. (kind like described in: viewtopic.php?f=3&t=999&p=5495#p5495).
This data is stored by Thingspeak and I can read it back (and download, and display and.. ) via a browser.

But, I want to show the latest data from 1 field in the published data on a display so I need to read the data back for "field 1".
So this channel I'm using is a private channel (which is falsely called "1547543" in this example).

To subscribe I need to add a device in Thingspeak and read the credentials. (Thingspeak, login, MyChannels, add new device, store Client ID and Password).
I need to make sure that the new device is granted access to the channel that it needs to read data from. (in device settings => Authorized Channel and select the channel you want to read from, set to "Allow Subscribe").

So in this example I will use channel "1547543" to show field 1 on a device that has these credentials:
Client ID/Username: hgGItui!uy798787reyurt5
Password: iug44§44hè4sduiyiuy!5857

Use these in the MQTT component settings..

Off course these are not real :-)

channels/1547543/subscribe can be used to subscribe to the whole channel (not tested yet)
channels/1547543/subscribe/fields/field1 will be used to only receive the content of field 1 and display it.

First issue is the watchdogtimer for the ESP32 Wroom.
I'm using supplementary code" specified in "project options" to get rid of the watchdog timer issues.
I'm disabling it for a short while as first step.

Then I'm initialising the whole thing and print some stuff on the display.

There are some things that are obsolete but I don't have the time to delete these steps in the Flowchart right now. But how it works is not different from the example I've used from the Wiki.
Try and edit the time between two PINGS by changing the "PingCounter" variable.
But also the "keep alive" parameter in the MQTT component properties.
If the display shows the "subscribe/connected" message too much, change these parameters.

I hope this example can be of any use to study the MQTT readback from a Thingspeak channel.
I will try and help if there are any questions.

Thanks to all the people that helped me with this one!
MQTT met display voor MM.fcfx
(25.99 KiB) Downloaded 51 times

chipfryer27
Valued Contributor
Posts: 1147
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 284 times
Been thanked: 412 times

Re: My working MQTT Thingspeak subscribe...

Post by chipfryer27 »

Hi MJU20

Thanks for sharing especially as I do appreciate that it is a work in progress.

I can send data to TS using api keys without issue in both hardware and in simulation (really cool feature the simulation. Thanks Matrix). However I cannot do much at all using the MQTT component so I am keen to see how others may be achieving.

I've entered my credentials into your chart and ran. I've set Network Component to Simulate DLL so that I can get out but the display constantly loops printing "Connected 0 /Subscribed 0" indicating a failure to connect to TS.

I notice in your charts from around January, you didn't use the MQTT component to establish a connection, only using it to Publish, but this time you are using it to connect. I also notice that your credentials are in excess of 20 characters although the MQTT component states a maximum of 20 (mine are 24 characters, yours appear to be 23).

Perhaps the Matrix team could confirm if the maximum size of Client Identifier / Username / Password strings used in the MQTT component are indeed limited to 20 characters or if this is a "typo". If they are limited at 20, would it be possible to increase the strings to perhaps 30 or such like?

Regards

LeighM
Valued Contributor
Posts: 401
Joined: Mon Dec 07, 2020 1:00 pm
Has thanked: 70 times
Been thanked: 217 times

Re: My working MQTT Thingspeak subscribe...

Post by LeighM »

Looking at the internal buffer sizes, it looks like these were increased to 32 bytes.
So 31 plus null string terminator would be the limit.
The Topic buffer is 20 bytes, so 19 plus string terminator.
Hope that helps.
Leigh

edit: Just done another check and found that the Topic (Subscribe and Publish) is not limited to 20,
but is only limited by the user's buffer size.

chipfryer27
Valued Contributor
Posts: 1147
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 284 times
Been thanked: 412 times

Re: My working MQTT Thingspeak subscribe...

Post by chipfryer27 »

Thanks Leigh

That's one potential issue removed. I was hoping though that they were limited to 20 as that would have explained a few things.

Back to the drawing board / ThingsBoard / being bored :)

Regards

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

Re: My working MQTT Thingspeak subscribe...

Post by MJU20 »

The Thingspeak credentials are indeed 23 or 24 characters.

Back to being boiled.

chipfryer27
Valued Contributor
Posts: 1147
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 284 times
Been thanked: 412 times

Re: My working MQTT Thingspeak subscribe...

Post by chipfryer27 »

Hi

Annoyed that I couldn't connect using the MQTT component I set up using an ESP8266 as an external WiFi module, meaning I could capture what was being sent to / from the microcontroller.

Long story short, I wasn't connecting to mqtt3.thingspeak.com so nothing was getting through. When I created my channel a month or so ago it created an MQTT "device" for me and I was using this. I have no idea why but this wouldn't work for whatever reason as the credentials supplied wouldn't let me connect.

I created a new MQTT device from within ThingSpeak and using these credentials I connected straight away to publish using the MQTT component.

Regards

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

Re: My working MQTT Thingspeak subscribe...

Post by MJU20 »

But now it works? Happy to hear!

Thanks for sharing.

chipfryer27
Valued Contributor
Posts: 1147
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 284 times
Been thanked: 412 times

Re: My working MQTT Thingspeak subscribe...

Post by chipfryer27 »

Hi

Discovered why I couldn't connect before recreating the MQTT Device.......

When you create a new MQTT device you immediately get the option to download the credentials (good idea). After about an hour (I think) this option is removed and whilst you can still view and copy all other credentials, you can't with the password.... I hadn't done this as it was at least a month ago when I created it, so when I copied / pasted I didn't notice the paste was actually wrong.

To make matters worse I nearly had to read the documentation......

Regards

Post Reply