static void wifi_task(void *pvParameters) { uint8_t status = 0; uint8_t retries = 30; struct sdk_station_config config = { .ssid = WIFI_SSID, .password = WIFI_PASS, }; printf("WiFi: connecting to WiFi\n\r"); sdk_wifi_set_opmode(STATION_MODE); sdk_wifi_station_set_config(&config); while(1) { while ((status != STATION_GOT_IP) && (retries)){ status = sdk_wifi_station_get_connect_status(); printf("%s: status = %d\n\r", __func__, status ); if( status == STATION_WRONG_PASSWORD ){ printf("WiFi: wrong password\n\r"); break; } else if( status == STATION_NO_AP_FOUND ) { printf("WiFi: AP not found\n\r"); break; } else if( status == STATION_CONNECT_FAIL ) { printf("WiFi: connection failed\r\n"); break; } vTaskDelay( 1000 / portTICK_RATE_MS ); --retries; } if (status == STATION_GOT_IP) { printf("WiFi: Connected\n\r"); xSemaphoreGive( wifi_alive ); taskYIELD(); } while ((status = sdk_wifi_station_get_connect_status()) == STATION_GOT_IP) { xSemaphoreGive( wifi_alive ); taskYIELD(); } printf("WiFi: disconnected\n\r"); sdk_wifi_station_disconnect(); vTaskDelay( 1000 / portTICK_RATE_MS ); } }
void sntp_task(void *pvParameters) { while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) { vTaskDelay(10); }; /* * Set one of SNTP_OPMODE_LISTENONLY or SNTP_OPMODE_POLL */ /* * SNTP_OPMODE_LISTENONLY * just needs the mode set, no server names required * (requires broadcast NTP on your network) */ sntp_setoperatingmode(SNTP_OPMODE_LISTENONLY); /* * SNTP_OPMODE_POLL * Needs one or more server names set * additional servers are "fail over" * Can use a DNS name or an address literal * LWIP can also be configured with SNTP_GET_SERVERS_FROM_DHCP * (DHCP-specified SNTP servers untested at this time) * * NOTE: Early testing with polling shows higher deviations * than seen with broadcast, even with RTT compensation * Cause unknown at this time, but believed to be within SNTP * amd not part of timekeeping itself. */ /* sntp_setoperatingmode(SNTP_OPMODE_POLL); sntp_setservername(0, "ntp_a.example.com"); sntp_setservername(1, "ntp_b.example.com"); sntp_setservername(2, "ntp_c.example.com"); */ /* Once set up, this is all it takes */ sntp_init(); /* * Have high-priority thread "parked", might as well use it * Show calling gettimeofday() once an hour to check for timer wrap * (the SNTP process itself, if connected, should be sufficient) */ while (1) { vTaskDelay(60 * 60 * (1000 / portTICK_PERIOD_MS)); printf("gettimeofday(NULL, NULL)\n"); gettimeofday(NULL, NULL); } }
static void main_task(void *pvParams) { struct sdk_station_config config; strcpy((char*)config.ssid, config_get_ssid()); strcpy((char*)config.password, config_get_ssid_pass()); sdk_wifi_set_opmode(STATION_MODE); sdk_wifi_station_set_config(&config); while (true) { uint8_t status = sdk_wifi_station_get_connect_status(); if (status == STATION_GOT_IP) { mqtt_task(); } else { printf("Not connected\n"); vTaskDelay(1000/portTICK_RATE_MS); } } }