Sending json data to serial and display in msg payload

Hi, i plan to sending json data to serial and display to msg. payload but didnt work. and for continue i want sending json data to serial and read by hc12 but didnt work

i applied to software serial for tracer where part didnt work. when it works i will send to hc12 by software serial and parser json data

//Concentrator

#include <SoftwareSerial.h>
#include <ArduinoJson.h>
#define rxPin 5
#define txPin 4
#define setPin 6

SoftwareSerial HC12(rxPin, txPin); // ke arah sensor node
//SoftwareSerial gateway(

void setup() {
Serial.begin(19200);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
HC12.begin(9600);
if (HC12.isListening()) {
Serial.println("OK");
} else {
Serial.println("NOK");
}
}

// keep in sync with ESP_NOW sensor struct
struct attribute((packed)) SENSOR_DATA {
uint16_t sessionid;
int idmeter[10];
int angkastandmeter[10];
boolean state[10];
} sensorData;

void loop() {
//Parsing paket data (JSON) yang diterima dari Nodered
while (Serial.available()) {
delay (500);

StaticJsonDocument<300> doc; //alokasi memori untuk data yg disimpan ke dokuemn
DeserializationError error = deserializeJson(doc, Serial);//untuk parsing jsdon input dan disimpan ke json document
if (error) {
  Serial.println(F("deserializeJson() failed: "));
  Serial.println(error.c_str());
  return;
}
JsonObject sessionid = doc["sessionid"].as<JsonObject>();
JsonObject N = doc["N"].as<JsonObject>();
JsonArray idmeter = doc["idmeter"].as<JsonArray>();
JsonArray angkastandmeter = doc["angkastandmeter"].as<JsonArray>();
JsonArray state = doc["state"].as<JsonArray>();
JsonObject uplink = doc["uplink"].as<JsonObject>();

sensorData.sessionid = doc["sessionid"];
for (int i = 0; i < N ; i++) {
  sensorData.idmeter[i] = doc["idmeter"];
  sensorData.angkastandmeter[i] = doc["angkastandmeter"];
  sensorData.state[i] = doc["state"];
}

delay(500);
uint8_t bs[sizeof(sensorData)];
memcpy(bs, &sensorData, sizeof(sensorData));

HC12.println("$$");         //just for indicator
HC12.write(sizeof(bs));
HC12.write(bs, sizeof(bs));

}

while (HC12.available()) {
delay(2000);
if (HC12.read() == '&') {
while ( ! HC12.available() ) {
delay(1);
}
if ( HC12.read() == '&' ) {
byte len = HC12.read();
while (HC12.available() < len) {
delay(1);
}
HC12.readBytes((char*)&sensorData, len);
StaticJsonDocument<300> dok;
JsonObject sessionid = dok.createNestedObject("sessionid");
dok["sessionid"] = sensorData.sessionid;
JsonArray id = dok["idmeter"].to();
JsonArray astd = dok["angkastandmeter"].to();
JsonArray st = dok["state"].to();
for (int i = 0; i < 10; i++) {
id.add(sensorData.idmeter[i]);
astd.add(sensorData.angkastandmeter[i]);
st.add(sensorData.state[i]);
}
delay(500);
serializeJsonPretty(dok, Serial);
}
}
}
}

please help me

Are you asking for help with the Arduino code or do you think it is a problem in node-red?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.