// #include <gpio_viewer.h> // Must me the first include in your project
// GPIOViewer gpio_viewer;
#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0;
#else
static const BaseType_t app_cpu = 1;
#endif
#include <DHT.h>
#include <WiFi.h>
#include "time.h"
#ifndef LED_BUILTIN
#define LED_BUILTIN 2 // Specify the on which is your LED
#endif
// Sensor
#define DHTPIN 23 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT22
DHT dht(DHTPIN, DHTTYPE);
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const char *ntpServer = "0.id.pool.ntp.org";
const long gmtOffset_sec = 25200;
const int daylightOffset_sec = 0;
// pins
const int LED1 = 18;
const int LED2 = 19;
const int LED3 = 21;
const int LED4 = 22;
// Global value
int humidity_value = 0;
int temperature_value = 0;
// Define tasks.
TaskHandle_t TaskLed1;
TaskHandle_t TaskLed2;
TaskHandle_t TaskLed3;
TaskHandle_t TaskLed4;
TaskHandle_t TaskSensor;
void setup() {
Serial.begin(115200);
// Inspect our own high water mark on entering the task
UBaseType_t uxHighWaterMark;
uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);
Serial.print("HighWaterMark : ");
Serial.println(uxHighWaterMark);
// connect to WiFi
unsigned long StartTime = millis();
Serial.printf("Connecting to %s ", ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.persistent(false);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print(" Connected: ");
Serial.println(WiFi.localIP());
unsigned long CurrentTime = millis();
unsigned long ElapsedTime = CurrentTime - StartTime;
Serial.print("Elapsed Time to Connect: ");
Serial.println(ElapsedTime);
// init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
// Set up tasks to run independently.
uint32_t blink_delay = 1000; // Delay between changing state on LED pin
xTaskCreate(
TaskBlink,
"Task Blink", // A name just for humans
2048, // The stack size can be checked by calling `uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);`
(void *)&blink_delay, // Task parameter which can modify the task behavior. This must be passed as pointer to void.
3, // Priority
NULL); // Task handle is not used here - simply pass NULL
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
xTaskCreatePinnedToCore(
TaskLed1code, // Task function.
"TaskLed1", // name of task.
2048, // Stack size of task
NULL, // parameter of the task
1, // priority of the task
&TaskLed1, // Task handle to keep track of created task
CONFIG_ARDUINO_RUNNING_CORE); // Core on which the task will run
//0); // pin task to core 0
xTaskCreatePinnedToCore(
TaskLed2code, // Task function.
"TaskLed2", // name of task.
2048, // Stack size of task
NULL, // parameter of the task
1, // priority of the task
&TaskLed2, // Task handle to keep track of created task
CONFIG_ARDUINO_RUNNING_CORE); // Core on which the task will run
//1); // pin task to core 1
xTaskCreatePinnedToCore(
TaskLed3code, // Task function.
"TaskLed3", // name of task.
2048, // Stack size of task
NULL, // parameter of the task
1, // priority of the task
&TaskLed3, // Task handle to keep track of created task
CONFIG_ARDUINO_RUNNING_CORE); // Core on which the task will run
xTaskCreatePinnedToCore(
TaskLed4code, // Task function.
"TaskLed4", // name of task.
2048, // Stack size of task
NULL, // parameter of the task
1, // priority of the task
&TaskLed3, // Task handle to keep track of created task
CONFIG_ARDUINO_RUNNING_CORE); // Core on which the task will run
xTaskCreatePinnedToCore(
TaskSensorcode, // Task function.
"TaskSensor", // name of task.
2048, // Stack size of task
NULL, // parameter of the task
4, // priority of the task
&TaskSensor, // Task handle to keep track of created task
CONFIG_ARDUINO_RUNNING_CORE); // Core on which the task will run
//gpio_viewer.begin();
}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Sync time in progress...");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}
void TaskBlink(void *pvParameters) { // This is a task.
uint32_t blink_delay = *((uint32_t *)pvParameters);
// initialize digital LED_BUILTIN on pin 2 as an output.
pinMode(LED_BUILTIN, OUTPUT);
for (;;) { // A Task shall never return or exit.
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
// arduino-esp32 has FreeRTOS configured to have a tick-rate of 1000Hz and portTICK_PERIOD_MS
// refers to how many milliseconds the period between each ticks is, ie. 1ms.
delay(blink_delay);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
Serial.print(F(" LED 0 Core: "));
Serial.println(xPortGetCoreID());
delay(blink_delay);
}
}
// TaskLed1code: blinks an LED1 every 1s
void TaskLed1code(void *pvParameters) {
while (1) {
Serial.print(F(" LED 1 Core: "));
Serial.println(xPortGetCoreID());
digitalWrite(LED1, HIGH);
vTaskDelay(1000 / portTICK_PERIOD_MS);
digitalWrite(LED1, LOW);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
vTaskDelete(TaskLed1);
}
// TaskLed2code: blinks an LED2 every 2s
void TaskLed2code(void *pvParameters) {
while (1) {
Serial.print(F(" LED 2 Core: "));
Serial.println(xPortGetCoreID());
digitalWrite(LED2, HIGH);
vTaskDelay(2000 / portTICK_PERIOD_MS);
digitalWrite(LED2, LOW);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
vTaskDelete(TaskLed2);
}
// TaskLed3code: blinks an LED3 every 3s
void TaskLed3code(void *pvParameters) {
while (1) {
Serial.print(F(" LED 3 Core: "));
Serial.println(xPortGetCoreID());
digitalWrite(LED3, HIGH);
vTaskDelay(3000 / portTICK_PERIOD_MS);
digitalWrite(LED3, LOW);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
vTaskDelete(TaskLed3);
}
// TaskLed3code: blinks an LED3 every 4s
void TaskLed4code(void *pvParameters) {
while (1) {
Serial.print(F(" LED 4 Core: "));
Serial.println(xPortGetCoreID());
digitalWrite(LED4, HIGH);
vTaskDelay(4000 / portTICK_PERIOD_MS);
digitalWrite(LED4, LOW);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
vTaskDelete(TaskLed3);
}
// TaskSensorcode: read DHT22 sensor
void TaskSensorcode(void *pvParameters) {
(void)pvParameters;
dht.begin();
while (1) // A Task shall never return or exit.
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
} else {
printLocalTime(); // it will take some time to sync time :)
Serial.print(F("Temperature: "));
Serial.print(t);
Serial.print(F("°C | Humidity: "));
Serial.print(h);
Serial.println(F("% "));
humidity_value = (int)h;
temperature_value = (int)t;
}
Serial.print(F("Sensor Core: "));
Serial.println(xPortGetCoreID());
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
vTaskDelete(TaskSensor);
}
void loop() {
}