ESP32C3 WiFi Woes

Rockmonton
Posts: 2
Joined: Thu Oct 17, 2024 4:52 am

ESP32C3 WiFi Woes

Postby Rockmonton » Thu Oct 17, 2024 5:40 am

Hey all,

I'm having the hardest time getting wifi to connect to an esp32c3 supermini.

Error codes on retries are receiving the following errors:

Code: Select all

[WiFi] WiFi is disconnected (first 30s or so, not terribly surprising)
[WiFi] SSID not found (Majority of time)
[WiFi] WiFi Status: 0 (occasionally for 10-15 seconds at a time)
Application is to do some data logging to a google sheet of some environmental sensors and a servo for sampling selection.
Using platform io in VSCode with the lolin_c3_Mini profile and arduino framework.

Do I need to explicitly call WPA2-PSK (AES) anywhere?

Troubleshooting done so far:
Ensured ample supply of 5v power direct wired
Tested controls and sensors, they all connect successfully and function as intended.
I've managed to successfully scan the SSIDs and the one i'm trying to connect to does show up.
I have managed to successfully connect with other ESP32 devices with the arduino framework.
Encryption is WPA2 PSK
I've ensured WIFI 802.11 g, n, are both enabled
I've tried setting a static IP that's greenlit at my gateway.

Code as follows with some security retractions of private key, wpa2 passkey, etc. I've included the spaghetti and other stuff i've been prototyping, though it doesn't appear to have any impact or hangups as far as I can tell.

Code: Select all

#include <Arduino.h>
#include <Wire.h> // Include the Wire library for I2C communication
#include <I2C_Scan.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_AHTX0.h>
#include <ScioSense_ENS160.h>
#include <SPI.h>
#include <CheapStepper.h>
#include <SparkFunBME280.h>
#include <ESP_Google_Sheet_Client.h>
#include <time.h>
#include <WiFi.h>

const char*  ssid = "SHAW-756B";
const char*  password = "xxxs";

//Google Project ID
#define PROJECT_ID "iot-plant-monitor-xxx"

// Service Account's client email
#define CLIENT_EMAIL "xxx"

// Service Account's private key
const char PRIVATE_KEY[] PROGMEM = "xxx";
const char spreadsheetID[] = "xxx";

// NTP server to request epoch time
const char* ntpServer = "pool.ntp.org";

// Variable to save current epoch time
unsigned long epochTime; 

//Define Pins
const int SDA_Pin = 7;
const int SCL_Pin = 6;
const int LED_Pin = 8;
const int StepperA_Pin = 0;
const int StepperB_Pin = 1;
const int StepperC_Pin = 2;
const int StepperD_Pin = 3;
const int Fan_Pin = 21;
const int StepperPwr_Pin = 20;
boolean moveClockwise = true;
boolean fanStatus = true;
int DoorCounter = 0;
//Timing
long lastMsg = 0;

//Measured Variables and Calibration Constants
float RH;
float T_Air;
float Baro;
float Altitude;
// Initialize Modules
//Adafruit_AHTX0 aht;
ScioSense_ENS160      ens160(ENS160_I2CADDR_1);
//Adafruit_Sensor *aht_humidity, *aht_temp;
CheapStepper stepper (StepperA_Pin, StepperB_Pin, StepperC_Pin, StepperD_Pin); 
BME280 mySensor;
//Static IP Try?
// Set your Static IP address
IPAddress local_IP(10, 0, 0, 126);
IPAddress gateway(10, 0, 0, 1);
IPAddress subnet(255, 255, 255, 0);
//Declare Functions
void Read_AHT21();
void Read_ENS160();
void ScanWifi();
void tokenStatusCallback(TokenInfo info);
unsigned long getTime();
//SETUP LOOP

