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…

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

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

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

Cheers,

Craig

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

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.

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.

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] (GitHub - myDevicesIoT/Cayenne-MQTT-ESP: Cayenne MQTT ESP8266, ESP32 & NodeMCU 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

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.

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, GitHub - knolleary/pubsubclient: A client library for the Arduino Ethernet Shield that provides support 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.)

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

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.

I’m still relatively new to Arduino and Cayenne. I’m using the pubsubclient library and trying to connect to Cayenne. this is the bit of code to connect to the cayenne server:

const char* cayserver = “mqtt.mydevices.com”;

// Ethernet and MQTT related objects
EthernetClient ethClient;
PubSubClient cayClient(ethClient);

void setup()
{
// Useful for debugging purposes
Serial.begin(9600);

// Start the ethernet connection
Serial.println(“Begin Ethernet connection”);
Ethernet.begin(mac);

// Ethernet takes some time to boot!
delay(3000);

Serial.println(“Begin Cayenne connection”);
cayClient.setServer(cayserver, 1883);

// Attempt to connect to the server with the ID clientID
if (cayClient.connect(clientID, username, password))
{
Serial.println(“Cayenne Connection established”);

I use the same code to connect to a local Mosquitto broker, and it works. Is there something that needs to be done to connect to Cayenne’s broker?

I’d appreciate any advice. Thanks.

You can see some example code for pubsub here Using Node-RED as a Local Fallback Server

@jlima why do you want to use the pubsub library. You can use the much simpler library instead GitHub - myDevicesIoT/Cayenne-MQTT-Arduino: Cayenne MQTT Arduino Library

@adam If I’m reading it correctly, the code sample connects to the Node-RED server using the PubSubclient library, and the Node-RED server connects to the Cayenne dashboard through the flows. is there an example of using the pubsubclient to connect directly to Cayenne? Thanks!

@shramik_salgaonkar Ultimately, I’m looking at connecting to 2 different MQTT brokers from the Arduino devices. It takes input from 1 broker and updates the Cayenne dashboard thru the Cayenne MQTT broker. The cayenne mqtt client abstracts a lot of the details, and I thought using the pubsubclient would give me better control of the connections. Hope this makes sense.