![](/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
main.cpp@3:1e5d19eb7c9a, 2015-05-27 (annotated)
- Committer:
- coyotebush
- Date:
- Wed May 27 06:57:24 2015 +0000
- Revision:
- 3:1e5d19eb7c9a
- Parent:
- 2:ecc029c53752
- Child:
- 4:1acb5d26ec21
Read location from GPS
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 | 0:284e9c16e92d | 7 | #define TRIG_PIN D6 |
coyotebush | 0:284e9c16e92d | 8 | #define ECHO_PIN D5 |
coyotebush | 0:284e9c16e92d | 9 | #define SENSOR_NAME "Throop" |
coyotebush | 0:284e9c16e92d | 10 | |
coyotebush | 2:ecc029c53752 | 11 | #define CELLULAR_NETWORK 1 |
coyotebush | 2:ecc029c53752 | 12 | |
coyotebush | 2:ecc029c53752 | 13 | #ifdef CELLULAR_NETWORK |
coyotebush | 2:ecc029c53752 | 14 | #include "MDM.h" |
coyotebush | 2:ecc029c53752 | 15 | #define SIMPIN NULL |
coyotebush | 2:ecc029c53752 | 16 | #define APN "Broadband" |
coyotebush | 2:ecc029c53752 | 17 | #define USERNAME NULL |
coyotebush | 2:ecc029c53752 | 18 | #define PASSWORD NULL |
coyotebush | 2:ecc029c53752 | 19 | #else |
coyotebush | 2:ecc029c53752 | 20 | #include "EthernetInterface.h" |
coyotebush | 2:ecc029c53752 | 21 | #endif |
coyotebush | 0:284e9c16e92d | 22 | |
coyotebush | 0:284e9c16e92d | 23 | int main() |
coyotebush | 2:ecc029c53752 | 24 | { |
coyotebush | 3:1e5d19eb7c9a | 25 | GPSSerial gps; |
coyotebush | 3:1e5d19eb7c9a | 26 | |
coyotebush | 2:ecc029c53752 | 27 | #ifdef CELLULAR_NETWORK |
coyotebush | 2:ecc029c53752 | 28 | MDMSerial mdm; |
coyotebush | 2:ecc029c53752 | 29 | //mdm.setDebug(4); |
coyotebush | 2:ecc029c53752 | 30 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) |
coyotebush | 2:ecc029c53752 | 31 | return -1; |
coyotebush | 2:ecc029c53752 | 32 | #else |
coyotebush | 2:ecc029c53752 | 33 | EthernetInterface ethernet; |
coyotebush | 2:ecc029c53752 | 34 | ethernet.init(); // DHCP |
coyotebush | 2:ecc029c53752 | 35 | if (ethernet.connect() != 0) |
coyotebush | 2:ecc029c53752 | 36 | return -1; |
coyotebush | 2:ecc029c53752 | 37 | #endif |
coyotebush | 2:ecc029c53752 | 38 | HTTPClient http; |
coyotebush | 2:ecc029c53752 | 39 | |
coyotebush | 3:1e5d19eb7c9a | 40 | int gpsRet; |
coyotebush | 3:1e5d19eb7c9a | 41 | char gpsBuf[512]; |
coyotebush | 3:1e5d19eb7c9a | 42 | double lat = 0, lon = 0; |
coyotebush | 3:1e5d19eb7c9a | 43 | char latStr[10] = "", lonStr[10] = ""; |
coyotebush | 3:1e5d19eb7c9a | 44 | |
coyotebush | 0:284e9c16e92d | 45 | HCSR04 distS(TRIG_PIN, ECHO_PIN); |
coyotebush | 0:284e9c16e92d | 46 | double distV; |
coyotebush | 3:1e5d19eb7c9a | 47 | char distStr[10] = ""; |
coyotebush | 2:ecc029c53752 | 48 | char str[512]; |
coyotebush | 0:284e9c16e92d | 49 | |
coyotebush | 0:284e9c16e92d | 50 | while (true) { |
coyotebush | 3:1e5d19eb7c9a | 51 | // Distance sensor |
coyotebush | 0:284e9c16e92d | 52 | distS.start(); |
coyotebush | 0:284e9c16e92d | 53 | wait_ms(500); |
coyotebush | 0:284e9c16e92d | 54 | distV = distS.get_dist_cm(); |
coyotebush | 3:1e5d19eb7c9a | 55 | snprintf(distStr, 10, "%0.2f", distV); |
coyotebush | 3:1e5d19eb7c9a | 56 | printf("sensor reading: %s\r\n", distStr); |
coyotebush | 3:1e5d19eb7c9a | 57 | |
coyotebush | 3:1e5d19eb7c9a | 58 | // GPS |
coyotebush | 3:1e5d19eb7c9a | 59 | printf("trying GPS\r\n"); |
coyotebush | 3:1e5d19eb7c9a | 60 | while ((gpsRet = gps.getMessage(gpsBuf, sizeof(gpsBuf))) > 0) |
coyotebush | 3:1e5d19eb7c9a | 61 | { |
coyotebush | 3:1e5d19eb7c9a | 62 | int len = LENGTH(gpsRet); |
coyotebush | 3:1e5d19eb7c9a | 63 | char ch; |
coyotebush | 3:1e5d19eb7c9a | 64 | //printf("NMEA: %.*s\r\n", len-2, gpsBuf); |
coyotebush | 3:1e5d19eb7c9a | 65 | if ((PROTOCOL(gpsRet) == GPSParser::NMEA) && (len > 6) |
coyotebush | 3:1e5d19eb7c9a | 66 | && strncmp(gpsBuf, "$G", 2) == 0 |
coyotebush | 3:1e5d19eb7c9a | 67 | && strncmp(gpsBuf + 3, "GLL", 3) == 0 |
coyotebush | 3:1e5d19eb7c9a | 68 | && gps.getNmeaAngle(1,gpsBuf,len,lat) |
coyotebush | 3:1e5d19eb7c9a | 69 | && gps.getNmeaAngle(3,gpsBuf,len,lon) |
coyotebush | 3:1e5d19eb7c9a | 70 | && gps.getNmeaItem(6,gpsBuf,len,ch) |
coyotebush | 3:1e5d19eb7c9a | 71 | && ch == 'A') |
coyotebush | 3:1e5d19eb7c9a | 72 | { |
coyotebush | 3:1e5d19eb7c9a | 73 | printf("GPS Location: %.5f %.5f\r\n", lat, lon); |
coyotebush | 3:1e5d19eb7c9a | 74 | snprintf(latStr, 10, "%0.5f", lat); |
coyotebush | 3:1e5d19eb7c9a | 75 | snprintf(lonStr, 10, "%0.5f", lon); |
coyotebush | 3:1e5d19eb7c9a | 76 | break; |
coyotebush | 3:1e5d19eb7c9a | 77 | } |
coyotebush | 3:1e5d19eb7c9a | 78 | } |
coyotebush | 0:284e9c16e92d | 79 | |
coyotebush | 0:284e9c16e92d | 80 | //POST data |
coyotebush | 0:284e9c16e92d | 81 | HTTPMap map; |
coyotebush | 0:284e9c16e92d | 82 | HTTPText inText(str, 512); |
coyotebush | 0:284e9c16e92d | 83 | map.put("name", SENSOR_NAME); |
coyotebush | 3:1e5d19eb7c9a | 84 | map.put("value", distStr); |
coyotebush | 3:1e5d19eb7c9a | 85 | map.put("latitude", latStr); |
coyotebush | 3:1e5d19eb7c9a | 86 | map.put("longitude", lonStr); |
coyotebush | 0:284e9c16e92d | 87 | printf("\r\nTrying to post data...\r\n"); |
coyotebush | 1:929b55ff96b0 | 88 | int ret = http.post("http://trash.coreyford.name/sensor", map, &inText); |
coyotebush | 0:284e9c16e92d | 89 | if (!ret) |
coyotebush | 0:284e9c16e92d | 90 | { |
coyotebush | 0:284e9c16e92d | 91 | printf("Executed POST successfully - read %d characters\r\n", strlen(str)); |
coyotebush | 0:284e9c16e92d | 92 | printf("Result: %s\r\n", str); |
coyotebush | 0:284e9c16e92d | 93 | } |
coyotebush | 0:284e9c16e92d | 94 | else |
coyotebush | 0:284e9c16e92d | 95 | { |
coyotebush | 0:284e9c16e92d | 96 | printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); |
coyotebush | 0:284e9c16e92d | 97 | } |
coyotebush | 0:284e9c16e92d | 98 | |
coyotebush | 1:929b55ff96b0 | 99 | wait_ms(10000); |
coyotebush | 0:284e9c16e92d | 100 | } |
coyotebush | 0:284e9c16e92d | 101 | } |