اطلاعیه

Collapse
No announcement yet.

یکی کردن دو کد در ESP8266

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    یکی کردن دو کد در ESP8266

    سلام به همگی
    من دوتا کد برای ESP8266 دارم که میخوام یکی شه
    هر دو از یه لایبری استفاده میکنن و طرز کار جفتشونم یکیه
    اولین کد به اینصورته که وقتی به USB متصل شد به وایفایی که بهش دادیم متصل میشه و میتونیم از طریق یه وب پیج که تو سریال مانیتور نشون داده میشه رنگ ال ای دی RGB رو کنترل کنیم
    دومی هم مثل قبلی و همچیش مثل همه فقط فرقش اینه که بجای RGB دو تا دکمه سادست که ال ای دی ها رو کنترل میکنه
    جفتشون تو Html هستن و از یه وب پیج میان
    خودم سعی کردم یکی کنم تو وب پیج اومد ولی هیچکدوم کار نکرد
    لطفا سریعتر کمک کنید
    اگه بتونید برام یکی شده بفرستید خیلی ممنون میشم.
    کد ها:

    کنترل RGB:

    [FONT=&quot]/*********[/FONT]
    [FONT=&quot] Rui Santos[/FONT]
    [FONT=&quot] Complete project details at https://randomnerdtutorials.com [/FONT]
    [FONT=&quot]*********/[/FONT]

    [FONT=&quot]// Load Wi-Fi library[/FONT]
    [FONT=&quot]#include <ESP8266WiFi.h>[/FONT]

    [FONT=&quot]// Replace with your network credentials[/FONT]
    [FONT=&quot]const char* ssid = "*****";[/FONT]
    [FONT=&quot]const char* password = "*********";[/FONT]

    [FONT=&quot]// Set web server port number to 80[/FONT]
    [FONT=&quot]WiFiServer server(80);[/FONT]

    [FONT=&quot]// Decode HTTP GET value[/FONT]
    [FONT=&quot]String redString = "0";[/FONT]
    [FONT=&quot]String greenString = "0";[/FONT]
    [FONT=&quot]String blueString = "0";[/FONT]
    [FONT=&quot]int pos1 = 0;[/FONT]
    [FONT=&quot]int pos2 = 0;[/FONT]
    [FONT=&quot]int pos3 = 0;[/FONT]
    [FONT=&quot]int pos4 = 0;[/FONT]

    [FONT=&quot]// Variable to store the HTTP req uest[/FONT]
    [FONT=&quot]String header;[/FONT]

    [FONT=&quot]// Red, green, and blue pins for PWM control[/FONT]
    [FONT=&quot]const int redPin = 13; // 13 corresponds to GPIO13[/FONT]
    [FONT=&quot]const int greenPin = 12; // 12 corresponds to GPIO12[/FONT]
    [FONT=&quot]const int bluePin = 14; // 14 corresponds to GPIO14[/FONT]

    [FONT=&quot]// Setting PWM bit resolution[/FONT]
    [FONT=&quot]const int resolution = 256;[/FONT]

    [FONT=&quot]// Current time[/FONT]
    [FONT=&quot]unsigned long currentTime = millis();[/FONT]
    [FONT=&quot]// Previous time[/FONT]
    [FONT=&quot]unsigned long previousTime = 0; [/FONT]
    [FONT=&quot]// Define timeout time in milliseconds (example: 2000ms = 2s)[/FONT]
    [FONT=&quot]const long timeoutTime = 2000;[/FONT]

    [FONT=&quot]void setup() {[/FONT]
    [FONT=&quot] Serial.begin(115200);[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot] // configure LED PWM resolution/range and set pins to LOW[/FONT]
    [FONT=&quot] analogWriteRange(resolution);[/FONT]
    [FONT=&quot] analogWrite(redPin, 0);[/FONT]
    [FONT=&quot] analogWrite(greenPin, 0);[/FONT]
    [FONT=&quot] analogWrite(bluePin, 0);[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot] // Connect to Wi-Fi network with SSID and password[/FONT]
    [FONT=&quot] Serial.print("Connecting to ");[/FONT]
    [FONT=&quot] Serial.println(ssid);[/FONT]
    [FONT=&quot] WiFi.begin(ssid, password);[/FONT]
    [FONT=&quot] while (WiFi.status() != WL_CONNECTED) {[/FONT]
    [FONT=&quot] delay(500);[/FONT]
    [FONT=&quot] Serial.print(".");[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot] // Print local IP address and start web server[/FONT]
    [FONT=&quot] Serial.println("");[/FONT]
    [FONT=&quot] Serial.println("WiFi connected.");[/FONT]
    [FONT=&quot] Serial.println("IP address: ");[/FONT]
    [FONT=&quot] Serial.println(WiFi.localIP());[/FONT]
    [FONT=&quot] server.begin();[/FONT]
    [FONT=&quot]}[/FONT]

    [FONT=&quot]void loop(){[/FONT]
    [FONT=&quot] WiFiClient client = server.available(); // Listen for incoming clients[/FONT]

    [FONT=&quot] if (client) { // If a new client connects,[/FONT]
    [FONT=&quot] currentTime = millis();[/FONT]
    [FONT=&quot] previousTime = currentTime;[/FONT]
    [FONT=&quot] Serial.println("New Client."); // print a message out in the serial port[/FONT]
    [FONT=&quot] String currentLine = ""; // make a String to hold incoming data from the client[/FONT]
    [FONT=&quot] while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected[/FONT]
    [FONT=&quot] currentTime = millis();[/FONT]
    [FONT=&quot] if (client.available()) { // if there's bytes to read from the client,[/FONT]
    [FONT=&quot] char c = client.read(); // read a byte, then[/FONT]
    [FONT=&quot] Serial.write(c); // print it out the serial monitor[/FONT]
    [FONT=&quot] header += c;[/FONT]
    [FONT=&quot] if (c == '\n') { // if the byte is a newline character[/FONT]
    [FONT=&quot] // if the current line is blank, you got two newline characters in a row.[/FONT]
    [FONT=&quot] // that's the end of the client HTTP request, so send a response:[/FONT]
    [FONT=&quot] if (currentLine.length() == 0) {[/FONT]
    [FONT=&quot] // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)[/FONT]
    [FONT=&quot] // and a content-type so the client knows what's coming, then a blank line:[/FONT]
    [FONT=&quot] client.println("HTTP/1.1 200 OK");[/FONT]
    [FONT=&quot] client.println("Content-type:text/html");[/FONT]
    [FONT=&quot] client.println("Connection: close");[/FONT]
    [FONT=&quot] client.println();[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot] // Display the HTML web page[/FONT]
    [FONT=&quot] client.println("<!DOCTYPE html><html>");[/FONT]
    [FONT=&quot] client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");[/FONT]
    [FONT=&quot] client.println("<link rel=\"icon\" href=\"data:,\">");[/FONT]
    [FONT=&quot] client.println("<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\">");[/FONT]
    [FONT=&quot] client.println("<script src=\"https://cdnjs.cloudflare.com/ajax/libs/jscolor/2.0.4/jscolor.min.js\"></script>");[/FONT]
    [FONT=&quot] client.println("</head><body><div class=\"container\"><div class=\"row\"><h1>Room Light Color</h1></div>");[/FONT]
    [FONT=&quot] client.println("<a class=\"btn btn-primary btn-lg\" href=\"#\" id=\"change_color\" role=\"button\">Change Color</a> ");[/FONT]
    [FONT=&quot] client.println("<input class=\"jscolor {onFineChange:'update(this)'}\" id=\"rgb\"></div>");[/FONT]
    [FONT=&quot] 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=&quot] 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=&quot] // The HTTP response ends with another blank line[/FONT]
    [FONT=&quot] client.println();[/FONT]

    [FONT=&quot] // Request sample: /?r201g32b255&[/FONT]
    [FONT=&quot] // Red = 201 | Green = 32 | Blue = 255[/FONT]
    [FONT=&quot] if(header.indexOf("GET /?r") >= 0) {[/FONT]
    [FONT=&quot] pos1 = header.indexOf('r');[/FONT]
    [FONT=&quot] pos2 = header.indexOf('g');[/FONT]
    [FONT=&quot] pos3 = header.indexOf('b');[/FONT]
    [FONT=&quot] pos4 = header.indexOf('&');[/FONT]
    [FONT=&quot] redString = header.substring(pos1+1, pos2);[/FONT]
    [FONT=&quot] greenString = header.substring(pos2+1, pos3);[/FONT]
    [FONT=&quot] blueString = header.substring(pos3+1, pos4);[/FONT]
    [FONT=&quot] /*Serial.println(redString.toInt());[/FONT]
    [FONT=&quot] Serial.println(greenString.toInt());[/FONT]
    [FONT=&quot] Serial.println(blueString.toInt());*/[/FONT]
    [FONT=&quot] analogWrite(redPin, redString.toInt());[/FONT]
    [FONT=&quot] analogWrite(greenPin, greenString.toInt());[/FONT]
    [FONT=&quot] analogWrite(bluePin, blueString.toInt());[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot] // Break out of the while loop[/FONT]
    [FONT=&quot] break;[/FONT]
    [FONT=&quot] } else { // if you got a newline, then clear currentLine[/FONT]
    [FONT=&quot] currentLine = "";[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot] } else if (c != '\r') { // if you got anything else but a carriage return character,[/FONT]
    [FONT=&quot] currentLine += c; // add it to the end of the currentLine[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot] // Clear the header variable[/FONT]
    [FONT=&quot] header = "";[/FONT]
    [FONT=&quot] // Close the connection[/FONT]
    [FONT=&quot] client.stop();[/FONT]
    [FONT=&quot] Serial.println("Client disconnected.");[/FONT]
    [FONT=&quot] Serial.println("");[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot]}[/FONT]


    کد دکمه ها:

    [FONT=&quot]/*********[/FONT]
    [FONT=&quot] Rui Santos[/FONT]
    [FONT=&quot] Complete project details at https://randomnerdtutorials.com [/FONT]
    [FONT=&quot]*********/[/FONT]

    [FONT=&quot]// Load Wi-Fi library[/FONT]
    [FONT=&quot]#include <ESP8266WiFi.h>[/FONT]

    [FONT=&quot]// Replace with your network credentials[/FONT]
    [FONT=&quot]const char* ssid = "REPLACE_WITH_YOUR_SSID";[/FONT]
    [FONT=&quot]const char* password = "REPLACE_WITH_YOUR_PASSWORD";[/FONT]

    [FONT=&quot]// Set web server port number to 80[/FONT]
    [FONT=&quot]WiFiServer server(80);[/FONT]

    [FONT=&quot]// Variable to store the HTTP request[/FONT]
    [FONT=&quot]String header;[/FONT]

    [FONT=&quot]// Auxiliar variables to store the current output state[/FONT]
    [FONT=&quot]String output5State = "off";[/FONT]
    [FONT=&quot]String output4State = "off";[/FONT]

    [FONT=&quot]// Assign output variables to GPIO pins[/FONT]
    [FONT=&quot]const int output5 = 5;[/FONT]
    [FONT=&quot]const int output4 = 4;[/FONT]

    [FONT=&quot]// Current time[/FONT]
    [FONT=&quot]unsigned long currentTime = millis();[/FONT]
    [FONT=&quot]// Previous time[/FONT]
    [FONT=&quot]unsigned long previousTime = 0; [/FONT]
    [FONT=&quot]// Define timeout time in milliseconds (example: 2000ms = 2s)[/FONT]
    [FONT=&quot]const long timeoutTime = 2000;[/FONT]

    [FONT=&quot]void setup() {[/FONT]
    [FONT=&quot] Serial.begin(115200);[/FONT]
    [FONT=&quot] // Initialize the output variables as outputs[/FONT]
    [FONT=&quot] pinMode(output5, OUTPUT);[/FONT]
    [FONT=&quot] pinMode(output4, OUTPUT);[/FONT]
    [FONT=&quot] // Set outputs to LOW[/FONT]
    [FONT=&quot] digitalWrite(output5, LOW);[/FONT]
    [FONT=&quot] digitalWrite(output4, LOW);[/FONT]

    [FONT=&quot] // Connect to Wi-Fi network with SSID and password[/FONT]
    [FONT=&quot] Serial.print("Connecting to ");[/FONT]
    [FONT=&quot] Serial.println(ssid);[/FONT]
    [FONT=&quot] WiFi.begin(ssid, password);[/FONT]
    [FONT=&quot] while (WiFi.status() != WL_CONNECTED) {[/FONT]
    [FONT=&quot] delay(500);[/FONT]
    [FONT=&quot] Serial.print(".");[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot] // Print local IP address and start web server[/FONT]
    [FONT=&quot] Serial.println("");[/FONT]
    [FONT=&quot] Serial.println("WiFi connected.");[/FONT]
    [FONT=&quot] Serial.println("IP address: ");[/FONT]
    [FONT=&quot] Serial.println(WiFi.localIP());[/FONT]
    [FONT=&quot] server.begin();[/FONT]
    [FONT=&quot]}[/FONT]

    [FONT=&quot]void loop(){[/FONT]
    [FONT=&quot] WiFiClient client = server.available(); // Listen for incoming clients[/FONT]

    [FONT=&quot] if (client) { // If a new client connects,[/FONT]
    [FONT=&quot] Serial.println("New Client."); // print a message out in the serial port[/FONT]
    [FONT=&quot] String currentLine = ""; // make a String to hold incoming data from the client[/FONT]
    [FONT=&quot] currentTime = millis();[/FONT]
    [FONT=&quot] previousTime = currentTime;[/FONT]
    [FONT=&quot] while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected[/FONT]
    [FONT=&quot] currentTime = millis(); [/FONT]
    [FONT=&quot] if (client.available()) { // if there's bytes to read from the client,[/FONT]
    [FONT=&quot] char c = client.read(); // read a byte, then[/FONT]
    [FONT=&quot] Serial.write(c); // print it out the serial monitor[/FONT]
    [FONT=&quot] header += c;[/FONT]
    [FONT=&quot] if (c == '\n') { // if the byte is a newline character[/FONT]
    [FONT=&quot] // if the current line is blank, you got two newline characters in a row.[/FONT]
    [FONT=&quot] // that's the end of the client HTTP request, so send a response:[/FONT]
    [FONT=&quot] if (currentLine.length() == 0) {[/FONT]
    [FONT=&quot] // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)[/FONT]
    [FONT=&quot] // and a content-type so the client knows what's coming, then a blank line:[/FONT]
    [FONT=&quot] client.println("HTTP/1.1 200 OK");[/FONT]
    [FONT=&quot] client.println("Content-type:text/html");[/FONT]
    [FONT=&quot] client.println("Connection: close");[/FONT]
    [FONT=&quot] client.println();[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot] // turns the GPIOs on and off[/FONT]
    [FONT=&quot] if (header.indexOf("GET /5/on") >= 0) {[/FONT]
    [FONT=&quot] Serial.println("GPIO 5 on");[/FONT]
    [FONT=&quot] output5State = "on";[/FONT]
    [FONT=&quot] digitalWrite(output5, HIGH);[/FONT]
    [FONT=&quot] } else if (header.indexOf("GET /5/off") >= 0) {[/FONT]
    [FONT=&quot] Serial.println("GPIO 5 off");[/FONT]
    [FONT=&quot] output5State = "off";[/FONT]
    [FONT=&quot] digitalWrite(output5, LOW);[/FONT]
    [FONT=&quot] } else if (header.indexOf("GET /4/on") >= 0) {[/FONT]
    [FONT=&quot] Serial.println("GPIO 4 on");[/FONT]
    [FONT=&quot] output4State = "on";[/FONT]
    [FONT=&quot] digitalWrite(output4, HIGH);[/FONT]
    [FONT=&quot] } else if (header.indexOf("GET /4/off") >= 0) {[/FONT]
    [FONT=&quot] Serial.println("GPIO 4 off");[/FONT]
    [FONT=&quot] output4State = "off";[/FONT]
    [FONT=&quot] digitalWrite(output4, LOW);[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot] // Display the HTML web page[/FONT]
    [FONT=&quot] client.println("<!DOCTYPE html><html>");[/FONT]
    [FONT=&quot] client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");[/FONT]
    [FONT=&quot] client.println("<link rel=\"icon\" href=\"data:,\">");[/FONT]
    [FONT=&quot] // CSS to style the on/off buttons [/FONT]
    [FONT=&quot] // Feel free to change the background-color and font-size attributes to fit your preferences[/FONT]
    [FONT=&quot] client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");[/FONT]
    [FONT=&quot] client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");[/FONT]
    [FONT=&quot] client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");[/FONT]
    [FONT=&quot] client.println(".button2 {background-color: #77878A;}</style></head>");[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot] // Web Page Heading[/FONT]
    [FONT=&quot] client.println("<body><h1>ESP8266 Web Server</h1>");[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot] // Display current state, and ON/OFF buttons for GPIO 5 [/FONT]
    [FONT=&quot] client.println("<p>GPIO 5 - State " + output5State + "</p>");[/FONT]
    [FONT=&quot] // If the output5State is off, it displays the ON button [/FONT]
    [FONT=&quot] if (output5State=="off") {[/FONT]
    [FONT=&quot] client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");[/FONT]
    [FONT=&quot] } else {[/FONT]
    [FONT=&quot] client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");[/FONT]
    [FONT=&quot] } [/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot] // Display current state, and ON/OFF buttons for GPIO 4 [/FONT]
    [FONT=&quot] client.println("<p>GPIO 4 - State " + output4State + "</p>");[/FONT]
    [FONT=&quot] // If the output4State is off, it displays the ON button [/FONT]
    [FONT=&quot] if (output4State=="off") {[/FONT]
    [FONT=&quot] client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");[/FONT]
    [FONT=&quot] } else {[/FONT]
    [FONT=&quot] client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>");[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot] client.println("</body></html>");[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot] // The HTTP response ends with another blank line[/FONT]
    [FONT=&quot] client.println();[/FONT]
    [FONT=&quot] // Break out of the while loop[/FONT]
    [FONT=&quot] break;[/FONT]
    [FONT=&quot] } else { // if you got a newline, then clear currentLine[/FONT]
    [FONT=&quot] currentLine = "";[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot] } else if (c != '\r') { // if you got anything else but a carriage return character,[/FONT]
    [FONT=&quot] currentLine += c; // add it to the end of the currentLine[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot] // Clear the header variable[/FONT]
    [FONT=&quot] header = "";[/FONT]
    [FONT=&quot] // Close the connection[/FONT]
    [FONT=&quot] client.stop();[/FONT]
    [FONT=&quot] Serial.println("Client disconnected.");[/FONT]
    [FONT=&quot] Serial.println("");[/FONT]
    [FONT=&quot] }[/FONT]
    [FONT=&quot]}[/FONT]
    جدیدترین ویرایش توسط Kiarashkn; ۱۶:۰۷ ۱۳۹۸/۰۵/۱۴.

    #2
    پاسخ : یکی کردن دو کد در ESP8266

    سلام
    من میخوام با این ماژول وصل شم به اینترنت و از طریق اینترنت یک رله رو کنترل کنم کسی میتونه کمکم کنه
    یعنی ماژول وصل شه به مودم وایفای و از راه دور سایت رو باز کنم و از طریق دکمه ها خاموش روشنش کنم
    ممنون میشم راهنمایی کنید خیلی وقته دنبال این پروژه هستم

    دیدگاه

    لطفا صبر کنید...
    X