Advertisement
Guest User

wlan-phpto-R-ESP8266-12E-fiebi

a guest
Dec 31st, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.54 KB | None | 0 0
  1. /*
  2.     This sketch demonstrates how to set up a simple HTTP-like server.
  3.     The server will set a GPIO pin depending on the request
  4.       http://server_ip/gpio/0 will set the GPIO2 low,
  5.       http://server_ip/gpio/1 will set the GPIO2 high
  6.     server_ip is the IP address of the ESP8266 module, will be
  7.     printed to Serial when the module is connected.
  8. */
  9.  
  10. #include <ESP8266WiFi.h>
  11.  
  12. #ifndef STASSID
  13. #define STASSID "WLAN-SSID"
  14. #define STAPSK  "WLAN-Passwort"
  15. #endif
  16.  
  17. const char* ssid = STASSID;
  18. const char* password = STAPSK;
  19.  
  20. // Hier die MAC Adresse eingeben
  21. // funktioniert nicht - die Änderung der MAC
  22. // byte mac[] = {
  23. //  0xA4, 0xCF, 0x12, 0xF1, 0xEB, 0x16 };
  24.  
  25. // Eine IP im lokalen Netzwerk angeben. Dazu am besten die IP
  26. // des PCs herausfinden (googlen!) und die letzte Zahl abändern
  27. // IP einstellen funktioniert auch nicht
  28. // IPAddress ip(192,168,50,16);
  29.  
  30.  
  31. // Create an instance of the server
  32. // specify the port to listen on as an argument
  33. WiFiServer server(80);
  34.  
  35. void setup() {
  36.   Serial.begin(115200);
  37.  
  38.   // prepare LED
  39.   pinMode(LED_BUILTIN, OUTPUT);
  40.   digitalWrite(LED_BUILTIN, 0);
  41.  
  42.   // Connect to WiFi network
  43.   Serial.println();
  44.   Serial.println();
  45.   Serial.print(F("Connecting to "));
  46.   Serial.println(ssid);
  47.  
  48.   WiFi.mode(WIFI_STA);
  49.   WiFi.begin(ssid, password);
  50.  
  51.   while (WiFi.status() != WL_CONNECTED) {
  52.     delay(500);
  53.     Serial.print(F("."));
  54.   }
  55.   Serial.println();
  56.   Serial.println(F("WiFi connected"));
  57.  
  58.   // Start the server
  59.   server.begin();
  60.   Serial.println(F("Server started"));
  61.  
  62.   // Print the IP address
  63.   Serial.println(WiFi.localIP());
  64. }
  65.  
  66. void loop() {
  67.   // Check if a client has connected
  68.   WiFiClient client = server.available();
  69.   if (!client) {
  70.     return;
  71.   }
  72.   Serial.println(F("new client"));
  73.  
  74.   client.setTimeout(5000); // default is 1000
  75.  
  76.   // Read the first line of the request
  77.   String req = client.readStringUntil('\r');
  78.   Serial.println(F("request: "));
  79.   Serial.println(req);
  80.  
  81.   // Match the request
  82.   // 0 -> ein und 1-> aus komisch, ist aber so
  83.   int val;
  84.   if (req.indexOf(F("/gpio/0")) != -1) {
  85.     val = 0;
  86.   } else if (req.indexOf(F("/gpio/1")) != -1) {
  87.     val = 1;
  88.   } else {
  89.     Serial.println(F("invalid request"));
  90.     val = digitalRead(LED_BUILTIN);
  91.   }
  92.  
  93.   // Set LED according to the request
  94.   digitalWrite(LED_BUILTIN, val);
  95.  
  96.   // read/ignore the rest of the request
  97.   // do not client.flush(): it is for output only, see below
  98.   while (client.available()) {
  99.     // byte by byte is not very efficient
  100.     client.read();
  101.   }
  102.  
  103.   // Send the response to the client
  104.   // it is OK for multiple small client.print/write,
  105.   // because nagle algorithm will group them into one single packet
  106.  client.println("HTTP/1.1 200 OK");
  107.  client.println("Content-Type: text/html");
  108.  client.println("Connection: close"); // Verbindung wird nach Antwort beendet
  109.  client.println("Refresh: 2"); // Seite alle 2 Sekunden neu abfragen
  110.  client.println();
  111.  // Ab hier berginnt der HTML-Code, der an den Browser geschickt wird
  112.  client.println("<!DOCTYPE HTML>");
  113.  client.println("<html>");
  114.  client.print("<HEAD><TITLE>");
  115.  client.print("NodeMCU Fiebi Board");
  116.  client.println("</TITLE>");
  117.  client.println("</HEAD><BODY>");
  118.  client.println("<b>Hallo Welt!</b><br> /<br />");
  119.  client.print("Node MCU V3 ESP8266WiFi l&auml;uft seit: ");
  120.  // Gibt es noch was anderes? - sonst Tage Stunden Minuten Sekunden ... ausrechnen lassen :-)
  121.  client.print(millis()/1000);
  122.  client.println(" s.<br /><br />");
  123.  client.print("Photowiderstand an Analogpin 0 zeigt: <b>");
  124.  // liest analog-Port A0 und schreibt es in die HTML-Seite
  125.  client.print(analogRead(A0));
  126.  Serial.print("Wert des Analog-Ports 0: ");
  127.  // liest analog-Port A0 und schreibt es in die Serielle Console
  128.  Serial.println(analogRead(A0));
  129.  client.println("</b><br />");
  130.   client.print(F("<br>WLAN-LED ist zur Zeit  <b>"));
  131.   // 0 -> ein und 1-> aus komisch, ist aber so  
  132.   client.print((val) ? F("aus") : F("an"));
  133.   client.print(F("</b><br><br>Klick <a href='http://"));
  134.   client.print(WiFi.localIP());
  135.   // 0 -> ein und 1-> aus komisch, ist aber so
  136.   client.print(F("/gpio/0'>hier</a>, um den LED GPIO einzuschalten, oder <a href='http://"));
  137.   client.print(WiFi.localIP());
  138.   client.print(F("/gpio/1'>hier</a>, um den LED GPIO auszuschalten.</body></html>"));
  139.  
  140.   // The client will actually be *flushed* then disconnected
  141.   // when the function returns and 'client' object is destroyed (out-of-scope)
  142.   // flush = ensure written data are received by the other side
  143.   Serial.println(F("Disconnecting from client"));
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement