/*
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 "DHT.h"
#include <LiquidCrystal_I2C.h>
#define DHTPIN 15 // GPIO that is connected to the sensor
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const byte LED = 25;
const byte LED1 = 26;
unsigned long lastMsg = 0;
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 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(2, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(LED1, OUTPUT);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
dht.begin();
// 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...");
lastMsg = millis();
}
void loop() {
if ((millis() - lastMsg) > 1000) {
ArduinoCloud.update();
// Your code here
ReadSensor();
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Temperature:");
LCD.print(temperature);
LCD.print(" C");
LCD.setCursor(0, 1);
LCD.print("Humidity:");
LCD.print(humidity);
LCD.print(" %");
if (ArduinoCloud.connected()) {
LCD.setCursor(0, 2);
LCD.print("connected");
}
else {
LCD.setCursor(0, 2);
LCD.print("not connected");
}
if (temperature > 20 && temperature < 40) {
digitalWrite(LED, HIGH);
led = true;
} else {
digitalWrite(LED, LOW);
led = false;
}
if (humidity > 60 && humidity < 88) {
digitalWrite(LED1, HIGH);
led1 = true;
} else {
digitalWrite(LED1, LOW);
led1 = false;
}
lastMsg = millis();
}
}
/*
Since Led1 is READ_WRITE variable, onLed1Change() is
executed every time a new value is received from IoT Cloud.
*/
void onLampu1Change() {
// Add your code here to act upon Led1 change
if (led) {
digitalWrite(LED, HIGH);
led = true;
} else {
digitalWrite(LED, LOW);
led = false;
}
if (led1) {
digitalWrite(LED1, HIGH);
led1 = true;
} else {
digitalWrite(LED1, LOW);
led1 = false;
}
}
/*
Since Led2 is READ_WRITE variable, onLed2Change() is
executed every time a new value is received from IoT Cloud.
*/
/*
Since DHT is READ_WRITE variable, onDHTChange() is
executed every time a new value is received from IoT Cloud.
*/
void onDhtTempChange() {
// Add your code here to act upon Temp change
}
/*
Since Humidity is READ_WRITE variable, onHumidityChange() is
executed every time a new value is received from IoT Cloud.
*/
void onDhtHumChange() {
// Add your code here to act upon Humidity change
}
void ReadSensor() {
float t = dht.readTemperature();
float u = dht.readHumidity();
if (isnan(u) || isnan(t)) {
Serial.println(F("data error!"));
return;
}
temperature = t;
humidity = u;
}
/*
Since Led is READ_WRITE variable, onLedChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLedChange() {
// Add your code here to act upon Led change
}