Advertisement
Guest User

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

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