trash over HTTP

Dependencies:   C027_Support HTTPClient TrashSensors mbed Crypto

Fork of SLOTrashHTTP by Corey Ford

Collects sensor readings and sends them to https://trash.coreyford.name/

Server code: https://github.com/coyotebush/trash-map

main.cpp

Committer:
coyotebush
Date:
2015-05-27
Revision:
2:ecc029c53752
Parent:
1:929b55ff96b0
Child:
3:1e5d19eb7c9a

File content as of revision 2:ecc029c53752:

#include "mbed.h"
#include "HTTPClient.h"

#include "hcsr04.h"

#define TRIG_PIN D6
#define ECHO_PIN D5
#define SENSOR_NAME "Throop"

#define CELLULAR_NETWORK 1

#ifdef CELLULAR_NETWORK
#include "MDM.h"
#define SIMPIN      NULL
#define APN         "Broadband"
#define USERNAME    NULL
#define PASSWORD    NULL 
#else
#include "EthernetInterface.h"
#endif

int main() 
{   
#ifdef CELLULAR_NETWORK
    MDMSerial mdm;
    //mdm.setDebug(4);
    if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
        return -1;
#else
    EthernetInterface ethernet;
    ethernet.init(); // DHCP
    if (ethernet.connect() != 0)
        return -1;
#endif
    HTTPClient http;
    
    HCSR04 distS(TRIG_PIN, ECHO_PIN);
    double distV;
    char distString[10];
    char str[512];
    
    while (true) {
        distS.start();
        wait_ms(500); 
        distV = distS.get_dist_cm();
        snprintf(distString, 10, "%0.2f", distV);
        printf("sensor reading: %s\r\n", distString);
        
        //POST data
        HTTPMap map;
        HTTPText inText(str, 512);
        map.put("name", SENSOR_NAME);
        map.put("value", distString);
        printf("\r\nTrying to post data...\r\n");
        int ret = http.post("http://trash.coreyford.name/sensor", map, &inText);
        if (!ret)
        {
          printf("Executed POST successfully - read %d characters\r\n", strlen(str));
          printf("Result: %s\r\n", str);
        }
        else
        {
          printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode());
        }
        
        wait_ms(10000);
    }
}