#include <Arduino.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <WiFi.h>
#include <DHT.h>
#include <WiFiMulti.h>
#include <WiFiClientSecure.h>

WiFiMulti WiFiMulti;
TaskHandle_t readerHandle;

DHT dht(26, DHT22);


[[noreturn]] void reader(void *pvParameters) {
    dht.begin();
    while (true) {
        if (WiFiClass::status() == WL_CONNECTED) {
            int temp = dht.readTemperature();
            Serial.printf("Task 1 Reads %d\n", temp);
            vTaskDelay(pdMS_TO_TICKS(1000));
        }
    }
}


void setup() {
  Serial.begin(9600);
    pinMode(2, OUTPUT);
    WiFi.mode(WIFI_AP_STA);
    WiFi.enableLongRange(true);
    WiFi.softAP("ESP-32-Server", "12345678910");
    WiFi.begin("Wokwi-GUEST");

    while (WiFiClass::status() != WL_CONNECTED) {
        Serial.println("Connecting..");
        delay(500);
    }
    Serial.printf("[WS]: Connected to %s\n", WiFi.SSID().c_str());
    xTaskCreate(reader, "reader", 4048, nullptr, 1, &readerHandle);
}

void loop() {
  delay(10); // this speeds up the simulation
}