ESP 8266 程序问题

ESP 8266 Program Issue

描述

我正在 ESP-8266(01) 上编写一个网络服务器,它将使用 Arduino Mega 控制 3 个锁。

锁将由蓝牙 ESP 8266(从 phone 获取方法)和使用我的笔记本电脑浏览器控制。

我可以使用蓝牙 HC-05(轻松)完成这一切。

我遇到的问题是我的 ESP-8266 wifi 模块。

我需要从 ESP 发送 3 个不同的字符串供 Arduino 读取。 如果是“AAAA”,Arduino 会读取它并解锁一号门,如果是 BBBB,则打开二号门,等等……

所以我使用了这段代码:

#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 个门发送数据但是2到3次然后模块自行重置....

这是我从串口监视器得到的:

enter image description here

现在我已经搜索了很多,它监视着我在代码中做错了什么。

这是我的 ESP 的连接方式:

vcc 和 CHPD 到 3.3V 稳压器

gnd 到 powersuply 的 gnd 和 arduino gnd...

GP 0 ------> 3.3v 带 2.2k 电阻(非闪烁状态)。

Rst ------> 3.3v 带电阻

接收到发送

TX 到 RX

有什么解决办法吗?

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

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

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

在 Mega 上,我的序列号开始于 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 = "AeDrkiILA!$#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();
}