/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/c8653c70-19cf-4c81-be45-92fde1df7950

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  CloudLight led;
  CloudTemperatureSensor temperature;
  CloudRelativeHumidity humidity;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);

unsigned long lastMsg = 0;

void setup() {
  // Initialize serial and wait for port to open:
  //Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  //delay(1500);
  //Serial.println("Init");

  pinMode(34, INPUT);

  LCD.init();
  LCD.backlight();
  LCD.setCursor(0, 0);
  LCD.print("Connecting to ");
  LCD.setCursor(0, 1);
  LCD.print("WiFi ");

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
  */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

  LCD.clear();
  LCD.setCursor(0, 0);
  LCD.println("Online");
  LCD.setCursor(0, 1);
  LCD.println("Updating time...");

}

void loop() {  
  ArduinoCloud.update();
  // Your code here

  if ((millis() - lastMsg) > 1000) {
    sensorValue = analogRead(34);

    LCD.clear();
    LCD.setCursor(0, 0);
    LCD.print("Data Sent:");
    LCD.print(sensorValue);

    if (ArduinoCloud.connected()) {
      LCD.setCursor(0, 2);
      LCD.print("connected");
    }
    else {
      LCD.setCursor(0, 2);
      LCD.print("not connected");
    }
      lastMsg = millis();
  }
}