Simple code to upload temperature readings to SensorUp SensorThings Playground (http://pg.sensorup.com) from the XBee moudle. It works with mbed LPC1768. (https://developer.mbed.org/platforms/mbed-LPC1768/)

Dependencies:   C12832 EthernetNetIf LM75B mbed

Committer:
robinlk
Date:
Mon Jan 11 05:37:01 2016 +0000
Revision:
0:a3d37c44560a
Simple code to upload temperature readings to SensorUp SensorThings Playground (http://pg.sensorup.com) from the XBee moudle. It works with mbed LPC1768. (https://developer.mbed.org/platforms/mbed-LPC1768/)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robinlk 0:a3d37c44560a 1 /*
robinlk 0:a3d37c44560a 2 * This content is released under the (https://opensource.org/licenses/MIT) MIT License.
robinlk 0:a3d37c44560a 3 *
robinlk 0:a3d37c44560a 4 * Simple code to upload temperature readings to SensorUp SensorThings Playground (http://pg.sensorup.com)
robinlk 0:a3d37c44560a 5 * from the XBee moudle.
robinlk 0:a3d37c44560a 6 * It works with mbed LPC1768. (https://developer.mbed.org/platforms/mbed-LPC1768/)
robinlk 0:a3d37c44560a 7 */
robinlk 0:a3d37c44560a 8 #include "mbed.h"
robinlk 0:a3d37c44560a 9 #include "EthernetNetIf.h"
robinlk 0:a3d37c44560a 10 #include "TCPSocket.h"
robinlk 0:a3d37c44560a 11 #include "TinyHTTP.h"
robinlk 0:a3d37c44560a 12 #include "LM75B.h"
robinlk 0:a3d37c44560a 13 #include "C12832.h"
robinlk 0:a3d37c44560a 14
robinlk 0:a3d37c44560a 15 /*
robinlk 0:a3d37c44560a 16 * Change the DATASTREAM_ID_TEMP to the id of you SensorThigns Datastream.
robinlk 0:a3d37c44560a 17 * You can get the Datastream <id> from the SensorUp playground's Observation
robinlk 0:a3d37c44560a 18 * API Request:/st-playground/proxy/v1.0/Datastreams(<id>)/Observations
robinlk 0:a3d37c44560a 19 */
robinlk 0:a3d37c44560a 20 const int DATASTREAM_ID_TEMP = 155384;
robinlk 0:a3d37c44560a 21 /*
robinlk 0:a3d37c44560a 22 * Change the ACCESS_TOKEN to the token of you SensorThigns Datastream
robinlk 0:a3d37c44560a 23 * You can get the ACCESS_TOKEN from the SensorUp playground's Observation
robinlk 0:a3d37c44560a 24 * API Request: St-P-Access-Token: 2c0fd43e-8471-4139-a86b-be5e32d7d732
robinlk 0:a3d37c44560a 25 */
robinlk 0:a3d37c44560a 26 const char *ACCESS_TOKEN = "2c0fd43e-8471-4139-a86b-be5e32d7d732";
robinlk 0:a3d37c44560a 27 /*
robinlk 0:a3d37c44560a 28 * Interval(second) to post temperature
robinlk 0:a3d37c44560a 29 */
robinlk 0:a3d37c44560a 30 const int INTERVAL = 5;
robinlk 0:a3d37c44560a 31 //Ethernet network interface
robinlk 0:a3d37c44560a 32 EthernetNetIf eth;
robinlk 0:a3d37c44560a 33 //LCD
robinlk 0:a3d37c44560a 34 C12832 lcd(p5, p7, p6, p8, p11);
robinlk 0:a3d37c44560a 35 Serial xbee1(p9, p10);
robinlk 0:a3d37c44560a 36 DigitalOut rst1(p30);
robinlk 0:a3d37c44560a 37
robinlk 0:a3d37c44560a 38 const char *HOSTNAME = "pg-api.sensorup.com";
robinlk 0:a3d37c44560a 39 const char *DATASTREAM_URL = "/st-playground/proxy/v1.0/Datastreams(%d)/Observations";
robinlk 0:a3d37c44560a 40
robinlk 0:a3d37c44560a 41 int postToServer(float temp)
robinlk 0:a3d37c44560a 42 {
robinlk 0:a3d37c44560a 43 Host host;
robinlk 0:a3d37c44560a 44 char msg[50],uri[100], head[160];
robinlk 0:a3d37c44560a 45 // header
robinlk 0:a3d37c44560a 46 snprintf(head, sizeof(head), "Content-type: application/json\r\nSt-P-Access-Token: %s\r\n", ACCESS_TOKEN);
robinlk 0:a3d37c44560a 47 // uri
robinlk 0:a3d37c44560a 48 snprintf(uri, sizeof(uri), DATASTREAM_URL, DATASTREAM_ID_TEMP);
robinlk 0:a3d37c44560a 49 // msg
robinlk 0:a3d37c44560a 50 snprintf(msg, sizeof(msg), "{\"result\":%.3f\n}", temp);
robinlk 0:a3d37c44560a 51
robinlk 0:a3d37c44560a 52 host.setName(HOSTNAME);
robinlk 0:a3d37c44560a 53 host.setPort(HTTP_PORT);
robinlk 0:a3d37c44560a 54 return httpRequest(METHOD_POST, &host, uri, head, msg);
robinlk 0:a3d37c44560a 55 }
robinlk 0:a3d37c44560a 56
robinlk 0:a3d37c44560a 57 int main () {
robinlk 0:a3d37c44560a 58 EthernetErr ethErr;
robinlk 0:a3d37c44560a 59 Host host;
robinlk 0:a3d37c44560a 60 int r;
robinlk 0:a3d37c44560a 61 rst1 = 0; //Set reset pin to 0
robinlk 0:a3d37c44560a 62 wait_ms(1);
robinlk 0:a3d37c44560a 63 rst1 = 1; //Set reset pin to 1
robinlk 0:a3d37c44560a 64 wait_ms(1);
robinlk 0:a3d37c44560a 65 float sensor;
robinlk 0:a3d37c44560a 66
robinlk 0:a3d37c44560a 67 // Start the Ethernet connection
robinlk 0:a3d37c44560a 68 ethErr = eth.setup();
robinlk 0:a3d37c44560a 69 if(ethErr) {
robinlk 0:a3d37c44560a 70 printf("connect error\r\n");
robinlk 0:a3d37c44560a 71 return -1;
robinlk 0:a3d37c44560a 72 }
robinlk 0:a3d37c44560a 73 printf("start\r\n");
robinlk 0:a3d37c44560a 74
robinlk 0:a3d37c44560a 75 while (1) {
robinlk 0:a3d37c44560a 76 if(xbee1.readable()){
robinlk 0:a3d37c44560a 77 lcd.cls();
robinlk 0:a3d37c44560a 78 lcd.locate(0,3);
robinlk 0:a3d37c44560a 79 wait(1);
robinlk 0:a3d37c44560a 80 xbee1.scanf("%f", &sensor);
robinlk 0:a3d37c44560a 81 if(sensor < 50 && sensor > -20) {
robinlk 0:a3d37c44560a 82 lcd.printf("reading %.3f\n", sensor);
robinlk 0:a3d37c44560a 83 r = postToServer(sensor);
robinlk 0:a3d37c44560a 84 wait(INTERVAL);
robinlk 0:a3d37c44560a 85 }
robinlk 0:a3d37c44560a 86 }
robinlk 0:a3d37c44560a 87 }
robinlk 0:a3d37c44560a 88 }