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
main.cpp@4:1acb5d26ec21, 2015-05-28 (annotated)
- Committer:
- coyotebush
- Date:
- Thu May 28 04:29:26 2015 +0000
- Revision:
- 4:1acb5d26ec21
- Parent:
- 3:1e5d19eb7c9a
- Child:
- 5:21e76b5af13b
Wrap readings in JSON+HMAC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
coyotebush | 0:284e9c16e92d | 1 | #include "mbed.h" |
coyotebush | 0:284e9c16e92d | 2 | #include "HTTPClient.h" |
coyotebush | 0:284e9c16e92d | 3 | |
coyotebush | 0:284e9c16e92d | 4 | #include "hcsr04.h" |
coyotebush | 3:1e5d19eb7c9a | 5 | #include "GPS.h" |
coyotebush | 0:284e9c16e92d | 6 | |
coyotebush | 4:1acb5d26ec21 | 7 | #include "SHA1.h" |
coyotebush | 4:1acb5d26ec21 | 8 | #include "HMAC.h" |
coyotebush | 4:1acb5d26ec21 | 9 | |
coyotebush | 0:284e9c16e92d | 10 | #define TRIG_PIN D6 |
coyotebush | 0:284e9c16e92d | 11 | #define ECHO_PIN D5 |
coyotebush | 4:1acb5d26ec21 | 12 | #define HTTP_ENDPOINT "http://trash.coreyford.name/sensor" |
coyotebush | 0:284e9c16e92d | 13 | #define SENSOR_NAME "Throop" |
coyotebush | 0:284e9c16e92d | 14 | |
coyotebush | 2:ecc029c53752 | 15 | #define CELLULAR_NETWORK 1 |
coyotebush | 2:ecc029c53752 | 16 | |
coyotebush | 2:ecc029c53752 | 17 | #ifdef CELLULAR_NETWORK |
coyotebush | 2:ecc029c53752 | 18 | #include "MDM.h" |
coyotebush | 2:ecc029c53752 | 19 | #define SIMPIN NULL |
coyotebush | 2:ecc029c53752 | 20 | #define APN "Broadband" |
coyotebush | 2:ecc029c53752 | 21 | #define USERNAME NULL |
coyotebush | 2:ecc029c53752 | 22 | #define PASSWORD NULL |
coyotebush | 2:ecc029c53752 | 23 | #else |
coyotebush | 2:ecc029c53752 | 24 | #include "EthernetInterface.h" |
coyotebush | 2:ecc029c53752 | 25 | #endif |
coyotebush | 0:284e9c16e92d | 26 | |
coyotebush | 0:284e9c16e92d | 27 | int main() |
coyotebush | 4:1acb5d26ec21 | 28 | { |
coyotebush | 4:1acb5d26ec21 | 29 | char macAddress[6]; |
coyotebush | 4:1acb5d26ec21 | 30 | mbed_mac_address(macAddress); |
coyotebush | 4:1acb5d26ec21 | 31 | printf("MAC address is %02X%02X%02X%02X%02X%02X\r\n", macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]); |
coyotebush | 4:1acb5d26ec21 | 32 | |
coyotebush | 3:1e5d19eb7c9a | 33 | GPSSerial gps; |
coyotebush | 3:1e5d19eb7c9a | 34 | |
coyotebush | 2:ecc029c53752 | 35 | #ifdef CELLULAR_NETWORK |
coyotebush | 2:ecc029c53752 | 36 | MDMSerial mdm; |
coyotebush | 2:ecc029c53752 | 37 | //mdm.setDebug(4); |
coyotebush | 2:ecc029c53752 | 38 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) |
coyotebush | 2:ecc029c53752 | 39 | return -1; |
coyotebush | 2:ecc029c53752 | 40 | #else |
coyotebush | 2:ecc029c53752 | 41 | EthernetInterface ethernet; |
coyotebush | 2:ecc029c53752 | 42 | ethernet.init(); // DHCP |
coyotebush | 2:ecc029c53752 | 43 | if (ethernet.connect() != 0) |
coyotebush | 2:ecc029c53752 | 44 | return -1; |
coyotebush | 2:ecc029c53752 | 45 | #endif |
coyotebush | 2:ecc029c53752 | 46 | HTTPClient http; |
coyotebush | 2:ecc029c53752 | 47 | |
coyotebush | 3:1e5d19eb7c9a | 48 | int gpsRet; |
coyotebush | 3:1e5d19eb7c9a | 49 | char gpsBuf[512]; |
coyotebush | 3:1e5d19eb7c9a | 50 | double lat = 0, lon = 0; |
coyotebush | 3:1e5d19eb7c9a | 51 | |
coyotebush | 0:284e9c16e92d | 52 | HCSR04 distS(TRIG_PIN, ECHO_PIN); |
coyotebush | 0:284e9c16e92d | 53 | double distV; |
coyotebush | 2:ecc029c53752 | 54 | char str[512]; |
coyotebush | 0:284e9c16e92d | 55 | |
coyotebush | 0:284e9c16e92d | 56 | while (true) { |
coyotebush | 3:1e5d19eb7c9a | 57 | // Distance sensor |
coyotebush | 0:284e9c16e92d | 58 | distS.start(); |
coyotebush | 0:284e9c16e92d | 59 | wait_ms(500); |
coyotebush | 0:284e9c16e92d | 60 | distV = distS.get_dist_cm(); |
coyotebush | 4:1acb5d26ec21 | 61 | printf("sensor reading: %0.2f\r\n", distV); |
coyotebush | 3:1e5d19eb7c9a | 62 | |
coyotebush | 3:1e5d19eb7c9a | 63 | // GPS |
coyotebush | 3:1e5d19eb7c9a | 64 | printf("trying GPS\r\n"); |
coyotebush | 3:1e5d19eb7c9a | 65 | while ((gpsRet = gps.getMessage(gpsBuf, sizeof(gpsBuf))) > 0) |
coyotebush | 3:1e5d19eb7c9a | 66 | { |
coyotebush | 3:1e5d19eb7c9a | 67 | int len = LENGTH(gpsRet); |
coyotebush | 3:1e5d19eb7c9a | 68 | char ch; |
coyotebush | 3:1e5d19eb7c9a | 69 | //printf("NMEA: %.*s\r\n", len-2, gpsBuf); |
coyotebush | 3:1e5d19eb7c9a | 70 | if ((PROTOCOL(gpsRet) == GPSParser::NMEA) && (len > 6) |
coyotebush | 3:1e5d19eb7c9a | 71 | && strncmp(gpsBuf, "$G", 2) == 0 |
coyotebush | 3:1e5d19eb7c9a | 72 | && strncmp(gpsBuf + 3, "GLL", 3) == 0 |
coyotebush | 3:1e5d19eb7c9a | 73 | && gps.getNmeaAngle(1,gpsBuf,len,lat) |
coyotebush | 3:1e5d19eb7c9a | 74 | && gps.getNmeaAngle(3,gpsBuf,len,lon) |
coyotebush | 3:1e5d19eb7c9a | 75 | && gps.getNmeaItem(6,gpsBuf,len,ch) |
coyotebush | 3:1e5d19eb7c9a | 76 | && ch == 'A') |
coyotebush | 3:1e5d19eb7c9a | 77 | { |
coyotebush | 3:1e5d19eb7c9a | 78 | printf("GPS Location: %.5f %.5f\r\n", lat, lon); |
coyotebush | 3:1e5d19eb7c9a | 79 | break; |
coyotebush | 3:1e5d19eb7c9a | 80 | } |
coyotebush | 3:1e5d19eb7c9a | 81 | } |
coyotebush | 0:284e9c16e92d | 82 | |
coyotebush | 4:1acb5d26ec21 | 83 | // Combine readings in JSON |
coyotebush | 4:1acb5d26ec21 | 84 | char json[255]; |
coyotebush | 4:1acb5d26ec21 | 85 | snprintf(json, 255, "{\"distance\":%0.1f,\"latitude\":%0.5f,\"longitude\":%0.5f}", distV, lat, lon); |
coyotebush | 4:1acb5d26ec21 | 86 | |
coyotebush | 4:1acb5d26ec21 | 87 | // Compute a MAC (message authentication code) of the value, |
coyotebush | 4:1acb5d26ec21 | 88 | // using our MAC (media access control) address as a key. |
coyotebush | 4:1acb5d26ec21 | 89 | // Yay acronyms. |
coyotebush | 4:1acb5d26ec21 | 90 | SHA1 hasher; |
coyotebush | 4:1acb5d26ec21 | 91 | HMAC hmacer(&hasher, (uint8_t *)macAddress, sizeof(macAddress)); |
coyotebush | 4:1acb5d26ec21 | 92 | hmacer.update((uint8_t *)json, strlen(json)); |
coyotebush | 4:1acb5d26ec21 | 93 | uint8_t hmac[20]; |
coyotebush | 4:1acb5d26ec21 | 94 | hmacer.finalize(hmac); |
coyotebush | 4:1acb5d26ec21 | 95 | char hmacHex[41], *hmacHexPtr = hmacHex; |
coyotebush | 4:1acb5d26ec21 | 96 | for (int i = 0; i < 20; i++) |
coyotebush | 4:1acb5d26ec21 | 97 | hmacHexPtr += sprintf(hmacHexPtr, "%02X", hmac[i]); |
coyotebush | 4:1acb5d26ec21 | 98 | |
coyotebush | 0:284e9c16e92d | 99 | //POST data |
coyotebush | 0:284e9c16e92d | 100 | HTTPMap map; |
coyotebush | 0:284e9c16e92d | 101 | HTTPText inText(str, 512); |
coyotebush | 0:284e9c16e92d | 102 | map.put("name", SENSOR_NAME); |
coyotebush | 4:1acb5d26ec21 | 103 | map.put("value", json); |
coyotebush | 4:1acb5d26ec21 | 104 | map.put("mac", hmacHex); |
coyotebush | 0:284e9c16e92d | 105 | printf("\r\nTrying to post data...\r\n"); |
coyotebush | 4:1acb5d26ec21 | 106 | int ret = http.post(HTTP_ENDPOINT, map, &inText); |
coyotebush | 0:284e9c16e92d | 107 | if (!ret) |
coyotebush | 0:284e9c16e92d | 108 | { |
coyotebush | 0:284e9c16e92d | 109 | printf("Executed POST successfully - read %d characters\r\n", strlen(str)); |
coyotebush | 0:284e9c16e92d | 110 | printf("Result: %s\r\n", str); |
coyotebush | 0:284e9c16e92d | 111 | } |
coyotebush | 0:284e9c16e92d | 112 | else |
coyotebush | 0:284e9c16e92d | 113 | { |
coyotebush | 0:284e9c16e92d | 114 | printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); |
coyotebush | 0:284e9c16e92d | 115 | } |
coyotebush | 0:284e9c16e92d | 116 | |
coyotebush | 1:929b55ff96b0 | 117 | wait_ms(10000); |
coyotebush | 0:284e9c16e92d | 118 | } |
coyotebush | 0:284e9c16e92d | 119 | } |