Serialize temperature data in JSON format

Hi all!

I'm trying to write a continuous flow of temperature data + timestamp from my DS1820 sensor and serialize it in JSON format to eventually be read and deserialized by another computer. However, as I'm new to Arduino JSON I am unsure of what is needed and what are the separate elements/actions to go about this.

  • What code should be added to the code below?
  • Should I work with a IOT cloud server like Adafruit IO (mentioned in the ArduinoJson book)?

Thank you in advance :slight_smile:

I have attached my arduino setup (image) and my current code

#include <DS18B20.h>

DS18B20 ds(2);

void setup() {
  Serial.begin(9600);
  Serial.print("Devices: ");
  Serial.println(ds.getNumberOfDevices());
  Serial.println();
}

void loop() {
  while (ds.selectNext()) {
    switch (ds.getFamilyCode()) {
      case MODEL_DS18S20:
        Serial.println("Model: DS18S20/DS1820");
        break;
      case MODEL_DS1822:
        Serial.println("Model: DS1822");
        break;
      case MODEL_DS18B20:
        Serial.println("Model: DS18B20");
        break;
      default:
        Serial.println("Unrecognized Device");
        break;
    }

    uint8_t address[8];
    ds.getAddress(address);

    Serial.print("Address:");
    for (uint8_t i = 0; i < 8; i++) {
      Serial.print(" ");
      Serial.print(address[i]);
    }
    Serial.println();

    Serial.print("Resolution: ");
    Serial.println(ds.getResolution());

    Serial.print("Power Mode: ");
    if (ds.getPowerMode()) {
      Serial.println("External");
    } else {
      Serial.println("Parasite");
    }

    Serial.print("Temperature: ");
    Serial.print(ds.getTempC());
    Serial.print(" C / ");
    Serial.print(ds.getTempF());
    Serial.println(" F");
    Serial.println();
  }

  delay(10000);
}

There is the ArduinoJSON library you can download and use to generate the JSON. After you generate it, you will have to decide where you will be sending it. If you are just sending it to the attached PC, you can use Serial. If you want to do some sort if IoT thing, you will need wifi and a cloud service like you mention.

Why the requirement to use JSON? It may be more than you need.

Alternatively, your JSON object sounds fairly simple, you could just create it manually with sprintf.

Thank you for your replies,

blh64:
There is the ArduinoJSON library you can download and use to generate the JSON. After you generate it, you will have to decide where you will be sending it. If you are just sending it to the attached PC, you can use Serial. If you want to do some sort if IoT thing, you will need wifi and a cloud service like you mention.

Why the requirement to use JSON? It may be more than you need.

The reason I would like to use JSON is because the data has to be read into a computational model that works with a different programming language. JSON would be a good universal way to write the data. It won't stay with this temperature sensors, eventually I will add 5+ sensors to the arduino board.

I have downloaded the ArduinoJSON library but am unsure of how to generate the JSON within my sensor program code. Should I implement a code like the JsonGeneratorExample within my sensor program?

wildbill:
Alternatively, your JSON object sounds fairly simple, you could just create it manually with sprintf.

Thank you, I will dive into the sprintf function.