2015-12-03 41 views
1

說明:ESP 8266計劃發行

我作出(01)上的ESP-8266 Web服務器,將控制使用的Arduino米加3個鎖定。

鎖將由藍牙,ESP 8266(從手機獲得方式)和使用我的筆記本電腦瀏覽器控制。

- 我可以用藍牙HC-05(輕鬆地)做到這一切。

我面對的問題是與我的ESP-8266 wifi模塊。

我需要從ESP發送3個不同的字符串給arduino來閱讀它。 如果它的「AAAA」的Arduino會讀它和開鎖之一,如果它BBBB然後門2和等等等等.....

所以我用下面的代碼:

#include <ESP8266WiFi.h> 

const char* ssid = "Do-Not-Ask-For-Password"; 
const char* password = "ITISMYPASSWORD"; 

//const char* ssid = "`STAR-NET-Azhar-32210352"; 
//const char* password = "ITISMYPASSWORD"; 

int ledPin = 2; // GPIO2 
WiFiServer server(80); 
// Update these with values suitable for your network. 
IPAddress ip(192,168,8,128); //Node static IP 
IPAddress gateway(192,168,8,1); 
IPAddress subnet(255,255,255,0); 


void setup() { 
    Serial.begin(115200); 
    delay(10); 

    pinMode(ledPin, OUTPUT); 
    digitalWrite(ledPin, LOW); 

    // Connect to WiFi network 
    Serial.println(); 
    Serial.println(); 
    Serial.print("connecting to "); 
    Serial.println(ssid); 

    WiFi.begin(ssid, password); 
    WiFi.config(ip, gateway, subnet); 

    while (WiFi.status() != WL_CONNECTED) { 
    delay(500); 
    Serial.print("."); 
    } 
    Serial.println(""); 
    Serial.println("WiFi connected"); 

    // Start the server 
    server.begin(); 
    Serial.println("Server started"); 

    // Print the IP address 
    Serial.print("Use this URL to connect: "); 
    Serial.print("http://"); 
    Serial.print(WiFi.localIP()); 
    Serial.println("/"); 

} 

void loop() { 
    ESP.wdtDisable(); 
    // Check if a client has connected 
    WiFiClient client = server.available(); 
    if (!client) { 
    return; 
    } 

    // Wait until the client sends some data 
// Serial.println("new client"); 
// while(!client.available()){ 
    // delay(1); 

// } 
    if(!client.available()){ 
    delay(1); 
    client.flush(); 
    } 

    // Read the first line of the request 
    String request = client.readStringUntil('\r'); 
    //Serial.println(request); 
    client.flush(); 

    // Match the request 

    int value1 = LOW; 
    int value2 = LOW; 
    int value3 = LOW; 
    if (request.indexOf("/LOCK=ON1") != -1) { 
    //digitalWrite(ledPin, HIGH); 
    value1 = HIGH; 
    Serial.println("AAAA"); 

    } 
    if(request.indexOf("/LOCK=ON2") != -1){ 
    value2 = HIGH; 
    Serial.println("BBBB"); 

    } 
    if(request.indexOf("/LOCK=ON3") != -1){ 
    value3 = HIGH; 
    Serial.println("CCCC"); 

    } 


// Set ledPin according to the request 
//digitalWrite(ledPin, value); 

    // Return the response 
    client.println("HTTP/1.1 200 OK"); 
    client.println("Content-Type: text/html"); 
    client.println(""); // do not forget this one 
    client.println("<!DOCTYPE HTML>"); 
    client.println("<html>"); 
client.println("<br><br>"); 
    client.print("LOCK ONE IS NOW: "); 
    if(value1 == HIGH) { 
    client.print("UNLOCKED"); 
    // Serial.println("A"); 
    } else { 
    client.print("LOCKED"); 
    } 
    client.println("<br><br>"); 
    client.print("LOCK TWO IS NOW: "); 
    if(value2 == HIGH) { 
    client.print("UNLOCKED"); 
    // Serial.println("B"); 
    } else { 
    client.print("LOCKED"); 
    } 
    client.println("<br><br>"); 
    client.print("LOCK THREE IS NOW: "); 
    if(value3 == HIGH) { 
    client.print("UNLOCKED"); 
    //Serial.println("C"); 
    } else { 
    client.print("LOCKED"); 
    } 


    client.println("<br><br>"); 
    client.println("CLICK <a href=\"/LOCK=ON1\">HERE</a> TO UNLOCK THE LOCK ONE<br>"); 
    client.println("CLICK <a href=\"/LOCK=ON2\">HERE</a> TO UNLOCK THE LOCK TWO<br>"); 
    client.println("CLICK <a href=\"/LOCK=ON3\">HERE</a> TO UNLOCK THE LOCK THREE<br>"); 



    client.println("</html>"); 

    delay(100); 
    //Serial.println("client disconnected"); 
// Serial.println(""); 

} 

