Skip to content

Instantly share code, notes, and snippets.

@d4rckh
Created Nov 16, 2019
Embed
What would you like to do?
Wireless Wifi
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
// Replace with your network credentials
const char* ssid = "not a free wifi";
const char* password = "pass";
ESP8266WebServer server(80); //instantiate server at port 80 (http port)
//int LEDPin = 13;
String ledstatus = "off";
void setup(void){
pinMode(5, OUTPUT);
//the HTML of the web page
//make the LED pin output and initially turned off
delay(1000);
Serial.begin(115200);
WiFi.begin(ssid, password); //begin WiFi connection
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/ledstatus", [](){
server.send(200, "application/json", "{\"ledstatus\": \"" + ledstatus + "\"}");
});
server.on("/turnon", [](){
ledstatus = "on";
digitalWrite(5, HIGH);
server.send(200, "application/json", "success");
delay(1000);
});
server.on("/turnoff", [](){
ledstatus = "off";
digitalWrite(5, LOW);
server.send(200, "application/json", "success");
delay(1000);
});
server.begin();
Serial.println("Web server started!");
}
void loop(void){
server.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment