MQTT on thingspeak

Has anyone been successful in getting MQTT to work with Thingspeak? I have filled out the route as best as I could with Thingspeak MQTT info:
MQTT broker at the URL mqtt.thingspeak.com and port 1883 with the MQTT Topic set as:
channels/channelD/publish/fields/field1/writeAPIKey

It will not even give a error in the Details field on the Event page.

Hi @jm14830 thanks for the question, and welcome to the community!

We have a ThingSpeak tutorial in the docs, but it uses HTTTP, not MQTT, and Iā€™ve not personally tried MQTT with thingspeak. Would the HTTP approach work for your application?

Thanks for the warm welcome.

No, I specifically need MQTT. Iā€™m not sure how to interpret all of the MQTT fields on the Routes page. Most are self explanatory, but the way it talks about Topics is weird or I just donā€™t understand what it is trying to say. Thingspeak tutorials also mention MQTT parameters that Blues does not mention. I am pretty new to MQTT myself.

Understood. Can you send me a pointer to the ThingSpeak doc for MQTT that youā€™re looking at? Iā€™ll see if I can fill in the gaps for you.

Also, what shows up in the Route Log tab on the event details page?

Hi, the ThingSpeak MQTT API is

Host: mqtt.thingspeak.com
Port: 1883 or 8883 (or Websocket: 80, 443)
PUB -t ā€˜channels/CHANNEL_ID/publish/WRITE_API_KEYā€™ -m ā€˜field1=42&field2=23ā€™
SUB -t ā€˜channels/CHANNEL_ID/subscribe/FORMAT/READ_API_KEYā€™

https://ch.mathworks.com/help/thingspeak/publishtoachannelfeed.html
https://ch.mathworks.com/help/thingspeak/subscribetoachannelfeed.html

Cheers,
Thomas

Here is another page I found that also has some info for Thingspeak.
https://www.mathworks.com/help/thingspeak/mqtt-basics.html

In my Routes Log it just says " No Route Data available". It doesnā€™t even give an error.

@jm14830
It took me a bit to realized the format, but I think this is something we can accomplish with a JSONata transform on the MQTT route.

Havenā€™t had a chance to test this very rigorously yet, but I think this is worth trying.

In the Notehub Route configuration,

  1. Set the Transform JSON field to JSONata Expression
  2. Set the JSONata Expression field to
'field1='& body.field1 &'&field2=' & body.field2

Here is the JSON I developed the JSONata expression with:

{
    "event": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "session": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "device": "dev:012345698901234",
    "sn": "my-serial-number",
    "product": "product:my.email:my-project-name",
    "received": 1614952682.994716,
    "routed": 1614952683,
    "req": "note.add",
    "when": 1614952680,
    "file": "data.qo",
    "body": {
        "field1": "value1",
        "field2": 42
    },
    "tower_when": 1614966165,
    "tower_lat": 42.570162499999995,
    "tower_lon": -70.868796875,
    "tower_country": "US",
    "tower_location": "Beverly MA",
    "tower_timezone": "America/New_York",
    "tower_id": "310,410,1045,16136720",
    "log": {
        "app:2606f411-dea6-44a0-9743-1130f57d77d8/route:c28a25e26f90fb6fb64bb51a8a65e877": {
            "status": "200"
        }
    }
}

Here is the expected output from the JSONata expression

"field1=value1&field2=42"

Here is my example Route config in Notehub (not sure this is 100% correct as I havenā€™t tested with Thingspeak yet)

gwolff. I tried your example almost to the letter. Although I have a different file and I only have a field1, not field2. I still do not get anything in my Route Log tab. Is there a way to see what Notehub is actually sending? I would expect at least to see an error message where Thingspeak kicked it back, but nothing. If you get to test it please let me know if it works for you. I appreciate the help.

I did some testing. I think I may have found the issue.
The JSONata expression produces a string which is encapsulated by ā€œā€ characters, which seems to cause some issue with the Thingspeak data ingress.

Iā€™ll see what else we can try to work around that.

@jm14830
Iā€™ve been looking into this. Iā€™m not sure MQTT is required. The MQTT option for ThingSpeak is just an API to read and write data from the ThingSpeak channel.

When writing data to the channel, the HTTP API produces the same result as using the MQTT option.

You can use either API on the same channel. And if you write to the ThingSpeak channel via the HTTP API, you can still subscribe to the channel updates via MQTT.

I did test this to make sure it works.

@jm14830
One of my colleagues pointed out you can send " as an empty character.

I donā€™t exactly know what that means, but if you format the JSONata expression as follows:

"&field1=" & body.field1 & "&field2=" & body.field2 & "&"

it works.

That is the leading ā€œ&ā€ and closing ā€œ&ā€ do some magic.

Adding the ā€œ&ā€ worked. IT IS MAGIC!!! Thanks for your help. I never would have figured that one out.

1 Like