ESP8266 "RAW" MQTT messages using the PubSub Library question


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…

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…



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.





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



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.





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



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 (, 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

But then…
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,



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


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



Thanks @rsiegel !

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

syntax eg:

I also tried
but can´t seem to get it working.



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] ( 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


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.



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:

1 Like


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.



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:

1 Like


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,

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.)


1 Like


Hi All… Im having a similar problem, Im able to publish a fix value on CAYENNE but cannot publish the sensor actual data, I can only publish virtual (fake) values… I know this must be a basic issue but I haven’t been able to solve it… can anyone give me a hand?

Im connecting the BH1750 o my RSPI 3+, dont know much about coding but I have downloaded a code from here that works locally and makes actual readings on screen.



in your other post, you mentioned that you not receiving any data on the cayenne dashboard. so better, not create multiple threads on the same topic.