Skocz do zawartości


  • Zawartość

  • Rejestracja

  • Ostatnio


0 Neutralna

O kamillx

  • Ranga
  1. wygląda to tak co o tym myślicie ? /********* Kamil Tekieli Complete project details at *********/ // Load Wi-Fi library #include <ESP8266WiFi.h> // Replace with your network credentials const char* ssid = "xx"; const char* password = "xx"; // Set web server port number to 8090 WiFiServer server(8090); // Variable to store the HTTP request String header; // Auxiliar variables to store the current output state String btState = "off"; // przypisane piny na nodemcu const int bt = 5; // Current time unsigned long currentTime = millis(); // Previous time unsigned long previousTime = 0; // Define timeout time in milliseconds (example: 2000ms = 2s) const long timeoutTime = 2000; void setup() { Serial.begin(115200); // Initialize the output variables as outputs pinMode(bt, OUTPUT); // Set outputs to LOW digitalWrite(bt, LOW); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); } void loop(){ WiFiClient client = server.available(); // Listen for incoming clients if (client) { // If a new client connects, Serial.println("New Client."); // print a message out in the serial port String currentLine = ""; // make a String to hold incoming data from the client currentTime = millis(); previousTime = currentTime; while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected currentTime = millis(); if (client.available()) { // if there's bytes to read from the client, char c =; // read a byte, then Serial.write(c); // print it out the serial monitor header += c; if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); // turns the GPIOs on and off if (header.indexOf("GET /brama/on") >= 0) { Serial.println("GPIO 5 on"); btState = "on"; digitalWrite(bt, HIGH); delay(1000); } else if ( btState == "on") { Serial.println("GPIO 5 off"); btState = "off"; digitalWrite(bt, LOW); } // Display the HTML web page client.println("<!DOCTYPE html><html>"); client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); client.println("<link rel=\"icon\" href=\"data:,\">"); // CSS to style the on/off buttons // Feel free to change the background-color and font-size attributes to fit your preferences client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}"); client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;"); client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}"); client.println(".button2 {background-color: #77878A;}</style></head>"); // Web Page Heading client.println("<body><h1>ESP8266 Web Server</h1>"); // Display current state, and ON/OFF buttons for GPIO 5 client.println("<p>GPIO 5 - State " + btState + "</p>"); // If the output5State is off, it displays the ON button if (btState=="off") { client.println("<p><a href=\"/brama/on\"><button class=\"button\">ON</button></a></p>"); } else { client.println("<p><a href=\"/brama/off\"><button class=\"button button2\">OFF</button></a></p>"); } client.println("</body></html>"); // The HTTP response ends with another blank line client.println(); // Break out of the while loop break; } else { // if you got a newline, then clear currentLine currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } } } // Clear the header variable header = ""; // Close the connection client.stop(); Serial.println("Client disconnected."); Serial.println(""); } }
  2. Witam jesli mógł bym was prosić o pomoc potrzebuje aby moje nodemcu pracowało nastepująco: po wciśnięciu przycisku On na stronie: przekaźnik włącza się na sekunde po czym wraca do pozycji OFF Mam taki kod: // ESP8266 Web Server Example // Target Hardware: esp8266 NodeMCU // // Creates a web server over wifi and allows controlling an led and relay // while also displaying temperature and humidity data from an SHT-21 sensor. // // NodeMCU Pins // SCL: D1 // SDA: D2 // LED: D3 // Relay: D5 // // Required libraries and board files: // esp8266 install from boards manager // SHT21 // // Gadget Reboot #include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #include "SHT21.h" ESP8266WebServer server(80); // create webserver object to listen for HTTP requests on port 80 SHT21 SHT21; // create i2c temperature/humidity sensor object const char* ssid = "ssid"; // wifi credentials const char* password = "password"; #define ledPin D3 #define relayPin D5 String htmlPage; // webpage text to be sent out by server when main page is accessed void setup() { Serial.begin(9600); SHT21.begin(); // init temp/humidity sensor digitalWrite(ledPin, LOW); // led is off initially pinMode(ledPin, OUTPUT); digitalWrite(relayPin, HIGH); // relay is not energized initially (active low) pinMode(relayPin, OUTPUT); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to WiFi..."); WiFi.mode(WIFI_STA); WiFi.disconnect(); // disconnect if previously connected delay(100); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected."); Serial.print("IP: "); Serial.println(WiFi.localIP()); // functions to call when client requests are received server.on("/", handleRoot); server.on("/LEDTOGGLE", handleLEDToggle); server.on("/LEDON", handleLEDOn); server.on("/LEDOFF", handleLEDOff); server.on("/RELAYTOGGLE", handleRelayToggle); server.on("/RELAYON", handleRelayOn); server.on("/RELAYOFF", handleRelayOff); server.onNotFound(handleNotFound); server.begin(); // start web server Serial.println("HTTP server started"); } void loop(void) { server.handleClient(); // listen for HTTP requests from clients } // client request handling functions // send main web page when ip+"/" is accessed void handleRoot() { buildHtmlPage(); server.send(200, "text/html", htmlPage); } // toggle led and redirect to main page void handleLEDToggle() { digitalWrite(ledPin, !digitalRead(ledPin)); server.sendHeader("Location", "/"); server.send(303); } // turn led on and redirect to main page void handleLEDOn() { digitalWrite(ledPin, HIGH); server.sendHeader("Location", "/"); server.send(303); } // turn led off and redirect to main page void handleLEDOff() { digitalWrite(ledPin, LOW); server.sendHeader("Location", "/"); server.send(303); } // toggle relay and redirect to main page void handleRelayToggle() { digitalWrite(relayPin, !digitalRead(relayPin)); server.sendHeader("Location", "/"); server.send(303); } // turn relay on and redirect to main page void handleRelayOn() { digitalWrite(relayPin, LOW); server.sendHeader("Location", "/"); server.send(303); } // turn relay off and redirect to main page void handleRelayOff() { digitalWrite(relayPin, HIGH); server.sendHeader("Location", "/"); server.send(303); } // send HTTP status 404: Not Found when there's no handler for the client request void handleNotFound() { server.send(404, "text/plain", "404: Not found"); } // create the html page for the root path of the web server void buildHtmlPage() { htmlPage = "<!DOCTYPE html>"; htmlPage += "<html>"; htmlPage += "<head>"; // header section htmlPage += "<title>ESP8266 Web Server</title>"; // title for browser window htmlPage += "<meta http-equiv=\"refresh\" content=\"10\">"; // auto-refresh the page every 10 seconds to show new data htmlPage += "<meta charset=\"UTF-8\">"; // UTF-8 allows the degree symbol to display correctly htmlPage += "</head>"; htmlPage += "<BODY bgcolor='#E0E0D0'>"; // body section, set background color // retrieve temperature/humidity from sensor to display on main page htmlPage += "<br>Temperature °C: " + String(SHT21.getTemperature()); htmlPage += "<br>Humidity %RH: " + String(SHT21.getHumidity()); htmlPage += "<br>"; // show led status and action buttons String ledState = ((digitalRead(ledPin)) ? "on" : "off"); htmlPage += "<br>LED: " + ledState; htmlPage += "<form action=\"/LEDON\" method=\"POST\"><input type=\"submit\" value=\"LED On\"></form>"; htmlPage += "<form action=\"/LEDOFF\" method=\"POST\"><input type=\"submit\" value=\"LED Off\"></form>"; htmlPage += "<form action=\"/LEDTOGGLE\" method=\"POST\"><input type=\"submit\" value=\"Toggle LED\"></form>"; // show relay status and action buttons String relayState = ((!digitalRead(relayPin)) ? "on" : "off"); htmlPage += "<br>Relay: " + relayState; htmlPage += "<form action=\"/RELAYON\" method=\"POST\"><input type=\"submit\" value=\"Relay On\"></form>"; htmlPage += "<form action=\"/RELAYOFF\" method=\"POST\"><input type=\"submit\" value=\"Relay Off\"></form>"; htmlPage += "<form action=\"/RELAYTOGGLE\" method=\"POST\"><input type=\"submit\" value=\"Toggle Relay\"></form>"; htmlPage += "</body>"; htmlPage += "</html>"; } czy pomógł by mi ktoś dopisać tą część kodu bo teraz działa ON.OFF gdzie samemu trzeba klikać. Z góry bardzo dziękuje..
  • Utwórz nowe...