GSM library for nucleo L152RE
Dependencies: Adafruit_GSM
GSM_Wrapper.cpp@1:9e13d8a84808, 2015-12-05 (annotated)
- Committer:
- ptcrews
- Date:
- Sat Dec 05 07:29:49 2015 +0000
- Revision:
- 1:9e13d8a84808
- Child:
- 3:922cf54e97ec
GSM library for the full project.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ptcrews | 1:9e13d8a84808 | 1 | #include "GSM_Wrapper.h" |
ptcrews | 1:9e13d8a84808 | 2 | |
ptcrews | 1:9e13d8a84808 | 3 | void GSM_Sensor::changePowerState() |
ptcrews | 1:9e13d8a84808 | 4 | { |
ptcrews | 1:9e13d8a84808 | 5 | key.write(1); |
ptcrews | 1:9e13d8a84808 | 6 | wait(2); |
ptcrews | 1:9e13d8a84808 | 7 | key.write(0); |
ptcrews | 1:9e13d8a84808 | 8 | wait(2); |
ptcrews | 1:9e13d8a84808 | 9 | key.write(1); |
ptcrews | 1:9e13d8a84808 | 10 | wait(2); |
ptcrews | 1:9e13d8a84808 | 11 | } |
ptcrews | 1:9e13d8a84808 | 12 | |
ptcrews | 1:9e13d8a84808 | 13 | void GSM_Sensor::setup() |
ptcrews | 1:9e13d8a84808 | 14 | { |
ptcrews | 1:9e13d8a84808 | 15 | printf("Starting FONA\n"); |
ptcrews | 1:9e13d8a84808 | 16 | if(!fona.begin(9600)){ |
ptcrews | 1:9e13d8a84808 | 17 | changePowerState(); |
ptcrews | 1:9e13d8a84808 | 18 | printf("Cannot find FONA\n"); |
ptcrews | 1:9e13d8a84808 | 19 | wait(2); |
ptcrews | 1:9e13d8a84808 | 20 | } |
ptcrews | 1:9e13d8a84808 | 21 | if(!fona.begin(9600)){ |
ptcrews | 1:9e13d8a84808 | 22 | changePowerState(); |
ptcrews | 1:9e13d8a84808 | 23 | return; |
ptcrews | 1:9e13d8a84808 | 24 | } |
ptcrews | 1:9e13d8a84808 | 25 | printf("After begin\n"); |
ptcrews | 1:9e13d8a84808 | 26 | fona.setGPRSNetworkSettings("pwg", "", ""); |
ptcrews | 1:9e13d8a84808 | 27 | printf("After set setting\n"); |
ptcrews | 1:9e13d8a84808 | 28 | bool enable = false; |
ptcrews | 1:9e13d8a84808 | 29 | int i = 0; |
ptcrews | 1:9e13d8a84808 | 30 | while(enable != true) { |
ptcrews | 1:9e13d8a84808 | 31 | if(i > 5) break; |
ptcrews | 1:9e13d8a84808 | 32 | i++; |
ptcrews | 1:9e13d8a84808 | 33 | fona.enableGPRS(true); |
ptcrews | 1:9e13d8a84808 | 34 | fona.enableGPRS(false); |
ptcrews | 1:9e13d8a84808 | 35 | enable = fona.enableGPRS(true); |
ptcrews | 1:9e13d8a84808 | 36 | } |
ptcrews | 1:9e13d8a84808 | 37 | printf("After enable\n"); |
ptcrews | 1:9e13d8a84808 | 38 | } |
ptcrews | 1:9e13d8a84808 | 39 | |
ptcrews | 1:9e13d8a84808 | 40 | bool GSM_Sensor::sendOverHTTP(char* url, uint8_t* data, int dlength) |
ptcrews | 1:9e13d8a84808 | 41 | { |
ptcrews | 1:9e13d8a84808 | 42 | uint16_t statuscode; |
ptcrews | 1:9e13d8a84808 | 43 | int16_t length; |
ptcrews | 1:9e13d8a84808 | 44 | if (!fona.HTTP_POST_start(url, "text/plain", data, dlength, &statuscode, (uint16_t *)&length)) { |
ptcrews | 1:9e13d8a84808 | 45 | printf("Failed!\r\n"); |
ptcrews | 1:9e13d8a84808 | 46 | return false; |
ptcrews | 1:9e13d8a84808 | 47 | } |
ptcrews | 1:9e13d8a84808 | 48 | while (length > 0) { |
ptcrews | 1:9e13d8a84808 | 49 | while (fona.readable()) { |
ptcrews | 1:9e13d8a84808 | 50 | char c = fona.getc(); |
ptcrews | 1:9e13d8a84808 | 51 | putchar(c); |
ptcrews | 1:9e13d8a84808 | 52 | length--; |
ptcrews | 1:9e13d8a84808 | 53 | if (! length) break; |
ptcrews | 1:9e13d8a84808 | 54 | } |
ptcrews | 1:9e13d8a84808 | 55 | } |
ptcrews | 1:9e13d8a84808 | 56 | printf("\r\n****\r\n"); |
ptcrews | 1:9e13d8a84808 | 57 | fona.HTTP_POST_end(); |
ptcrews | 1:9e13d8a84808 | 58 | return true; |
ptcrews | 1:9e13d8a84808 | 59 | } |
ptcrews | 1:9e13d8a84808 | 60 | |
ptcrews | 1:9e13d8a84808 | 61 | bool GSM_Sensor::send(uint8_t* data, size_t size) |
ptcrews | 1:9e13d8a84808 | 62 | { |
ptcrews | 1:9e13d8a84808 | 63 | setup(); |
ptcrews | 1:9e13d8a84808 | 64 | printf("Setup GSM\n"); |
ptcrews | 1:9e13d8a84808 | 65 | bool res = sendOverHTTP(URL, data, size); |
ptcrews | 1:9e13d8a84808 | 66 | if(!res) res = sendOverHTTP(URL, data, size); |
ptcrews | 1:9e13d8a84808 | 67 | printf("After sent data\n"); |
ptcrews | 1:9e13d8a84808 | 68 | return res; |
ptcrews | 1:9e13d8a84808 | 69 | } |