HiLetgo ESP8266 NodeMCU CP2102 ESP-12E
The goal is to create a softAP to connect to and then change to Station mode and connect to a Wi-Fi network. Currently the network ssid's and encryption types are put into a button generator but doesn't allow me to do anything useful with them. Get/post require a variable in the <button><\button> tag. The first bit of code looks messy to me I don't really understand it and would like to do something more like the second. No idea where to go from here or if this is even the way to go about it. There is still a lot more to work through and figure out. Any tips or pointers would be greatly appreciated.
#include <ESP8266WiFi.h>
#include <RTCVars.h>
WiFiServer server(80);
RTCVars state; // create the state object
int reset_counter;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
String header;
unsigned long stationNumDelay = 3000;
unsigned long SNDpreviousTime = 0;
unsigned long networkScanDelay = 2000;
unsigned long NSDpreviousTime = 0;
unsigned long wwwDelay = 100;
unsigned long wwwPreviousTime = 0;
void setup() {
Serial.begin (115200);
Serial.println();
state.registerVar( &reset_counter ); // we send a pointer to each of our variables
if (state.loadFromRTC()) { // we load the values from rtc memory back into the registered variables
reset_counter++;
Serial.println("This is reset no. " + (String)reset_counter);
state.saveToRTC(); // since we changed a state relevant variable, we store the new values
} else {
reset_counter = 0;
state.saveToRTC();
}
Serial.println("hello");
server.begin();
if (reset_counter == 0 ) {
APmode();
} else {
stationMode();
}
}
void APmode() {
const String saId = "garbageProject";
WiFi.mode(WIFI_AP);
WiFi.softAP(saId);
}
void stationMode() {
const char* ssid = "PirateCove";
const char* password = "NUA00dgp01000";
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
unsigned long currentTime = millis();
if (currentTime - wwwPreviousTime >= wwwDelay) {
www();
wwwPreviousTime = millis();
}
if (currentTime - NSDpreviousTime >= networkScanDelay) {
networkScan();
Serial.println(reset_counter);
NSDpreviousTime = millis();
}
}
void networkScan() {
int i = 0;
int numSSID = WiFi.scanNetworks();
while ( i < numSSID) {
String scannedSSID = (WiFi.SSID(i));
unsigned int SSIDlength = scannedSSID.length();
if (SSIDlength > 10) {
scannedSSID.remove(10);
}
Serial.print(i + 1);
Serial.print(" ");
Serial.print(scannedSSID);
Serial.print("\t Encryption: ");
printEncryptionType(WiFi.encryptionType(i));
if ( i + 1 == numSSID) {
Serial.println();
}
i++;
}
}
void printEncryptionType(int thisType) {
// read the encryption type and print out the name:
switch (thisType) {
case ENC_TYPE_WEP:
Serial.println("WEP");
break;
case ENC_TYPE_TKIP:
Serial.println("WPA");
break;
case ENC_TYPE_CCMP:
Serial.println("WPA2");
break;
case ENC_TYPE_NONE:
Serial.println("None");
break;
case ENC_TYPE_AUTO:
Serial.println("Auto");
break;
}
}
void www() {
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
currentTime = millis();
previousTime = currentTime;
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color:#4095c6; border-radius: 12px; color: white; padding: 16px 40px;transition-duration: 0.4s;}");
client.println(".button:hover{background-color:#AED6F1;}");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}</style></head>");
// Web Page Heading
client.println("<body><h1>ESP8266 Web Server</h1>");
int i = 0;
String enc;
int numSSID = WiFi.scanNetworks();
while ( i < numSSID) {
String scannedSSID = (WiFi.SSID(i));
unsigned int SSIDlength = scannedSSID.length();
if (SSIDlength > 10) {
scannedSSID.remove(10);
}
char encryption = (WiFi.encryptionType(i));
switch (encryption) {
case ENC_TYPE_WEP:
enc = ("Encryption WEP");
break;
case ENC_TYPE_TKIP:
enc = ("Encryption TKIP");
break;
case ENC_TYPE_CCMP:
enc = ("Encryption WPA2");
break;
case ENC_TYPE_NONE:
enc = ("Encryption none");
break;
}
client.print("<p><button class=\"button\">" + scannedSSID + " " + enc + "</button></p>");
i++;
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.flush();
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}