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@9:ce77a3614cd7, 2015-11-15 (annotated)
- Committer:
- coyotebush
- Date:
- Sun Nov 15 22:11:42 2015 +0000
- Revision:
- 9:ce77a3614cd7
- Parent:
- 8:2398817ac9c2
- Child:
- 10:313495291942
Add moisture sensor
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 | 5:21e76b5af13b | 5 | #include "GroveTemp.h" |
coyotebush | 3:1e5d19eb7c9a | 6 | #include "GPS.h" |
coyotebush | 0:284e9c16e92d | 7 | |
coyotebush | 7:49f2a1277737 | 8 | #include "Watchdog.h" |
coyotebush | 7:49f2a1277737 | 9 | |
coyotebush | 4:1acb5d26ec21 | 10 | #include "SHA1.h" |
coyotebush | 4:1acb5d26ec21 | 11 | #include "HMAC.h" |
coyotebush | 4:1acb5d26ec21 | 12 | |
coyotebush | 5:21e76b5af13b | 13 | #define TRIG_PIN A2 |
coyotebush | 5:21e76b5af13b | 14 | #define ECHO_PIN A1 |
coyotebush | 9:ce77a3614cd7 | 15 | #define MOISTURE_PIN A3 |
coyotebush | 9:ce77a3614cd7 | 16 | #define HTTP_ENDPOINT "http://192.241.205.91/sensor" |
coyotebush | 0:284e9c16e92d | 17 | #define SENSOR_NAME "Throop" |
coyotebush | 0:284e9c16e92d | 18 | |
coyotebush | 2:ecc029c53752 | 19 | #define CELLULAR_NETWORK 1 |
coyotebush | 2:ecc029c53752 | 20 | |
coyotebush | 2:ecc029c53752 | 21 | #ifdef CELLULAR_NETWORK |
coyotebush | 2:ecc029c53752 | 22 | #include "MDM.h" |
coyotebush | 2:ecc029c53752 | 23 | #define SIMPIN NULL |
coyotebush | 2:ecc029c53752 | 24 | #define APN "Broadband" |
coyotebush | 2:ecc029c53752 | 25 | #define USERNAME NULL |
coyotebush | 2:ecc029c53752 | 26 | #define PASSWORD NULL |
coyotebush | 2:ecc029c53752 | 27 | #else |
coyotebush | 2:ecc029c53752 | 28 | #include "EthernetInterface.h" |
coyotebush | 2:ecc029c53752 | 29 | #endif |
coyotebush | 0:284e9c16e92d | 30 | |
coyotebush | 0:284e9c16e92d | 31 | int main() |
coyotebush | 4:1acb5d26ec21 | 32 | { |
coyotebush | 4:1acb5d26ec21 | 33 | char macAddress[6]; |
coyotebush | 4:1acb5d26ec21 | 34 | mbed_mac_address(macAddress); |
coyotebush | 4:1acb5d26ec21 | 35 | 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 | 36 | |
coyotebush | 7:49f2a1277737 | 37 | Watchdog wdt; |
coyotebush | 7:49f2a1277737 | 38 | |
coyotebush | 3:1e5d19eb7c9a | 39 | GPSSerial gps; |
coyotebush | 3:1e5d19eb7c9a | 40 | |
coyotebush | 2:ecc029c53752 | 41 | #ifdef CELLULAR_NETWORK |
coyotebush | 2:ecc029c53752 | 42 | MDMSerial mdm; |
coyotebush | 2:ecc029c53752 | 43 | //mdm.setDebug(4); |
coyotebush | 2:ecc029c53752 | 44 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) |
coyotebush | 2:ecc029c53752 | 45 | return -1; |
coyotebush | 2:ecc029c53752 | 46 | #else |
coyotebush | 2:ecc029c53752 | 47 | EthernetInterface ethernet; |
coyotebush | 2:ecc029c53752 | 48 | ethernet.init(); // DHCP |
coyotebush | 2:ecc029c53752 | 49 | if (ethernet.connect() != 0) |
coyotebush | 2:ecc029c53752 | 50 | return -1; |
coyotebush | 2:ecc029c53752 | 51 | #endif |
coyotebush | 2:ecc029c53752 | 52 | HTTPClient http; |
coyotebush | 2:ecc029c53752 | 53 | |
coyotebush | 6:43ac8ef67a03 | 54 | bool first = true; |
coyotebush | 6:43ac8ef67a03 | 55 | |
coyotebush | 3:1e5d19eb7c9a | 56 | int gpsRet; |
coyotebush | 3:1e5d19eb7c9a | 57 | char gpsBuf[512]; |
coyotebush | 3:1e5d19eb7c9a | 58 | double lat = 0, lon = 0; |
coyotebush | 3:1e5d19eb7c9a | 59 | |
coyotebush | 0:284e9c16e92d | 60 | HCSR04 distS(TRIG_PIN, ECHO_PIN); |
coyotebush | 0:284e9c16e92d | 61 | double distV; |
coyotebush | 2:ecc029c53752 | 62 | char str[512]; |
coyotebush | 0:284e9c16e92d | 63 | |
coyotebush | 5:21e76b5af13b | 64 | GroveTempSensor tempS; |
coyotebush | 5:21e76b5af13b | 65 | double tempV; |
coyotebush | 5:21e76b5af13b | 66 | |
coyotebush | 9:ce77a3614cd7 | 67 | AnalogIn moistureS(MOISTURE_PIN); |
coyotebush | 9:ce77a3614cd7 | 68 | double moistureV; |
coyotebush | 9:ce77a3614cd7 | 69 | |
coyotebush | 7:49f2a1277737 | 70 | wdt.kick(30.0); |
coyotebush | 7:49f2a1277737 | 71 | |
coyotebush | 0:284e9c16e92d | 72 | while (true) { |
coyotebush | 3:1e5d19eb7c9a | 73 | // Distance sensor |
coyotebush | 0:284e9c16e92d | 74 | distS.start(); |
coyotebush | 0:284e9c16e92d | 75 | wait_ms(500); |
coyotebush | 0:284e9c16e92d | 76 | distV = distS.get_dist_cm(); |
coyotebush | 8:2398817ac9c2 | 77 | printf("Distance reading: %0.2f cm\r\n", distV); |
coyotebush | 3:1e5d19eb7c9a | 78 | |
coyotebush | 5:21e76b5af13b | 79 | // Temperature sensor |
coyotebush | 5:21e76b5af13b | 80 | tempV = tempS.getTemp(); |
coyotebush | 8:2398817ac9c2 | 81 | printf("Temperature reading: %0.2f C\r\n", tempV); |
coyotebush | 5:21e76b5af13b | 82 | |
coyotebush | 9:ce77a3614cd7 | 83 | // Moisture sensor |
coyotebush | 9:ce77a3614cd7 | 84 | moistureV = 1.0 - moistureS; |
coyotebush | 9:ce77a3614cd7 | 85 | printf("Moisture reading: %0.1f%%\r\n", moistureV * 100); |
coyotebush | 9:ce77a3614cd7 | 86 | |
coyotebush | 3:1e5d19eb7c9a | 87 | // GPS |
coyotebush | 8:2398817ac9c2 | 88 | printf("Checking for GPS messages\r\n"); |
coyotebush | 3:1e5d19eb7c9a | 89 | while ((gpsRet = gps.getMessage(gpsBuf, sizeof(gpsBuf))) > 0) |
coyotebush | 3:1e5d19eb7c9a | 90 | { |
coyotebush | 3:1e5d19eb7c9a | 91 | int len = LENGTH(gpsRet); |
coyotebush | 3:1e5d19eb7c9a | 92 | char ch; |
coyotebush | 3:1e5d19eb7c9a | 93 | //printf("NMEA: %.*s\r\n", len-2, gpsBuf); |
coyotebush | 3:1e5d19eb7c9a | 94 | if ((PROTOCOL(gpsRet) == GPSParser::NMEA) && (len > 6) |
coyotebush | 3:1e5d19eb7c9a | 95 | && strncmp(gpsBuf, "$G", 2) == 0 |
coyotebush | 3:1e5d19eb7c9a | 96 | && strncmp(gpsBuf + 3, "GLL", 3) == 0 |
coyotebush | 3:1e5d19eb7c9a | 97 | && gps.getNmeaAngle(1,gpsBuf,len,lat) |
coyotebush | 3:1e5d19eb7c9a | 98 | && gps.getNmeaAngle(3,gpsBuf,len,lon) |
coyotebush | 3:1e5d19eb7c9a | 99 | && gps.getNmeaItem(6,gpsBuf,len,ch) |
coyotebush | 3:1e5d19eb7c9a | 100 | && ch == 'A') |
coyotebush | 3:1e5d19eb7c9a | 101 | { |
coyotebush | 3:1e5d19eb7c9a | 102 | printf("GPS Location: %.5f %.5f\r\n", lat, lon); |
coyotebush | 3:1e5d19eb7c9a | 103 | break; |
coyotebush | 3:1e5d19eb7c9a | 104 | } |
coyotebush | 3:1e5d19eb7c9a | 105 | } |
coyotebush | 7:49f2a1277737 | 106 | wdt.kick(); |
coyotebush | 0:284e9c16e92d | 107 | |
coyotebush | 4:1acb5d26ec21 | 108 | // Combine readings in JSON |
coyotebush | 4:1acb5d26ec21 | 109 | char json[255]; |
coyotebush | 5:21e76b5af13b | 110 | snprintf(json, 255, |
coyotebush | 9:ce77a3614cd7 | 111 | "{\"distance\":%0.1f,\"temperature\":%0.1f,\"moisture\":%0.2f,\"latitude\":%0.5f,\"longitude\":%0.5f,\"first\":%d}", |
coyotebush | 9:ce77a3614cd7 | 112 | distV, tempV, moistureV, lat, lon, first); |
coyotebush | 4:1acb5d26ec21 | 113 | |
coyotebush | 4:1acb5d26ec21 | 114 | // Compute a MAC (message authentication code) of the value, |
coyotebush | 4:1acb5d26ec21 | 115 | // using our MAC (media access control) address as a key. |
coyotebush | 4:1acb5d26ec21 | 116 | // Yay acronyms. |
coyotebush | 4:1acb5d26ec21 | 117 | SHA1 hasher; |
coyotebush | 4:1acb5d26ec21 | 118 | HMAC hmacer(&hasher, (uint8_t *)macAddress, sizeof(macAddress)); |
coyotebush | 4:1acb5d26ec21 | 119 | hmacer.update((uint8_t *)json, strlen(json)); |
coyotebush | 4:1acb5d26ec21 | 120 | uint8_t hmac[20]; |
coyotebush | 4:1acb5d26ec21 | 121 | hmacer.finalize(hmac); |
coyotebush | 4:1acb5d26ec21 | 122 | char hmacHex[41], *hmacHexPtr = hmacHex; |
coyotebush | 4:1acb5d26ec21 | 123 | for (int i = 0; i < 20; i++) |
coyotebush | 4:1acb5d26ec21 | 124 | hmacHexPtr += sprintf(hmacHexPtr, "%02X", hmac[i]); |
coyotebush | 4:1acb5d26ec21 | 125 | |
coyotebush | 0:284e9c16e92d | 126 | //POST data |
coyotebush | 0:284e9c16e92d | 127 | HTTPMap map; |
coyotebush | 0:284e9c16e92d | 128 | HTTPText inText(str, 512); |
coyotebush | 0:284e9c16e92d | 129 | map.put("name", SENSOR_NAME); |
coyotebush | 4:1acb5d26ec21 | 130 | map.put("value", json); |
coyotebush | 4:1acb5d26ec21 | 131 | map.put("mac", hmacHex); |
coyotebush | 8:2398817ac9c2 | 132 | printf("\r\nTrying to POST data to server...\r\n"); |
coyotebush | 4:1acb5d26ec21 | 133 | int ret = http.post(HTTP_ENDPOINT, map, &inText); |
coyotebush | 0:284e9c16e92d | 134 | if (!ret) |
coyotebush | 0:284e9c16e92d | 135 | { |
coyotebush | 0:284e9c16e92d | 136 | printf("Executed POST successfully - read %d characters\r\n", strlen(str)); |
coyotebush | 0:284e9c16e92d | 137 | printf("Result: %s\r\n", str); |
coyotebush | 0:284e9c16e92d | 138 | } |
coyotebush | 0:284e9c16e92d | 139 | else |
coyotebush | 0:284e9c16e92d | 140 | { |
coyotebush | 0:284e9c16e92d | 141 | printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); |
coyotebush | 0:284e9c16e92d | 142 | } |
coyotebush | 0:284e9c16e92d | 143 | |
coyotebush | 6:43ac8ef67a03 | 144 | first = false; |
coyotebush | 7:49f2a1277737 | 145 | wdt.kick(); |
coyotebush | 1:929b55ff96b0 | 146 | wait_ms(10000); |
coyotebush | 7:49f2a1277737 | 147 | wdt.kick(); |
coyotebush | 0:284e9c16e92d | 148 | } |
coyotebush | 0:284e9c16e92d | 149 | } |