1

A friend and I have been working on an Arduino Sketch that does a request to a local API, parses that data and prints it out on an LCD Display.

When we upload the sketch to the Arduino it does exaclty what it should and shows the following on the LCD Display: LCD with Data

But when we disconnect the Arduino from the PC and reconnect it to the PC (or to any other power source), the following shows up on the LCD: enter image description here

It seems as if the Arduino simply does not restart the Sketch and stays idle, while giving the LCD a weird output. When we open the Serial Monitor, in the Arduino IDE, the Arduino reboots and the correct information shows up again. Pressing the reset button on the Arduino itself did not work and seems to do nothing.

When trying other examples (e.g. Blink) it does restart on reconnecting to the PC, so it seems that the problem lies with our code.

Here is our full code:

#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoJson.h> // Library to deserialize JSON
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);

char ssid[] = "WifiSSID";
char pass[] = "WifiPassword";
bool reading = false;

int refreshRate = 5000;

int status = WL_IDLE_STATUS;
IPAddress server(192,168,1,201);

WiFiClient client;

char json[512];
StaticJsonDocument<512> doc;

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

  memset(json, 0, sizeof json);
  
  lcd.init();
  lcd.backlight();

  lcd.setCursor(4, 1); 
  lcd.print("CONNECTING.."); 
  status = WiFi.begin(ssid, pass);

  if (status != WL_CONNECTED) {
    while(status != WL_CONNECTED) {
      Serial.println("Retrying connection in two seconds...");
      delay(2000);
      status = WiFi.begin(ssid, pass);
    }
  }
  else {
    lcd.setCursor(0, 1); 
    lcd.print("                    ");
  
    Serial.println("Connected to wifi");
    Serial.println("\nStarting connection...");
    sendRequest();
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    if (c == '[') {
      reading = true;
      memset(json, 0, sizeof json);
    } else if (c == ']') {
      reading = false;
      printJson();
    } else if (reading) {
      int len = strlen(json);
      json[len] = c;
      json[len+1] = '\0';
    }
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    client.stop();
    delay(refreshRate);
    sendRequest();
  }
}

void sendRequest() {
  if (client.connect(server, 80)) {
      Serial.println("Connected to server. Requesting data...");
      client.println("GET /api/v1/smartmeter?json=object&limit=1&round=on HTTP/1.0");
      client.println();
    }
}

void printJson() {
  DeserializationError error = deserializeJson(doc, json);

  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    return;
  }

  int energy = doc["CONSUMPTION_W"];
  float gas = doc["CONSUMPTION_GAS_M3"];
  Serial.println(json);
  Serial.println(energy);
  Serial.println(gas);
  
  lcd.setCursor(0, 0); 
  lcd.print("CONSUMPTION ENERGY:"); 
  lcd.setCursor(0, 1); 
  lcd.print(energy); 
  lcd.setCursor(8, 1); 
  lcd.print("WATT"); 
  
  lcd.setCursor(0, 2); 
  lcd.print("CONSUMPTION GAS:"); 
  lcd.setCursor(0, 3); 
  lcd.print(gas); 
  lcd.setCursor(8, 3); 
  lcd.print("M3"); 
}

Would any of you maybe know why the Arduino refuses to restart on reconnecting to the PC or a power source, and how to fix it?

Thanks in advance!

5

You've tagged the question with both nano and arduino-nano-33-iot, but I'm guessing between the two you're using the latter, because on Arduinos with native USB (not a plain Arduino Nano) while (!Serial) continue; waits for a serial connection, e.g. Serial Monitor, to be connected.

This doesn't happen when you're on a power supply, or just connecting it to a machine without opening the port.

If you want blink an led inside this loop to see that it is in fact stuck there.

1
  • 1
    Hey mage! We removed the while (!Serial) continue; from the code and it works perfectly now! Thanks so much for the quick answer. Cheers!
    – Mithras
    Jun 7 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.