ESP 8266计划问题

时间:2015-12-03 21:16:35

标签: arduino arduino-ide esp8266

说明

我在ESP-8266(01)上制作一个网络服务器,它将使用Arduino Mega控制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但是为两到三次然后模块重置它自己....

这是我从串行监视器获得的:

enter image description here

现在,我已经搜索了很多,并且它在代码中看了我做错了什么。

这就是我的ESP的连接方式:

vcc&amp; CHPD至3.3V ragulator

gnd of powersuply和arduino gnd ......

GP 0 ------&gt; 3.3v,2.2k电阻(非闪烁状态)。

Rst ------&gt; 3.3v带电阻

RX到TX

TX到RX

任何想法如何修复它?

还有其他方法可以控制ESP 8266模块的Arduino引脚(6个不同的引脚)吗?

我已经尝试了很多天请帮助!!

1 个答案:

答案 0 :(得分:0)

好的我发帖给那些面临同样问题的人!!我拿了Hello ESP示例并将其修改为Print Serial !!!

On Mega My Serial在9600开始,我在9600开始串行ESP以及:|所以这就是代码....慢速服务器和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();
}
相关问题