Go Down

Topic: Arduino and eps8266 (Read 315 times) previous topic - next topic

alexgr2006

hello all..
i have this code and sometimes working but working with delay can help me anyone? i need to open one led from my mobile direct with esp8266...my code is:

    #include <SoftwareSerial.h>
   
    #define DEBUG true
     
    SoftwareSerial esp8266(11,10); // make RX Arduino line is pin 11, make TX Arduino line is pin 10.
                                 // This means that you need to connect the TX line from the esp to the Arduino's pin 11
                                 // and the RX line from the esp to the Arduino's pin 10
    void setup()
    {
      Serial.begin(9600);
      esp8266.begin(115200); // your esp's baud rate might be different
     
      pinMode(8,OUTPUT);
      digitalWrite(8,LOW);
     
      pinMode(6,OUTPUT);
      digitalWrite(6,LOW);
     
      pinMode(9,OUTPUT);
      digitalWrite(9,LOW);
       
      Serial.println(sendData("AT+RST\r\n",2000,DEBUG)); // reset module
      Serial.println(sendData("AT+CWMODE=3\r\n",1000,DEBUG)); // configure as access point   AP=2, STA=1, 3=BOTH
      Serial.println(sendData("AT+CIFSR\r\n",1000,DEBUG)); // get ip address
      Serial.println(sendData("AT+CIPMUX=1\r\n",1000,DEBUG)); // configure for multiple connections
      Serial.println(sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG)); // turn on server on port 80
    }
     
    void loop()
    {
     /* String t;
    while (esp8266.available())
      {
        t+=(char)esp8266.read();//read byte,convert to char and append it to string
      }//end while
     
      Serial.println(t);
      delay(2000);
          if (t.length())
          { // if string is not empty
                if (t=="/?on")
                {
                digitalWrite(9, HIGH);
                Serial.write("LED is ON \n");
                }//end if
                else if (t=="+IPD,0,457=GET /?off HTTP/1.1")
                {
                  digitalWrite(9, LOW);
                Serial.write("LED is OFF \n");
                }
                         
           
            }//end if
 */
 
     
      if(esp8266.available()) // check if the esp is sending a message
      {
     

       
        if(esp8266.find("+IPD,"))
        {
         delay(1000); // wait for the serial buffer to fill up (read all the serial data)
         // get the connection id so that we can then disconnect
         int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns
                                               // the ASCII decimal value and 0 (the first decimal number) starts at 48
//**************************************************
               String t;
               
                esp8266.find("/?"); // advance cursor to "pin="
               while (esp8266.available())
                 {
                  t+=(char)esp8266.read();//read byte,convert to char and append it to string
                  if (t.length()==2){break;}
                 }
              //  t=(char)esp8266.read();
               //  t+=(char)esp8266.read();
                 Serial.println(t);
                 
              if(t=="on")
                    {
             digitalWrite(8, HIGH);
                Serial.write("LED is ON \n");
               
                    }
              else if(t=="of")
                {
                  digitalWrite(8, LOW);
                Serial.write("LED is OFF \n");
               
                }

                if(t=="6o")
                    {
             digitalWrite(6, HIGH);
                Serial.write("LED is ON \n");
               
                    }
              else if(t=="6f")
                {
                  digitalWrite(6, LOW);
                Serial.write("LED is OFF \n");
               
                }
//***************************************************
              /*
         esp8266.find("pin="); // advance cursor to "pin="
         
         int pinNumber = (esp8266.read()-48)*10; // get first number i.e. if the pin 13 then the 1st number is 1, then multiply to get 10
         pinNumber += (esp8266.read()-48); // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number
         
         digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin   
         */
         // make close command
         String closeCommand = "AT+CIPCLOSE=";
         closeCommand+=connectionId; // append connection id
         closeCommand+="\r\n";
         
         sendData(closeCommand,1000,DEBUG); // close connection.

      }
    }
   
    }//loop
    /*
    * Name: sendData
    * Description: Function used to send data to ESP8266.
    * Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
    * Returns: The response from the esp8266 (if there is a reponse)
    */
    String sendData(String command, const int timeout, boolean debug)
    {
        String response = "";
       
        esp8266.print(command); // send the read character to the esp8266
       
        long int time = millis();
       
        while( (time+timeout) > millis())
        {
          while(esp8266.available())
          {
           
            // The esp has data so display its output to the serial window
            char c = esp8266.read(); // read the next character.
            response+=c;
           
          } 
         
       
        }
       
        if(debug)
        {
          Serial.print(response);
        }
       
        return response;
    }

PaulS

Quote
but working with delay can help me anyone?
Code: [Select]
if(esp8266.find("+IPD,"))
        {
         delay(1000); // wait for the serial buffer to fill up (read all the serial data)

That comment is nonsense. It is NOT how to read serial data properly. The sender needs to send some kind of end of packet marker. The receiver needs to read and store data, starting when the + arrives (do NOT use find()), stopping when the end of packet marker arrives. It may take a 100,000 passes through loop() for that to happen. But, so what? The number of iterations of loop() is irrelevant.

What is relevant is that there is NO delay().

alexgr2006

hello again i am noob in arduino can you fix my code?
when i use application from android the respone time is not good... i have to wait 2-3 sec to give the next command to work, and that is problem cause i want to control car with this...
Sorry for my english... if  u want something else tell me plz!

my application have 4 buttons and i use code on event button press(forward) and button release(stop).
on button press i send  (Http://192.168.4.1/?on) and on release i send (Http://192.168.4.1/?of)

i hope that help you!

PaulS

Quote
i hope that help you!
That doesn't help me at all. It is QUITE clear what your problem is. Get rid of the stupid delay() call.


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy