PubSubClient

Good afternoon,

I am using the Arduino Library PubSubClient.

According to https://developers.mydevices.com/cayenne/docs/cayenne-mqtt-api/ it is possible to send multiple sensor variables to multiple channels simultaneously using

Topic: v1/ username /things/ clientID /data/

and Payload:
[
{
“channel”: 1,
“value”: 16.4,
“type”: “temp”,
“unit”: “c”
},
{
“channel”: 2,
“value”: 75,
“type”: “rel_hum”,
“unit”: “p”
},
{
“channel”: 5,
“value”: 75,
“type”: “batt”,
“unit”: “v”
}
]

I can not get it to work. I did manage to send sigle sensor values, one at a time
Attach my code:

//WiFi global declarations
#include <WiFi.h>
const char* ssid     = "xxxx";
const char* password = "xxxx";


#include <PubSubClient.h>


//(String batcap,String tempgyro,String temp,String tilt,String batvolt,String grav,String abv, float signalstrength, String mqtt_username, String mqtt_password, String mqtt_clientid)

void pubToCayenne(String batcap,String tempgyro,String temp,String tilt,String batvolt,String grav,String abv, String  signalstrength, String mqtt_username, String mqtt_password, String mqtt_client_id)
{
 WiFiClient wifiClient;  //MQTT things
 PubSubClient client("mqtt.mydevices.com", 1883,wifiClient);

 if (client.connect(mqtt_client_id.c_str(), mqtt_username.c_str(), mqtt_password.c_str())) 
 {
  Serial.println("Connected to Cayenne");
 }
 delay(3000);
 String topic="v1/" +mqtt_username +"/things/" +mqtt_client_id+"/data/";        
 String payload;
 payload="[";
 
 payload+="{\"channel\": 0,\"value\": "+String(000)+"}";  //iTilt id number
 payload+=",{\"channel\": 1,\"value\": "+batcap+"}";
 payload+=",{\"channel\": 2,\"value\": "+tempgyro+"}";
 payload+=",{\"channel\": 3,\"value\": "+String(1.08)+"}";  //Firmware Version
 payload+=",{\"channel\": 4,\"value\": "+temp+"}";
 payload+=",{\"channel\": 5,\"value\": "+tilt+"}";
 payload+=",{\"channel\": 6,\"value\": "+batvolt+"}";
 payload+=",{\"channel\": 7,\"value\": "+grav+"}";
 payload+=",{\"channel\": 8,\"value\": "+String(65)+"}"; //pub interval
 payload+=",{\"channel\": 9,\"value\": "+String(1.055)+"}"; //original gravity
 payload+=",{\"channel\": 10,\"value\": "+abv+"}";
 payload+=",{\"channel\": 11,\"value\": "+signalstrength+"}"; 

 payload+="]";

 Serial.println("Topic: "+topic);
 Serial.println("Payload: "+payload);
 
 client.publish(topic.c_str(),payload.c_str());
 delay(10000);
}




void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("Try to connect to WiFi");

  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println("WiFi Connected"); 
}

void loop() {
pubToCayenne("20","26","25","44","4.14","1.005","4.8","78","xxxxx","xxxxx","xxxxx");
ESP.deepSleep(60000000000);
}

Please advice.

you can use the cayenne arduino library instead GitHub - myDevicesIoT/Cayenne-MQTT-Arduino: Cayenne MQTT Arduino Library

Your topic is wrong, should be: v1/username/things/clientID/data/json as per Cayenne Docs

Tnx, I am sorted for now. The Cayenne-MQTT-Arduino library is great, but takes to long for my battery powered device which is offline most of the time.

1 Like

Tnx

You can also take a look at my project here Battery Powered ESP8266 Temperature/Humidity Monitor with DHT11