Quantcast
Channel: G1Tech
Viewing all articles
Browse latest Browse all 22

ESP8266 Web Server

$
0
0

Title

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

MDNSResponder mdns;

// Replace with your network credentials
const char* ssid = "SSID";
const char* password = "Passwd";

ESP8266WebServer server(80);

String webPage = "";
int test_pin = 16;

void setup(void){
  webPage += "<h1>G1Tech: ESP8266 Web Server</h1><p>LED #16 <a href=\"LEDOn\"><button>ON</button></a>&nbsp;<a href=\"LEDOff\"><button>OFF</button></a></p>";
  
  pinMode(test_pin, OUTPUT);
  digitalWrite(test_pin, LOW);

  delay(1000);
  Serial.begin(9600);
  WiFi.begin(ssid, password);
  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());
  
  if (mdns.begin("esp8266", WiFi.localIP())) {
    Serial.println("MDNS responder started");
  }
  
  server.on("/", [](){
    server.send(200, "text/html", webPage);
  });
  server.on("/LEDOn", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(test_pin, LOW);
    Serial.println(digitalRead(test_pin));
    delay(1000);
  });
  server.on("/LEDOff", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(test_pin, HIGH);
    Serial.println(digitalRead(test_pin));
    delay(1000); 
  });

 
  server.begin();
  Serial.println("HTTP server started");
}
 
void loop(void){
  server.handleClient();
}

[свернуть]

Viewing all articles
Browse latest Browse all 22

Trending Articles