Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Vielen Dank an die Entwickler der Beispielprogramme und an Mats Vanselow - http://www.mats-vanselow.de !
- #include <ESP8266WiFi.h>
- // Dank an funduino.de
- // laut https://funduino.de/nr-42-oled-display-ssd1306-128x64-128x32
- #include <Wire.h>
- #include "SSD1306Ascii.h"
- #include "SSD1306AsciiWire.h"
- #define I2C_ADDRESS 0x3C
- SSD1306AsciiWire oled;
- // eingefügt
- #ifndef STASSID
- #define STASSID "Meine-ID"
- #define STAPSK "Mein-PWD"
- #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());
- // ab hier zum OLED-Display
- Serial.println(F("OLED-Display Start"));
- Wire.begin();
- Wire.setClock(400000L);
- oled.begin(&Adafruit128x64, I2C_ADDRESS);
- // bis hierher
- }
- 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
- // nun die OLED Ausgaben
- // laut https://funduino.de/nr-42-oled-display-ssd1306-128x64-128x32
- //
- oled.setFont(fixed_bold10x15); // Auswahl der Schriftart
- oled.clear(); //Löschen der aktuellen Displayanzeige
- oled.println(" ");
- oled.println("Photo_R bei:"); //Text in der ersten Zeile. "Println" sorgt dabei für einen Zeilensprung.
- oled.println(analogRead(A0));
- Serial.println(F("Disconnecting from client"));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement