Xgk
Xgk

Reputation: 41

arduino and esp8266 interface

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.

Upvotes: 1

Views: 936

Answers (3)

Muhammad Aamir Mughal
Muhammad Aamir Mughal

Reputation: 71

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. :)

Upvotes: 0

MVSRI
MVSRI

Reputation: 142

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

Upvotes: 0

iohans
iohans

Reputation: 858

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).

Upvotes: 1

Related Questions