WiFi Events not managing LEDs

I have an ESP32 devkit set up to monitor my wifi connection.


Here is my main code

/*
* ESP32 - get wifi credentials from serial monitor
* using wifi events
* code from wifimanager
*  https://docs.espressif.com/projects/arduino-esp32/en/latest/tutorials/preferences.html
*  https://randomnerdtutorials.com/esp32-save-data-permanently-preferences/

* problems with save creds, retrieve creds, serGetCreds - string, pointer or array of char.
*/
// #include "webstats.h"     // declarations of variables to calculate statistics and handle time
#include <WiFi.h>         //WiFi functions - supports wifi.ino
#include <time.h>         //support for time functions
#include <Preferences.h>  // for functions relating to flash memory

Preferences myprefs;  //create an instance of the library, called myprefs

#define BAUDRATE 115200

//lights and switches  :  shown here so we can see what lights are being turned on/off
int trigger = 4;  //button to GND; press to clear the existing connection and allow reconfiguration
int yLed = 18;    //yellow led + 1k to GND
int bLed  = 19 ;   //blue LED + 1k to GND (presently green)
int rLed  = 21;    //red LED + 1k to GND

//hardware management
boolean triggerState = 0;  //moitor to check for a change
boolean triggerEvent = 0;  // 1 shows a trigger event has occurred

//for preferences storage
#define RW_MODE false
#define RO_MODE true

String ssid = "";  //hidden for forum
String pass = "";

/* Configuration of NTP */
#define MY_NTP_SERVER "pool.ntp.org"
#define MY_TZ "GMT0"  //not working - not used
time_t now;           // this is the epoch
tm tm;

//  *** loop timing  ***
unsigned long timeWas, timeNow;
unsigned long interval = 60000; // 

//const IPAddress remote_ip(192, 168, 1, 1);  //ping the router
//const IPAddress remote_ip(192, 168, 1, 22);  //to ping network printer
//const IPAddress remote_ip(185, 217, 104, 171); //to ping TheHUT: doesnt work if in header file
const IPAddress remote_ip(9, 9, 9, 9);  //to ping Quad9

// function prototypes wifi.ino
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info);
void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
String waitForSerialInput(String prompt);  //prompt and read a string from serial io
void serGetCreds();                        //input new credentials from serial io
void getCreds();                           //get saved credentials from myprefs
void saveCreds(String ssid, String pass);  //save credentials to myprefs
void printLocalTime();

// function prototypes hardio.ino
void initIO();             //initialise hardware IO devices all leds off
void flashAll(int count);  //flash all LEDs count times

void setup() {
  Serial.begin(BAUDRATE);
  Serial.println();
  delay(10000); //allow to clear serial moonitor
  initIO();  //initialise hardware IO devices all leds off
  Serial.println("Serial comms established");
  //create a new storage space on the flash memory with the credentials namespace.  false=read-write (true = read only)
  myprefs.begin("credentials", RW_MODE);
  getCreds();
  Serial.print("Retrieved values: SSID: ");
  Serial.print(ssid);
  Serial.print(" and PASS: ");
  Serial.println(pass);

  // delete old config
  // WiFi.disconnect(true);

  WiFi.onEvent(WiFiStationConnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED);
  WiFi.onEvent(WiFiGotIP, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
  WiFi.onEvent(WiFiStationDisconnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);

  //connectWiFi();
  WiFi.begin(ssid, pass);
  configTime(0, 0, MY_NTP_SERVER);  // --> Here is the IMPORTANT ONE LINER needed in your sketch!
  flashAll(10);  //shows all LEDs are connected and working
  timeWas = millis();
}

void loop() {
  digitalWrite(yLed, HIGH);
  timeNow = millis();
  if(timeNow - timeWas > interval){
  Serial.print("loop: ");
  printLocalTime();
  timeWas = timeNow; //reset timer
  }
  if  (!digitalRead(trigger) != triggerState ){  //they are different so button has been pressed
      //triggerEvent = 1;
      triggerState = 1;
       //clear ssid and password and save them
      ssid = " ";
      pass = " ";
      saveCreds(ssid, pass);
      // delete old config
      WiFi.disconnect(true);
      digitalWrite(rLed, HIGH);
      delay(1000);
      digitalWrite(rLed, LOW);
      triggerState = 0;  //ready for another trigger:: should this go elsewhere?
     
    }
}

and the wifi events - in a tab wifi.ino


void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
  Serial.println("WiFiStationConnected - Connected to AP successfully!");
  digitalWrite(bLed, HIGH);
}  //show blue led while wifi is on

void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
  Serial.println("WiFiGotIP - WiFi connected");
  Serial.print("IP number is ");
  Serial.print(WiFi.localIP());
  Serial.print("  MAC address is ");
  Serial.println(WiFi.macAddress());
  Serial.print("WiFi connection signal Strength (RRSI): ");
  Serial.println(WiFi.RSSI());
  digitalWrite(bLed, HIGH);
}

void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
  Serial.println("WiFiStationDisconnected - Disconnected from WiFi access point");
  Serial.print("WiFi lost connection. Reason: ");
  Serial.println(info.wifi_sta_disconnected.reason);
  digitalWrite(bLed, LOW);
  Serial.println("Trying to Reconnect");
  WiFi.begin(ssid, pass);
}

the serial monitor shows the wifi events are beng called - but the LEDs dont respond properly
eg when the wifi is connected I'd expect the blue LED to be lit - and its not.

Do you think it might help if you posted the sketch that is running ?

1 Like

you caught me in mid-post Bob!
The whole sketch is in 4 tabs.

My tip from practical experience:

Build your own logic analyser using Serial.println("what I want to know") at program locations that interest you.

Who wrote the programme?

As you will see paul the code is full of serial prints.
So eg in WiFiStationConnected

this gets printed

Serial.println("WiFiStationConnected - Connected to AP successfully!");

but the led doesnt turn on.

digitalWrite(bLed, HIGH);

It was I

Have you set the LED pins to be outputs using pinMode()?
I can't see anywhere.

Does all that.

and they do all flash, so its not the config.

I'm guessing here - its had me mytsified but ..

Assumng the WiFi event is an interrupt, would the led value be changed, but restored on exiting the ISR?

Even though this is a digital write to a fixed GPIO location?

IDK but that's why you should just keep it from doing:

const int bLed  = 19 ;   //blue LED + 1k to GND (presently green)

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.