GSM library for nucleo L152RE

Dependencies:   Adafruit_GSM

Dependents:   Full-Project

Committer:
ptcrews
Date:
Mon Dec 07 00:05:29 2015 +0000
Revision:
3:922cf54e97ec
Parent:
1:9e13d8a84808
Added comments.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ptcrews 1:9e13d8a84808 1 #include "GSM_Wrapper.h"
ptcrews 1:9e13d8a84808 2
ptcrews 3:922cf54e97ec 3 /* Function: changePowerState
ptcrews 3:922cf54e97ec 4 * --------------------------
ptcrews 3:922cf54e97ec 5 * Changes the power state by pulling the
ptcrews 3:922cf54e97ec 6 * key to ground for 2 seconds (write(0)). No
ptcrews 3:922cf54e97ec 7 * other way to turn GSM on/off.
ptcrews 3:922cf54e97ec 8 */
ptcrews 1:9e13d8a84808 9 void GSM_Sensor::changePowerState()
ptcrews 1:9e13d8a84808 10 {
ptcrews 1:9e13d8a84808 11 key.write(1);
ptcrews 1:9e13d8a84808 12 wait(2);
ptcrews 1:9e13d8a84808 13 key.write(0);
ptcrews 1:9e13d8a84808 14 wait(2);
ptcrews 1:9e13d8a84808 15 key.write(1);
ptcrews 1:9e13d8a84808 16 wait(2);
ptcrews 1:9e13d8a84808 17 }
ptcrews 1:9e13d8a84808 18
ptcrews 3:922cf54e97ec 19 /* Function: setup
ptcrews 3:922cf54e97ec 20 * ---------------
ptcrews 3:922cf54e97ec 21 * Has to be called each time the FONA
ptcrews 3:922cf54e97ec 22 * is turned on. Sets the baud rate. Attempts
ptcrews 3:922cf54e97ec 23 * to change the power state to turn it on if it
ptcrews 3:922cf54e97ec 24 * fails. Sets up the network settings, then
ptcrews 3:922cf54e97ec 25 * attempts to enable GPRS for ENABLE_GPRS_ATTEMPTS times.
ptcrews 3:922cf54e97ec 26 */
ptcrews 1:9e13d8a84808 27 void GSM_Sensor::setup()
ptcrews 1:9e13d8a84808 28 {
ptcrews 1:9e13d8a84808 29 printf("Starting FONA\n");
ptcrews 3:922cf54e97ec 30 if(!fona.begin(FONA_BAUD_RATE)){
ptcrews 1:9e13d8a84808 31 changePowerState();
ptcrews 1:9e13d8a84808 32 printf("Cannot find FONA\n");
ptcrews 1:9e13d8a84808 33 wait(2);
ptcrews 1:9e13d8a84808 34 }
ptcrews 3:922cf54e97ec 35 if(!fona.begin(FONA_BAUD_RATE)){
ptcrews 1:9e13d8a84808 36 changePowerState();
ptcrews 1:9e13d8a84808 37 return;
ptcrews 1:9e13d8a84808 38 }
ptcrews 1:9e13d8a84808 39 printf("After begin\n");
ptcrews 3:922cf54e97ec 40 fona.setGPRSNetworkSettings(NETWORK_APN, "", ""); // Note: May need to change other fields for different SIM cards
ptcrews 1:9e13d8a84808 41 printf("After set setting\n");
ptcrews 1:9e13d8a84808 42 bool enable = false;
ptcrews 1:9e13d8a84808 43 int i = 0;
ptcrews 1:9e13d8a84808 44 while(enable != true) {
ptcrews 3:922cf54e97ec 45 if(i > ENABLE_GPRS_ATTEMPTS) break;
ptcrews 1:9e13d8a84808 46 i++;
ptcrews 1:9e13d8a84808 47 fona.enableGPRS(true);
ptcrews 1:9e13d8a84808 48 fona.enableGPRS(false);
ptcrews 1:9e13d8a84808 49 enable = fona.enableGPRS(true);
ptcrews 1:9e13d8a84808 50 }
ptcrews 1:9e13d8a84808 51 printf("After enable\n");
ptcrews 1:9e13d8a84808 52 }
ptcrews 1:9e13d8a84808 53
ptcrews 3:922cf54e97ec 54 /* Function: sendOverHTTP
ptcrews 3:922cf54e97ec 55 * ----------------------
ptcrews 3:922cf54e97ec 56 * Sends the data over HTTP to the given URL.
ptcrews 3:922cf54e97ec 57 */
ptcrews 1:9e13d8a84808 58 bool GSM_Sensor::sendOverHTTP(char* url, uint8_t* data, int dlength)
ptcrews 1:9e13d8a84808 59 {
ptcrews 1:9e13d8a84808 60 uint16_t statuscode;
ptcrews 1:9e13d8a84808 61 int16_t length;
ptcrews 1:9e13d8a84808 62 if (!fona.HTTP_POST_start(url, "text/plain", data, dlength, &statuscode, (uint16_t *)&length)) {
ptcrews 1:9e13d8a84808 63 printf("Failed!\r\n");
ptcrews 1:9e13d8a84808 64 return false;
ptcrews 1:9e13d8a84808 65 }
ptcrews 1:9e13d8a84808 66 while (length > 0) {
ptcrews 1:9e13d8a84808 67 while (fona.readable()) {
ptcrews 1:9e13d8a84808 68 char c = fona.getc();
ptcrews 1:9e13d8a84808 69 putchar(c);
ptcrews 1:9e13d8a84808 70 length--;
ptcrews 1:9e13d8a84808 71 if (! length) break;
ptcrews 1:9e13d8a84808 72 }
ptcrews 1:9e13d8a84808 73 }
ptcrews 1:9e13d8a84808 74 printf("\r\n****\r\n");
ptcrews 1:9e13d8a84808 75 fona.HTTP_POST_end();
ptcrews 1:9e13d8a84808 76 return true;
ptcrews 1:9e13d8a84808 77 }
ptcrews 1:9e13d8a84808 78
ptcrews 3:922cf54e97ec 79 /* Function: send
ptcrews 3:922cf54e97ec 80 * --------------
ptcrews 3:922cf54e97ec 81 * Public wrapper function for sendOverHTTP.
ptcrews 3:922cf54e97ec 82 */
ptcrews 1:9e13d8a84808 83 bool GSM_Sensor::send(uint8_t* data, size_t size)
ptcrews 1:9e13d8a84808 84 {
ptcrews 1:9e13d8a84808 85 setup();
ptcrews 1:9e13d8a84808 86 printf("Setup GSM\n");
ptcrews 1:9e13d8a84808 87 bool res = sendOverHTTP(URL, data, size);
ptcrews 1:9e13d8a84808 88 if(!res) res = sendOverHTTP(URL, data, size);
ptcrews 1:9e13d8a84808 89 printf("After sent data\n");
ptcrews 1:9e13d8a84808 90 return res;
ptcrews 1:9e13d8a84808 91 }