Web interface from NodeMCU – Part 3, List available WiFi networks

WiFiNewLibrary.h (Download from the attachment given below)

WiFiNewLibrary.cpp

#include "WiFiNewLibrary.h"

WiFiNewLibrary::WiFiNewLibrary()
{
	
}

void WiFiNewLibrary::setupConfigPortal()
{
	server.reset(new ESP8266WebServer(80));

	if(_apPassword != NULL)
	{
		if(strlen(_apPassword) < 8 || strlen(_apPassword) > 63)
		{
			Serial.println("Invalid password...");
			_apPassword = NULL;	
		}	
	}

	if(_apPassword != NULL)
	{
		WiFi.softAP(_apName, _apPassword);
	}
	else
	{	
		WiFi.softAP(_apName);	
	}

	Serial.println("AP IP Address");
	Serial.println(WiFi.softAPIP());

	server -> on(String(F("/")).c_str(), std::bind(&WiFiNewLibrary::handleRoot, this));
	server -> on(String(F("/wifi")).c_str(), std::bind(&WiFiNewLibrary::handleWiFi, this, true));
	server -> begin();

	while(1)
	{
		server -> handleClient();
	}
}

void WiFiNewLibrary::handleRoot()
{
	_customHeadElement = "";

	String page = FPSTR(HTTP_HEADER);
	page.replace("{v}", "Options");
	page += FPSTR(HTTP_STYLE);
	page += _customHeadElement;
	page += FPSTR(HTTP_HEADER_END);
	page += String(F("<h1>"));
	page += _apName;
	page += String(F("</h1>"));
	page += FPSTR(HTTP_PORTAL_OPTIONS);
	page += FPSTR(HTTP_END);

	server -> sendHeader("Content-Length", String(page.length()));
	server -> send(200, "text/html", page);		
}

void WiFiNewLibrary::handleWiFi(boolean scan)
{
	_customHeadElement = "Available WiFi networks";

	String page = FPSTR(HTTP_HEADER);
	page.replace("{v}", "Config ESP");
	page += FPSTR(HTTP_STYLE);
	page += _customHeadElement;
	page += FPSTR(HTTP_HEADER_END);

	if(scan)
	{
		int numNetworks = WiFi.scanNetworks();
		int indices[numNetworks];

		for(int i=0;i<numNetworks;i++)
		{
			indices[i] = i;			
		}

		for(int i=0;i<numNetworks;i++)
		{
			for(int j=i+1;j<numNetworks;j++)
			{
				if(WiFi.RSSI(indices[j]) > WiFi.RSSI(indices[i]))
				{
					std::swap(indices[i], indices[j]);
				}
			}
		}
		
		if(_removeDuplicateAPs)
		{
			String cssid;
			for(int i=0;i<numNetworks;i++)
			{
				if(indices[i] == -1)
					continue;
				cssid = WiFi.SSID(indices[i]);
				for(int j=i+1;j<numNetworks;j++)
				{
					if(cssid == WiFi.SSID(indices[j]))
					{
						indices[j] = -1;
					}
				}
			}
		}

		for(int i=0;i<numNetworks;i++)
		{
			if(indices[i] == -1)
				continue;

			String item = FPSTR(HTTP_ITEM);

			int quality = getRSSIasQuality(WiFi.RSSI(indices[i]));		
			item.replace("{v}", String(WiFi.SSID(indices[i])));
			item.replace("{r}", String(quality));

			if(WiFi.encryptionType(indices[i]) != ENC_TYPE_NONE)
			{
				item.replace("{i}", "l");
			}
			else
			{
				item.replace("{i}", "");
			}
			page += item;			
		}
	}

	page += FPSTR(HTTP_END);
					
	server -> sendHeader("Content-Length", String(page.length()));
	server -> send(200, "text/html", page);
}

int	 WiFiNewLibrary::getRSSIasQuality(int RSSI)
{
	int quality;

	if(RSSI <= -100)	
	{
		quality = 0;	
	}
	else if(RSSI >= -50)
	{
		quality = 100;
	}
	else
	{
		quality = 2 * (RSSI + 100);
	}

	return quality;
}

keywords.txt

WiFiNewLibrary		keyword1

ArduinoProgram.ino

#include <WiFiNewLibrary.h>

WiFiNewLibrary wifiNewLibrary;

void setup() {
  Serial.begin(115200);
  // put your setup code here, to run once:
  wifiNewLibrary.setupConfigPortal();
}

void loop() {
  // put your main code here, to run repeatedly:

}

Download the complete program files.

Published by

Leave a Reply

X