Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This sketch demonstrates how to set up a simple HTTP-like server.
- The server will set a GPIO pin depending on the request
- http://server_ip/gpio/0 will set the GPIO2 low,
- http://server_ip/gpio/1 will set the GPIO2 high
- server_ip is the IP address of the ESP8266 module, will be
- printed to Serial when the module is connected.
- */
- #include <ESP8266WiFi.h>
- #ifndef STASSID
- #define STASSID "WLAN-SSID"
- #define STAPSK "WLAN-Passwort"
- #endif
- const char* ssid = STASSID;
- const char* password = STAPSK;
- // Hier die MAC Adresse eingeben
- // funktioniert nicht - die Änderung der MAC
- // byte mac[] = {
- // 0xA4, 0xCF, 0x12, 0xF1, 0xEB, 0x16 };
- // Eine IP im lokalen Netzwerk angeben. Dazu am besten die IP
- // des PCs herausfinden (googlen!) und die letzte Zahl abändern
- // IP einstellen funktioniert auch nicht
- // IPAddress ip(192,168,50,16);
- // Create an instance of the server
- // specify the port to listen on as an argument
- WiFiServer server(80);
- void setup() {
- Serial.begin(115200);
- // prepare LED
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, 0);
- // Connect to WiFi network
- Serial.println();
- Serial.println();
- Serial.print(F("Connecting to "));
- Serial.println(ssid);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(F("."));
- }
- Serial.println();
- Serial.println(F("WiFi connected"));
- // Start the server
- server.begin();
- Serial.println(F("Server started"));
- // Print the IP address
- Serial.println(WiFi.localIP());
- }
- void loop() {
- // Check if a client has connected
- WiFiClient client = server.available();
- if (!client) {
- return;
- }
- Serial.println(F("new client"));
- client.setTimeout(5000); // default is 1000
- // Read the first line of the request
- String req = client.readStringUntil('\r');
- Serial.println(F("request: "));
- Serial.println(req);
- // Match the request
- // 0 -> ein und 1-> aus komisch, ist aber so
- int val;
- if (req.indexOf(F("/gpio/0")) != -1) {
- val = 0;
- } else if (req.indexOf(F("/gpio/1")) != -1) {
- val = 1;
- } else {
- Serial.println(F("invalid request"));
- val = digitalRead(LED_BUILTIN);
- }
- // Set LED according to the request
- digitalWrite(LED_BUILTIN, val);
- // read/ignore the rest of the request
- // do not client.flush(): it is for output only, see below
- while (client.available()) {
- // byte by byte is not very efficient
- client.read();
- }
- // Send the response to the client
- // it is OK for multiple small client.print/write,
- // because nagle algorithm will group them into one single packet
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: close"); // Verbindung wird nach Antwort beendet
- client.println("Refresh: 2"); // Seite alle 2 Sekunden neu abfragen
- client.println();
- // Ab hier berginnt der HTML-Code, der an den Browser geschickt wird
- client.println("<!DOCTYPE HTML>");
- client.println("<html>");
- client.print("<HEAD><TITLE>");
- client.print("NodeMCU Fiebi Board");
- client.println("</TITLE>");
- client.println("</HEAD><BODY>");
- client.println("<b>Hallo Welt!</b><br> /<br />");
- client.print("Node MCU V3 ESP8266WiFi läuft seit: ");
- // Gibt es noch was anderes? - sonst Tage Stunden Minuten Sekunden ... ausrechnen lassen :-)
- client.print(millis()/1000);
- client.println(" s.<br /><br />");
- client.print("Photowiderstand an Analogpin 0 zeigt: <b>");
- // liest analog-Port A0 und schreibt es in die HTML-Seite
- client.print(analogRead(A0));
- Serial.print("Wert des Analog-Ports 0: ");
- // liest analog-Port A0 und schreibt es in die Serielle Console
- Serial.println(analogRead(A0));
- client.println("</b><br />");
- client.print(F("<br>WLAN-LED ist zur Zeit <b>"));
- // 0 -> ein und 1-> aus komisch, ist aber so
- client.print((val) ? F("aus") : F("an"));
- client.print(F("</b><br><br>Klick <a href='http://"));
- client.print(WiFi.localIP());
- // 0 -> ein und 1-> aus komisch, ist aber so
- client.print(F("/gpio/0'>hier</a>, um den LED GPIO einzuschalten, oder <a href='http://"));
- client.print(WiFi.localIP());
- client.print(F("/gpio/1'>hier</a>, um den LED GPIO auszuschalten.</body></html>"));
- // The client will actually be *flushed* then disconnected
- // when the function returns and 'client' object is destroyed (out-of-scope)
- // flush = ensure written data are received by the other side
- Serial.println(F("Disconnecting from client"));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement