Hi Guys,
I was given a task to get data from a web API in Arduino, I struggled a lot as there is no specific documentation on it. Finally I achieved it, below is the code for the same.
#include <SoftwareSerial.h>
const byte rxPin = 2; // Wire this to Tx Pin of SIM900
const byte txPin = 3; // Wire this to Rx Pin of SIM900
SoftwareSerial SIM900(rxPin, txPin);
float temp;
int tempPin = 0;
void setup()
{
SIM900.begin(9600);
Serial.begin(115200);
delay(1000);
SIM900.println("AT+CIPSHUT");
delay(1000);
printSerialData();
SIM900.println("AT+CIPMUX=0") ;
delay(2000);
printSerialData();
SIM900.println("AT+CGATT=1");
delay(1000);
printSerialData();
//here my sim is of Airtel hence i have mentioned airtelgprs.com
SIM900.println("AT+CSTT=\"air telgprs.com\",\"\",\"\"");// setting the APN,2nd parameter empty works for all networks
delay(5000);
printSerialData();
SIM900.println();
SIM900.println("AT+CIICR");
delay(6000);
printSerialData();
SIM900.println("AT+CIFSR"); //init the HTTP request
delay(2000);
printSerialData();
delay(2000);
printSerialData();
//String cmd="GET /test.php HTTP/1.1\r\nHost: test.in\r\nUser-Agent: SIM900_HTTP_CLIENT\r\nCache- Control: no-cache\r\nConnection: keep-alive\r\n\r\n";
String cmd="GET /title/ipsum/content/blah HTTP/1.1\r\nHost: echo.jsontest.com\r\nUser- Agent: SIM900_HTTP_CLIENT\r\nCache- Control: no-cache\r\nConnection: keep-alive\r\n\r\n";
delay(6000);
SIM900.println("AT+CIPSEND="+ String(cmd.length()));
if(SIM900.find(">")){
Serial.println("Sending Request..");
SIM900.print(cmd);
String c;
while(SIM900.available()>0){
c=SIM900.readString();
Serial.println(c);
};
delay(10000);
while(SIM900.available()>0){
c=SIM900.readString();
Serial.println(c);
}
delay(10000);
while(SIM900.available()>0){
c=SIM900.readString();
Serial.println(c);
}
}
delay(1000);
SIM900.write(0x1A);
delay(2000);
printSerialData();
//sendtemp();
SIM900.println("AT+CIPCLOSE");
printSerialData();
SIM900.println("AT+CIPSHUT");
delay(1000);
printSerialData();
}
void loop()
{
}
void printSerialData()
{
while(SIM900.available()!=0)
Serial.write(SIM900.read());
}
void sendtemp()
{
temp = analogRead(tempPin);
temp = temp * 0.48828125;
Serial.print("TEMPERATURE = ");
Serial.print(temp);
Serial.print("*C");
Serial.println();
delay(5000);
SIM900.println(temp);
delay(3000);
printSerialData();
delay(3000);
printSerialData();
}
Thanks
Ritesh
No comments:
Post a Comment
Comments are welcome, Please join me on my Linked In account
http://in.linkedin.com/pub/ritesh-tandon/21/644/33b