SIM800 TCP/IP init code using Arduino Mega

Discussion in 'Embedded Systems and Microcontrollers' started by Vindhyachal Takniki, Oct 12, 2016.

  1. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    348
    6
    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.


    Code (Text):
    1. void sim800_clean_buffer(char *buffer, uint8_t count);
    2. void sim800_read_buffer(char *buf, uint8_t count , uint16_t timeout);
    3. int8_t sim800_send_cmd_and_wait_for_response(const char* cmd, const char *resp, unsigned int timeout);
    4. int8_t sim800_wait_for_resp(const char *resp, unsigned int timeout);
    5.  
    6.  
    7. void setup()
    8. {
    9. /* init serial port for msg */
    10.     Serial.begin(9600);
    11.  
    12. /* init serial port for sending data to SIM800 */  
    13.     Serial1.begin(9600);
    14. }
    15.  
    16. void loop()
    17. {
    18.     char ipAddr[33];
    19.     int8_t idx;
    20.     uint8_t init_again;
    21.  
    22. /* Start connection */
    23.     Serial.println("GPRS - TCP Connection Test...");
    24.  
    25.    
    26. /* basis command must be cleared */  
    27.     while(0 != sim800_send_cmd_and_wait_for_response("AT\r\n" , "OK" , 5000U));  
    28.     while(0 != sim800_send_cmd_and_wait_for_response("ATE1\r\n" , "OK" , 5000U));
    29.  
    30. /* full functioanlity */    
    31.     while(0 != sim800_send_cmd_and_wait_for_response("AT+CFUN=1\r\n" , "OK" , 5000U));  
    32.  
    33. /* SIM status */
    34.     while(0 != sim800_send_cmd_and_wait_for_response("AT+CPIN?\r\n" , "+CPIN: READY" , 5000U));
    35.  
    36. /* GPRS service status */
    37.     while(0 != sim800_send_cmd_and_wait_for_response("AT+CGATT?\r\n" , "+CGATT: 1" , 5000U));  
    38.  
    39. /* check if already registerd */
    40.     sim800_clean_buffer(ipAddr , 33);        /* fill array with null */
    41.     Serial1.write("AT+CIFSR\r\n");           /* get local IP address assigned */
    42.     sim800_read_buffer(ipAddr , 32 , 5000);  /* read the loack IP in a aray */
    43.     ipAddr[32] = '\0';                       /* make sure array is null termianted since next it will be converted into string */
    44.  
    45.     String str(ipAddr);                      /* convert array into string */
    46.     Serial.println(str);                    
    47.     Serial.println(str.length());
    48.     idx = str.indexOf("ERROR");              /* if ERROR is received then if have to register SIM800 again otherwise it is already registerd */
    49.     if(idx >= 0)
    50.     {
    51.         init_again = 1U;
    52.     }
    53.     else
    54.     {
    55.         init_again = 0U;
    56.     }
    57.  
    58.     if(1U == init_again)
    59.     {
    60.     /* Join gprs */
    61.         while(0 != sim800_send_cmd_and_wait_for_response("AT+CSTT=\"airtelgprs.com\",\"\",\"\"\r\n" , "OK" , 5000U));
    62.         while(0 != sim800_send_cmd_and_wait_for_response("AT+CIICR\r\n" , "OK" , 5000U));
    63.         while(0 != sim800_send_cmd_and_wait_for_response("AT+CIFSR\r\n" , "." , 5000U));    /* "." for since IP address will have "." */
    64.     }
    65.  
    66.     delay(1000);
    67.    
    68. }
    69.  
    70.  
    71. void sim800_clean_buffer(char *buffer, uint8_t count)
    72. {
    73.     for(uint8_t i = 0; i < count; i++)
    74.     {
    75.         buffer[i] = '\0';
    76.     }
    77. }
    78.  
    79.  
    80. void sim800_read_buffer(char *buf, uint8_t count , uint16_t timeout)
    81. {
    82.     unsigned long timerStart;
    83.     unsigned long timerEnd;
    84.     uint8_t i = 0U;
    85.  
    86.  
    87.     timerStart = millis();
    88.     while(1)
    89.     {
    90.         if(Serial1.available())
    91.         {
    92.             if(i < count)
    93.             {
    94.                 buf[i++] = Serial1.read();
    95.             }
    96.             else
    97.             {
    98.                 break;
    99.             }
    100.         }
    101.      
    102.         timerEnd = millis();
    103.         if((timerEnd - timerStart) > timeout)
    104.         {
    105.             break;
    106.         }
    107.     }
    108.  
    109. /* flush data */
    110.     while(Serial1.available())
    111.     {
    112.         (void)Serial1.read();
    113.     }
    114. }
    115.  
    116.  
    117. int8_t sim800_send_cmd_and_wait_for_response(const char* cmd, const char *resp, unsigned int timeout)
    118. {
    119.     Serial1.write(cmd);
    120.     return sim800_wait_for_resp(resp , timeout);
    121. }
    122.  
    123.  
    124.  
    125. int8_t sim800_wait_for_resp(const char *resp, unsigned int timeout)
    126. {
    127.     unsigned long timerStart;
    128.     unsigned long timerEnd;
    129.     char rx_str[51];
    130.     uint8_t cnt;
    131.     int8_t idx;
    132.    
    133.     for(cnt = 0U ; cnt < 51U ; cnt++)
    134.     {
    135.         rx_str[cnt] = '\0';
    136.     }
    137.     cnt = 0U;
    138.    
    139.     timerStart = millis();
    140.     while(1)
    141.     {
    142.         if(Serial1.available())
    143.         {
    144.             if(cnt < 50U)
    145.             {
    146.                 rx_str[cnt] = Serial1.read();
    147.                 cnt++;
    148.             }
    149.         }
    150.      
    151.         timerEnd = millis();
    152.         if((timerEnd - timerStart) > timeout)
    153.         {
    154.             break;
    155.         }
    156.     }
    157.     rx_str[50] = '\0';
    158.  
    159. /* flush data */
    160.     while(Serial1.available())
    161.     {
    162.         (void)Serial1.read();
    163.     }
    164.  
    165.     String str(rx_str);
    166.     Serial.println(str);
    167.     Serial.println(str.length());
    168.     idx = str.indexOf(resp);
    169.     if(idx >= 0)
    170.     {  
    171.         return 0;
    172.     }
    173.     else
    174.     {
    175.         return -1;
    176.     }
    177. }
    178.  
    179.  
    180.  
    181.  
     
Loading...