ota.cpp: stabilize and speedup wifi login
This commit is contained in:
		
							parent
							
								
									d43657be23
								
							
						
					
					
						commit
						2854f2dd13
					
				
							
								
								
									
										34
									
								
								src/ota.cpp
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								src/ota.cpp
									
									
									
									
									
								
							@ -21,7 +21,8 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
using namespace std;
 | 
					using namespace std;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const BintrayClient paxexpress(PAXEXPRESS_USER, PAXEXPRESS_REPO, PAXEXPRESS_PACKAGE);
 | 
					const BintrayClient paxexpress(PAXEXPRESS_USER, PAXEXPRESS_REPO,
 | 
				
			||||||
 | 
					                               PAXEXPRESS_PACKAGE);
 | 
				
			||||||
// usage of paxexpress: see https://github.com/paxexpress/docs
 | 
					// usage of paxexpress: see https://github.com/paxexpress/docs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Connection port (HTTPS)
 | 
					// Connection port (HTTPS)
 | 
				
			||||||
@ -41,6 +42,15 @@ inline String getHeaderValue(String header, String headerName) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void start_ota_update() {
 | 
					void start_ota_update() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  uint8_t mac[6];
 | 
				
			||||||
 | 
					  char clientId[20];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // hash 6 byte MAC to 4 byte hash
 | 
				
			||||||
 | 
					  esp_eth_get_mac(mac);
 | 
				
			||||||
 | 
					  const uint32_t hashedmac = myhash((const char *)mac, 6);
 | 
				
			||||||
 | 
					  snprintf(clientId, 20, "paxcounter_%08x", hashedmac);
 | 
				
			||||||
 | 
					  const char *host = clientId;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  switch_LED(LED_ON);
 | 
					  switch_LED(LED_ON);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// init display
 | 
					// init display
 | 
				
			||||||
@ -65,8 +75,28 @@ void start_ota_update() {
 | 
				
			|||||||
  ESP_LOGI(TAG, "Starting Wifi OTA update");
 | 
					  ESP_LOGI(TAG, "Starting Wifi OTA update");
 | 
				
			||||||
  ota_display(1, "**", WIFI_SSID);
 | 
					  ota_display(1, "**", WIFI_SSID);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  WiFi.disconnect(true);
 | 
				
			||||||
 | 
					  WiFi.config(INADDR_NONE, INADDR_NONE,
 | 
				
			||||||
 | 
					              INADDR_NONE); // call is only a workaround for bug in WiFi class
 | 
				
			||||||
 | 
					  // see https://github.com/espressif/arduino-esp32/issues/806
 | 
				
			||||||
 | 
					  WiFi.setHostname(host);
 | 
				
			||||||
  WiFi.mode(WIFI_STA);
 | 
					  WiFi.mode(WIFI_STA);
 | 
				
			||||||
 | 
					  WiFi.begin();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // Connect to WiFi network
 | 
				
			||||||
 | 
					  // workaround applied here to bypass WIFI_AUTH failure
 | 
				
			||||||
 | 
					  // see https://github.com/espressif/arduino-esp32/issues/2501
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // 1st try
 | 
				
			||||||
  WiFi.begin(WIFI_SSID, WIFI_PASS);
 | 
					  WiFi.begin(WIFI_SSID, WIFI_PASS);
 | 
				
			||||||
 | 
					  while (WiFi.status() == WL_DISCONNECTED) {
 | 
				
			||||||
 | 
					    delay(500);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  // 2nd try
 | 
				
			||||||
 | 
					  if (WiFi.status() != WL_CONNECTED) {
 | 
				
			||||||
 | 
					    WiFi.begin(WIFI_SSID, WIFI_PASS);
 | 
				
			||||||
 | 
					    delay(500);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  uint8_t i = WIFI_MAX_TRY;
 | 
					  uint8_t i = WIFI_MAX_TRY;
 | 
				
			||||||
  int ret = 1; // 0 = finished, 1 = retry, -1 = abort
 | 
					  int ret = 1; // 0 = finished, 1 = retry, -1 = abort
 | 
				
			||||||
@ -74,7 +104,6 @@ void start_ota_update() {
 | 
				
			|||||||
  while (i--) {
 | 
					  while (i--) {
 | 
				
			||||||
    ESP_LOGI(TAG, "Trying to connect to %s, attempt %u of %u", WIFI_SSID,
 | 
					    ESP_LOGI(TAG, "Trying to connect to %s, attempt %u of %u", WIFI_SSID,
 | 
				
			||||||
             WIFI_MAX_TRY - i, WIFI_MAX_TRY);
 | 
					             WIFI_MAX_TRY - i, WIFI_MAX_TRY);
 | 
				
			||||||
    delay(10000); // wait for stable connect
 | 
					 | 
				
			||||||
    if (WiFi.status() == WL_CONNECTED) {
 | 
					    if (WiFi.status() == WL_CONNECTED) {
 | 
				
			||||||
      // we now have wifi connection and try to do an OTA over wifi update
 | 
					      // we now have wifi connection and try to do an OTA over wifi update
 | 
				
			||||||
      ESP_LOGI(TAG, "Connected to %s", WIFI_SSID);
 | 
					      ESP_LOGI(TAG, "Connected to %s", WIFI_SSID);
 | 
				
			||||||
@ -89,6 +118,7 @@ void start_ota_update() {
 | 
				
			|||||||
      if (WiFi.status() == WL_CONNECTED)
 | 
					      if (WiFi.status() == WL_CONNECTED)
 | 
				
			||||||
        goto end; // OTA update finished or OTA max attemps reached
 | 
					        goto end; // OTA update finished or OTA max attemps reached
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    delay(10000); // wait for stable connect
 | 
				
			||||||
    WiFi.reconnect();
 | 
					    WiFi.reconnect();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user