Go Down

Topic: Process String form MQTT with a ESP8266 (Read 91 times) previous topic - next topic

Grilli303

Sep 18, 2019, 03:31 pm Last Edit: Sep 18, 2019, 03:38 pm by Grilli303
Hi there, I need your help please.

In my Project I use the RTL_433 program and send the data to a MQTT broker.
I recieve the data with a MQTT Client (Mosquitto) on a ESP8266.
So far, so god, but I don't know how to get the Values I need out of this string.

Here is an example of a String i get:

"{"time":"2019-09-11 14:11:28","model":"inFactory sensor","id":80,""temperature_C":17.33333,"humidit y":66}"

I need the temperature and humidity value and want to send this values to a display.
Do you have any ideas how I can do that ?

I already tried to use some Arduino JSON library examples, but it doesent work.


Thank you for your help.

PS: you can reply in English or German
PS: Du kannst in English oder Deutsch antworten

wildbill

Assuming that that is a C string, not a String, you can use strstr to look for something in the string. temperature_c perhaps. then you know that the actual temperature data you're looking at will appear fifteen characters later so you can pass that address to atof to get the value. 

Grilli303

Thank you for your quick reply. I did not understand your answer, "Assuming that that is a C string".
I use the Arduino Software for programming the ESP8266. Is it possible to use strstr command, when I write my programing code with the Arduino Software ? If not, is there a similiar command available ?
Do I have some alternative Software to write a sourcecode for an ESP8266 ?
 

Danois90

Why does the JSON library not work, what errors do you get?
Instead of mocking what's wrong, teach what's right! ;)
When you get help, remember to thank the helper and give some karma!
Please, do NOT send me any private messages!!

Grilli303

I tried an Example Code that uses the JSON library but I get the following error:
invalid digit "9" in octal constant.
But the Variable for the String exaple in this Code is defined as a const char.
Whith the my example String it desent't work.
if I remove the time part of the String I get the following error:
missing terminating " character


/*
  JSON Object

  This sketch demonstrates how to use various features
  of the Official Arduino JSON library, in particular for JSON objects.

  This example code is in the public domain.
*/

#include <Arduino_JSON.h>

//const char input[] = "{\"result\":true,\"count\":42,\"foo\":\"bar\"}";
const char input[] = "{"time":"2019-09-11 14:11:28","model":"inFactory sensor","id":80,""temperature_C":17.33333,"humidit y":66}";

void setup() {
  Serial.begin(9600);
  while (!Serial);

  demoParse();

  demoCreation();
}

void loop() {
}

void demoParse() {
  Serial.println("parse");
  Serial.println("=====");

  JSONVar myObject = JSON.parse(input);

  // JSON.typeof(jsonVar) can be used to get the type of the var
  if (JSON.typeof(myObject) == "undefined") {
    Serial.println("Parsing input failed!");
    return;
  }

  Serial.print("JSON.typeof(myObject) = ");
  Serial.println(JSON.typeof(myObject)); // prints: object

  // myObject.hasOwnProperty(key) checks if the object contains an entry for key
  if (myObject.hasOwnProperty("result")) {
    Serial.print("myObject[\"result\"] = ");

    Serial.println((bool) myObject["result"]);
  }

  if (myObject.hasOwnProperty("count")) {
    Serial.print("myObject[\"count\"] = ");

    Serial.println((int) myObject["count"]);
  }

  if (myObject.hasOwnProperty("count")) {
    Serial.print("myObject[\"count\"] = ");

    Serial.println((double) myObject["count"]);
  }

  if (myObject.hasOwnProperty("foo")) {
    Serial.print("myObject[\"foo\"] = ");

    Serial.println((const char*) myObject["foo"]);
  }

  // JSON vars can be printed using print or println
  Serial.print("myObject = ");
  Serial.println(myObject);

  Serial.println();
}

void demoCreation() {
  Serial.println("creation");
  Serial.println("========");

  JSONVar myObject;

  myObject["hello"] = "world";
  myObject["true"] = true;
  myObject["x"] = 42;

  Serial.print("myObject.keys() = ");
  Serial.println(myObject.keys());

  // JSON.stringify(myVar) can be used to convert the json var to a String
  String jsonString = JSON.stringify(myObject);

  Serial.print("JSON.stringify(myObject) = ");
  Serial.println(jsonString);

  Serial.println();

  // myObject.keys() can be used to get an array of all the keys in the object
  JSONVar keys = myObject.keys();

  for (int i = 0; i < keys.length(); i++) {
    JSONVar value = myObject[keys];

    Serial.print("JSON.typeof(myObject[");
    Serial.print(keys);
    Serial.print("]) = ");
    Serial.println(JSON.typeof(value));

    Serial.print("myObject[");
    Serial.print(keys);
    Serial.print("] = ");
    Serial.println(value);

    Serial.println();
  }

  Serial.println();

  // setting a value to undefined can remove it from the object
  myObject["x"] = undefined;

  // you can also change a value
  myObject["hello"] = "there!";

  Serial.print("myObject = ");
  Serial.println(myObject);
}

Danois90

#5
Sep 18, 2019, 08:42 pm Last Edit: Sep 18, 2019, 08:42 pm by Danois90
Please use code tags when posting code. The JSON is not valid, a comma is ill positioned (right after "80") and the quotes are not escaped. Try to replace:

Code: [Select]
const char input[] = "{"time":"2019-09-11 14:11:28","model":"inFactory sensor","id":80,""temperature_C":17.33333,"humidit y":66}";

with:

Code: [Select]
const char input[] = "{\"time\":\"2019-09-11 14:11:28\",\"model\":\"inFactory sensor\",\"id\":80\",\"temperature_C\":17.33333,\"humidit y\":66}";

Look carefully at the differences.
Instead of mocking what's wrong, teach what's right! ;)
When you get help, remember to thank the helper and give some karma!
Please, do NOT send me any private messages!!

wildbill

Moreover, using the JSON library for this little thing is overkill. Here it is old school:
Code: [Select]

const char input[] = "{\"time\":\"2019 - 09 - 11 14: 11: 28\",\"model\":\"inFactory sensor\",\"id\":80,\"temperature_C\": 17.33333, \"humidity\": 66}";

void setup()
{
Serial.begin(115200);
char *ptr=strstr(input,"temperature_C");
float tempC=atof(&ptr[15]);
Serial.print("Temperature: ");
Serial.println(tempC);
}

void loop()
{
}


Go Up