m5stack / M5StickC-Plus

M5StickCPlus Arduino Library
MIT License
359 stars 90 forks source link

Cannot connect Wifi on Arduino Idle #35

Closed trinhgiahuy closed 2 years ago

trinhgiahuy commented 2 years ago

I read through #10 but it does not solve.

I tried to connect to my home WiFi (network band on 2.4 GHz) with Arduino Idle 1.18.19

The Wifi.status() returned 3 (WL_CONNECTED) while Wifi.begin() returned 6(WL_DISCONNECTED)

Original code

#include <M5StickCPlus.h>
#include <WiFi.h>
//#include <HTTPClient.h>

#include "arduino_secrets.h" 

#define PIR_GROVE_PIN 33
#define LED_PIN 10

#define LED_ON LOW
#define LED_OFF HIGH

const char *ssid = "BumBeo";        // your network SSID (name)
const char *pass = "38636300";
int keyIndex = 0;   

int status = WL_IDLE_STATUS;

void setup(){
  M5.begin(true, true, true);             //Init M5StickC plus
  Serial.begin(9600);

  M5.Lcd.setRotation(3);
  M5.Lcd.setTextSize(2);
  M5.Lcd.setCursor(20, 15);
  M5.Lcd.println("PIR motion test");
  M5.Lcd.setCursor(30, 30);
  M5.Lcd.println(" Person dected?");

  pinMode(PIR_GROVE_PIN,INPUT);
  pinMode(LED_PIN,OUTPUT);

  // check for the presence of the shield:
  Serial.println("Checking for wifi hardware...");
  /*
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }
  */
  //Serial.println("Wifi shield found!");
  // attempt to connect to WiFi network:
  Serial.println("Attempting to connect to Wifi network");
  status = WiFi.begin(ssid, pass);

  while (status != WL_CONNECTED) {
    if(status == WL_DISCONNECTED){
      Serial.println("status == WL_DISCONNECTED");
    }
    Serial.print("Wifi.begin: ");
    Serial.println(status);
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    Serial.print("Wifi.status: ");
    Serial.println(WiFi.status());  
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    //status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWiFiStatus();
}

void loop(){

  //static float tmp = 0;
  int motionVal = digitalRead(PIR_GROVE_PIN);
  if(motionVal)
    digitalWrite(LED_PIN,LED_ON);
  else
    digitalWrite(LED_PIN,LED_OFF);
  M5.Lcd.setCursor(80, 60);
  M5.Lcd.printf("%3d",motionVal);
  delay(1000);         //Delay 1s
}

void printWiFiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Arduino output terminal

...
Wrote 698288 bytes (443959 compressed) at 0x00010000 in 7.8 seconds (effective 718.3 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 128...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (128 compressed) at 0x00008000 in 0.1 seconds (effective 385.7 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

Serial terminal

status == WL_DISCONNECTED
Wifi.begin: 6
Attempting to connect to SSID: BumBeo
Wifi.status: 3
trinhgiahuy commented 2 years ago

SOLVED

WiFi.begin(ssid,password) and have a problem that it always return 6, even when it has connected.

Use Wifi.status() to check condition. If you change the SSID and password, it is normal that right after changing it, the WiFi is in a 'WL_DISCONNECTED' state.

You could use WiFi.waitForConnectResult() to block all execution until the connection is established, or if you don't want to block, just poll WiFi.status() in the loop.