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-25
Revision:
1:929b55ff96b0
Parent:
0:284e9c16e92d
Child:
2:ecc029c53752

File content as of revision 1:929b55ff96b0:

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

//------------------------------------------------------------------------------------
// You need to configure these cellular modem / SIM parameters.
// These parameters are ignored for LISA-C200 variants and can be left NULL.
//------------------------------------------------------------------------------------
#include "MDM.h"
//! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
#define SIMPIN      NULL
/*! The APN of your network operator SIM, sometimes it is "internet" check your 
    contract with the network operator. You can also try to look-up your settings in 
    google: https://www.google.de/search?q=APN+list */
#define APN         "Broadband"
//! Set the user name for your APN, or NULL if not needed
#define USERNAME    NULL
//! Set the password for your APN, or NULL if not needed
#define PASSWORD    NULL 
//------------------------------------------------------------------------------------

#include "hcsr04.h"

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

char str[512];

int main() 
{
    HCSR04 distS(TRIG_PIN, ECHO_PIN);
    double distV;
    char distString[10];
    
    // turn on the supplies of the Modem
    MDMSerial mdm;
    //mdm.setDebug(4); // enable this for debugging issues 
    if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
        return -1;
    HTTPClient http;
    
    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);
    }
}