Created
January 30, 2016 15:21
-
-
Save knight-of-ni/6268b575e05236372196 to your computer and use it in GitHub Desktop.
WiFiManager OnDemand Portal with web server
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
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino | |
//needed for library | |
#include <ESP8266WebServer.h> | |
#include <DNSServer.h> | |
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager | |
#define GPIO0 0 | |
#define GPIO2 2 | |
unsigned long previousMillis = 0; | |
WiFiClient client; | |
ESP8266WebServer server(80); | |
void handle_root() | |
{ | |
server.send(200, "text/plain", "Hello from the esp8266, read from /gpio0 or /gpio2"); | |
delay(100); | |
} | |
void WiFiStatus() { | |
byte percentQ = 0; | |
if (WiFi.RSSI() <= -100) { | |
percentQ = 0; | |
} else if (WiFi.RSSI() >= -50) { | |
percentQ = 100; | |
} else { | |
percentQ = 2 * (WiFi.RSSI() + 100); | |
} | |
Serial.println(""); | |
Serial.println("WiFi Status"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("MAC address: "); | |
Serial.println(WiFi.macAddress()); | |
Serial.print("SSID: "); | |
Serial.println(WiFi.SSID()); | |
Serial.print("Signal Strength (%): "); | |
Serial.println(percentQ); | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
Serial.println("\n Starting"); | |
// Configure our GPIO's as inputs | |
pinMode(GPIO0, INPUT_PULLUP); | |
pinMode(GPIO2, INPUT_PULLUP); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin("ssid", "psk"); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
server.on("/", handle_root); | |
server.on("/gpio0", [](){ // if you add this subdirectory to your webserver call, you get text below :) | |
server.send(200, "text/plain", "Current value of GPIO0: "+String(digitalRead(GPIO0))); // send to someones browser when asked | |
}); | |
server.on("/gpio2", [](){ // if you add this subdirectory to your webserver call, you get text below :) | |
server.send(200, "text/plain", "Current value of GPIO2: "+String(digitalRead(GPIO2))); // send to someones browser when asked | |
}); | |
server.begin(); | |
Serial.println(""); | |
Serial.println("HTTP server started"); | |
} | |
void loop() { | |
// Causes the WiFi Status to be repeated every 10 seconds. Probably not necessary. | |
if ( millis() - previousMillis > 10000 ) { | |
WiFiStatus(); | |
Serial.println("GPIO0 state: "+String(digitalRead(GPIO0))); | |
Serial.println("GPIO2 state: "+String(digitalRead(GPIO2))); | |
previousMillis = millis(); | |
} | |
if ( !digitalRead(GPIO0) ) { // Reprogram button was pressed | |
//server.~ESP8266WebServer(); | |
WiFi.softAPdisconnect(true); | |
delay(500); | |
//WiFi.mode(WIFI_STA); | |
//WiFi.disconnect(); | |
//delay(500); | |
delete &server; | |
WiFi.disconnect(); | |
//WiFiManager | |
//Local intialization. Once its business is done, there is no need to keep it around | |
WiFiManager wifiManager; | |
//reset settings - for testing | |
//wifiManager.resetSettings(); | |
//sets timeout until configuration portal gets turned off | |
//useful to make it all retry or go to sleep | |
//in seconds | |
//wifiManager.setTimeout(120); | |
//it starts an access point with the specified name | |
//here "AutoConnectAP" | |
//and goes into a blocking loop awaiting configuration | |
//WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY WITH SDK 1.5 , update to at least 1.5.1 | |
//WiFi.mode(WIFI_STA); | |
if (!wifiManager.startConfigPortal("ESP8266AutoConnectAP", "electricity")) { | |
Serial.println("failed to connect and hit timeout"); | |
delay(3000); | |
//reset and try again, or maybe put it to deep sleep | |
ESP.reset(); | |
delay(5000); | |
} | |
//if you get here you have connected to the WiFi | |
Serial.println("connected...yeey :)"); | |
} | |
server.handleClient(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment