/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/3a2697bd-68e1-4f81-92a0-ada162152e48 

  Arduino IoT Cloud Variables description

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

  CloudLight lED_output;
  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 "DHT.h"
// #include "thingProperties.h"
#define DHTPIN 14
#define DHTTYPE DHT22
#define SECRET_SSID "Wokwi-GUEST"
#define SECRET_OPTIONAL_PASS ""
#define SECRET_DEVICE_KEY "Y7A8VLITHRYGFDXOLSIL"

// Code generated by Arduino IoT Cloud, DO NOT EDIT.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char DEVICE_LOGIN_NAME[]  = "3edde1a7-e432-45c5-b9ae-ff30dce10717";

const char SSID[]               = SECRET_SSID;    // Network SSID (name)
const char PASS[]               = SECRET_OPTIONAL_PASS;    // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[]  = SECRET_DEVICE_KEY;    // Secret device password

void onLEDOutputChange();

CloudLight lED_output;
CloudTemperatureSensor temperature;
CloudRelativeHumidity humidity;

void initProperties(){

  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.addProperty(lED_output, READWRITE, ON_CHANGE, onLEDOutputChange);
  ArduinoCloud.addProperty(temperature, READ, ON_CHANGE, NULL);
  ArduinoCloud.addProperty(humidity, READ, ON_CHANGE, NULL);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

DHT dht(DHTPIN, DHTTYPE);

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

  // 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();
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  float hum = dht.readHumidity();
  float temp = dht.readTemperature();
if (isnan(hum) || isnan(temp) ){
  Serial.println(F("Failed to read from DHT sensor!"));
  return;
}
humidity = hum;
temperature = temp;
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print("°C");
Serial.print(" Humidity: ");
Serial.print(hum);
Serial.print("%");
Serial.println();
delay(1000);

}

/*
  Since LEDOutput is READ_WRITE variable, onLEDOutputChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLEDOutputChange()  {
  // Add your code here to act upon LEDOutput change
  if(lED_output ==1)
  {
    digitalWrite(2,HIGH);
  }
  else{
    digitalWrite(2,LOW);
  }
}