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@1:929b55ff96b0, 2015-05-25 (annotated)
- Committer:
- coyotebush
- Date:
- Mon May 25 03:19:34 2015 +0000
- Revision:
- 1:929b55ff96b0
- Parent:
- 0:284e9c16e92d
- Child:
- 2:ecc029c53752
Cleanup
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 | //------------------------------------------------------------------------------------ |
coyotebush | 0:284e9c16e92d | 5 | // You need to configure these cellular modem / SIM parameters. |
coyotebush | 0:284e9c16e92d | 6 | // These parameters are ignored for LISA-C200 variants and can be left NULL. |
coyotebush | 0:284e9c16e92d | 7 | //------------------------------------------------------------------------------------ |
coyotebush | 0:284e9c16e92d | 8 | #include "MDM.h" |
coyotebush | 0:284e9c16e92d | 9 | //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual. |
coyotebush | 0:284e9c16e92d | 10 | #define SIMPIN NULL |
coyotebush | 0:284e9c16e92d | 11 | /*! The APN of your network operator SIM, sometimes it is "internet" check your |
coyotebush | 0:284e9c16e92d | 12 | contract with the network operator. You can also try to look-up your settings in |
coyotebush | 0:284e9c16e92d | 13 | google: https://www.google.de/search?q=APN+list */ |
coyotebush | 0:284e9c16e92d | 14 | #define APN "Broadband" |
coyotebush | 0:284e9c16e92d | 15 | //! Set the user name for your APN, or NULL if not needed |
coyotebush | 0:284e9c16e92d | 16 | #define USERNAME NULL |
coyotebush | 0:284e9c16e92d | 17 | //! Set the password for your APN, or NULL if not needed |
coyotebush | 0:284e9c16e92d | 18 | #define PASSWORD NULL |
coyotebush | 0:284e9c16e92d | 19 | //------------------------------------------------------------------------------------ |
coyotebush | 0:284e9c16e92d | 20 | |
coyotebush | 0:284e9c16e92d | 21 | #include "hcsr04.h" |
coyotebush | 0:284e9c16e92d | 22 | |
coyotebush | 0:284e9c16e92d | 23 | #define TRIG_PIN D6 |
coyotebush | 0:284e9c16e92d | 24 | #define ECHO_PIN D5 |
coyotebush | 0:284e9c16e92d | 25 | #define SENSOR_NAME "Throop" |
coyotebush | 0:284e9c16e92d | 26 | |
coyotebush | 0:284e9c16e92d | 27 | char str[512]; |
coyotebush | 0:284e9c16e92d | 28 | |
coyotebush | 0:284e9c16e92d | 29 | int main() |
coyotebush | 0:284e9c16e92d | 30 | { |
coyotebush | 0:284e9c16e92d | 31 | HCSR04 distS(TRIG_PIN, ECHO_PIN); |
coyotebush | 0:284e9c16e92d | 32 | double distV; |
coyotebush | 0:284e9c16e92d | 33 | char distString[10]; |
coyotebush | 0:284e9c16e92d | 34 | |
coyotebush | 0:284e9c16e92d | 35 | // turn on the supplies of the Modem |
coyotebush | 0:284e9c16e92d | 36 | MDMSerial mdm; |
coyotebush | 0:284e9c16e92d | 37 | //mdm.setDebug(4); // enable this for debugging issues |
coyotebush | 0:284e9c16e92d | 38 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) |
coyotebush | 0:284e9c16e92d | 39 | return -1; |
coyotebush | 0:284e9c16e92d | 40 | HTTPClient http; |
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 | } |