trash over HTTP

Dependencies:   C027_Support HTTPClient TrashSensors mbed Crypto

Fork of SLOTrashHTTP by Corey Ford

Collects sensor readings and sends them to https://trash.coreyford.name/

Server code: https://github.com/coyotebush/trash-map

Committer:
coyotebush
Date:
Wed May 27 06:03:25 2015 +0000
Revision:
2:ecc029c53752
Parent:
1:929b55ff96b0
Child:
3:1e5d19eb7c9a
Optional Ethernet support

Who changed what in which revision?

UserRevisionLine numberNew 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 0:284e9c16e92d 5
coyotebush 0:284e9c16e92d 6 #define TRIG_PIN D6
coyotebush 0:284e9c16e92d 7 #define ECHO_PIN D5
coyotebush 0:284e9c16e92d 8 #define SENSOR_NAME "Throop"
coyotebush 0:284e9c16e92d 9
coyotebush 2:ecc029c53752 10 #define CELLULAR_NETWORK 1
coyotebush 2:ecc029c53752 11
coyotebush 2:ecc029c53752 12 #ifdef CELLULAR_NETWORK
coyotebush 2:ecc029c53752 13 #include "MDM.h"
coyotebush 2:ecc029c53752 14 #define SIMPIN NULL
coyotebush 2:ecc029c53752 15 #define APN "Broadband"
coyotebush 2:ecc029c53752 16 #define USERNAME NULL
coyotebush 2:ecc029c53752 17 #define PASSWORD NULL
coyotebush 2:ecc029c53752 18 #else
coyotebush 2:ecc029c53752 19 #include "EthernetInterface.h"
coyotebush 2:ecc029c53752 20 #endif
coyotebush 0:284e9c16e92d 21
coyotebush 0:284e9c16e92d 22 int main()
coyotebush 2:ecc029c53752 23 {
coyotebush 2:ecc029c53752 24 #ifdef CELLULAR_NETWORK
coyotebush 2:ecc029c53752 25 MDMSerial mdm;
coyotebush 2:ecc029c53752 26 //mdm.setDebug(4);
coyotebush 2:ecc029c53752 27 if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
coyotebush 2:ecc029c53752 28 return -1;
coyotebush 2:ecc029c53752 29 #else
coyotebush 2:ecc029c53752 30 EthernetInterface ethernet;
coyotebush 2:ecc029c53752 31 ethernet.init(); // DHCP
coyotebush 2:ecc029c53752 32 if (ethernet.connect() != 0)
coyotebush 2:ecc029c53752 33 return -1;
coyotebush 2:ecc029c53752 34 #endif
coyotebush 2:ecc029c53752 35 HTTPClient http;
coyotebush 2:ecc029c53752 36
coyotebush 0:284e9c16e92d 37 HCSR04 distS(TRIG_PIN, ECHO_PIN);
coyotebush 0:284e9c16e92d 38 double distV;
coyotebush 0:284e9c16e92d 39 char distString[10];
coyotebush 2:ecc029c53752 40 char str[512];
coyotebush 0:284e9c16e92d 41
coyotebush 0:284e9c16e92d 42 while (true) {
coyotebush 0:284e9c16e92d 43 distS.start();
coyotebush 0:284e9c16e92d 44 wait_ms(500);
coyotebush 0:284e9c16e92d 45 distV = distS.get_dist_cm();
coyotebush 0:284e9c16e92d 46 snprintf(distString, 10, "%0.2f", distV);
coyotebush 0:284e9c16e92d 47 printf("sensor reading: %s\r\n", distString);
coyotebush 0:284e9c16e92d 48
coyotebush 0:284e9c16e92d 49 //POST data
coyotebush 0:284e9c16e92d 50 HTTPMap map;
coyotebush 0:284e9c16e92d 51 HTTPText inText(str, 512);
coyotebush 0:284e9c16e92d 52 map.put("name", SENSOR_NAME);
coyotebush 0:284e9c16e92d 53 map.put("value", distString);
coyotebush 0:284e9c16e92d 54 printf("\r\nTrying to post data...\r\n");
coyotebush 1:929b55ff96b0 55 int ret = http.post("http://trash.coreyford.name/sensor", map, &inText);
coyotebush 0:284e9c16e92d 56 if (!ret)
coyotebush 0:284e9c16e92d 57 {
coyotebush 0:284e9c16e92d 58 printf("Executed POST successfully - read %d characters\r\n", strlen(str));
coyotebush 0:284e9c16e92d 59 printf("Result: %s\r\n", str);
coyotebush 0:284e9c16e92d 60 }
coyotebush 0:284e9c16e92d 61 else
coyotebush 0:284e9c16e92d 62 {
coyotebush 0:284e9c16e92d 63 printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode());
coyotebush 0:284e9c16e92d 64 }
coyotebush 0:284e9c16e92d 65
coyotebush 1:929b55ff96b0 66 wait_ms(10000);
coyotebush 0:284e9c16e92d 67 }
coyotebush 0:284e9c16e92d 68 }