Skip to content

Instantly share code, notes, and snippets.

@d4rckh
Created November 16, 2019 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d4rckh/5cf8ed690dcdde1296101a4b218905e8 to your computer and use it in GitHub Desktop.
Save d4rckh/5cf8ed690dcdde1296101a4b218905e8 to your computer and use it in GitHub Desktop.
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