Created
January 21, 2020 21:38
-
-
Save zaphodikus/33cb45eccd06b3c8a53e611b5c5df046 to your computer and use it in GitHub Desktop.
a make tutorial code dump
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
// https://www.instructables.com/id/Programming-a-HTTP-Server-on-ESP-8266-12E/ | |
#include <ESP8266WiFi.h> | |
const short int BUILTIN_LED1 = 2; //GPIO2 | |
const short int BUILTIN_LED2 = 16;//GPIO16 | |
#define LED_PIN BUILTIN_LED2 | |
WiFiServer server(80); //Initialize the server on Port 80 | |
void setup() { | |
WiFi.mode(WIFI_AP); //Our ESP8266-12E is an AccessPoint | |
WiFi.softAP("Hello_IoT", "12345678"); // Provide the (SSID, password); . | |
server.begin(); // Start the HTTP Server | |
//Looking under the hood | |
Serial.begin(115200); //Start communication between the ESP8266-12E and the monitor window | |
IPAddress HTTPS_ServerIP= WiFi.softAPIP(); // Obtain the IP of the Server | |
Serial.print("Server IP is: "); // Print the IP to the monitor window | |
Serial.println(HTTPS_ServerIP); | |
pinMode(LED_PIN, OUTPUT); //GPIO16 is an OUTPUT pin; | |
digitalWrite(LED_PIN, LOW); //Initial state is ON | |
} | |
void loop() { | |
WiFiClient client = server.available(); | |
if (!client) { | |
return; | |
} | |
//Looking under the hood | |
Serial.println("Somebody has connected :)"); | |
//Read what the browser has sent into a String class and print the request to the monitor | |
String request = client.readStringUntil('\r'); | |
//Looking under the hood | |
Serial.println(request); | |
// Handle the Request | |
if (request.indexOf("/OFF") != -1){ | |
digitalWrite(LED_PIN, HIGH); } | |
else if (request.indexOf("/ON") != -1){ | |
digitalWrite(LED_PIN, LOW); | |
} | |
// Prepare the HTML document to respond and add buttons: | |
String s = "HTTP/1.1 200 OK\r\n"; | |
s += "Content-Type: text/html\r\n\r\n"; | |
s += "<!DOCTYPE HTML>\r\n<html>\r\n"; | |
s += "<br><input type=\"button\" name=\"b1\" value=\"Turn LED ON\" onclick=\"location.href='/ON'\">"; | |
s += "<br><br><br>"; | |
s += "<input type=\"button\" name=\"bi\" value=\"Turn LED OFF\" onclick=\"location.href='/OFF'\">"; | |
s += "</html>\n"; | |
//Serve the HTML document to the browser. | |
client.flush(); //clear previous info in the stream | |
client.print(s); // Send the response to the client | |
delay(1); | |
Serial.println("Client disonnected"); //Looking under the hood | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment