2015-12-03 42 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(); 
}