moeskerv / esp32-weather-station

26 stars 9 forks source link

SCREEN STUCK AT CONNECT TO WIFI PROGRESS BAR #7

Open hungphongtrn opened 1 year ago

hungphongtrn commented 1 year ago

Hi moeskerv, thank you for your amazing work! I am trying to build a similar one with ESP32 WROOM and another ILI9341 TFT 2.4inch, but without the SPS30, so I modified the code a little bit in the "loop"

void loop() {

    //static int cycle = 0;
    //static int task = 1;
    static unsigned long nextMillis = 0;
    static int lastSecond = 60;

    char *dstAbbrev;
    time_t now;
    struct tm * timeinfo;

    if (millis() >= nextMillis) { // if it is time, perform tasks scheduled
        // if (((task >> UPDATE_TURN_SDS30_ON) & 1U))  {
        //     if (sps30_start_measurement() < 0) {
        //         Serial.println("Error starting measurement");
        //     }
        //     else {
        //         Serial.println("SPS30 on");
        //         spsState = 1;
        //     }
        //     // next sleep time and tasks
        //     task &= ~(1UL << UPDATE_TURN_SDS30_ON);
        //     task |= 1UL << UPDATE_SENSORS;
        //     if (cycle == 0) task |= 1UL << UPDATE_FORECAST;
        //     nextMillis = millis() + SDS30_SETTLE_SECS * 1000; // next task in 30s
        // }
            // task &= ~(1UL << UPDATE_SENSORS);
            // ++cycle %= 3;
            // spsState = 0;
        Serial.println("Hello");
        updateSensorData();

        // connect to WiFi as we need to fetch some data
        if (WiFi.status() != WL_CONNECTED) {
                connectWifi();
            }

            if (WiFi.status() == WL_CONNECTED) {
                // update weather data from openweather              
                updateWeatherData();
                // disconnect WiFi
                WiFi.disconnect();
                while (WiFi.status() == WL_CONNECTED) delay(500);
                WiFi.mode(WIFI_OFF);

            }
            // shedule next task
            nextMillis = millis() +  UPDATE_INTERVAL_SECS  * 1000;
            // task |= 1UL << UPDATE_TURN_SDS30_ON;

        // set display brightness depending on time
        now = dstAdjusted.time(&dstAbbrev);
        timeinfo = localtime (&now);

        // turn on/off LCD backlight at night
        if ((timeinfo->tm_hour > 20) || (timeinfo->tm_hour < 6)) { // turn off from 21h - 6h
            ledcWrite(BACKLIGHT_PWM_CHN, 32);  // set to 12.5%
        }
        else {
            ledcWrite(BACKLIGHT_PWM_CHN, 128);  // set to 50%
        }

    }

    // get current time and check if the display has to be updated
    now = dstAdjusted.time(&dstAbbrev);
    timeinfo = localtime (&now);

    if (timeinfo->tm_sec != lastSecond) {
        Serial.println("Screening successful");
        lastSecond = timeinfo->tm_sec;
        // update screen
        gfx.fillBuffer(MINI_BLACK);
        Serial.println("Filled Buffer succesfully");
        drawTime();
        drawWifiQuality();
        drawForecast(0, 0);
        drawCurrentWeather();
        drawSensorValues();
        gfx.commit();
    }

    delay(100); // wait 100ms
}

The code uploaded flawlessly, however the screen was stuck at the WIFI progress bar without changing to the main screen. May you help me with this? Thank you a lot!