GSM library for nucleo L152RE

Dependencies:   Adafruit_GSM

Dependents:   Full-Project

GSM_Wrapper.cpp

Committer:
ptcrews
Date:
2015-12-05
Revision:
1:9e13d8a84808
Child:
3:922cf54e97ec

File content as of revision 1:9e13d8a84808:

#include "GSM_Wrapper.h"

void GSM_Sensor::changePowerState()
{
    key.write(1);
    wait(2);
    key.write(0);
    wait(2);
    key.write(1);
    wait(2);
}

void GSM_Sensor::setup()
{
    printf("Starting FONA\n");
    if(!fona.begin(9600)){
        changePowerState();
        printf("Cannot find FONA\n");
        wait(2);
    }
    if(!fona.begin(9600)){
        changePowerState(); 
        return;
    }
    printf("After begin\n");
    fona.setGPRSNetworkSettings("pwg", "", "");
    printf("After set setting\n");
    bool enable = false;
    int i = 0;
    while(enable != true) {
        if(i > 5) break;
        i++;
        fona.enableGPRS(true);
        fona.enableGPRS(false);
        enable = fona.enableGPRS(true);
    }
    printf("After enable\n");
}

bool GSM_Sensor::sendOverHTTP(char* url, uint8_t* data, int dlength)
{
    uint16_t statuscode;
    int16_t length;
    if (!fona.HTTP_POST_start(url, "text/plain", data, dlength, &statuscode, (uint16_t *)&length)) {
        printf("Failed!\r\n");
        return false;
    }
    while (length > 0) {
        while (fona.readable()) {
            char c = fona.getc();
            putchar(c);
            length--;
            if (! length) break;
        }
    }
    printf("\r\n****\r\n");
    fona.HTTP_POST_end();
    return true;
}

bool GSM_Sensor::send(uint8_t* data, size_t size)
{
    setup();
    printf("Setup GSM\n");
    bool res = sendOverHTTP(URL, data, size);
    if(!res) res = sendOverHTTP(URL, data, size);
    printf("After sent data\n");   
    return res;
}