#include "DHTesp.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <string.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String serverNamePost = "https://postman-echo.com/post";
DHTesp dhtSensor;
int dhtPin = 15;
int ledPin = 14;
int pirPin = 27;
int pirState = LOW;
int val = 0;
//Task 1: blink LED
void BlinkLED( void *pvParameters );
//Task 2: Read data from sensors
void ReadSensors( void *pvParameters );
//Task 3: Send data to server
void SendData(void *pvParameters);
struct SensorData {
float temperature;
float humidity;
int motion;
};
QueueHandle_t dataQueue; // queue to pass data from reader to sender
void setup() {
Serial.begin(9600);
// init dht
dhtSensor.setup(dhtPin, DHTesp::DHT22);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(pirPin, INPUT); // declare sensor as input
Serial.println("Multi-tasking with FreeRTOS ...");
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Wait for WiFi... ");
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to WiFi ...");
delay(1000);
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
dataQueue = xQueueCreate(30, sizeof(SensorData));
if (dataQueue == NULL) {
Serial.println("Failed to create queue!");
while (1);
}
//Create the task 1 to run BlinkLED
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
);
// task 2
xTaskCreate(ReadSensors, "Read sensors", 1024, NULL, 1, NULL);
// task 3
xTaskCreate(SendData, "Send data", 4096, NULL, 1, NULL);
delay(500);
}
//The function for Task 1
void BlinkLED( void *pvParameters ){
for(;;){ // infinite 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 ...");
}
}
//The function for Task 2
void ReadSensors( void *pvParameters ){
for(;;){ // infinite loop
// get data from dht
TempAndHumidity data = dhtSensor.getTempAndHumidity();
val = digitalRead(pirPin);
SensorData sensorData = {
.temperature = data.temperature,
.humidity = data.humidity,
.motion = val
};
// send data to the queue
if (xQueueSend(dataQueue, &sensorData, 0) != pdPASS) {
Serial.println("Failed to send data to the queue!");
}
vTaskDelay(2000 / portTICK_PERIOD_MS); // Wait for 2 seconds
}
}
void SendData(void* pvParameters){
for(;;){ // infinite loop
SensorData data;
// Receive data from the queue
if (xQueueReceive(dataQueue, &data, portMAX_DELAY) == pdPASS) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// send POST request (url-encoded)
http.begin(serverNamePost);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "api_key=tPmAT5Ab3j7F9&temp=" + String(data.temperature, 2) + "&hum=" + String(data.humidity, 1) + "&motion=" + String(data.motion);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code (post url-encoded req): ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error code: "); Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void loop() {
}