ESP8266 "RAW" MQTT messages using the PubSub Library question


#1

Hi Guys!

I’m trying to use MQTT in it’s most crude form, just for compatibility between platforms.
I’m trying Cayenne, because I really like the iOS app integration.

ESP8266 just using…
include PubSubClient.h
include ESP8266WiFi.h

If I’d like to send a value to the dashboard, what would be the correct format?
In the DOCs section, found that we need to send it like this…

v1/username/things/clientID/data/channel
I guess it’s…
v1/MQTT username/things/clientID/data/channel

  1. This would be the correct way of sending data to Channel 10?

client.publish(“v1/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/things/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/” valueStr “/10”);

I believe the PubSub library would expect something like…
client.publish(USERNAME/PREAMBLE/TOPIC, valueStr);
Which indeed is a different format.

Another related question:
2) How do I subscribe to this same “topic”, from another esp8266?

Thanks in advance for your time…
Cheers!
Marc


#2

Hi @marc,

I don’t know if anyone has had it compile on an ESP yet. I tried a while ago and couldn’t make it work with Cayenne. Maybe I’ll revisit it and help this cause.

Looks like you are on the right track.

One idea might be to have a local NodeRed mqtt server to aggregate all your date, and then publish it all to Cayenne through there. @adam has been successful doing this.

Cheers,

Craig


#3

@kreggly is right, I have a project posted here that explains it all. Lucky for you, I even used an ESP8266!


#4

Were you able to go direct from the ESP to Cayenne without NodeRed as the middle man, @adam?

When I tried, my messages werent liked by Cayenne.

Wizard needs code badly.

Cheers,

Craig


#5

I’m pretty sure I did at some point. Let me look around tonight and see if I can find the code.


#6

Hi guys!

Thanks for your help @kreggly and @adam

I’m really trying to avoid the Node, just esp8266 directly to Cayenne.
In the meantime I decided to check if the message format I was sending to Cayenne was ok, using MQTT.fx. I can authenticate correctly (mqtt.mydevices.com), but I can’t seem to be able to publish data to the dashboard.

Maybe my mqtt messages are incorrectly formatted.
The BYOT Docs say…

Send Sensor data
v1/username/things/clientID/data/channel

But then…
Examples
Send Sensor Data to Channel 2⇒ PUB v1/A1234B5678C/things/0123-4567-89AB-CDEF/data/2temp,c=20.7

So I believe at this point I’m a bit confused here.
What would be the correct way? If testing from MQTT.fx, should it be quoted ,""?

I guess I need to sort out this first step, and then move on to code the esp8266.
Thanks guys,
Cheers!
Marc


#7

You shouldn’t need to quote it, the data part goes in the lower box in MQTT.fx, like this:

So you’d have v1/MQTT_USERNAME/things/CLIENT_ID/data/2

and then

temp,c=20.7

Then pressing ‘publish’ should send that test data to the Cayenne dashboard and create a widget for you automatically.


#8

Thanks @rsiegel !

So, if I understand correctly…
Everything is in the same line, no spaces…

syntax eg:
v1/MQTT_USERNAME/things/CLIENT_ID/data/2temp,c=20.7

I also tried
v1/MQTT_USERNAME/things/CLIENT_ID/data/2,temp,c=20.7
but can´t seem to get it working.


#9

I was just showing how to send some test data through MQTT.fx, where you’d split the temp,c=20.7 part off to a different field.

I’m not familiar with the PubSub library, is that from our repo? If so I can ask one of our devs if I can’t figure it it out. I can take a look regardless if you have a link.

If you use our [Cayenne MQTT ESP8266 Library] (https://github.com/myDevicesIoT/Cayenne-MQTT-ESP8266) I’m more familiar how to send this sort of data – we provide commands like

client.luxWrite(2, 155.55) to send the value 155.55 lux to Cayenne on MQTT Channel 2

or

client.virtualWrite(6, 17.222, "analog_sensor", "null") to pass the generic value 17.222 to MQTT Channel 6

I’m working on a table of all the supported values for those 3rd and 4th parameters for virtualWrite() since the table we have in the docs is a bit out of date/incorrect in some places. I’ll post it today or tomorrow and link it here as well.


#10

Hey! Thanks @rsiegel

Got it working from MQTT lens and MQTT.fx
Figured it out, simply put in mqtt jargon…

topic: v1/MQTT_USERNAME/things/CLIENT_ID/data/2
message: temp,c=20.7

Seems so obviousl now :blush:


#11

Awesome! Let me know if you run into any trouble building those statements for other data types, that’s what I’m looking to make easier with this table I’m working on.


#12

As promised, here is v1.0 of my completed table. I’ll likely add a few more examples/notes in the future, but this should be useful to get started with the various data types, or if you have data in a format not enumerated by our data / unit types list:


#13

Hi there!

Finally, I got the esp8266 to send and receive “raw” MQTT messages using the PubSubClient library. Thanks @kreggly @adam and @rsiegel for all your help!

I believe this might be useful for anyone looking to do something similar, so I’ll leave the arduino sketch code with comments.

BTW, if anyone is willing to help me optimze the “string” generation for the actual mqtt message, you’re welcome. Cheers!

Marc / spacefolder

MQTT Raw Data to Cayenne Dashboard,
using the great Nick O’Leary’s PubSubClient
(Arduino Client for MQTT, https://github.com/knolleary/pubsubclient)

What does it do?
This sketch will attempt to connect to Cayenne MQTT server via Wifi, subscribe to a “dashboard button” topic, to turn on/off the esp8266 built in led. Also it will publish temperature readings from a Dallas DS18b20 digital temperature sensor.

In a near future, the esp8266 (NodeMCU v2) should act as a smart thermostat, controlling my central heater/boiler. It should be possible to remotely check ambient temperature and boiler status using Cayenne Dashboard and turn the heater on/off (in case I left home in a hurry!, etc.)

CODE> https://github.com/spacefolder/esp8266-MQTT-v1/blob/master/esp8266-MQTT-v1.cpp