#include <Arduino.h>
#include <Wire.h>
#include <DHT.h>
//#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
//Pin definition for DHT
#define DHTPIN 15
#define DHTTYPE DHT22
//Pin definition for LCD Screen
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
//Pin definition for PIR Sensor and LED
#define LEDPIN 27
#define PIR_PIN 14
int val = LOW; //variable for the motion sensor
float humidity = 0.0; //variable for humidity
float temperature = 0.0; //variable for temperature
DHT dht(DHTPIN, DHTTYPE); //Create an object for the DHT Sensor
//LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES); //Create an object for the LCD Screen
//Wifi Connection Details
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String serverName = "https://postman-echo.com/post";
//Declaration of task functions
//Task 1: Blink LED
void BlinkLED (void *Parameters){
for(;;){ //infintie Loop
// Turn the LED on
digitalWrite(LEDPIN, HIGH);
// Pause the task for 500ms
vTaskDelay(500 / portTICK_PERIOD_MS);
// Turn the LED off
digitalWrite(LEDPIN, LOW);
// Pause the task again for 500ms
vTaskDelay(500 / portTICK_PERIOD_MS);
Serial.println("LED is blinking ...");
}
}
//Task 2: Read sensors
void ReadSensors (void *Parameters){
for(;;){ //infintie Loop
val = digitalRead(PIR_PIN); // Read movement sensor
humidity = dht.readHumidity(); // Read humidity
temperature = dht.readTemperature(); // Read Temperature
// Print humidity and temperature in the console
Serial.print("Humidity: " + String(humidity) + "% \n");
Serial.print("Temperature: " + String(temperature) + "°C \n");
Serial.println(" --------- ");
// Pause the task again for 500ms
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
//Task 3: Read sensors
void SendingData (void *Parameters){
for(;;){ //infintie Loop
//Prepare Json String
DynamicJsonDocument doc(1024);
String jsonstr;
JsonObject root = doc.to<JsonObject>();
root["temperature"] = temperature;
root["humidity"] = humidity;
root["Movement"] = val;
serializeJson(doc, jsonstr);
String httpRequestData = jsonstr;
//Send information
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverName.c_str()); // Send HTTP POST request
//Set the content-type
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(httpRequestData); //Send the request
//Analyse response code
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: "); Serial.println(httpResponseCode);
WiFi.begin(ssid, password, 6); //Reconnect when the connection is lost
}
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
// Pause the task again for 500ms
vTaskDelay(1000 / portTICK_PERIOD_MS);
Serial.println("Sending Data ...");
}
}
void setup() {
//Pin configuration
pinMode(LEDPIN, OUTPUT);
pinMode(PIR_PIN, INPUT);
// Sensors initialize
dht.begin();
//lcd.init();
//lcd.backlight();
// Start serial communication
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password, 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
//Create Tasks
xTaskCreate(
BlinkLED, // Function that should be called
"Blink LED", // Name of the task (for debugging)
1024, // Stack size (bytes)
NULL, // Parameter to pass
1, // Task priority
NULL // Task handle
);
xTaskCreate(
ReadSensors, // Function that should be called
"ReadSensors", // Name of the task (for debugging)
2048, // Stack size (bytes)
NULL, // Parameter to pass
2, // Task priority
NULL // Task handle
);
xTaskCreate(
SendingData, // Function that should be called
"SendingData", // Name of the task (for debugging)
4096, // Stack size (bytes)
NULL, // Parameter to pass
3, // Task priority
NULL // Task handle
);
}
void loop() {
// Print humidity and temperature on the LCD
/*
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("%");
*/
delay(1000);
}