結果我獲取有關瀏覽器是(這正是我所需要的):

enter image description here

問題:

現在問題是有些時候它錯過了我發送的串行數據,例如,如果我點擊解鎖門1,它發送數據,我點擊解鎖門2它沒有,當我重置它發送所有3的數據,但爲兩到3次,然後模塊復位它的自我....

這是我得到了從串行監視器:

enter image description here

現在我有很多搜索和watchdre什麼我做錯了什麼代碼。

這是我的ESP是如何連接:

VCC & CHPD至3.3V ragulator

GND到powersuply的GND和Arduino的GND ...

GP 0 ----- - > 3.3v和2.2k電阻(非閃爍狀態)。

Rst的------> 3.3V與電阻

RX到TX

TX到RX

任何想法如何解決呢?

是否有任何其他方式來控制從ESP 8266模塊的Arduino引腳(6 diffrnt引腳)?

我一直試圖從很多天請幫助!

回答

0

好吧我正在發佈對於那些面臨同樣問題的人!我採取了你好ESP的例子,並修改它打印序列!

在兆豐我的串行在9600開始,我開始串行在ESP在9600以及:所以這是代碼....降低服務器和WDT問題幾乎沒有了....

#include <ESP8266WiFi.h> 
#include <WiFiClient.h> 
#include <ESP8266WebServer.h> 
#include <ESP8266mDNS.h> 

//const int RELAY_PIN = 2; //RELAY 

const char* ssid = "Do-Not-Ask-For-Password"; 
const char* password = "AeDrki$32ILA!$#2"; 
MDNSResponder mdns; 
ESP8266WebServer server(80); 

void handleRoot() { 
    server.send(200, "text/plain", "hWelcome To Door Unlock Project By Haziq Sheikh"); 
} 

void handleNotFound(){ 
    String message = "File Not Found\n\n"; 
    message += "URI: "; 
    message += server.uri(); 
    message += "\nMethod: "; 
    message += (server.method() == HTTP_GET)?"GET":"POST"; 
    message += "\nArguments: "; 
    message += server.args(); 
    message += "\n"; 
    for (uint8_t i=0; i<server.args(); i++){ 
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; 
    } 
    server.send(404, "text/plain", message); 
} 

void setup(void){ 
// pinMode(RELAY_PIN, OUTPUT); 
    Serial.begin(9600); 
    WiFi.begin(ssid, password); 
    Serial.println(""); 

    // Wait for connection 
    while (WiFi.status() != WL_CONNECTED) { 
    delay(500); 
    Serial.print("."); 
    //digitalWrite(RELAY_PIN, 1); 
    } 
    Serial.println(""); 
    Serial.print("Connected to "); 
    Serial.println(ssid); 
    Serial.print("IP address: "); 
    Serial.println(WiFi.localIP()); 
// digitalWrite(RELAY_PIN, 0); 

    if (mdns.begin("esp8266", WiFi.localIP())) { 
    Serial.println("MDNS responder started"); 
    } 

    server.on("/", handleRoot); 

    server.on("/lock1", [](){ 
    server.send(200, "text/plain", "Okay Door One Unlocked"); 
    Serial.println("AAAA"); 
    }); 

    server.on("/lock2", [](){ 
    server.send(200, "text/plain", "Okay -- Door 2 Unlocked"); 
    Serial.println("BBBB"); 
    }); 

    server.on("/lock3", [](){ 
    server.send(200, "text/plain", "Okay -- Door 3 is Unlocked"); 
    Serial.println("CCCC"); 
    }); 

    server.onNotFound(handleNotFound); 

    server.begin(); 
    Serial.println("HTTP server started"); 
} 

void loop(void){ 
    server.handleClient(); 
}