#include <Arduino.h> //Create software serial object to communicate with SIM900
#include <SoftwareSerial.h>
/* Create object named SIM900 of the class SoftwareSerial */
SoftwareSerial SIM900(6, 7);
int sleep = 500;
int msg = 0;
void showSerialData()
{
Serial.println("Show serial data:");
while (SIM900.available())
{
char c = SIM900.read();
Serial.write(c);
}
Serial.println("");
}
void sendCommand(String command, int sl)
{
SIM900.println(command);
delay(sl);
showSerialData();
}
void initGPRS(String command)
{
SIM900.println("AT+SAPBR=0,1"); /* Close GPRS context */
sendCommand("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"", sleep); /* Connection type GPRS */
sendCommand("AT+SAPBR=3,1,\"APN\",\"internet.beeline.ru\"", sleep); /* APN of the provider */
sendCommand("AT+SAPBR=1,1", 2000); /* Open GPRS context */
sendCommand("AT+SAPBR=2,1", sleep); /* Query the GPRS context */
sendCommand("AT+HTTPINIT", sleep); /* Initialize HTTP service */
sendCommand("AT+HTTPPARA=\"CID\",1", sleep); /* Set parameters for HTTP session */
sendCommand(command, sleep); /* Set parameters for HTTP session */
sendCommand("AT+HTTPACTION=0", 8000); /* Start GET session */
msg = 2;
}
void checkRequest(String command)
{
SIM900.println("AT+HTTPREAD");
delay(10000);
while (SIM900.available())
{
char result = SIM900.read();
if (!result)
{
msg = 1;
return;
}
Serial.write(result);
}
msg = 3;
Serial.println("");
}
void endSessionGPRS()
{
sendCommand("AT+HTTPTERM", sleep); // Terminate the HTTP service
sendCommand("AT+CIPSHUT", sleep); // close or turn off network connection
sendCommand("AT+SAPBR=0,1", sleep); /* Close GPRS context */
msg = 0;
}
void toMsg()
{
int counter = 0;
msg = 1;
String url = "";
String command = "AT+HTTPPARA=\"URL\",\"";
url += "v-php.ru/ard?ard=";
url += counter;
url += "\"";
command += url;
Serial.println("BEGIN...");
Serial.println(url);
while (msg >= 1 && msg <= 3 && counter < 20)
{
if (msg == 1)
initGPRS(command);
if (msg == 2)
checkRequest(command);
if (msg == 3)
endSessionGPRS();
counter ++;
}
}
void setup()
{
SIM900.begin(9600); /* Define baud rate for software serial communication */
Serial.begin(9600); /* Define baud rate for serial communication */
toMsg();
}
void loop()
{
}