Flotsam / GSM

Dependencies:   Adafruit_GSM

Dependents:   Full-Project

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GSM_Wrapper.cpp Source File

GSM_Wrapper.cpp

00001 #include "GSM_Wrapper.h"
00002 
00003 /* Function: changePowerState
00004  * --------------------------
00005  * Changes the power state by pulling the
00006  * key to ground for 2 seconds (write(0)). No
00007  * other way to turn GSM on/off.
00008  */
00009 void GSM_Sensor::changePowerState()
00010 {
00011     key.write(1);
00012     wait(2);
00013     key.write(0);
00014     wait(2);
00015     key.write(1);
00016     wait(2);
00017 }
00018 
00019 /* Function: setup
00020  * ---------------
00021  * Has to be called each time the FONA
00022  * is turned on. Sets the baud rate. Attempts
00023  * to change the power state to turn it on if it
00024  * fails. Sets up the network settings, then
00025  * attempts to enable GPRS for ENABLE_GPRS_ATTEMPTS times.
00026  */
00027 void GSM_Sensor::setup()
00028 {
00029     printf("Starting FONA\n");
00030     if(!fona.begin(FONA_BAUD_RATE)){
00031         changePowerState();
00032         printf("Cannot find FONA\n");
00033         wait(2);
00034     }
00035     if(!fona.begin(FONA_BAUD_RATE)){
00036         changePowerState(); 
00037         return;
00038     }
00039     printf("After begin\n");
00040     fona.setGPRSNetworkSettings(NETWORK_APN, "", ""); // Note: May need to change other fields for different SIM cards
00041     printf("After set setting\n");
00042     bool enable = false;
00043     int i = 0;
00044     while(enable != true) {
00045         if(i > ENABLE_GPRS_ATTEMPTS) break;
00046         i++;
00047         fona.enableGPRS(true);
00048         fona.enableGPRS(false);
00049         enable = fona.enableGPRS(true);
00050     }
00051     printf("After enable\n");
00052 }
00053 
00054 /* Function: sendOverHTTP
00055  * ----------------------
00056  * Sends the data over HTTP to the given URL.
00057  */
00058 bool GSM_Sensor::sendOverHTTP(char* url, uint8_t* data, int dlength)
00059 {
00060     uint16_t statuscode;
00061     int16_t length;
00062     if (!fona.HTTP_POST_start(url, "text/plain", data, dlength, &statuscode, (uint16_t *)&length)) {
00063         printf("Failed!\r\n");
00064         return false;
00065     }
00066     while (length > 0) {
00067         while (fona.readable()) {
00068             char c = fona.getc();
00069             putchar(c);
00070             length--;
00071             if (! length) break;
00072         }
00073     }
00074     printf("\r\n****\r\n");
00075     fona.HTTP_POST_end();
00076     return true;
00077 }
00078 
00079 /* Function: send
00080  * --------------
00081  * Public wrapper function for sendOverHTTP.
00082  */
00083 bool GSM_Sensor::send(uint8_t* data, size_t size)
00084 {
00085     setup();
00086     printf("Setup GSM\n");
00087     bool res = sendOverHTTP(URL, data, size);
00088     if(!res) res = sendOverHTTP(URL, data, size);
00089     printf("After sent data\n");   
00090     return res;
00091 }