Search code examples
arduinoarduino-ideesp8266

ESP 8266 Program Issue


Description

I am writing a web server on ESP-8266(01), that will control 3 locks using Arduino Mega.

The locks will be controlled by Bluetooth ESP 8266 (get method from phone) and using my laptops browser.

I can do it all with bluetooth HC-05 (easily).

The issue I am facing is with my ESP-8266 wifi module.

I need to send 3 different strings from ESP for Arduino to read it. If it's "AAAA" Arduino will read it and unlock door one, if it BBBB then door 2 and etc etc.....

So I used this code:

#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("");
 
}

The Result I Get on Browser is ( Which is What i need):

enter image description here

ISSUE:

Now The Issue Is Some Times it misses the serial data i send, For Example if i click unlock door 1, It sends data and i click unlock door 2 it does nothn and when i reset it sends data from all 3 but for two to 3 times and then module resets it self ....

This Is What I got From Serial Monitor:

enter image description here

Now i have searched alot and its watchdre out what i did wrong in the code.

this is how my ESP is connected:

vcc & CHPD to 3.3V ragulator

gnd to gnd of powersuply and to arduino gnd...

GP 0 ------> 3.3v with 2.2k Resistor ( in non flashing state).

Rst ------> 3.3v with resistor

RX to TX

TX to RX

Any Ideas how to fix it?

Is There any other way to control Arduino pins (6 different pins) from ESP 8266 module??

ive been trying from many days please help!!


Solution

  • Ok I am Posting It For those who are facing the same problem!! i took the Hello ESP example And Modified it to Print Serial!!!

    On Mega My Serial Was Started at 9600 and i started Serial On ESP at 9600 aswell :| So This is the Code .... Slow Server and WDT issue is Almost Gone....

    #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();
    }