This project utilizes an ESP8266 NodeMCU microcontroller to create a visual and auditory indicator for monitoring the status of a WiFi router connection. Based on the connection status, the script controls an LED and a buzzer to provide real-time feedback to the user.
Read more: ESP8266 NodeMCU Project – WiFi Status IndicatorContents
Features
- LED Indicator: The script utilizes a GPIO pin connected to an LED to visually indicate the WiFi connection status. When the router is connected, the LED blinks periodically. If the connection is lost, the LED remains off.
- Buzzer Alarm: A buzzer connected to another GPIO pin provides audible feedback. When the WiFi connection is lost, the buzzer emits an alarm sound at regular intervals. Once the connection is restored, the buzzer stops.
- WiFi Event Monitoring: The script registers event handlers for WiFi connection events such as connection, disconnection, and obtaining an IP address. It prints relevant information to the console for debugging purposes.
- Automatic Status Monitoring: The NodeMCU board continuously monitors the WiFi connection status in the background. It automatically adjusts the LED and buzzer states based on changes in the connection status.
Overall, this project offers a simple yet effective solution for monitoring the status of a WiFi router connection, making it suitable for applications where real-time feedback on network connectivity is essential.
Required for ESP8266 WiFi Status Indicator
- Required NodeMCU Modules (Firmware): GPIO Module, WiFi Module
- Required Hardware: ESP8266 with Programmer (or) NodeMCU Dev Kit, LED- 3v3 or 5v, Buzzer
- Required Software Tools: ESPlorer IDE Tool
Code
ESP8266 WiFi Status Indicator
--[[ Firmware: NodeMCU custom build by frightanic.com Modules: bit, enduser_setup, file, gpio, http, i2c, mdns, mqtt, net, node, ow, pwm, rtcfifo, rtcmem, rtctime, sntp, tmr, uart, wifi Required NodeMCU Modules: GPIO, WiFi, Node. Tested By: Arun(20170219) Example Name: AEW_WiFi_Router-Connection-Status-Indicator.lua --]] -- LED initialization LED = 1 -- GPIO-5 as connect to LED gpio.mode(LED, gpio.OUTPUT) gpio.write(LED, gpio.LOW) -- Buzzer initialization Buzzer = 2 -- GPIO-04 as connect to Buzzer gpio.mode(Buzzer, gpio.OUTPUT) gpio.write(Buzzer, gpio.LOW) -- WiFi initialization station_cfg = {} station_cfg.ssid = "Arun" station_cfg.pwd = "ArunEworld" -- Change your SSID and Password wifi.setmode(wifi.STATION) wifi.sta.config(station_cfg) wifi.sta.connect() -- Wifi Event Monitoring wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T) print("\n\tSTA - CONNECTED" .. "\n\tSSID: " .. T.SSID .. "\n\tBSSID: " .. T.BSSID .. "\n\tChannel: " .. T.channel) Wifi_Router_Status = 1 end) wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) print("\n\tSTA - DISCONNECTED" .. "\n\tSSID: " .. T.SSID .. "\n\tBSSID: " .. T.BSSID .. "\n\treason: " .. T.reason) Wifi_Router_Status = 0 end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) print("\n\tSTA - GOT IP" .. "\n\tStation IP: " .. T.IP .. "\n\tSubnet mask: " .. T.netmask .. "\n\tGateway IP: " .. T.gateway) sta_ip = T.IP end) -- Blink LED function function Blink_Led() if (0 == gpio.read(LED)) then gpio.write(LED, gpio.HIGH) elseif (1 == gpio.read(LED)) then gpio.write(LED, gpio.LOW) end tmr.wdclr() end -- Buzzer Alarm function function Buzzer_Alarm() if (0 == gpio.read(Buzzer)) then gpio.write(Buzzer, gpio.HIGH) elseif (1 == gpio.read(Buzzer)) then gpio.write(Buzzer, gpio.LOW) end tmr.delay(100000) tmr.wdclr() end tmr.alarm(0, 500, 1, function() if (1 == Wifi_Router_Status) then Blink_Led() gpio.write(Buzzer, gpio.LOW) elseif (0 == Wifi_Router_Status) then Buzzer_Alarm() gpio.write(LED, gpio.LOW) end end) -- Every second twice
Code Explanation
This code acts as a WiFi connection status indicator using LEDs and a buzzer, offering both visual and auditory feedback based on the connection status.
Module Import and Test Information:
- The script begins with comments detailing the firmware, modules, and testing specifics.
- It lists the required modules and mentions testing conducted by Arun on a specified date.
Initialization:
- Initializes GPIO pins for the LED and buzzer.
- Sets GPIO-5 (LED) as an output and initializes it to LOW.
- Sets GPIO-04 (buzzer) as an output and initializes it to LOW.
WiFi Initialization:
- Creates a table (station_cfg) to store the SSID and password for WiFi connection.
- Configures the WiFi mode to station mode.
- Establishes a connection to the WiFi network using the provided credentials.
WiFi Event Monitoring:
- Registers event handlers for WiFi connection events such as STA_CONNECTED, STA_DISCONNECTED, and STA_GOT_IP.
- Prints relevant information like SSID, BSSID, IP address, etc., upon these events.
- Updates the Wifi_Router_Status variable based on the connection status.
Function Definitions:
- Defines two functions: Blink_Led() and Buzzer_Alarm().
- Blink_Led() toggles the LED state between ON and OFF.
- Buzzer_Alarm() toggles the Buzzer state between ON and OFF with a delay of 0.1 second.
Main Loop:
- Sets up a timer alarm to execute a function every 0.5 seconds.
- Within the timer function:
- Checks the Wifi_Router_Status variable.
- If WiFi connects (Wifi_Router_Status = 1), it blinks the LED and disables the buzzer.
- If WiFi disconnects (Wifi_Router_Status = 0), it triggers the buzzer alarm and disables the LED.
- This loop repeats every 0.5 seconds, producing a blinking LED effect and triggering the buzzer if the WiFi connection is lost.
Comments:
- The code includes comments throughout, explaining each section and function for improved readability and comprehension.