Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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