/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/7946428b-c804-4d4e-8c9b-c1d9f2e73f64
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
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);
#define TEMP_THRESHOLD 25.0
#define lED 5
// Initialize LCD screen with I2C address 0x27
LiquidCrystal_I2C lcd(0x27, 16, 2);
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);
dht.begin();
// Initialize LCD screen
lcd.init();
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("Temperature ");
lcd.setCursor(0, 1);
lcd.print("Monitoring System");
// Initialize LED pin as an output
pinMode(lED, OUTPUT);
// 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
readSensor();
Serial.println();
Serial.print("Humidity: ");
Serial.print(hUMIDITY);
Serial.print("%");
Serial.print(" | ");
Serial.print("Temperature: ");
Serial.print(tEMPERATURE);
Serial.print("°C");
// Display temperature and humidity on LCD screen
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(tEMPERATURE);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(hUMIDITY);
lcd.print("%");
// Check if temperature exceeds threshold
if (tEMPERATURE > TEMP_THRESHOLD) {
// Display danger message on LCD
lcd.setCursor(0, 1);
lcd.print("Danger!Temp>");
lcd.print(TEMP_THRESHOLD);
lcd.print("C");
// Blink LED for emergency indication
// blinkLED();
digitalWrite(lED, HIGH);
}
// Delay between readings
delay(5000);
}
void readSensor(){
float t = dht.readTemperature();
float u = dht.readHumidity();
if (isnan(u) || isnan(t)) {
Serial.println(F("Failed to read sensor!"));
return;
}
tEMPERATURE = t;
hUMIDITY = u;
delay(5000);
}
void blinkLED() {
// Blink LED for emergency indication
for (int i = 0; i < 3; i++) {
digitalWrite(lED, HIGH); // Turn LED on
delay(500); // Wait for 500 milliseconds
digitalWrite(lED, LOW); // Turn LED off
delay(500); // Wait for 500 milliseconds
}
}
void onLEDChange() {
// Get the new LED state from Arduino Cloud
bool newLEDState;
}