mlu: these extra spaces were not in the actual code,
here is the full test code:
it is based on the cooking hacks code:
http://www.cooking-hacks.com/index....orials/arduino-gprs-gsm-quadband-sim900#step8
#include <Time.h>
#include <Streaming.h>
time_t t;
int8_t answer;
int onModulePin= 9;
HardwareSerial Uart = HardwareSerial();
char data[512];
int data_size;
char aux_str[100];
char aux;
int x = 0;
void setup(){
Uart.begin(19200);
pinMode(onModulePin, OUTPUT);
Serial.begin(19200);
Serial.println("Starting...");
power_on();
delay(3000);
sendATcommand("AT+IPR=19200","ok",2000);
while (sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 2000) == 0);
// answer = sendATcommand("AT+IPR=?", "OK", 10000);
sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"APN\",\"internet\"", "OK", 2000);
// sendATcommand("AT+SAPBR=3,1,\"USER\",\"\"", "OK", 2000);
// sendATcommand("AT+SAPBR=3,1,\"PWD\",\"\"", "OK", 2000);
answer= sendATcommand("AT+SAPBR=2,1", "+SAPBR: 1,1", 2000);
if(answer!=1)
while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) == 0)
{
delay(5000);
}
}
void loop(){
// Initializes HTTP service
answer = sendATcommand("AT+HTTPINIT", "OK", 10000);
if (answer == 1)
{
// Sets CID parameter
answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000);
if (answer == 1)
{
// Sets url
answer = sendData();
if (answer == 1)
{
// Starts GET action
answer = sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:0,200", 10000);
if (answer == 1)
{
x=0;
sprintf(aux_str, "AT+HTTPREAD=%d,100", x);
if (sendATcommand2(aux_str, "+HTTPREAD:", "ERROR", 30000) == 1)
{
data_size = 0;
while(Uart.available()==0);
aux = Uart.read();
do{
data_size *= 10;
data_size += (aux-0x30);
while(Uart.available()==0);
aux = Uart.read();
Serial.print(aux);
}
while(aux != 0x0D);
if (data_size > 0)
{
while(Uart.available() <= data_size);
Uart.read();
for (int y = 0; y < data_size; y++)
{
data[x] = Uart.read();
Serial.print( data[x]);
x++;
}
data[x] = '\0';
}
else
{
Serial.println("Download finished");
}
}
else if (answer == 2)
{
Serial.println("Error from HTTP");
}
else
{
Serial.println("Error getting the url");
data_size = 0;
}
Serial.print("Data received: ");
Serial.println(data);
}
else
{
Serial.println("Error getting the url");
}
}
else
{
Serial.println("Error setting the url");
}
}
else
{
Serial.println("Error setting the CID");
}
}
else
{
Serial.println("Error initializating");
}
sendATcommand("AT+HTTPTERM", "OK", 5000);
}
void power_on(){
uint8_t answer=0;
// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
// waits for an answer from the module
while(answer == 0){
// Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){
int i;
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while( Uart.available() > 0) Uart.read(); // Clean the input buffer
Uart.println(ATcommand);
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Uart.available() != 0){
response[x] = Uart.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));
Serial.println(response);
return answer;
}
int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
char* expected_answer2, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while( Uart.available() > 0) Uart.read(); // Clean the input buffer
Uart.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Uart.available() != 0){
response[x] = Uart.read();
x++;
// check if the desired answer 1 is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
// check if the desired answer 2 is in the response of the module
if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
// Waits for the asnwer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));
Serial.println(response);
return answer;
}
int8_t getClock(){
int second, minute, hour, dayOfWeek, dayOfMonth, month, year;
int timeout=1000;
uint8_t x=0,y=0, answer=0;
char response[100];
unsigned long previous;
answer=0;
memset(response, '\0', 100); // Initialize the string
delay(100);
while( Uart.available() > 0) Uart.read(); // Clean the input buffer
Uart.println("AT+CCLK?"); // Send the AT command
y=0;
x = 0;
previous = millis();
// this loop waits for the answer
x=0;
do{
if(Uart.available() != 0){
response[x] = Uart.read();
x++;
// check if the desired answer is in the response of the module
if(x >37)answer=1;
}
// Waits for the asnwer with time out
}
while((answer== 0) && ((millis() - previous) < timeout));
year=(response[20]-48)*10+(response[21]-48);
month=(response[23]-48)*10+(response[24]-48);
dayOfMonth=(response[26]-48)*10+(response[27]-48);
hour=(response[29]-48)*10+(response[30]-48);
minute=(response[32]-48)*10+(response[33]-48);
second=(response[35]-48)*10+(response[36]-48);
setTime(hour,minute,second, dayOfMonth,month,year);
Serial.println(response);
return answer;
}
int8_t sendData(){
char* expected_answer1="OK";
unsigned int timeout=5000;
int i;
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while( Uart.available() > 0) Uart.read(); // Clean the input buffer
Uart.println("AT+HTTPPARA=\"URL\",\"http://212.1.208.94/system/ping.php?id=1&data=1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;0;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;0;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;0;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;0;0;1;2;0;0;0;0;1;5;5;-1461633991;0;0;0;0;0;1;1;1970;\"");
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Uart.available() != 0){
response[x] = Uart.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));
Serial.println(response);
return answer;
}