Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By upesh.jindal
#56706 Alright. So I went ahead and thought of experimenting a little bit and moved the code to send commands to loop() using an array of commands. What I noticed was that subsequent commands went fine but then the response is randomly junk. Sometimes the response is OK and sometimes it's junk. Following is my updated code.

Code: Select all
#include "Arduino.h"
#include "SoftwareSerial.h"

int rxPin = 2;
int txPin = 3;

String commands[] = {"AT\r\n", "AT\r\n", "AT+GMR\r\n", "AT+CWLAP\r\n"
                    , "AT+CIPSTART=\"TCP\",\"www.thingspeak.com\",80\r\n"
                    , "AT+CIPCLOSE\r\n"};
int command_number = 0;

SoftwareSerial soft (rxPin, txPin);

void sendCommand (String cmd, int del);

void setup ()
{
  pinMode (rxPin, INPUT);
  pinMode (txPin, OUTPUT);
 
  Serial.begin (115200);

  Serial.println ("Starting up...");

  soft.begin (115200);

  while(!Serial) {;}

  while (!soft) {;}
}

void loop ()
{
    sendCommand (commands[command_number++],command_number*1000);

    if (command_number == 6)
      command_number = 0;
    delay(200);
}

void sendCommand (String cmd, int del)
{
  char data;
  soft.print (cmd);

   delay (del);

   while (!soft.available()) {;}

   Serial.println (soft.available());

   while (soft.available())
   {
      data = soft.read();
      Serial.print (data);
      delay(10);
   }
}


And here is the response from serial monitor. If you notice the response sometimes is incomplete and sometimes it's junk. So it's really unreliable. On the other hand, when I communicate using Arduino RX/TX and some serial term app then the response is perfectly fine with same baud rate. Only difference is that I move the connection from pin 2 and 3 to RX and TX.

Starting up...
11
AZCH�H�j�H�11
AT


OK
63
AT+GMR

AT vdrsion:0.40.0.0(Aug 8 2015 14:45:58)
SDK vershon63
AT+CWLAP

+CWLAP:(4,"ATT7YXn7J6",-90,"f8:18:97:`c:2b:16",1,76)59
AT+CIPSTART="TCP","www.thiogspeak.com",80

CONNECT

OK
28
AT+CIPCLOSE

CLOSED

OK
10
AT


OK
11
AT


O�C�63
AT+GMR

AT vession:0.40.0.0(Aug 8 2015 14:45:58(
SDK version63
AT+CWL@P

+CWLAP:(3,"DHRECT-A0-HP FNVY 4520 sdries",-89,"dc:4a
User avatar
By upesh.jindal
#56733 Solved it finally!

1. Baud rate on ESP8266 12-F is 115200 by default. I brought it down to 4800.
2. Buffer size on ESP8266 is 256 but on SoftwareSerial it is 64 bytes so most of the times buffer overflows so either we get junk or missing data. I increased the buffer to 256 bytes in SoftwareSerial.h
3. 4800 is the minimum baud rate at which ESP8266 12-F communicates. I tried lowering it down but then it spews junk. I had to flash it to make it respond.

Having said that: having 256 buffer is still not enough because some commands or data from some web server may surpass 256 bytes. So a better solution is needed.

Until then... I'm happy with the experiments!