1. I have to write SIM800 TCP/IP code using mega2560. Earlier I was trying to use seeeduino library on my locally purchased SIM800. It has three pins Rx, Tx & Gnd as output.
2. Somehow seeeduino was not working on this although I had disabled the pin status section & rx & tx were as mentioned in seeeduino library. I must be making some mistake. SO I left it & wrote own code.
3. Below is my code to initialize TCP/IP before sending data. I took reference from seeeduino library.
4. Checked the code on console giving correct response. IS below code ok? Anu correction required. Any command missong?
5. One problem I have currently is that while reading for response from SIM800, I wait for complete timeout period. Like if I have 5000 millisecond timeout then wait till 5 sec & read as much data is available. How can I shift the code such that as soon as correct response comes, code should break.
void sim800_clean_buffer(char *buffer, uint8_t count);
void sim800_read_buffer(char *buf, uint8_t count , uint16_t timeout);
int8_t sim800_send_cmd_and_wait_for_response(const char* cmd, const char *resp, unsigned int timeout);
int8_t sim800_wait_for_resp(const char *resp, unsigned int timeout);
void setup()
{
/* init serial port for msg */
Serial.begin(9600);
/* init serial port for sending data to SIM800 */
Serial1.begin(9600);
}
void loop()
{
char ipAddr[33];
int8_t idx;
uint8_t init_again;
/* Start connection */
Serial.println("GPRS - TCP Connection Test...");
/* basis command must be cleared */
while(0 != sim800_send_cmd_and_wait_for_response("AT\r\n" , "OK" , 5000U));
while(0 != sim800_send_cmd_and_wait_for_response("ATE1\r\n" , "OK" , 5000U));
/* full functioanlity */
while(0 != sim800_send_cmd_and_wait_for_response("AT+CFUN=1\r\n" , "OK" , 5000U));
/* SIM status */
while(0 != sim800_send_cmd_and_wait_for_response("AT+CPIN?\r\n" , "+CPIN: READY" , 5000U));
/* GPRS service status */
while(0 != sim800_send_cmd_and_wait_for_response("AT+CGATT?\r\n" , "+CGATT: 1" , 5000U));
/* check if already registerd */
sim800_clean_buffer(ipAddr , 33); /* fill array with null */
Serial1.write("AT+CIFSR\r\n"); /* get local IP address assigned */
sim800_read_buffer(ipAddr , 32 , 5000); /* read the loack IP in a aray */
ipAddr[32] = '\0'; /* make sure array is null termianted since next it will be converted into string */
String str(ipAddr); /* convert array into string */
Serial.println(str);
Serial.println(str.length());
idx = str.indexOf("ERROR"); /* if ERROR is received then if have to register SIM800 again otherwise it is already registerd */
if(idx >= 0)
{
init_again = 1U;
}
else
{
init_again = 0U;
}
if(1U == init_again)
{
/* Join gprs */
while(0 != sim800_send_cmd_and_wait_for_response("AT+CSTT=\"airtelgprs.com\",\"\",\"\"\r\n" , "OK" , 5000U));
while(0 != sim800_send_cmd_and_wait_for_response("AT+CIICR\r\n" , "OK" , 5000U));
while(0 != sim800_send_cmd_and_wait_for_response("AT+CIFSR\r\n" , "." , 5000U)); /* "." for since IP address will have "." */
}
delay(1000);
}
void sim800_clean_buffer(char *buffer, uint8_t count)
{
for(uint8_t i = 0; i < count; i++)
{
buffer[i] = '\0';
}
}
void sim800_read_buffer(char *buf, uint8_t count , uint16_t timeout)
{
unsigned long timerStart;
unsigned long timerEnd;
uint8_t i = 0U;
timerStart = millis();
while(1)
{
if(Serial1.available())
{
if(i < count)
{
buf[i++] = Serial1.read();
}
else
{
break;
}
}
timerEnd = millis();
if((timerEnd - timerStart) > timeout)
{
break;
}
}
/* flush data */
while(Serial1.available())
{
(void)Serial1.read();
}
}
int8_t sim800_send_cmd_and_wait_for_response(const char* cmd, const char *resp, unsigned int timeout)
{
Serial1.write(cmd);
return sim800_wait_for_resp(resp , timeout);
}
int8_t sim800_wait_for_resp(const char *resp, unsigned int timeout)
{
unsigned long timerStart;
unsigned long timerEnd;
char rx_str[51];
uint8_t cnt;
int8_t idx;
for(cnt = 0U ; cnt < 51U ; cnt++)
{
rx_str[cnt] = '\0';
}
cnt = 0U;
timerStart = millis();
while(1)
{
if(Serial1.available())
{
if(cnt < 50U)
{
rx_str[cnt] = Serial1.read();
cnt++;
}
}
timerEnd = millis();
if((timerEnd - timerStart) > timeout)
{
break;
}
}
rx_str[50] = '\0';
/* flush data */
while(Serial1.available())
{
(void)Serial1.read();
}
String str(rx_str);
Serial.println(str);
Serial.println(str.length());
idx = str.indexOf(resp);
if(idx >= 0)
{
return 0;
}
else
{
return -1;
}
}