Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » JSON Data from Paho mqtt client(Not able to publish json data from nodemcu using arduino ide i got rc = -1 when publishing)
JSON Data from Paho mqtt client [message #1796111] Fri, 05 October 2018 09:25 Go to next message
vivek kumar is currently offline vivek kumarFriend
Messages: 6
Registered: October 2018
Junior Member
#define MQTTCLIENT_QOS2 1
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>



#include <IPStack.h>
#include <Countdown.h>
#include <MQTTClient.h>



int arrivedcount = 0;
char* state ;


int led = D7 ;

#define WLAN_SSID "*********"
#define WLAN_PASS "********"

void messageArrived(MQTT::MessageData& md)
{
MQTT::Message &message = md.message;

Serial.print("Message ");

Serial.print(++arrivedcount);
Serial.print(" arrived: qos ");
Serial.print(message.qos);
Serial.print(", retained ");
Serial.print(message.retained);
Serial.print(", dup ");
Serial.print(message.dup);
Serial.print(", packetid ");
Serial.println(message.id);
Serial.print("Payload ");

Serial.println((char*)message.payload);



if(strcmp((char*)message.payload, "chk") == 0)
{
state = (char*)"off" ;
chkpub() ;
delay(100) ;

}

if(strcmp((char*)message.payload, "onn") == 0)
{
//Serial.println("Bhaiya ji smile please");
digitalWrite(led, HIGH);
}
else if(strcmp((char*)message.payload, "off") == 0)
{
//Serial.println("Bhaiya ji band ho gai");
digitalWrite(led, LOW);
}


}


WiFiClient Client;
IPStack ipstack(Client);
MQTT::Client<IPStack, Countdown, 50, 1> client = MQTT::Client<IPStack, Countdown, 50, 1>(ipstack);

byte mac[] = { 0x60, 0x01, 0x94, 0x23, 0x7B, 0x58 }; // replace with your device's MAC


const char* topic = "/scarlet/maro/artr/Fan/047b5f12";

void connect()
{
char hostname[] = "test.mosquitto.org";
int port = 1883;

Serial.print("Connecting to ");
Serial.print(hostname);
Serial.print(":");
Serial.println(port);

int rc = ipstack.connect(hostname, port);
if (rc != 1)
{
Serial.print("rc from TCP connect is ");
Serial.println(rc);
}

Serial.println("MQTT connecting");
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 3;
data.clientID.cstring = (char*)"/scarlet/maro/artr/Fan/047b5f12";
rc = client.connect(data);
if (rc != 0)
{
Serial.print("rc from MQTT connect is ");
Serial.println(rc);
}
Serial.println("MQTT connected");

rc = client.subscribe("/SCARLET/MARO/ARTR/FAN/047B5F12", MQTT::QOS2, messageArrived);
if (rc != 0)
{
Serial.print("rc from MQTT subscribe is ");
Serial.println(rc);

}
Serial.println("MQTT subscribed");

}

void setup()
{
pinMode(led, OUTPUT);
Serial.begin(115200);
delay(10);

Serial.print("Connecting to ");
Serial.println(WLAN_SSID);

WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();

Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());


Serial.println("MQTT Hello example");
connect();
}

MQTT::Message message;


String sPayload;
char* cPayload;

void loop()
{
if (!client.isConnected())
connect();



arrivedcount = 0;

// Send and receive QoS 1 message

StaticJsonBuffer<200> jsonBuffer;
JsonObject& Data = jsonBuffer.createObject();
Data["sensor"] = "Temp";
Data["vlaue"] = gettemp();
//payload["light_level"] = analogRead(sensorPin);
sPayload = "";
Data.printTo(sPayload);
cPayload = &sPayload[0u];

//Serial.println(cPayload) ;


message.payload = (void*)cPayload;
//Serial.println((char*)message.payload) ;


message.qos = MQTT::QOS1;

message.payloadlen = strlen(cPayload)+1;
//Serial.println(message.payloadlen) ;

/************Here I got problem in publishing json Data***********i get rc = -1 **********************************/
/*************************************************************************************************************************/
int rc = client.publish(topic, message);

/**************************************************************************************************************************/
/**************************************************************************************************************************/
Serial.println(rc) ;

delay(100);
}

float count = 0.0 ;
float gettemp()
{
count += 1.0 ;
return count;
}

void chkpub()
{
// Send and receive QoS 1 message
message.payload = (void*)state;


Serial.println(state) ;
message.qos = MQTT::QOS1;
message.payloadlen = strlen(state)+1;
int rc = client.publish(topic, message);
delay(100);
}

Re: JSON Data from Paho mqtt client [message #1796427 is a reply to message #1796111] Fri, 12 October 2018 17:33 Go to previous messageGo to next message
Ian Craggs is currently offline Ian CraggsFriend
Messages: 83
Registered: March 2013
Member
You've set the max packet size in the MQTT client constructor to 50. The publish packet contains both the topic name and the payload as well as a few other bytes of overhead. You may need to increase your max packet size
Re: JSON Data from Paho mqtt client [message #1796447 is a reply to message #1796427] Sat, 13 October 2018 07:48 Go to previous message
vivek kumar is currently offline vivek kumarFriend
Messages: 6
Registered: October 2018
Junior Member
Sir, thank you very much with bottom of my heart...
Previous Topic:Basic MQTT over websockets example
Next Topic:Java Client
Goto Forum:
  


Current Time: Fri Apr 26 01:31:24 GMT 2024

Powered by FUDForum. Page generated in 0.02985 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top