![](/media/cache/group/160px-Gnome-fs-trash-full.svg.png.50x50_q85.png)
trash over HTTP
Dependencies: C027_Support HTTPClient TrashSensors mbed Crypto
Fork of SLOTrashHTTP by
Collects sensor readings and sends them to https://trash.coreyford.name/
Server code: https://github.com/coyotebush/trash-map
Diff: main.cpp
- Revision:
- 4:1acb5d26ec21
- Parent:
- 3:1e5d19eb7c9a
- Child:
- 5:21e76b5af13b
--- a/main.cpp Wed May 27 06:57:24 2015 +0000 +++ b/main.cpp Thu May 28 04:29:26 2015 +0000 @@ -4,8 +4,12 @@ #include "hcsr04.h" #include "GPS.h" +#include "SHA1.h" +#include "HMAC.h" + #define TRIG_PIN D6 #define ECHO_PIN D5 +#define HTTP_ENDPOINT "http://trash.coreyford.name/sensor" #define SENSOR_NAME "Throop" #define CELLULAR_NETWORK 1 @@ -21,7 +25,11 @@ #endif int main() -{ +{ + char macAddress[6]; + mbed_mac_address(macAddress); + printf("MAC address is %02X%02X%02X%02X%02X%02X\r\n", macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]); + GPSSerial gps; #ifdef CELLULAR_NETWORK @@ -40,11 +48,9 @@ int gpsRet; char gpsBuf[512]; double lat = 0, lon = 0; - char latStr[10] = "", lonStr[10] = ""; HCSR04 distS(TRIG_PIN, ECHO_PIN); double distV; - char distStr[10] = ""; char str[512]; while (true) { @@ -52,8 +58,7 @@ distS.start(); wait_ms(500); distV = distS.get_dist_cm(); - snprintf(distStr, 10, "%0.2f", distV); - printf("sensor reading: %s\r\n", distStr); + printf("sensor reading: %0.2f\r\n", distV); // GPS printf("trying GPS\r\n"); @@ -71,21 +76,34 @@ && ch == 'A') { printf("GPS Location: %.5f %.5f\r\n", lat, lon); - snprintf(latStr, 10, "%0.5f", lat); - snprintf(lonStr, 10, "%0.5f", lon); break; } } + // Combine readings in JSON + char json[255]; + snprintf(json, 255, "{\"distance\":%0.1f,\"latitude\":%0.5f,\"longitude\":%0.5f}", distV, lat, lon); + + // Compute a MAC (message authentication code) of the value, + // using our MAC (media access control) address as a key. + // Yay acronyms. + SHA1 hasher; + HMAC hmacer(&hasher, (uint8_t *)macAddress, sizeof(macAddress)); + hmacer.update((uint8_t *)json, strlen(json)); + uint8_t hmac[20]; + hmacer.finalize(hmac); + char hmacHex[41], *hmacHexPtr = hmacHex; + for (int i = 0; i < 20; i++) + hmacHexPtr += sprintf(hmacHexPtr, "%02X", hmac[i]); + //POST data HTTPMap map; HTTPText inText(str, 512); map.put("name", SENSOR_NAME); - map.put("value", distStr); - map.put("latitude", latStr); - map.put("longitude", lonStr); + map.put("value", json); + map.put("mac", hmacHex); printf("\r\nTrying to post data...\r\n"); - int ret = http.post("http://trash.coreyford.name/sensor", map, &inText); + int ret = http.post(HTTP_ENDPOINT, map, &inText); if (!ret) { printf("Executed POST successfully - read %d characters\r\n", strlen(str));