Skip to main content
Tweeted twitter.com/StackArduino/status/1396435746798571522
Became Hot Network Question
edited title
Link
krystof18
  • 315
  • 1
  • 4
  • 14

getting Getting all data of my JSON object from Serial.read() at once

Source Link
krystof18
  • 315
  • 1
  • 4
  • 14

getting all data from Serial.read() at once

I'm new to Arduino and I'm building a project that fetches data from a website (using ESP8266) and then transfers them to my Arduino UNO via serial port.

Data coming from ESP8266 every 20s - [{"x": 0,"y": 0,"rgb": [255, 200, 174]},{"x": 1,"y": 0,"rgb": [255, 200, 174]},{"x": 2,"y": 0,"rgb": [255, 200, 174]}]

But when I print the data like this


void loop() {
  
  if (Serial.available()) {
    Serial.write(Serial.read()); // This line prints only one character out of whole string
    
    data =  Serial.read(); // so this doesn't work

    DeserializationError error = deserializeJson(response, data);
    if (error) {
      Serial.print(F("deserializeJson() failed: "));
      Serial.println(error.f_str());
      return;
    }
    for (JsonObject elem : response.as<JsonArray>()) {
      int x = elem["x"]; // 2, 2, 2
      int y = elem["y"]; // 2, 2, 2
      JsonArray rgb = elem["rgb"];
    
      int rgb_0 = rgb[0]; // 255, 255, 255
      int rgb_1 = rgb[1]; // 255, 255, 255
      int rgb_2 = rgb[2]; // 255, 255, 255
      //Serial.print(x);
    }
  }
  delay(1000);   //then it waits 1 second and goes from the start
}

How can I make it to get the whole string and save it into a variable?

Thanks, much for answering.