Hi
Here is a Web Server for a Wemos D1R2.
This program does seem to work.
I would like it if others would look it over and tell me what is good or bad.
I don't want to keep doing something that is considered a bad programming practice.
I did try to thank everybody who helped me along the way.
#include <ESP8266WiFi.h>
#include <user_interface.h>
String req;
String OnOffString;
const char* ssid = "****";
const char* password = "********";
unsigned int xPageHits = 0;
unsigned int led4 = D6;
IPAddress staticIP(192, 168, 1, 102);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns1(192, 168, 1, 1);
WiFiServer server(80);
WiFiClient client;
void setup()
{
pinMode(led4, OUTPUT);
digitalWrite(led4, 0);
OnOffString = "Off ";
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.config(staticIP, gateway, subnet, dns1);
WiFi.hostname("Wemos102");
while (WiFi.status() != WL_CONNECTED) // Connecting to Network
{
delay(500);
Serial.print("."); // Print Dots while NOT Connected
} // End of While Connected Loop
PrintElCrapo(); // Print Network Information
server.begin(); // Start Server
} // End of Setup Loop
void loop()
{ // * * * * * * * Main Loop * * * * * * * *
client = server.available(); // Check if a client has connected
if (!client)
{
return;
} // End of If Client Connect Once a Client has Connected it will pass through here
while (!client.available()) // Wait until the client sends some data
{
delay(1);
} // End of While Client
req = client.readStringUntil('\r'); // Read the first line of the request
Serial.println("* *");
Serial.println(req); // req is what is sent back from the client browser
client.flush();
// * * Match the request * *
int val;
if (req.indexOf("GET /1") != -1) // indexof returns -1 if not found
{
val = 1;
}
else if (req.indexOf("GET /0") != -1)
{
val = 2;
}
else if (req.indexOf("favicon") != -1)
val = 9;
else if (req.indexOf("GET") != -1)
val = 3;
else
{ // This is for -not- 1,2,3,9
client.stop();
return;
} // End of Match Else
if (val == 9) return;
if (val == 2)
{
val = 3;
digitalWrite(led4, 0);
OnOffString = "Off ";
}
if (val == 1)
{
val = 3;
digitalWrite(led4, 1);
OnOffString = "On ";
}
if (val == 3)
{
PrintToWeb();
}
} // End of Main Loop
// * * * * * * * * * * SubRoutines * * * * * * * * * * *
void PrintToWeb()
{
xPageHits = xPageHits + 1;
client.println("<!DOCTYPE HTML><html lang=\"en\">");
client.println("<head><meta charset=\"UTF-8\"><title>RobWemos102</title></head>");
client.println("<body>");
client.println("<font face=\"Arial\"><font size=\"5\"><font color=\"blue\"><h1>
");
client.println("<center>--- Control Robs Lights ---</center>");
client.println("
");
client.print("<center><font size=\"7\"><a href=1>Turn On</a>
<a href=0>Turn Off</a></center>");
client.println("</h1></font>");
client.println("
");
client.println("<center><font size=\"4\"><font color=\"Navy\"><h1>");
client.print("The Lights Are ");
client.println(OnOffString);
client.println("
");
client.print(" Number of Page Hits = ");
client.println(xPageHits);
client.println("</h1></font></center>");
client.println("</body></html>");
} // End of Print To Web
void PrintElCrapo()
{
delay(2000);
Serial.println();
Serial.println(" Wifi Connected");
Serial.print(" IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Subnet Mask: ");
Serial.println(WiFi.subnetMask());
Serial.print(" Gateway IP: ");
Serial.println(WiFi.gatewayIP());
Serial.print(" Dns IP 0-1: ");
Serial.print(WiFi.dnsIP(0));
Serial.print(" , ");
Serial.println(WiFi.dnsIP(1));
Serial.print("Mac Address: ");
Serial.println(WiFi.macAddress());
Serial.print(" Host Name: ");
Serial.println(WiFi.hostname());
if (WiFi.isConnected())
{
Serial.print(ssid);
Serial.println(" Is still Connected");
} // End of If Connected
else
{
Serial.print(ssid);
Serial.println(" Is NOT Connected");
} // End of Else Connected
Serial.print("Sleep Mode: ");
Serial.println(WiFi.getSleepMode ());
Serial.print("Mode:" );
Serial.println(WiFi.getMode ());
} // End of PrintElCrapo