Using ESP_NOW with loop and delays

  arduino-ide, c++, delay, esp32, loops

I’m trying to receive a data from one esp32 to another.
I’m also doing some looping with delays for reading a sensor data and switch on/off relay cooling.
This device also use ESPAsyncWebServer as API server (not included in code for the size).
I’m receiving the data from one eps32 in POST request to API right now. I would like to change this as to be able to recieve with esp_now. I was doing some experiments, but the data receving is delayed because of delay method in loop.
Is there a way to make this asynchronized as for example the ESPA above?

I also tried a way with comparing a milis() (time) to wait for a loop, but I thing that is too "resource consuming" task, to let it compare in full speed in loop like a vortex forever.

here is my loop


void loop() {
    if (WiFi.status() == WL_CONNECTED) {
        float temp_check = dht.readTemperature();

        if (temp_check == 0) {
            delay(1000);
            temp_check = dht.readTemperature();
        }

        if (temp_check > 32.0 && *cooling_switch_p == false) {
            Serial.println("[ INF ] Too high temperature, switch on a cooling system");
            digitalWrite(COOLING_RELAY, LOW);
            *cooling_switch_p = true;
        }
        else if (temp_check < 30.0  && *cooling_switch_p == true) {
            Serial.println("[ INF ] Normal temperature, switch off a cooling system");
            digitalWrite(COOLING_RELAY, HIGH);
            *cooling_switch_p = false;
        }

        Serial.print("[ DBG ] Light Switch: ");
        Serial.println(String(light_switch));
        Serial.println("");

        Serial.print("[ DBG ] Pump Switch: ");
        Serial.println(String(pump_switch));
        Serial.println("");
        delay(5000);
    }

    else {
        Serial.println("[ ERR ] Wifi not connected. Exiting program");
        delay(9999);
        exit(0);
    }
}

Source: Windows Questions C++

LEAVE A COMMENT