While using my GPRS shield (Seeed Studio v1.4), Any idea why I am getting "Last-Modified: Mon, 1" c" catMtame." garbled text? There are 100 char fail-safes in place...
Any light you can shed on this would be very much appreciated, thanks in advance.
AT+CIPSEND=62
GET / HTTP/1.1
Host: www.microsoft.com
Connection: close
SEND OK
HTTP/1.1 200 OK
Cache-Control: no-cache
Last-Modified: Mon, 1" c" catMtame.
CLOSED
Code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8);
char response[100];
void setup() {
Serial.begin(115200); // HUMAN COM
mySerial.begin(19200); // SOFTWARE COM
power_on();
while( sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 1000)== 0 );
}
void loop() {
if (Serial.available()) {
switch(Serial.read()) {
case 'h':
SubmitHttpRequest();
break;
case 'i':
// Closes the socket
sendATcommand2("AT+CIPCLOSE", "CLOSE OK", "ERROR", 10000);
sendATcommand2("AT+CIPSHUT", "OK", "ERROR", 10000);
break;
}
}
}
void SubmitHttpRequest() {
char aux_str[50];
char ip_data[]="GET / HTTP/1.1\r\nHost: a.me\r\nConnection: close\r\n\r\n"; //62 chars
Serial.println("-------------------------------------------------------------------");
// Sets the APN, user name and password
if (sendATcommand2("AT+CSTT=\"epc.tmobile.com\",\"\",\"\"", "OK", "ERROR", 30000) == 1) {
Serial.println("-------------------------------------------------------------------");
// Brings Up Wireless Connection
if (sendATcommand2("AT+CIICR", "OK", "ERROR", 30000) == 1) {
Serial.println("-------------------------------------------------------------------");
// Gets Local IP Address
if (sendATcommand2("AT+CIFSR", ".", "ERROR", 10000) == 1) {
Serial.println("-------------------------------------------------------------------");
// Opens a TCP socket
if (sendATcommand2("AT+CIPSTART=\"TCP\",\"a.me\",\"80\"", "CONNECT OK", "CONNECT FAIL", 30000) == 1) {
Serial.println("-------------------------------------------------------------------");
// Sends data size
sprintf(aux_str,"AT+CIPSEND=%d", strlen(ip_data)); //62
if (sendATcommand2(aux_str, ">", "ERROR", 10000) == 1) {
Serial.println("-------------------------------------------------------------------");
// Sends data
GetData(ip_data, 10000);
Serial.println("-------------------------------------------------------------------");
}
} else {
Serial.println("Error openning the connection");
}
} else {
Serial.println("Error getting the IP address");
}
} else {
Serial.println("Error bring up wireless connection");
}
} else {
Serial.println("Error setting the APN");
}
}
void power_on() {
if (sendATcommand2("AT", "OK", "OK", 2000) == 0) {
Serial.println("Powering up...");
pinMode(9, OUTPUT);
digitalWrite(9,LOW);
delay(1000);
digitalWrite(9,HIGH);
delay(2000);
digitalWrite(9,LOW);
delay(3000);
while(sendATcommand2("AT", "OK", "OK", 2000) == 0);
}
}
int8_t sendATcommand2(char* ATcommand, char* expected_answer1, char* expected_answer2, unsigned int timeout){
uint8_t answer=0;
char response[100];
unsigned long previous;
memset(response, NULL, 100);
mySerial.println(ATcommand);
int chrnum = 0;
previous = millis();
do {
if(mySerial.available() != 0){
response[chrnum] = mySerial.read();
chrnum++;
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
else if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
}
while((answer == 0) && ((millis() - previous) < timeout));
Serial.println(response);
return answer;
}
int8_t GetData(char* ATcommand, unsigned int timeout){
unsigned long previous;
memset(response, NULL, 100);
mySerial.println(ATcommand);
int chrnum = 0;
previous = millis();
do {
if (mySerial.available() > 0) {
response[chrnum++] = mySerial.read();
if (chrnum > 98) {
Serial.println(response);
memset(response, NULL, 100);
chrnum=0;
}
}
} while(((millis() - previous) < timeout));
Serial.println(response);
}