Created
July 31, 2017 01:35
-
-
Save samueltcsantos/870f71d7511e32062f44d6956c64ffd9 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
#include <ESP8266WiFi.h> | |
const char* ssid = "Wi-Fi_Network"; | |
const char* password = "network#123"; | |
WiFiServer server(80); | |
void setup() { | |
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output | |
Serial.begin(115200); | |
delay(10); | |
//connect to wifi network | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to"); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while(WiFi.status() != WL_CONNECTED){ | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi Connected"); | |
server.begin(); | |
Serial.println("Server Started"); | |
Serial.print("IP Address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
WiFiClient client = server.available(); | |
if (!client){ | |
return; | |
} | |
Serial.println("new Client"); | |
while(!client.available()){ | |
delay(1); | |
} | |
String req = client.readStringUntil('\r'); | |
Serial.println(req); | |
client.flush(); | |
int val; | |
if (req.indexOf("/gpio/0") != -1){ | |
val = 0; | |
} | |
else if (req.indexOf("/gpio/1") != -1){ | |
val = 1; | |
} | |
else{ | |
Serial.println("Invalid request"); | |
client.stop(); | |
return; | |
} | |
digitalWrite(LED_BUILTIN, val); | |
client.flush(); | |
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; | |
s += "<!DOCTYPE html>\r\n"; | |
s += "<html>\r\n"; | |
s += "GPIO is now "; | |
s += (val) ? "high" : "low"; | |
s += "</html>\n"; | |
client.print(s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment