این برنامه یک سری مشکلات داشت. که برخی جاهاش را اصلاح کردم. برای من کرش هم نداشت و داره به درستی کار میکنه. سورس برنامه به صورت زیر است.
من از ماژول Air640S استفاده کردم. در بین دوستان اگر کسی با آپدیت فریمور مشکل داشت بهم بگه چون این ماژول عالی هست وقتی یاد بگیرید چطور ازش استفاده کنید.
با آپلود کد بالا و رفتن به آدرس هایی که در ارتباط سریال به نمایش در میاد میتونید LED متصل به پایه 13 را کنترل کنید.کد:/* Arduino Due - ESP 8266 WiFi Module Serial (Tx/Rx) communicate to PC via USB Serial3 (Tx3/Rx3) connect to ESP8266 Tx3 - ESP8266 Rx Rx3 - ESP8266 Tx ESP8266 CH_PD Connect to ESP8266 VCC */ #define ASCII_0 48 String SSID = "SSID"; String PASSWORD = "PASS"; int LED = 13; boolean FAIL_8266 = false; String stroffHTML = "<!doctype html>\ <html>\ <head>\ <title>in the name of god</title>\ </head>\ <body>\ <H1>LED Current State : off</H1>\ </body>\ </html>"; String stronHTML = "<!doctype html>\ <html>\ <head>\ <title>in the name of god</title>\ </head>\ <body>\ <H1>LED Current State : on</H1>\ </body>\ </html>"; #define BUFFER_SIZE 128 char buffer[BUFFER_SIZE]; void setup() { pinMode(LED, OUTPUT); digitalWrite(LED, LOW); delay(300); digitalWrite(LED, HIGH); delay(200); digitalWrite(LED, LOW); delay(300); digitalWrite(LED, HIGH); delay(200); digitalWrite(LED, LOW); delay(300); digitalWrite(LED, HIGH); delay(200); digitalWrite(LED, LOW); do{ Serial.begin(9600); //ESP8266.begin(9600); Serial1.begin(9600); //Wait Serial Monitor to start while(!Serial); Serial.println("--- Start ---"); //ESP8266.println("AT+RST"); Serial1.println("AT+RST"); delay(1000); if(Serial1.find("OK")) { Serial.println("Module is ready"); Serial1.println("AT+CWMODE=1"); delay(2000); //Quit existing AP, for demo Serial.println("Quit AP"); Serial1.println("AT+CWQAP"); delay(1000); clearESP8266SerialBuffer(); if(cwJoinAP()) { Serial.println("CWJAP Success"); FAIL_8266 = false; delay(3000); clearESP8266SerialBuffer(); //Get and display my IP sendESP8266Cmdln("AT+CIFSR", 1000); //Set multi connections sendESP8266Cmdln("AT+CIPMUX=1", 1000); //Setup web server on port 80 sendESP8266Cmdln("AT+CIPSERVER=1,80",1000); Serial.println("Server setup finish"); }else{ Serial.println("CWJAP Fail"); delay(500); FAIL_8266 = true; } }else{ Serial.println("Module have no response."); delay(500); FAIL_8266 = true; } }while(FAIL_8266); digitalWrite(LED, HIGH); } void loop(){ if(Serial1.available()){ Serial.println("Something received"); delay(1000); if(Serial1.find("+IPD,")){ String action; Serial.println("+IPD, found"); int connectionId = Serial1.read()-48; Serial.println("connectionId: " + String(connectionId)); Serial.println("--------------- DEBUG ----------------A"); char temp = char(Serial1.read()); while ((temp != '/') && (Serial1.available())){ Serial.print(temp); temp = char(Serial1.read()); } Serial.println("\n--------------- END DEBUG ----------------"); boolean isFound = Serial1.find("len="); Serial.print("is Found : "); Serial.println(isFound); char s = Serial1.read(); if(s=='0'){ action = "LED is OFF"; digitalWrite(LED, LOW); sendHTTPResponse(connectionId, stroffHTML); }else if(s=='1'){ action = "LED is ON"; digitalWrite(LED, HIGH); sendHTTPResponse(connectionId, stronHTML); }else { action = "led=?"; } Serial.println(action); //sendHTTPResponse(connectionId, action); } } } void sendHTTPResponse(int id, String content) { String response; response = "\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n"; response += "<body align=center>"; response += "<p>"; response += "<a href=\"http://192.168.1.110/len=1\">Turn On LED</a><p>"; response += "<a href=\"http://192.168.1.110/len=0\">Turn Off LED</a><p>"; response += content; response += "</body></html>\n"; String cmd = "AT+CIPSEND="; cmd += id; cmd += ","; cmd += response.length(); Serial.println("--- AT+CIPSEND ---"); sendESP8266Cmdln(cmd, 1000); Serial.println("--- data ---"); sendESP8266Data(response, 1000); /* String response; response = "HTTP/1.1 200 OK\r\n"; response += "Content-Type: text/html; charset=UTF-8\r\n"; response += "Content-Length: "; response += content.length(); response += "\r\n"; response +="Connection: close\r\n\r\n"; response += content; String cmd = "AT+CIPSEND="; cmd += id; cmd += ","; cmd += response.length(); Serial.println("--- AT+CIPSEND ---"); sendESP8266Cmdln(cmd, 1000); Serial.println("--- data ---"); sendESP8266Data(response, 1000); */ } boolean waitOKfromESP8266(int timeout) { do{ Serial.println("wait OK..."); delay(1000); if(Serial1.find("OK")) { return true; } }while((timeout--)>0); return false; } boolean cwJoinAP() { String cmd="AT+CWJAP=\"" + SSID + "\",\"" + PASSWORD + "\""; Serial1.println(cmd); return waitOKfromESP8266(10); } //Send command to ESP8266, assume OK, no error check //wait some time and display respond void sendESP8266Cmdln(String cmd, int waitTime) { Serial1.println(cmd); delay(waitTime); clearESP8266SerialBuffer(); } //Basically same as sendESP8266Cmdln() //But call ESP8266.print() instead of call ESP8266.println() void sendESP8266Data(String data, int waitTime) { Serial1.print(data); delay(waitTime); clearESP8266SerialBuffer(); } //Clear and display Serial Buffer for ESP8266 void clearESP8266SerialBuffer() { Serial.println("= clearESP8266SerialBuffer() ="); while (Serial1.available() > 0) { char a = Serial1.read(); Serial.write(a); } Serial.println("=============================="); }
به عنوان مثال برای من آدرس به صورت زیر بود
با رفتن به آدرس فوق LED روشن و در صورتی که به آدرس زیر بروید LED خاموش میگردد. مد نظرتون باشه میتوانید از لینک های موجود در خود صفحه HTML برای روشن و خاموش کردن استفاده کنیدکد:http://192.168.1.110/len=1
کد:http://192.168.1.110/len=0


6 لایک
LinkBack URL
About LinkBacks



پاسخ با نقل قول