Change Serial monitor to JSON output

Hi, I'm using mine Raspberry Pi 4 with Homeassistant as home automation.
Now I trying to make with mine Arduino UNO a weather station.
Every sensor is working, and I want to sent it with a ESP8266 module (wifi) to mine Raspberry.
But this doesn't work fine :frowning: Now I'm trying to do it with connected the Arduino directly to the raspberry.
The raspberry can read the values from the serial monitor from mine Arduino.
But to use these values in home assistant I need to use the JSON style.

Now mine question, how can I change mine skatch so I see a JSON style output?

this is the sketch that I maked/found on many different websites :wink:

#include </Users/tomjanssen/Documents/Arduino/sketch_compleet_With_Jason/OneWire.h>
#include </Users/tomjanssen/Documents/Arduino/sketch_compleet_With_Jason/DallasTemperature.h>
#include <ArduinoJson.h>

#define ONE_WIRE_BUS 5        // input temperatuur sensor

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

 float Celcius=0;               // t.b.v. de temperatuur sensor
 float Fahrenheit=0;            // t.b.v. de temperatuur sensor
 int ldrPin = A1;              // input pin t.b.v. de licht sensor
int ldrVal = 0;               // Waarde van potmeter t.b.v. de licht sensor
const int sensorMin = 0;     // regen sensor minimum 
const int sensorMax = 1024;  // regen sensor maximum

void setup(void)
{
  
  Serial.begin(9600);                          // Stel de seriële monitor in
  sensors.begin();
}

void loop(void)
{ 
  sensors.requestTemperatures();                  // temperatuur sensor
  Celcius=sensors.getTempCByIndex(0);             // temperatuur sensor
  Fahrenheit=sensors.toFahrenheit(Celcius);       // temperatuur sensor
  Serial.print(" C  ");                           // temperatuur sensor
  Serial.print(Celcius);                         // temperatuur sensor
  Serial.print(" F  ");                           // temperatuur sensor
  Serial.println(Fahrenheit);                   // temperatuur sensor
  
  delay(100);                                   //interval uitlezen waardes
  
   ldrVal = analogRead(ldrPin);              // Lees de analoge waarde van de LDR licht sensor
  Serial.println(ldrVal);                    // Toon de waarde in de seriële monitor licht sensor
 
    int sensorReading = analogRead(A0);       // lees de regen sensor op analog A0:
   // map the sensor range (four options):
  // ex: 'long int map(long int, long int, long int, long int, long int)' 
      int range = map(sensorReading, sensorMin, sensorMax, 0, 3);  
      // range value:
  switch (range) {
 case 0:    // Sensor getting wet
    Serial.println("Stort regen");
    break;
 case 1:    // Sensor getting wet
    Serial.println("regen");
    break;
 case 2:    // Sensor dry - To shut this up delete the " Serial.println("'t is droog"); " below.
    Serial.println("'t is droog");
    break; 
  }
  delay(1);  // delay between reads
}

I hope to hear from you.
Thanks you a lot!

just use serial.print() to send out a properly formatted json, something that could look like this

[color=blue]{"temperatureC":[color=red]22[/color], "sensorValue":[color=red]217[/color], "message":[color=red]"Stort regen"[/color]}[/color]

the blue part is always the same, the red part comes from printing information from your sensors.

Mind the double quotes, they need to be escaped with an .

For example to get [color=blue]{"temperature":[/color] you would do Serial.print(F("{\"temperatureC\":"));

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