Skip to content

Instantly share code, notes, and snippets.

@benstr
Created October 5, 2017 09:16
Show Gist options
  • Save benstr/e43654239bf57a3f14d9068e12e82c76 to your computer and use it in GitHub Desktop.
Save benstr/e43654239bf57a3f14d9068e12e82c76 to your computer and use it in GitHub Desktop.
Sketch for using raw AT commands on a Seed Studio GPRS Shield with a Hologram.io SIM
/*
Note: this code is a demo for how to use a gprs shield to send
an http request to a test website (using the Hologram APN).
To communicate with the Arduino via terminal, set
the outgoing baud rate to 19200, and set line endings to
carriage return.
Then, in order to initiate the demo http request, enter
'h' into the terminal at the top of the serial monitor.
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8);
void setup()
{
mySerial.begin(19200); // the GPRS baud rate
Serial.begin(19200); // the GPRS baud rate
delay(500);
}
void loop()
{
// Input 'h' to run the test HTTP program
if (Serial.available())
switch(Serial.read())
{
case 'h':
SubmitHttpRequest();
break;
}
if (mySerial.available())
Serial.write(mySerial.read());
}
// SubmitHttpRequest()
//
// Note: the time of the delays are very important
void SubmitHttpRequest()
{
// Query signal strength of device
mySerial.println("AT+CSQ");
delay(100);
ShowSerialData();
// Check the status of Packet service attach. '0' implies device is not attached and '1' implies device is attached.
mySerial.println("AT+CGATT?");
delay(100);
ShowSerialData();
// Set the SAPBR, the connection type is using gprs
mySerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(1000);
ShowSerialData();
// Set the APN
mySerial.println("AT+SAPBR=3,1,\"APN\",\"hologram\"");
delay(4000);
ShowSerialData();
// Set the SAPBR, for detail you can refer to the AT command manual
mySerial.println("AT+SAPBR=1,1");
delay(2000);
ShowSerialData();
// Init the HTTP request
mySerial.println("AT+HTTPINIT");
delay(2000);
ShowSerialData();
// Set HTTP params, the second param is the website to request
mySerial.println("AT+HTTPPARA=\"URL\",\"hologram.io/test.html\"");
delay(1000);
ShowSerialData();
//Set the context ID
mySerial.println("AT+HTTPPARA=\"CID\",1");
delay(1000);
ShowSerialData();
// Submit the request
mySerial.println("AT+HTTPACTION=0");
// The delay is very important, the delay time is base on the
// return time from the website, if the return data is very
// large, the time required might be longer.
delay(10000);
ShowSerialData();
// Read the data from the accessed website
mySerial.println("AT+HTTPREAD");
delay(10000);
ShowSerialData();
// Close the HTTP connection and display the data
mySerial.println("AT+HTTPTERM");
delay(100);
}
// ShowSerialData()
// This is to show the data from gprs shield, to help
// see how the gprs shield submits an http request.
void ShowSerialData()
{
while(mySerial.available()!=0)
Serial.write(mySerial.read());
}
@ahmadhanis
Copy link

ahmadhanis commented Jan 25, 2019

Nice code...help a lot getting my sim900 GPRS http to work...I've been pulling my hair on how to send custom build url with get variable to send using
mySerial.println("AT+HTTPPARA=\"URL\",\"hologram.io/test.html\"");

this is my function
void sim900aSendHttp() { ATcomm = "AT+HTTPPARA=\"URL\",\"" ; ATcomm += "http://uumresearch.com/bustracking/update_location.php?busid=" ; ATcomm += String(busid); ATcomm += "&latitude="+String(gps.location.lat(),8); ATcomm += "&longitude="+String(gps.location.lng(),8); ATcomm += "\"" ; Serial.println("BUSID:"+busid+",LAT:"+String(gps.location.lat(),8)+"/LONG:"+String(gps.location.lat(),8); Serial.println(ATcomm); Serial.println(ATcomm); serialSIM800.println(ATcomm); delay(3000); serialSIM800.println("AT+HTTPACTION=0"); delay(8000); Serial.println(ATcomm); }

this is the url that I'm building
http://uumresearch.com/bustracking/update_location.php?busid=kdf8860&latitude=6.44505850&longitude=100.42456217

it seems that the at command only able to send until
http://uumresearch.com/bustracking/update_location.php?busid=kdf8860

i've tested using static url and it works..however when I concat all the variables that i need, it seems unable to perform the HTTPARA AT command

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment