
Simple code to upload temperature readings to SensorUp SensorThings Playground (http://pg.sensorup.com)from the LM75B temperature sensor in a mbed application board.It works with mbed LPC1768.
Dependencies: C12832 EthernetNetIf LM75B mbed
main.cpp@0:597aea501d68, 2015-12-10 (annotated)
- Committer:
- robinlk
- Date:
- Thu Dec 10 01:53:20 2015 +0000
- Revision:
- 0:597aea501d68
Simple code to upload temperature readings to SensorUp SensorThings Playground (http://pg.sensorup.com)from the LM75B temperature sensor in an mbed application board.It works with mbed LPC1768.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robinlk | 0:597aea501d68 | 1 | /* |
robinlk | 0:597aea501d68 | 2 | * This content is released under the (https://opensource.org/licenses/MIT) MIT License. |
robinlk | 0:597aea501d68 | 3 | * |
robinlk | 0:597aea501d68 | 4 | * Simple code to upload temperature readings to SensorUp SensorThings Playground (http://pg.sensorup.com) |
robinlk | 0:597aea501d68 | 5 | * from the LM75B temperature sensor(https://developer.mbed.org/cookbook/LM75B-Temperature-Sensor) |
robinlk | 0:597aea501d68 | 6 | * in an mbed application board(https://developer.mbed.org/cookbook/mbed-application-board) |
robinlk | 0:597aea501d68 | 7 | * It works with mbed LPC1768. (https://developer.mbed.org/platforms/mbed-LPC1768/) |
robinlk | 0:597aea501d68 | 8 | */ |
robinlk | 0:597aea501d68 | 9 | #include "mbed.h" |
robinlk | 0:597aea501d68 | 10 | #include "EthernetNetIf.h" |
robinlk | 0:597aea501d68 | 11 | #include "TCPSocket.h" |
robinlk | 0:597aea501d68 | 12 | #include "TinyHTTP.h" |
robinlk | 0:597aea501d68 | 13 | #include "LM75B.h" |
robinlk | 0:597aea501d68 | 14 | #include "C12832.h" |
robinlk | 0:597aea501d68 | 15 | |
robinlk | 0:597aea501d68 | 16 | /* |
robinlk | 0:597aea501d68 | 17 | * Change the DATASTREAM_ID_TEMP to the id of you SensorThigns Datastream. |
robinlk | 0:597aea501d68 | 18 | * You can get the Datastream <id> from the SensorUp playground's Observation |
robinlk | 0:597aea501d68 | 19 | * API Request:/st-playground/proxy/v1.0/Datastreams(<id>)/Observations |
robinlk | 0:597aea501d68 | 20 | */ |
robinlk | 0:597aea501d68 | 21 | const int DATASTREAM_ID_TEMP = 116680; |
robinlk | 0:597aea501d68 | 22 | /* |
robinlk | 0:597aea501d68 | 23 | * Change the ACCESS_TOKEN to the token of you SensorThigns Datastream |
robinlk | 0:597aea501d68 | 24 | * You can get the ACCESS_TOKEN from the SensorUp playground's Observation |
robinlk | 0:597aea501d68 | 25 | * API Request: St-P-Access-Token: 78cc7eb2-9394-4675-b231-ff0a377a3674 |
robinlk | 0:597aea501d68 | 26 | */ |
robinlk | 0:597aea501d68 | 27 | const char *ACCESS_TOKEN = "78cc7eb2-9394-4675-b231-ff0a377a3674"; |
robinlk | 0:597aea501d68 | 28 | /* |
robinlk | 0:597aea501d68 | 29 | * Interval(second) to post temperature |
robinlk | 0:597aea501d68 | 30 | */ |
robinlk | 0:597aea501d68 | 31 | const int INTERVAL = 5; |
robinlk | 0:597aea501d68 | 32 | // Temperature sensor |
robinlk | 0:597aea501d68 | 33 | LM75B sensor(p28,p27); |
robinlk | 0:597aea501d68 | 34 | //Ethernet network interface |
robinlk | 0:597aea501d68 | 35 | EthernetNetIf eth; |
robinlk | 0:597aea501d68 | 36 | //LCD |
robinlk | 0:597aea501d68 | 37 | C12832 lcd(p5, p7, p6, p8, p11); |
robinlk | 0:597aea501d68 | 38 | |
robinlk | 0:597aea501d68 | 39 | int postToServer(float temp) |
robinlk | 0:597aea501d68 | 40 | { |
robinlk | 0:597aea501d68 | 41 | Host host; |
robinlk | 0:597aea501d68 | 42 | char msg[50],uri[100], head[160]; |
robinlk | 0:597aea501d68 | 43 | // header |
robinlk | 0:597aea501d68 | 44 | snprintf(head, sizeof(head), "Content-type: application/json\r\nSt-P-Access-Token: %s\r\n", ACCESS_TOKEN); |
robinlk | 0:597aea501d68 | 45 | // uri |
robinlk | 0:597aea501d68 | 46 | snprintf(uri, sizeof(uri), "/st-playground/proxy/v1.0/Datastreams(%d)/Observations", DATASTREAM_ID_TEMP); |
robinlk | 0:597aea501d68 | 47 | // msg |
robinlk | 0:597aea501d68 | 48 | snprintf(msg, sizeof(msg), "{\"result\":%.3f\n}", temp); |
robinlk | 0:597aea501d68 | 49 | |
robinlk | 0:597aea501d68 | 50 | host.setName("pg-api.sensorup.com"); |
robinlk | 0:597aea501d68 | 51 | host.setPort(HTTP_PORT); |
robinlk | 0:597aea501d68 | 52 | return httpRequest(METHOD_POST, &host, uri, head, msg); |
robinlk | 0:597aea501d68 | 53 | } |
robinlk | 0:597aea501d68 | 54 | |
robinlk | 0:597aea501d68 | 55 | int main () { |
robinlk | 0:597aea501d68 | 56 | EthernetErr ethErr; |
robinlk | 0:597aea501d68 | 57 | Host host; |
robinlk | 0:597aea501d68 | 58 | int r; |
robinlk | 0:597aea501d68 | 59 | // Start the Ethernet connection |
robinlk | 0:597aea501d68 | 60 | ethErr = eth.setup(); |
robinlk | 0:597aea501d68 | 61 | if(ethErr) { |
robinlk | 0:597aea501d68 | 62 | printf("connect error\r\n"); |
robinlk | 0:597aea501d68 | 63 | return -1; |
robinlk | 0:597aea501d68 | 64 | } |
robinlk | 0:597aea501d68 | 65 | printf("start\r\n"); |
robinlk | 0:597aea501d68 | 66 | |
robinlk | 0:597aea501d68 | 67 | //Try to open the LM75B |
robinlk | 0:597aea501d68 | 68 | if (sensor.open()) { |
robinlk | 0:597aea501d68 | 69 | printf("Device detected!\n"); |
robinlk | 0:597aea501d68 | 70 | |
robinlk | 0:597aea501d68 | 71 | while (1) { |
robinlk | 0:597aea501d68 | 72 | lcd.cls(); |
robinlk | 0:597aea501d68 | 73 | lcd.locate(0,3); |
robinlk | 0:597aea501d68 | 74 | //Display the air temperature on LCD |
robinlk | 0:597aea501d68 | 75 | //The returned temperature is in degrees Celcius. |
robinlk | 0:597aea501d68 | 76 | lcd.printf("Temp = %.3f\n", (float)sensor); |
robinlk | 0:597aea501d68 | 77 | // Post temperature to SensorUp SensorThings Playground |
robinlk | 0:597aea501d68 | 78 | r = postToServer((float)sensor); |
robinlk | 0:597aea501d68 | 79 | printf("status %d\r\n", r); |
robinlk | 0:597aea501d68 | 80 | wait(INTERVAL); |
robinlk | 0:597aea501d68 | 81 | } |
robinlk | 0:597aea501d68 | 82 | } else { |
robinlk | 0:597aea501d68 | 83 | error("Device not detected!\n"); |
robinlk | 0:597aea501d68 | 84 | } |
robinlk | 0:597aea501d68 | 85 | return 0; |
robinlk | 0:597aea501d68 | 86 | } |