void setup() {
  //Define Pin Modes
  pinMode(StepperA_Pin, OUTPUT);
  pinMode(StepperB_Pin, OUTPUT);  
  pinMode(StepperC_Pin, OUTPUT);  
  pinMode(StepperD_Pin, OUTPUT);
  pinMode(Fan_Pin, OUTPUT);    
  pinMode(StepperPwr_Pin, OUTPUT);    
  DoorCounter =0;
  //Stepper Requirements
  stepper.setRpm(10); 
  //Start Serial Console
  Serial.begin(115200);
  while(!Serial); 
  Serial.println("Setting Up.....");  
  //Initialize I2C Bus
  Wire.begin(SDA_Pin,SCL_Pin); // Initialize the I2C bus as a master
  //Change BME280 to non-standard address and connect
  mySensor.setI2CAddress(0x76);
  Serial.println("Begin I2C");
  if (mySensor.beginI2C() == false) //Begin communication over I2C
    {
      Serial.println("The sensor did not respond. Please check wiring.");
      while(1); //Freeze
    }
  //Configure time
  configTime(0, 0, ntpServer); 
  //Try a Static IP For Troubleshooting -- Greenlit in Router Config
  if (!WiFi.config(local_IP, gateway, subnet)) {
   Serial.println("STA Failed to configure");
  }
  // Connect to Wi-Fi
  //ScanWifi(); //Debug/Scan Tool
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println(ssid);
  Serial.println(password);
      // Will try for about 600 seconds (1200x 500ms)
  int tryDelay = 500;
  int numberOfTries = 1200;
  // Wait for the WiFi event
  while (true) {
      
      switch(WiFi.status()) {
        case WL_NO_SSID_AVAIL:
          Serial.println("[WiFi] SSID not found");
          break;
        case WL_CONNECT_FAILED:
          Serial.print("[WiFi] Failed - WiFi not connected! Reason: ");
          return;
          break;
        case WL_CONNECTION_LOST:
          Serial.println("[WiFi] Connection was lost");
          break;
        case WL_SCAN_COMPLETED:
          Serial.println("[WiFi] Scan is completed");
          break;
        case WL_DISCONNECTED:
          Serial.println("[WiFi] WiFi is disconnected");
          break;
        case WL_CONNECTED:
          Serial.println("[WiFi] WiFi is connected!");
          Serial.print("[WiFi] IP address: ");
          Serial.println(WiFi.localIP());
          return;
          break;
        default:
          Serial.print("[WiFi] WiFi Status: ");
          Serial.println(WiFi.status());
          break;
      }
      delay(tryDelay);
      
      if(numberOfTries <= 0){
        Serial.print("[WiFi] Failed to connect to WiFi!");
        // Use disconnect function to force stop trying to connect
        WiFi.disconnect();
        return;
      } else {
        numberOfTries--;
      }
    }  
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();
  //Show I2C Devices on bus
  Scan_I2C();
  // Set the callback for Google API access token generation status (for debug only)
  GSheet.setTokenCallback(tokenStatusCallback);
  // Set the seconds to refresh the auth token before expire (60 to 3540, default is 300 seconds)
  GSheet.setPrerefreshSeconds(10 * 60);
  // Begin the access token generation for Google API authentication
  GSheet.begin(CLIENT_EMAIL, PROJECT_ID, PRIVATE_KEY);
}
 
