John L Errington MSc

Experiments with an Arduino microcontroller

Monitoring a WiFi connection with a NodeMCU ESP8266 WiFi Module

Over the years I've been reassured by the various lights on my router; however my new router only has one light - and it tells lies! So I decided to build a gadget to continuously monitor my wifi and broadband connection, with some indicators to show what is connected on my LAN. An additional feature was to record power outages.

I considered adding a wifi module to my trusty Arduino Micro Pro; but I discovered the ESP8266 can do all the necessary on its own, so I'm going to be using a Lolin NodeMCU V3. Its a much faster processor too, running at 80MHz (most Arduinos run at 8 or 16MHz); The board also includes some external Flash memory (1, 2 or 4MB depending on the board) you can use for logging data!

For this project I also needed to install two new libraries, both using "Tools - Manage libraries";

The PingLogger

Initially the aim was simply to ping a few different targets - some in my home network, some on the web; and the initial sketch worked well. I had an old 8 port ethernet switch which was an ideal box for the project, and soon it was sitting on a shelf flashing leds at me. Statistics were reported back on the serial port, but that wasnt suitable for its final location.

I decided I wanted to log the results, so I could see if the connection was lost.

This proved "interesting"; I needed to record the time and sets of statistics; so I needed:

  1. to connect to my router via WiFi
  2. to flash the LEDs at chosen rates
  3. to show a warning if the internet connection or power had failed
  4. to have a reliable and accurate record of the time
  5. to be able to store a large amount of data
  6. to be able to access that data remotely

 

The PingLogger circuit

circuit

A very simple circuit as you can see:

  • LED1 is blue, for wifi;
  • LED2 white, for the internet connection
  • LED3 - 7 amber for network devices;
  • LED8 green, for the router; and
  • LED9 red - for a failure warning;

resistors 470 ohm for the amber and 1k for the whites as they are brighter; and a simple push button to let me reset the Node when in operation.

Note LEDs 1-8 are ON when D0-7 are LOW;

LED9 needs to be tied to ground as if D8 is pulled high it prevents the NodeMCU operating correctly; so its ON when D8 is HIGH.

 

Power supply not shown as its powered through a usb connection; and connection to the network not shown as it uses the Node's on-board Wifi function.

 

Circuit diagram created with https://www.circuit-diagram.org/

 

The NodeMCU proved a very good choice for this application.

1: the NODE hardware supports WiFi with the ESP8266WiFi.h library

2: The hardware can provide PWM signals on the GPIO pins, supported by a library (core_esp8266_waveform.h) and a simple interface.

3: After trying others I found the Arduino's Time.lib gave me access to NTP time, and also maintained the time when the network, or NTP time was unavailable.

4: My NodeMCU has 4M of storage that can be used for data using the (deprecated) SPIFFS or LittleFS filing systems. I tried LittleFS and it was much too complicated; SPIFFS worked really well and as I was not using multiple directories but just storing a set of files it was suitable.

5: I was able to access the SPIFFS data via FTP using the ESP8266FtpServer.h library.

6: I could get NTP time reliably with the NTPClient library

 

As I have a few different Arduino project units I found accessing the PingLogger via FTP (with FileZilla) was unreliable until I realised the router was allocating different addresses each time;

SO I programmed the router to give most of my networked hardware FIXED IP ADDRESSES.

My network

my network

My router has 4 gigabit ethernet ports which distribute connections to my various PCs and devices in my home office. One is taken up with a connection to a power line adapter that shares the connection to the rest of the house; another by a dedicated data logger.

The remaining two ports connect through gigabit ethernet switches to PC's, Printer and NAS box.

The pinglogger connects to the router via WiFi, and frequently pings the network, checking

  • 1: PC1 (via 8 port Switch); 192.168.1.11
  • 2: PC3 (via 6 port Switch); 192.168.1.21
  • 3: PC4 (via 6 port Switch); 192.168.1.14
  • 4: NAS box; 192.168.1.24
  • 5: Printer; 192.168.1.22
  • 6: ESP32 development system 192.168.1.42
  • 7: the internet connection 107.162.133.62
  • 8: the WiFi connection

The link to the powerline adapter is not checked as its not always active.

Completed unit

pinglogger in box A piece of veroboard holds the NODE and resistors. The LEDs are adjusted to match the holes in the front of the box as you see above. All held in place with the aid of my trusty glue gun.

 

Not shown - additional daughter board to support R9 & LED9 on D8 for the internet / power fail warning.

The red LED is lit if the power has failed, or the ping to the internet target fails, and resets after a time depending on the quality of the connection.

NetMonitor Program description

The core of the program is a switch - case structure, using an index "target" that has two functions: it selects the URL to ping, and controls progress through the stages after the round of pings is complete.

The program pings the targets every minute, showing results by flashing the LEDs. After 60 rounds the average ping time and average packet loss for each target are available on the serial monitor. The most important - the sole target on the web - has its data saved to a SPIFFS file. Each days record starts near midnight, when two header lines are stored. Each hour the wifi status and ping stats for the internet target are recorded.

The stats show the

  • WiFi CONNECTION STATUS,
  • AVERAGE PING LOSS over 5 pings for each hour (so 0 - 5.0)
  • AVERAGE PING TIME (averaged over all pings) ( normally 18msec for my connection)

Each month a new file is created, and a year later its deleted so the 4M memory is never filled.

The serial output is not used when the unit is operating, but the SPIFFS files are accessible at any time via FTP, and formatted for import to EXCEL.

Completed sketch here NetMonitor