Last active
July 10, 2018 11:29
-
-
Save li2hub/b032a1e3dd747adee906b337ee374c33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****Libraries required****/ | |
#include <ESP8266WiFi.h> //ESP8266 main library | |
#include <ESP8266WebServer.h> //Library to handle Web Server commands | |
const char *ssid = "ESP8266"; //Name of the Access Point | |
const char *password = "12345678"; //Password of the Access Point | |
/****Define a web server at port 80 for HTTP****/ | |
ESP8266WebServer server(80); // Use port 443 for HTTPS | |
/****Let's include a custom (static) IP****/ | |
IPAddress apIP(1,3,5,7); //You can use your own numbers for the IP | |
/****An LED is connected to NodeMCU pin D1 (ESP8266 GPIO5) via a 3.3K Ohm resistor****/ | |
const int ledPin = D1; | |
bool ledState = false; | |
void setup() { | |
pinMode ( ledPin, OUTPUT ); | |
digitalWrite ( ledPin, 0 ); | |
delay(1000); | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.println("Configuring access point..."); | |
WiFi.mode(WIFI_AP_STA); | |
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); // subnet FF FF FF 00 | |
WiFi.softAP(ssid, password); // Use Wifi.softAP(ssid) if you want an unprotected Access Point | |
IPAddress myIP = WiFi.softAPIP(); | |
Serial.print("AP IP address: "); | |
Serial.println(myIP); | |
//The following commands tell the ESP which sub-routines to follow when a particular request is received | |
server.on ( "/", handleRoot ); | |
server.on ( "/led=1", handleRoot); | |
server.on ( "/led=0", handleRoot); | |
server.onNotFound ( handleNotFound ); | |
server.begin(); | |
Serial.println("HTTP server started"); | |
} | |
void loop() { | |
server.handleClient(); | |
} | |
void handleRoot() { | |
digitalWrite (ledPin, server.arg("led").toInt()); | |
ledState = digitalRead(ledPin); | |
/* Dynamically generate the LED toggle link, based on its current state (on or off)*/ | |
char ledText[80]; | |
if (ledState) { | |
strcpy(ledText, "LED is on. <a href=\"/?led=0\">Turn it OFF!</a>"); | |
} | |
else { | |
strcpy(ledText, "LED is OFF. <a href=\"/?led=1\">Turn it ON!</a>"); | |
} | |
ledState = digitalRead(ledPin); | |
char html[1000]; | |
int sec = millis() / 1000; | |
int min = sec / 60; | |
int hr = min / 60; | |
int proximity = analogRead(A0); | |
proximity = (int)(proximity)*100 / 1024; //converting the 0-1024 value to a percentage value | |
/****Build an HTML page to display on the web-server root address****/ | |
snprintf ( html, 1000, | |
"<html>\ | |
<head>\ | |
<meta name='viewport' content='width=device-width, initial-scale=1.0'>\ | |
<meta http-equiv='refresh' content='5'/>\ | |
<title>ESP8266 WiFi Network</title>\ | |
<style>\ | |
body { background-color:#FFFFFF; font-family: Bookman; font-size: 1.5em; Color:#000000; }\ | |
h1 { Color: #000000; }\ | |
</style>\ | |
</head>\ | |
<body>\ | |
<h1><center>ESP8266 Wi-Fi Demo</center></h1>\ | |
<p><center>Uptime: %02d:%02d:%02d</center></p>\ | |
<p><center>Proximity: %d %</center></p>\ | |
<p><center>%s</center><p>\ | |
<p><center>This page refreshes every 5 seconds. Click <a href=\"javascript:window.location.reload();\">here</a> to refresh the page now.</center></p>\ | |
</body>\ | |
</html>", | |
hr, min % 60, sec % 60, | |
proximity, | |
ledText | |
); | |
server.send ( 200, "text/html", html ); | |
} | |
//This function handles unknown requests | |
void handleNotFound() { | |
String message = "File Not Found\n\n"; | |
message += "URI: "; | |
message += server.uri(); | |
message += "\nMethod: "; | |
message += ( server.method() == HTTP_GET ) ? "GET" : "POST"; | |
message += "\nArguments: "; | |
message += server.args(); | |
message += "\n"; | |
for ( uint8_t i = 0; i < server.args(); i++ ) { | |
message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n"; | |
} | |
server.send ( 404, "text/plain", message ); | |
digitalWrite ( LED_BUILTIN, 1 ); //turn the built in LED on pin DO of NodeMCU off | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment