mc_workshop

ESP8266 CH340G USB

Apr 21st, 2019
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3. #include <ESP8266mDNS.h>
  4. ESP8266WebServer server(80);
  5. const int util = 0;
  6. const int led = 2;
  7. void handleRoot() {
  8.   digitalWrite(util, 1);
  9.   server.send(200, "text/plain", "ESP8266 STATUS: OK");
  10.   digitalWrite(util, 0);
  11. }
  12. void handleNotFound(){
  13.   digitalWrite(util, 1);
  14.   String message = "File not found\n\n";
  15.   message += "URI: ";
  16.   message += server.uri();
  17.   message += "\nMethod: ";
  18.   message += (server.method() == HTTP_GET)?"GET":"POST";
  19.   message += "\nArguments: ";
  20.   message += server.args();
  21.   message += "\n";
  22.   for (uint8_t i=0; i<server.args(); i++){
  23.     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  24.   }
  25.   server.send(404, "text/plain", message);
  26.   digitalWrite(util, 0);
  27. }
  28. void setup(void){
  29.   pinMode(2, OUTPUT);
  30.   pinMode(util, OUTPUT);
  31.   digitalWrite(util, 0);
  32.   Serial.begin(115200);
  33.   Serial.println("");
  34.   Serial.print("ESP8266 ON");
  35.   Serial.print("192.168.4.1");
  36.   server.on("/", handleRoot);
  37.   server.on("/inline", [](){
  38.     server.send(200, "text/plain", "OK");
  39.   });
  40.   server.on("/on", [](){
  41.     digitalWrite(led, 1);
  42.   server.send(200, "text/plain", "LED ON");
  43.   });
  44.   server.on("/off", [](){
  45.     digitalWrite(led, 0);
  46.   server.send(200, "text/plain", "LED OFF");
  47.   });
  48.   server.onNotFound(handleNotFound);
  49.   server.begin();
  50.   Serial.println("Server HTTP ON");
  51. }
  52. void loop(void){
  53.   server.handleClient();
  54. }
RAW Paste Data