Go Down

Topic: How to convert an object Json a Char[] to send a beam of mqtt? help (Read 931 times) previous topic - next topic

wanderjose

Hello, I have a drawback in my project, I do not have much knowledge on the subject, I need to extract readings from a sensor and then insert them into a json to send it through mqtt, I'm using a LDR for now, but the idea is to run the Code for any type of sensor.

The problem arises with because the pubsubclient library does not allow me to send json objects, I have to convert it to Char, , does anyone know how this can be solved problem?



Code:


#include <UIPEthernet.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

int tiempo = 500;
int i =0;
int foto = 1;
int medida = 0;
int luzled = 0;
// Update these with values suitable for your network.
byte server[] = { 192, 168, 1, 102 };
boolean blink = false;
char json[100];
EthernetClient ethClient;
PubSubClient client(server, 1883, ethClient);
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
void setup() {
 Serial.begin(115200);
 byte mac[] = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
 byte ip[] = { 192, 168, 1, 180};

 Serial.println("iniciando conexion");
 Ethernet.begin(mac, ip);

 if (client.connect("arduinoClient")) {
   client.publish("wander/ejemplo","ready");
   client.subscribe("wander/ejemplo");
 }
 Serial.println("conexion establecida");
 
 pinMode(6, OUTPUT);
 pinMode(3, OUTPUT);

}

void loop()
{
client.loop();


  medida = analogRead(foto);
  luzled = 255 - (medida/4);
Serial.println(luzled);
 for(i=0 ; i<2 ; i++)
 {
   analogWrite(6, luzled);
   analogWrite(3, luzled);
   delay(tiempo);}
   
 root["Dispositivo"] = "LDR";
 root["Luz"] = luzled;

 root.printTo(json);
 
   client.publish("wander/ejemplo",json);

  delay(3000);
}

When I load the code, on the output I only receive :

" {}
 {}
 {}
 {}... "


Besides that I can not send it through mqtt


I'm used Arduino UNO and Ethercard..


The assembly is well done, to the serial output I can appreciate the values of the LDR, and I can introduce them to the json and visualize the contents, but at the moment of printing it in the variable char, "char json [100];" On output I only receive "{}" ..


somebody could help me? Thanks.!

Go Up