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@0:284e9c16e92d, 2015-05-24 (annotated)
- Committer:
- coyotebush
- Date:
- Sun May 24 23:40:27 2015 +0000
- Revision:
- 0:284e9c16e92d
- Child:
- 1:929b55ff96b0
Initial sensor loop
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 | //GET data |
coyotebush | 0:284e9c16e92d | 43 | printf("\r\nTrying to fetch page...\r\n"); |
coyotebush | 0:284e9c16e92d | 44 | int ret = http.get("http://developer.mbed.org/media/uploads/donatien/hello.txt", str, 128); |
coyotebush | 0:284e9c16e92d | 45 | if (!ret) |
coyotebush | 0:284e9c16e92d | 46 | { |
coyotebush | 0:284e9c16e92d | 47 | printf("Page fetched successfully - read %d characters\r\n", strlen(str)); |
coyotebush | 0:284e9c16e92d | 48 | printf("Result: %s\r\n", str); |
coyotebush | 0:284e9c16e92d | 49 | } |
coyotebush | 0:284e9c16e92d | 50 | else |
coyotebush | 0:284e9c16e92d | 51 | { |
coyotebush | 0:284e9c16e92d | 52 | printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); |
coyotebush | 0:284e9c16e92d | 53 | } |
coyotebush | 0:284e9c16e92d | 54 | |
coyotebush | 0:284e9c16e92d | 55 | while (true) { |
coyotebush | 0:284e9c16e92d | 56 | distS.start(); |
coyotebush | 0:284e9c16e92d | 57 | wait_ms(500); |
coyotebush | 0:284e9c16e92d | 58 | distV = distS.get_dist_cm(); |
coyotebush | 0:284e9c16e92d | 59 | snprintf(distString, 10, "%0.2f", distV); |
coyotebush | 0:284e9c16e92d | 60 | printf("sensor reading: %s\r\n", distString); |
coyotebush | 0:284e9c16e92d | 61 | |
coyotebush | 0:284e9c16e92d | 62 | //POST data |
coyotebush | 0:284e9c16e92d | 63 | HTTPMap map; |
coyotebush | 0:284e9c16e92d | 64 | HTTPText inText(str, 512); |
coyotebush | 0:284e9c16e92d | 65 | map.put("name", SENSOR_NAME); |
coyotebush | 0:284e9c16e92d | 66 | map.put("value", distString); |
coyotebush | 0:284e9c16e92d | 67 | printf("\r\nTrying to post data...\r\n"); |
coyotebush | 0:284e9c16e92d | 68 | ret = http.post("http://trash.coreyford.name/sensor", map, &inText); |
coyotebush | 0:284e9c16e92d | 69 | if (!ret) |
coyotebush | 0:284e9c16e92d | 70 | { |
coyotebush | 0:284e9c16e92d | 71 | printf("Executed POST successfully - read %d characters\r\n", strlen(str)); |
coyotebush | 0:284e9c16e92d | 72 | printf("Result: %s\r\n", str); |
coyotebush | 0:284e9c16e92d | 73 | } |
coyotebush | 0:284e9c16e92d | 74 | else |
coyotebush | 0:284e9c16e92d | 75 | { |
coyotebush | 0:284e9c16e92d | 76 | printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); |
coyotebush | 0:284e9c16e92d | 77 | } |
coyotebush | 0:284e9c16e92d | 78 | |
coyotebush | 0:284e9c16e92d | 79 | wait_ms(5000); |
coyotebush | 0:284e9c16e92d | 80 | } |
coyotebush | 0:284e9c16e92d | 81 | |
coyotebush | 0:284e9c16e92d | 82 | |
coyotebush | 0:284e9c16e92d | 83 | mdm.disconnect(); |
coyotebush | 0:284e9c16e92d | 84 | mdm.powerOff(); |
coyotebush | 0:284e9c16e92d | 85 | |
coyotebush | 0:284e9c16e92d | 86 | while(true); |
coyotebush | 0:284e9c16e92d | 87 | } |