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:
- 3:1e5d19eb7c9a
- Parent:
- 2:ecc029c53752
- Child:
- 4:1acb5d26ec21
--- a/main.cpp Wed May 27 06:03:25 2015 +0000 +++ b/main.cpp Wed May 27 06:57:24 2015 +0000 @@ -2,6 +2,7 @@ #include "HTTPClient.h" #include "hcsr04.h" +#include "GPS.h" #define TRIG_PIN D6 #define ECHO_PIN D5 @@ -21,6 +22,8 @@ int main() { + GPSSerial gps; + #ifdef CELLULAR_NETWORK MDMSerial mdm; //mdm.setDebug(4); @@ -34,23 +37,53 @@ #endif HTTPClient http; + int gpsRet; + char gpsBuf[512]; + double lat = 0, lon = 0; + char latStr[10] = "", lonStr[10] = ""; + HCSR04 distS(TRIG_PIN, ECHO_PIN); double distV; - char distString[10]; + char distStr[10] = ""; char str[512]; while (true) { + // Distance sensor distS.start(); wait_ms(500); distV = distS.get_dist_cm(); - snprintf(distString, 10, "%0.2f", distV); - printf("sensor reading: %s\r\n", distString); + snprintf(distStr, 10, "%0.2f", distV); + printf("sensor reading: %s\r\n", distStr); + + // GPS + printf("trying GPS\r\n"); + while ((gpsRet = gps.getMessage(gpsBuf, sizeof(gpsBuf))) > 0) + { + int len = LENGTH(gpsRet); + char ch; + //printf("NMEA: %.*s\r\n", len-2, gpsBuf); + if ((PROTOCOL(gpsRet) == GPSParser::NMEA) && (len > 6) + && strncmp(gpsBuf, "$G", 2) == 0 + && strncmp(gpsBuf + 3, "GLL", 3) == 0 + && gps.getNmeaAngle(1,gpsBuf,len,lat) + && gps.getNmeaAngle(3,gpsBuf,len,lon) + && gps.getNmeaItem(6,gpsBuf,len,ch) + && ch == 'A') + { + printf("GPS Location: %.5f %.5f\r\n", lat, lon); + snprintf(latStr, 10, "%0.5f", lat); + snprintf(lonStr, 10, "%0.5f", lon); + break; + } + } //POST data HTTPMap map; HTTPText inText(str, 512); map.put("name", SENSOR_NAME); - map.put("value", distString); + map.put("value", distStr); + map.put("latitude", latStr); + map.put("longitude", lonStr); printf("\r\nTrying to post data...\r\n"); int ret = http.post("http://trash.coreyford.name/sensor", map, &inText); if (!ret)