microsoft / devkit-sdk

Arduino library and package for the MXChip IoT DevKit
https://aka.ms/devkit
MIT License
69 stars 52 forks source link

DevKit reboots in EMW10xxInterface::connect call when running this sketch #1060

Closed JerryYangKai closed 4 years ago

JerryYangKai commented 4 years ago
#include "AZ3166WiFi.h"
#include "Arduino.h"
#include "OledDisplay.h"

// 500 reboots, 2000 does not reboot
static const int CONNECTED_DELAY = 500;
static bool has_wifi = false;

static void init_wifi() {
  Screen.clean();
  Screen.print(0, "Connecting...");
  Serial.println("Connecting...");

  if (WiFi.begin() == WL_CONNECTED) {
    has_wifi = true;
    Screen.clean();
  } else {
    has_wifi = false;
    Screen.print(0, "No Wi-Fi");
  }
}

void setup() {
  Screen.init();
  init_wifi();

  while (!has_wifi) {
    Serial.println("Retrying in 5000 ms...");
    delay(5000);
    init_wifi();
  }
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("No wifi connection");
    Screen.clean();
    Serial.println("Disconnecting...");
    Screen.print(0, "Disconnecting...");
    WiFi.disconnect();

    Serial.println("Reconnecting...");
    Screen.print(0, "Reconnecting...");

    /*
     * AZ3166WiFi.cpp:64 WiFi.begin()
     *     v
     * AZ3166WiFi.cpp:76 SystemWiFiConnect()
     *     v
     * SystemWifi.cpp:53 (((EMW10xxInterface*)_defaultSystemNetwork)->connect((char*)ssid, (char*)pwd, NSAPI_SECURITY_WPA_WPA2, 0 );)
     *     v
     * EMW10xxInterface.cpp:44 EMW10xxInterface::connect(const char *ssid, const char *pass, nsapi_security_t security, uint8_t channel)
     */
    int value = WiFi.begin();
    has_wifi = (value == WL_CONNECTED);
    delay(3000);
  } else {
    Screen.print(0, "Connected.");
    Serial.println("Connected.");
    delay(CONNECTED_DELAY);
  }
}

This basic sketch just keeps trying to connect to Wi-Fi in a loop. If the CONNECTED_DELAY value is set low enough (like 500), the WiFi.begin(); call inside the loop() function will cause the device to reboot. I did some digging and the reboot happens when executing the EMW10xxInterface::connect function.

However, if CONNECTED_DELAY is set to something like 2000, the device will not reboot.


Hi @JerryYangKai, sorry I forgot a crucial part. The reboot happens when re-connecting to the network after losing Wi-Fi connection with a low CONNECTED_DELAY value. To lose and regain the connection I used a mobile Wi-Fi hotspot and simply turned it off and on.

Steps to reproduce:

  1. Allow device to connect to Wi-Fi
  2. Lose connection to Wi-Fi network (e.g. make device go out of range, shut off Wi-Fi network, or turn off mobile hotspot)
  3. Wait until device reports "No wifi connection" or green connection light is off
  4. Regain network connection (e.g. turn mobile hotspot back on)

At Step 4, device seems to either reboot or regain network connection without rebooting depending on the CONNECTED_DELAY.


Removed from https://github.com/VSChina/mbed-az3166-driver/issues/2

JerryYangKai commented 4 years ago

Closed since it caused by hardware driver.