سلام به همگی
من دوتا کد برای ESP8266 دارم که میخوام یکی شه
هر دو از یه لایبری استفاده میکنن و طرز کار جفتشونم یکیه
اولین کد به اینصورته که وقتی به USB متصل شد به وایفایی که بهش دادیم متصل میشه و میتونیم از طریق یه وب پیج که تو سریال مانیتور نشون داده میشه رنگ ال ای دی RGB رو کنترل کنیم
دومی هم مثل قبلی و همچیش مثل همه فقط فرقش اینه که بجای RGB دو تا دکمه سادست که ال ای دی ها رو کنترل میکنه
جفتشون تو Html هستن و از یه وب پیج میان
خودم سعی کردم یکی کنم تو وب پیج اومد ولی هیچکدوم کار نکرد
لطفا سریعتر کمک کنید
اگه بتونید برام یکی شده بفرستید خیلی ممنون میشم.
کد ها:
کنترل RGB:
کد دکمه ها:
من دوتا کد برای ESP8266 دارم که میخوام یکی شه
هر دو از یه لایبری استفاده میکنن و طرز کار جفتشونم یکیه
اولین کد به اینصورته که وقتی به USB متصل شد به وایفایی که بهش دادیم متصل میشه و میتونیم از طریق یه وب پیج که تو سریال مانیتور نشون داده میشه رنگ ال ای دی RGB رو کنترل کنیم
دومی هم مثل قبلی و همچیش مثل همه فقط فرقش اینه که بجای RGB دو تا دکمه سادست که ال ای دی ها رو کنترل میکنه
جفتشون تو Html هستن و از یه وب پیج میان
خودم سعی کردم یکی کنم تو وب پیج اومد ولی هیچکدوم کار نکرد
لطفا سریعتر کمک کنید
اگه بتونید برام یکی شده بفرستید خیلی ممنون میشم.
کد ها:
کنترل RGB:
[FONT="]/*********[/FONT]
[FONT="] Rui Santos[/FONT]
[FONT="] Complete project details at https://randomnerdtutorials.com [/FONT]
[FONT="]*********/[/FONT]
[FONT="]// Load Wi-Fi library[/FONT]
[FONT="]#include <ESP8266WiFi.h>[/FONT]
[FONT="]// Replace with your network credentials[/FONT]
[FONT="]const char* ssid = "*****";[/FONT]
[FONT="]const char* password = "*********";[/FONT]
[FONT="]// Set web server port number to 80[/FONT]
[FONT="]WiFiServer server(80);[/FONT]
[FONT="]// Decode HTTP GET value[/FONT]
[FONT="]String redString = "0";[/FONT]
[FONT="]String greenString = "0";[/FONT]
[FONT="]String blueString = "0";[/FONT]
[FONT="]int pos1 = 0;[/FONT]
[FONT="]int pos2 = 0;[/FONT]
[FONT="]int pos3 = 0;[/FONT]
[FONT="]int pos4 = 0;[/FONT]
[FONT="]// Variable to store the HTTP req uest[/FONT]
[FONT="]String header;[/FONT]
[FONT="]// Red, green, and blue pins for PWM control[/FONT]
[FONT="]const int redPin = 13; // 13 corresponds to GPIO13[/FONT]
[FONT="]const int greenPin = 12; // 12 corresponds to GPIO12[/FONT]
[FONT="]const int bluePin = 14; // 14 corresponds to GPIO14[/FONT]
[FONT="]// Setting PWM bit resolution[/FONT]
[FONT="]const int resolution = 256;[/FONT]
[FONT="]// Current time[/FONT]
[FONT="]unsigned long currentTime = millis();[/FONT]
[FONT="]// Previous time[/FONT]
[FONT="]unsigned long previousTime = 0; [/FONT]
[FONT="]// Define timeout time in milliseconds (example: 2000ms = 2s)[/FONT]
[FONT="]const long timeoutTime = 2000;[/FONT]
[FONT="]void setup() {[/FONT]
[FONT="] Serial.begin(115200);[/FONT]
[FONT="] [/FONT]
[FONT="] // configure LED PWM resolution/range and set pins to LOW[/FONT]
[FONT="] analogWriteRange(resolution);[/FONT]
[FONT="] analogWrite(redPin, 0);[/FONT]
[FONT="] analogWrite(greenPin, 0);[/FONT]
[FONT="] analogWrite(bluePin, 0);[/FONT]
[FONT="] [/FONT]
[FONT="] // Connect to Wi-Fi network with SSID and password[/FONT]
[FONT="] Serial.print("Connecting to ");[/FONT]
[FONT="] Serial.println(ssid);[/FONT]
[FONT="] WiFi.begin(ssid, password);[/FONT]
[FONT="] while (WiFi.status() != WL_CONNECTED) {[/FONT]
[FONT="] delay(500);[/FONT]
[FONT="] Serial.print(".");[/FONT]
[FONT="] }[/FONT]
[FONT="] // Print local IP address and start web server[/FONT]
[FONT="] Serial.println("");[/FONT]
[FONT="] Serial.println("WiFi connected.");[/FONT]
[FONT="] Serial.println("IP address: ");[/FONT]
[FONT="] Serial.println(WiFi.localIP());[/FONT]
[FONT="] server.begin();[/FONT]
[FONT="]}[/FONT]
[FONT="]void loop(){[/FONT]
[FONT="] WiFiClient client = server.available(); // Listen for incoming clients[/FONT]
[FONT="] if (client) { // If a new client connects,[/FONT]
[FONT="] currentTime = millis();[/FONT]
[FONT="] previousTime = currentTime;[/FONT]
[FONT="] Serial.println("New Client."); // print a message out in the serial port[/FONT]
[FONT="] String currentLine = ""; // make a String to hold incoming data from the client[/FONT]
[FONT="] while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected[/FONT]
[FONT="] currentTime = millis();[/FONT]
[FONT="] if (client.available()) { // if there's bytes to read from the client,[/FONT]
[FONT="] char c = client.read(); // read a byte, then[/FONT]
[FONT="] Serial.write(c); // print it out the serial monitor[/FONT]
[FONT="] header += c;[/FONT]
[FONT="] if (c == '\n') { // if the byte is a newline character[/FONT]
[FONT="] // if the current line is blank, you got two newline characters in a row.[/FONT]
[FONT="] // that's the end of the client HTTP request, so send a response:[/FONT]
[FONT="] if (currentLine.length() == 0) {[/FONT]
[FONT="] // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)[/FONT]
[FONT="] // and a content-type so the client knows what's coming, then a blank line:[/FONT]
[FONT="] client.println("HTTP/1.1 200 OK");[/FONT]
[FONT="] client.println("Content-type:text/html");[/FONT]
[FONT="] client.println("Connection: close");[/FONT]
[FONT="] client.println();[/FONT]
[FONT="] [/FONT]
[FONT="] // Display the HTML web page[/FONT]
[FONT="] client.println("<!DOCTYPE html><html>");[/FONT]
[FONT="] client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");[/FONT]
[FONT="] client.println("<link rel=\"icon\" href=\"data:,\">");[/FONT]
[FONT="] client.println("<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\">");[/FONT]
[FONT="] client.println("<script src=\"https://cdnjs.cloudflare.com/ajax/libs/jscolor/2.0.4/jscolor.min.js\"></script>");[/FONT]
[FONT="] client.println("</head><body><div class=\"container\"><div class=\"row\"><h1>Room Light Color</h1></div>");[/FONT]
[FONT="] client.println("<a class=\"btn btn-primary btn-lg\" href=\"#\" id=\"change_color\" role=\"button\">Change Color</a> ");[/FONT]
[FONT="] client.println("<input class=\"jscolor {onFineChange:'update(this)'}\" id=\"rgb\"></div>");[/FONT]
[FONT="] client.println("<script>function update(picker) {document.getElementById('rgb').innerHTML = Math.round(picker.rgb[0]) + ', ' + Math.round(picker.rgb[1]) + ', ' + Math.round(picker.rgb[2]);");[/FONT]
[FONT="] client.println("document.getElementById(\"change_color\").href=\"?r\" + Math.round(picker.rgb[0]) + \"g\" + Math.round(picker.rgb[1]) + \"b\" + Math.round(picker.rgb[2]) + \"&\";}</script></body></html>");[/FONT]
[FONT="] // The HTTP response ends with another blank line[/FONT]
[FONT="] client.println();[/FONT]
[FONT="] // Request sample: /?r201g32b255&[/FONT]
[FONT="] // Red = 201 | Green = 32 | Blue = 255[/FONT]
[FONT="] if(header.indexOf("GET /?r") >= 0) {[/FONT]
[FONT="] pos1 = header.indexOf('r');[/FONT]
[FONT="] pos2 = header.indexOf('g');[/FONT]
[FONT="] pos3 = header.indexOf('b');[/FONT]
[FONT="] pos4 = header.indexOf('&');[/FONT]
[FONT="] redString = header.substring(pos1+1, pos2);[/FONT]
[FONT="] greenString = header.substring(pos2+1, pos3);[/FONT]
[FONT="] blueString = header.substring(pos3+1, pos4);[/FONT]
[FONT="] /*Serial.println(redString.toInt());[/FONT]
[FONT="] Serial.println(greenString.toInt());[/FONT]
[FONT="] Serial.println(blueString.toInt());*/[/FONT]
[FONT="] analogWrite(redPin, redString.toInt());[/FONT]
[FONT="] analogWrite(greenPin, greenString.toInt());[/FONT]
[FONT="] analogWrite(bluePin, blueString.toInt());[/FONT]
[FONT="] }[/FONT]
[FONT="] // Break out of the while loop[/FONT]
[FONT="] break;[/FONT]
[FONT="] } else { // if you got a newline, then clear currentLine[/FONT]
[FONT="] currentLine = "";[/FONT]
[FONT="] }[/FONT]
[FONT="] } else if (c != '\r') { // if you got anything else but a carriage return character,[/FONT]
[FONT="] currentLine += c; // add it to the end of the currentLine[/FONT]
[FONT="] }[/FONT]
[FONT="] }[/FONT]
[FONT="] }[/FONT]
[FONT="] // Clear the header variable[/FONT]
[FONT="] header = "";[/FONT]
[FONT="] // Close the connection[/FONT]
[FONT="] client.stop();[/FONT]
[FONT="] Serial.println("Client disconnected.");[/FONT]
[FONT="] Serial.println("");[/FONT]
[FONT="] }[/FONT]
[FONT="]}[/FONT]
کد دکمه ها:
[FONT="]/*********[/FONT]
[FONT="] Rui Santos[/FONT]
[FONT="] Complete project details at https://randomnerdtutorials.com [/FONT]
[FONT="]*********/[/FONT]
[FONT="]// Load Wi-Fi library[/FONT]
[FONT="]#include <ESP8266WiFi.h>[/FONT]
[FONT="]// Replace with your network credentials[/FONT]
[FONT="]const char* ssid = "REPLACE_WITH_YOUR_SSID";[/FONT]
[FONT="]const char* password = "REPLACE_WITH_YOUR_PASSWORD";[/FONT]
[FONT="]// Set web server port number to 80[/FONT]
[FONT="]WiFiServer server(80);[/FONT]
[FONT="]// Variable to store the HTTP request[/FONT]
[FONT="]String header;[/FONT]
[FONT="]// Auxiliar variables to store the current output state[/FONT]
[FONT="]String output5State = "off";[/FONT]
[FONT="]String output4State = "off";[/FONT]
[FONT="]// Assign output variables to GPIO pins[/FONT]
[FONT="]const int output5 = 5;[/FONT]
[FONT="]const int output4 = 4;[/FONT]
[FONT="]// Current time[/FONT]
[FONT="]unsigned long currentTime = millis();[/FONT]
[FONT="]// Previous time[/FONT]
[FONT="]unsigned long previousTime = 0; [/FONT]
[FONT="]// Define timeout time in milliseconds (example: 2000ms = 2s)[/FONT]
[FONT="]const long timeoutTime = 2000;[/FONT]
[FONT="]void setup() {[/FONT]
[FONT="] Serial.begin(115200);[/FONT]
[FONT="] // Initialize the output variables as outputs[/FONT]
[FONT="] pinMode(output5, OUTPUT);[/FONT]
[FONT="] pinMode(output4, OUTPUT);[/FONT]
[FONT="] // Set outputs to LOW[/FONT]
[FONT="] digitalWrite(output5, LOW);[/FONT]
[FONT="] digitalWrite(output4, LOW);[/FONT]
[FONT="] // Connect to Wi-Fi network with SSID and password[/FONT]
[FONT="] Serial.print("Connecting to ");[/FONT]
[FONT="] Serial.println(ssid);[/FONT]
[FONT="] WiFi.begin(ssid, password);[/FONT]
[FONT="] while (WiFi.status() != WL_CONNECTED) {[/FONT]
[FONT="] delay(500);[/FONT]
[FONT="] Serial.print(".");[/FONT]
[FONT="] }[/FONT]
[FONT="] // Print local IP address and start web server[/FONT]
[FONT="] Serial.println("");[/FONT]
[FONT="] Serial.println("WiFi connected.");[/FONT]
[FONT="] Serial.println("IP address: ");[/FONT]
[FONT="] Serial.println(WiFi.localIP());[/FONT]
[FONT="] server.begin();[/FONT]
[FONT="]}[/FONT]
[FONT="]void loop(){[/FONT]
[FONT="] WiFiClient client = server.available(); // Listen for incoming clients[/FONT]
[FONT="] if (client) { // If a new client connects,[/FONT]
[FONT="] Serial.println("New Client."); // print a message out in the serial port[/FONT]
[FONT="] String currentLine = ""; // make a String to hold incoming data from the client[/FONT]
[FONT="] currentTime = millis();[/FONT]
[FONT="] previousTime = currentTime;[/FONT]
[FONT="] while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected[/FONT]
[FONT="] currentTime = millis(); [/FONT]
[FONT="] if (client.available()) { // if there's bytes to read from the client,[/FONT]
[FONT="] char c = client.read(); // read a byte, then[/FONT]
[FONT="] Serial.write(c); // print it out the serial monitor[/FONT]
[FONT="] header += c;[/FONT]
[FONT="] if (c == '\n') { // if the byte is a newline character[/FONT]
[FONT="] // if the current line is blank, you got two newline characters in a row.[/FONT]
[FONT="] // that's the end of the client HTTP request, so send a response:[/FONT]
[FONT="] if (currentLine.length() == 0) {[/FONT]
[FONT="] // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)[/FONT]
[FONT="] // and a content-type so the client knows what's coming, then a blank line:[/FONT]
[FONT="] client.println("HTTP/1.1 200 OK");[/FONT]
[FONT="] client.println("Content-type:text/html");[/FONT]
[FONT="] client.println("Connection: close");[/FONT]
[FONT="] client.println();[/FONT]
[FONT="] [/FONT]
[FONT="] // turns the GPIOs on and off[/FONT]
[FONT="] if (header.indexOf("GET /5/on") >= 0) {[/FONT]
[FONT="] Serial.println("GPIO 5 on");[/FONT]
[FONT="] output5State = "on";[/FONT]
[FONT="] digitalWrite(output5, HIGH);[/FONT]
[FONT="] } else if (header.indexOf("GET /5/off") >= 0) {[/FONT]
[FONT="] Serial.println("GPIO 5 off");[/FONT]
[FONT="] output5State = "off";[/FONT]
[FONT="] digitalWrite(output5, LOW);[/FONT]
[FONT="] } else if (header.indexOf("GET /4/on") >= 0) {[/FONT]
[FONT="] Serial.println("GPIO 4 on");[/FONT]
[FONT="] output4State = "on";[/FONT]
[FONT="] digitalWrite(output4, HIGH);[/FONT]
[FONT="] } else if (header.indexOf("GET /4/off") >= 0) {[/FONT]
[FONT="] Serial.println("GPIO 4 off");[/FONT]
[FONT="] output4State = "off";[/FONT]
[FONT="] digitalWrite(output4, LOW);[/FONT]
[FONT="] }[/FONT]
[FONT="] [/FONT]
[FONT="] // Display the HTML web page[/FONT]
[FONT="] client.println("<!DOCTYPE html><html>");[/FONT]
[FONT="] client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");[/FONT]
[FONT="] client.println("<link rel=\"icon\" href=\"data:,\">");[/FONT]
[FONT="] // CSS to style the on/off buttons [/FONT]
[FONT="] // Feel free to change the background-color and font-size attributes to fit your preferences[/FONT]
[FONT="] client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");[/FONT]
[FONT="] client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");[/FONT]
[FONT="] client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");[/FONT]
[FONT="] client.println(".button2 {background-color: #77878A;}</style></head>");[/FONT]
[FONT="] [/FONT]
[FONT="] // Web Page Heading[/FONT]
[FONT="] client.println("<body><h1>ESP8266 Web Server</h1>");[/FONT]
[FONT="] [/FONT]
[FONT="] // Display current state, and ON/OFF buttons for GPIO 5 [/FONT]
[FONT="] client.println("<p>GPIO 5 - State " + output5State + "</p>");[/FONT]
[FONT="] // If the output5State is off, it displays the ON button [/FONT]
[FONT="] if (output5State=="off") {[/FONT]
[FONT="] client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");[/FONT]
[FONT="] } else {[/FONT]
[FONT="] client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");[/FONT]
[FONT="] } [/FONT]
[FONT="] [/FONT]
[FONT="] // Display current state, and ON/OFF buttons for GPIO 4 [/FONT]
[FONT="] client.println("<p>GPIO 4 - State " + output4State + "</p>");[/FONT]
[FONT="] // If the output4State is off, it displays the ON button [/FONT]
[FONT="] if (output4State=="off") {[/FONT]
[FONT="] client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");[/FONT]
[FONT="] } else {[/FONT]
[FONT="] client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>");[/FONT]
[FONT="] }[/FONT]
[FONT="] client.println("</body></html>");[/FONT]
[FONT="] [/FONT]
[FONT="] // The HTTP response ends with another blank line[/FONT]
[FONT="] client.println();[/FONT]
[FONT="] // Break out of the while loop[/FONT]
[FONT="] break;[/FONT]
[FONT="] } else { // if you got a newline, then clear currentLine[/FONT]
[FONT="] currentLine = "";[/FONT]
[FONT="] }[/FONT]
[FONT="] } else if (c != '\r') { // if you got anything else but a carriage return character,[/FONT]
[FONT="] currentLine += c; // add it to the end of the currentLine[/FONT]
[FONT="] }[/FONT]
[FONT="] }[/FONT]
[FONT="] }[/FONT]
[FONT="] // Clear the header variable[/FONT]
[FONT="] header = "";[/FONT]
[FONT="] // Close the connection[/FONT]
[FONT="] client.stop();[/FONT]
[FONT="] Serial.println("Client disconnected.");[/FONT]
[FONT="] Serial.println("");[/FONT]
[FONT="] }[/FONT]
[FONT="]}[/FONT]
دیدگاه