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 NetLogger

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 NetLogger 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 NetLogger 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 NetLogger 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 185.217.104.171
  • 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, average packet loss and jitter (sigma) 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)
  • STANDARD DEVIATION of ping time (sigma)

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.

Example from SPIFFS via FTP

Date  Time  WiFIStatus%  lost  wait  sigma
14/03/2022  00:00:22 100 0 2.12 7.18
14/03/2022  01:00:21 100 0 23.27 3.95
14/03/2022  02:00:22 100 0 22.08 3.09
14/03/2022  03:00:21 100 0 21.88 2.4
14/03/2022  04:00:22 100 0 22.47 3.54
14/03/2022  05:00:22 100 0 22.55 2.95
14/03/2022  06:00:22 100 0 22.48 3.53
14/03/2022  07:00:22 100 0 22.4 2.72
14/03/2022  08:00:22 100 0 22.45 2.56
14/03/2022  08:35:49                    *** netLogger restart - mains failure? ***  
14/03/2022  08:35:49                    *** web available ***  
14/03/2022  09:34:53 100 0.12 32.47 40.59

 

Completed sketch here NetMonitor