[Tutorial] Connect mbed to OGC SensorThings API - Part II
This is a simple tutorial of connecting ARM mbed LPC1768 to OGC SensorThings API.
The tutorial is divided into two parts and this is the part 2. Part 1 will help you create SensorThings Datastream with the SensorUp SensorThings Playground. Part 2 will tell you how to use mbed to upload temperature readings to the Playground.
Tutorial Difficulty Level
Starter
Our guest blogger is Kan Luo, a PhD student at the University of Calgary. Read the original post here.
Part II: Upload Mbed temperature readings to the Playground
The mBed Complier is an lightweight online C/C++ IDE that you can quickly write your code, compile and download them to your mbed Microcontroller. Follow this step-by-step tutorial to upload temperature readings to the SensorThings API.
Register
First you have to signup and log in to the Complier.(https://developer.mbed.org/account/login/) .
Create Program
Click the New button to create an empty program.
Then click OK button.
Import Libraries
Right click your program. Then import the following libraries: mbed, C12832, EthernetNetIf, LM75B and TinyHTTP. The structure of the program likes this.
Program
I prefer TinyHTTP(https://developer.mbed.org/users/okini3939/code/TinyHTTP/shortlog) to the official HttpClient to send http request. Because it is easier to change the parameters of the request to our requirements.
Here is the complete source code. You can import my program here. https://developer.mbed.org/users/robinlk/code/mbed_sensorthings/.
Tip: Find your Datastream's ID and St-P-Access-Token in theSensorUp Playground. (Last step in the previous tutorial, i.e., part 1)
const int DATASTREAM_ID_TEMP = 152983; <-- update to your Datastream's ID const char *ACCESS_TOKEN = "2c0fd43e-8471-4139-a86b-be5e32d7d732"; <-- update to your St-P-Access-Token
/* * This content is released under the (https://opensource.org/licenses/MIT) MIT License. * * Simple code to upload temperature readings to SensorUp SensorThings Playground (http://pg.sensorup.com) * from the LM75B temperature sensor(https://developer.mbed.org/cookbook/LM75B-Temperature-Sensor) * in an mbed application board(https://developer.mbed.org/cookbook/mbed-application-board) * It works with mbed LPC1768. (https://developer.mbed.org/platforms/mbed-LPC1768/) */ #include "mbed.h" #include "EthernetNetIf.h" #include "TCPSocket.h" #include "TinyHTTP.h" #include "LM75B.h" #include "C12832.h" /* * Change the DATASTREAM_ID_TEMP to the id of you SensorThigns Datastream. * You can get the Datastream <id> from the SensorUp playground's Observation * API Request:/st-playground/proxy/v1.0/Datastreams(<id>)/Observations */ const int DATASTREAM_ID_TEMP = 152983; /* * Change the ACCESS_TOKEN to the token of you SensorThigns Datastream * You can get the ACCESS_TOKEN from the SensorUp playground's Observation * API Request: St-P-Access-Token: 2c0fd43e-8471-4139-a86b-be5e32d7d732 */ const char *ACCESS_TOKEN = "2c0fd43e-8471-4139-a86b-be5e32d7d732"; /* * Interval(second) to post temperature */ const int INTERVAL = 5; // Temperature sensor LM75B sensor(p28,p27); //Ethernet network interface EthernetNetIf eth; //LCD C12832 lcd(p5, p7, p6, p8, p11); int postToServer(float temp) { Host host; char msg[50],uri[100], head[160]; // header snprintf(head, sizeof(head), "Content-type: application/json\r\nSt-P-Access-Token: %s\r\n", ACCESS_TOKEN); // uri snprintf(uri, sizeof(uri), "/st-playground/proxy/v1.0/Datastreams(%d)/Observations", DATASTREAM_ID_TEMP); // msg snprintf(msg, sizeof(msg), "{\"result\":%.3f\n}", temp); host.setName("pg-api.sensorup.com"); host.setPort(HTTP_PORT); return httpRequest(METHOD_POST, &host, uri, head, msg); } int main () { EthernetErr ethErr; Host host; int r; // Start the Ethernet connection ethErr = eth.setup(); if(ethErr) { printf("connect error\r\n"); return -1; } printf("start\r\n"); //Try to open the LM75B if (sensor.open()) { printf("Device detected!\n"); while (1) { lcd.cls(); lcd.locate(0,3); //Display the air temperature on LCD //The returned temperature is in degrees Celcius. lcd.printf("Temp = %.3f\n", (float)sensor); // Post temperature to SensorUp SensorThings Playground r = postToServer((float)sensor); printf("status %d\r\n", r); wait(INTERVAL); } } else { error("Device not detected!\n"); } return 0; }
Compile and Run
Compile and download your program with the .bin file. (Shortcut: CRTL+D/ Command+D)
Then, copy the .bin to your mbed.
If everything goes well, you will see the correct response from the server. At the same time, you will see the air temperature on your mbed LCD.
Tips: You can try CoolTerm (http://freeware.the-meiers.org/) to print out the messages.
Display Data
Go back to your SensorThigns Playground. It will display the temperature data uploaded by your mbed.
Great! That's it. With what you just have done, you can create new applications by changing parameters, adding new sensors, etc. If you feel interested about the OGC SensorThings API. You can find more information from http://sensorup.com/docs/.