void loop() {
  bool ready = GSheet.ready();
  FirebaseJson response;
  FirebaseJson valueRange;
  epochTime = getTime();
  long now = millis();
  //Serial.println("LOOP");
  if (now - lastMsg > 30000) {
    Serial.println(WiFi.localIP());
    Serial.println(DoorCounter);    
    Serial.print("Door Position:  ");
    Serial.print(moveClockwise);
    Serial.println();
    fanStatus = !fanStatus;
    DoorCounter++;
    if (DoorCounter > 10){
      digitalWrite(StepperPwr_Pin, 1);
      delay(10);
      stepper.moveDegrees(moveClockwise, 95);
      digitalWrite(StepperPwr_Pin, 0);
      moveClockwise = !moveClockwise; 
      DoorCounter = 0; 
    }
    //Read_ENS160();
    //Scan_I2C();
    //Read_AHT21();
    digitalWrite(Fan_Pin, fanStatus);
    for (int j =0; j<3; j++)
    {
      Serial.print(".");
    } 
    Serial.print("Humidity: ");
    RH = mySensor.readFloatHumidity();
    Serial.print(RH, 2);
    Baro = mySensor.readFloatPressure();
    Serial.print(" Pressure: ");
    Serial.print(Baro, 0);
    Altitude = mySensor.readFloatAltitudeMeters();
    Serial.print(" Alt: ");
    Serial.print(Altitude, 1);
    T_Air = mySensor.readTempC();
    Serial.print(" Temp: ");
    Serial.print(T_Air, 2);
//Set Columns for Logging in Google Sheets
    valueRange.add("majorDimension", "COLUMNS");
    valueRange.set("values/[0]/[0]", epochTime);
    valueRange.set("values/[1]/[0]", T_Air);
    valueRange.set("values/[2]/[0]", RH);
    valueRange.set("values/[3]/[0]", Baro);
    valueRange.set("values/[4]/[0]", Altitude);
    valueRange.set("values/[5]/[0]", DoorCounter);
    Serial.println();
    bool success = GSheet.values.append(&response /* returned response */, spreadsheetID /* spreadsheet Id to append */, "Sheet1!A1" /* range to append */, &valueRange /* data range to append */);
    if (success){
        response.toString(Serial, true);
        valueRange.clear();
    }
    else{
        Serial.println(GSheet.errorReason());
    }
    //Serial.println(ESP.getFreeHeap());
    //Reset Timer
    lastMsg = now;
  } 
}
// Function that gets current epoch time
unsigned long getTime() {
  time_t now;
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    //Serial.println("Failed to obtain time");
    return(0);
  }
  time(&now);
  return now;
}
//Callback Function
void tokenStatusCallback(TokenInfo info){
    if (info.status == token_status_error){
        GSheet.printf("Token info: type = %s, status = %s\n", GSheet.getTokenType(info).c_str(), GSheet.getTokenStatus(info).c_str());
        GSheet.printf("Token error: %s\n", GSheet.getTokenError(info).c_str());
    }
    else{
        GSheet.printf("Token info: type = %s, status = %s\n", GSheet.getTokenType(info).c_str(), GSheet.getTokenStatus(info).c_str());
    }
}
//Scan WiFi (Troubleshooting)
void ScanWifi(){
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    delay(100);
    Serial.println("Scan start");

    // WiFi.scanNetworks will return the number of networks found.
    int n = WiFi.scanNetworks();
    Serial.println("Scan done");
    if (n == 0) {
        Serial.println("no networks found");
    } else {
        Serial.print(n);
        Serial.println(" networks found");
        Serial.println("Nr | SSID                             | RSSI | CH | Encryption");
        for (int i = 0; i < n; ++i) {
            // Print SSID and RSSI for each network found
            Serial.printf("%2d",i + 1);
            Serial.print(" | ");
            Serial.printf("%-32.32s", WiFi.SSID(i).c_str());
            Serial.print(" | ");
            Serial.printf("%4d", WiFi.RSSI(i));
            Serial.print(" | ");
            Serial.printf("%2d", WiFi.channel(i));
            Serial.print(" | ");
            switch (WiFi.encryptionType(i))
            {
            case WIFI_AUTH_OPEN:
                Serial.print("open");
                break;
            case WIFI_AUTH_WEP:
                Serial.print("WEP");
                break;
            case WIFI_AUTH_WPA_PSK:
                Serial.print("WPA");
                break;
            case WIFI_AUTH_WPA2_PSK:
                Serial.print("WPA2");
                break;
            case WIFI_AUTH_WPA_WPA2_PSK:
                Serial.print("WPA+WPA2");
                break;
            case WIFI_AUTH_WPA2_ENTERPRISE:
                Serial.print("WPA2-EAP");
                break;
            case WIFI_AUTH_WPA3_PSK:
                Serial.print("WPA3");
                break;
            case WIFI_AUTH_WPA2_WPA3_PSK:
                Serial.print("WPA2+WPA3");
                break;
            case WIFI_AUTH_WAPI_PSK:
                Serial.print("WAPI");
                break;
            default:
                Serial.print("unknown");
            }
            Serial.println();
            delay(10);
        }
    }
    Serial.println("");

    // Delete the scan result to free memory for code below.
    WiFi.scanDelete();
    WiFi.disconnect();
}

ESP_Sprite
Posts: 9680
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32C3 WiFi Woes

Postby ESP_Sprite » Fri Oct 18, 2024 2:32 am

Potentially you have a version where the manufacturer messed up the RF routing; see here.

Rockmonton
Posts: 2
Joined: Thu Oct 17, 2024 4:52 am

Re: ESP32C3 WiFi Woes

Postby Rockmonton » Fri Oct 18, 2024 4:31 am

Thank you, that is the behaviour I was seeing, and the measurements do match.

I did however find a workaround:

WiFi.setTxPower(WIFI_POWER_8_5dBm);

Knocking back wifi tx power significantly improved connections and speed. Tried it with one of the boards I thought I had blown up, and it also works now. It also seems that running on full TX power they get quite hot after steady use.

Who is online

Users browsing this forum: Baidu [Spider] and 31 guests