ESP32 Mongoose OS Interface WiFi

This code is written for ESP32 running Mongoose OS (MOS).
It monitors the Wi-Fi network connection status and prints updates to the serial console whenever the ESP32’s connection state changes. Also Refer: https://github.com/aruneworld/ESP8266/blob/master/Mongoose-OS/Examples/AEW_TCP_Web_Server.js, Let explain about ESP32 Mongoose OS Interface WiFi

WiFi Status Monitor

// Monitor network connectivity.
Net.setStatusEventHandler(function(ev, arg) {
let evs = "???";
if (ev === Net.STATUS_DISCONNECTED) {
evs = "DISCONNECTED";
} else if (ev === Net.STATUS_CONNECTING) {
evs = "CONNECTING";
} else if (ev === Net.STATUS_CONNECTED) {
evs = "CONNECTED";
} else if (ev === Net.STATUS_GOT_IP) {
evs = "GOT_IP";
}
print("== Net event:", ev, evs);
}, null);

Code Explanation

// Register a network status event handler
Net.setStatusEventHandler(function(ev, arg) {
  let evs = "???";
  • Net.setStatusEventHandler → Registers a callback function that will be called whenever the network status changes.
  • ev → Event code (numeric constant representing network state).
  • arg → Optional event argument (not used here).
  • evs → Human-readable string initialized to "???".

  if (ev === Net.STATUS_DISCONNECTED) {
    evs = "DISCONNECTED";
  } else if (ev === Net.STATUS_CONNECTING) {
    evs = "CONNECTING";
  } else if (ev === Net.STATUS_CONNECTED) {
    evs = "CONNECTED";
  } else if (ev === Net.STATUS_GOT_IP) {
    evs = "GOT_IP";
  }
  • Here the code maps network status codes to readable strings:
    • DISCONNECTED → No WiFi or lost connection.
    • CONNECTING → ESP32 is trying to connect to WiFi.
    • CONNECTED → Connected to AP, but no IP yet.
    • GOT_IP → Successfully connected and got an IP address from DHCP.

  print("== Net event:", ev, evs);
}, null);
  • Prints the event number (ev) and its readable string (evs) to the serial console.
  • The null at the end means no extra user-data is passed to the handler.

Example Output on Serial Monitor

When you boot the ESP32 with WiFi enabled, you might see logs like:

== Net event: 0 DISCONNECTED
== Net event: 1 CONNECTING
== Net event: 2 CONNECTED
== Net event: 3 GOT_IP

This lets you debug WiFi connectivity in real-time.


In short:

This code is a WiFi connection monitor for ESP32 with Mongoose OS. It listens for WiFi events and prints whether the ESP32 is disconnected, connecting, connected (without IP), or fully connected (with IP).

 

Please turn AdBlock off, and continue learning

Notice for AdBlock users

Please turn AdBlock off
Index

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading