1

I want to send some AT commands to esp8266 using arduino and get the reply from serial monitor. this is the code:(the purpose of this code is to update a thingspeak channel)

 #include<SoftwareSerial.h>
SoftwareSerial esp8266(3,2);
#define ID "user"
#define PASS "pass"
String apiKey = "apikey";
void setup() {
  Serial.setTimeout(5000);
  Serial.begin(9600);
  esp8266.begin(9600);
 // delay(1000);
     String command6="AT+RST";

  esp8266.println(command6);
  if(esp8266.available())
  {
    while(esp8266.available())
    {
      char c=esp8266.read();
      Serial.write(c);          
    }
  }
  delay(2000);
}

void loop() {
 delay(2000);
  String command="\nAT";

  esp8266.println(command);
  if(esp8266.available())
  {
    while(esp8266.available())
    {
      char c=esp8266.read();
      Serial.write(c);          
    }
  } 

   String cmd = "\nAT+CIPSTART=\"TCP\",\"";
  cmd += "144.212.80.11"; // api.thingspeak.com
  cmd += "\",80";

  esp8266.println(cmd);
  if(esp8266.available())
  {
    while(esp8266.available())
    {
      char c=esp8266.read();
      Serial.write(c);          
    }
  }
   delay(3000);
  String command3="\nAT+CIPSEND=200";

  esp8266.println(command3);
  if(esp8266.available())
  {
    while(esp8266.available())
    {
      char c=esp8266.read();
      Serial.write(c);          
    }
  }
    delay(1000);

    String getStr = "GET /update?api_key=";
    getStr += apiKey;
    getStr += "&field1=10";

  esp8266.println(getStr);
  esp8266.println("\r\r\r\r\r\r\r\r");
  if(esp8266.available())
  {
    while(esp8266.available())
    {
      char c=esp8266.read();
      Serial.write(c);          
    }
  }
  delay(15000);

}

user and pass are my wifi username and password. the problem is, the esp8266 responds "ok" to at commands but when it gets to the last parts, it gives me this:

A))-R¤%%JHÕ¨TUPZ="TCP","144.212.80.11",80
CONNECT
OK
ERROR
AT+CIPSEND=200
OK
> GET /update?api_key=apikey&field1=10
CAT
AT+CIPSTART="TCP","144.212.80.11",80
AT+CIPSEND=200
GET /update?api_key=apikey&field1=10
AT
AT+CIPSTART="TCP","144.212.80.11",80
busy s...

i have put a few delays inside the code but after it inserts the GET it gets back to the loop runs the program again with no delays and then esp8266 resets itself.

6
  • 2
    Can't help on this occasion, but I'd like to advise you not to post your private API Keys on a public site. Commented Feb 19, 2016 at 9:07
  • thanks :D I think I kinda found why its not working, I'm storing too much in the SRAM, cause strings take too much space, but i don't know how to solve it. Commented Feb 19, 2016 at 19:33
  • You can't have a look at storing data in EEPROM, see PROGMEM. Another halfway option is to add an SDcard/microSD card reader to your arduino. From a pragmatic point of view, if you can easily get your hands on a board with more memory (e.g. Arduino Mega), go for it and save time (throwing money at the problem). Commented Feb 20, 2016 at 9:48
  • yes i tested progmem but seems like its only for constant strings, the strings in my code are gonna have some variables like room temprature etc.. Commented Feb 22, 2016 at 5:44
  • upgrade to NodeMCU lua in esp. ESP has plenty of RAM and a huge flash (in comparison to arduino) Commented Feb 22, 2016 at 14:18

3 Answers 3

1

Besides waiting for the OK, you also need to make sure that you are using the right IP address for ThingSpeak. The offical static IP for ThingSpeak is 184.106.153.149 found here (http://www.mathworks.com/help/thingspeak/channel-settings.html#endpoints).

Sign up to request clarification or add additional context in comments.

Comments

0

Try using /n after the At command not before and also check the correct format for AT+CIPSEND

GET http://api.thingspeak.com/update?api_key=KTQXXXXXXXXXXXXX&field1=10 HTTP/1.0 \r\n\r\n

try this format

Comments

0

There are a few things to keep in mind when working with ESP8266 communicate over a network.

  • 1 The response might not be received in a constant time i.e 100ms or 1ms etc. there will always be random delay.
  • 2 Check if the ESP is not running out of current while making a GET/POST request.
  • 3 Check for every character/escape sequence ('\r' '\n' etc.) and place them into right place into your "Request" string.

This might help you: Arduino ESP8266 AT GET Request

Thank you. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.