Linklt One is a popular IoT development board for prototyping IoT applications. Costing only $59, it offers great value and versatility by combining GPS, GPRS (2.5G), Bluetooth and Wi-Fi in a single board [you can get it here]. This tutorial shows using an open standard-compliant approach to connect Linklt One and the Sensorup SensorThings Playground. All interactions between the LinkIt ONE board and the SensorUp cloud is compliant with OGC SensorThings API.

Tutorial Difficulty Level: Starter

Hardware components

Steps:

  1. First, you need to set up LinkIt ONE’s Arduino-based development environment. Please refer to LinkIt One’s resources.
  2. Register an account in SensorUp playground and get an API key.
  3. Use the following code to read the sensor readings from a Grove Light Sensor, and send the value to SensorUp playground over the Web.  Please note that you will need to replace the following variable values:
#define DATASTREAM_ID_TEMP 270982 -- update to your datastream id in the playground.<br />

and

#define ACCESS_TOKEN "d6ffe625-7e9f-42cb-8187-49d391107139" -- update the value of ACCESS_TOKEN<br />

Here is the complete Linklt One sketch. Just simply update the above two values, and you should be able to see new readings showing up in your Sensorup SensorThings playground.

<br />
/*<br />
 * 2016 SensorUp (http://www.sensorup.com)<br />
 * This content is released under the (https://opensource.org/licenses/MIT) MIT License.<br />
 *<br />
 * Simple code to upload light readings to SensorUp SensorThings Playground (http://pg.sensorup.com)<br />
 * from the grove light sensor(https://www.seeedstudio.com/item_detail.html?p_id=746).<br />
 *<br />
 * It works with Linkit One board . (https://www.seeedstudio.com/item_detail.html?p_id=2017)<br />
 */<br />
#include &amp;amp;lt;LWiFi.h&amp;amp;gt;<br />
#include &amp;amp;lt;LWiFiClient.h&amp;amp;gt;<br />
/*<br />
 Modify to your WIFI Access Credentials.<br />
*/<br />
#define WIFI_AP "YourWifi"<br />
#define WIFI_PASSWORD "YourWifiPwd"<br />
#define WIFI_AUTH LWIFI_WPA // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.<br />
LWiFiClient client;</p>
<p>/******************************************************************************************<br />
 * change the &amp;amp;lt;id&amp;amp;gt; from the line below (e.g., #define DATASTREAM_ID_LIGHT &amp;amp;lt;id&amp;amp;gt;) to the<br />
 * &amp;amp;lt;id&amp;amp;gt; of your SensorThings Datastream. You can get the Datastream &amp;amp;lt;id&amp;amp;gt; from the SensorUp<br />
 * playground's Observation API Request: /st-playground/proxy/v1.0/Datastreams(&amp;amp;lt;id&amp;amp;gt;)/Observations<br />
 *****************************************************************************************/</p>
<p>#define DATASTREAM_ID_LIGHT 270982</p>
<p>/******************************************************************************************<br />
 * change the &amp;amp;lt;token&amp;amp;gt; from the line below (e.g., #define ACCESS_TOKEN &amp;amp;lt;token&amp;amp;gt;) to the<br />
 * &amp;amp;lt;token&amp;amp;gt; of your SensorThings Datastream. You can get the St-P-Access-Token &amp;amp;lt;token&amp;amp;gt; from the SensorUp<br />
 * playground's Observation API Request: St-P-Access-Token: d6ffe625-7e9f-42cb-8187-49d391107139<br />
 *****************************************************************************************/</p>
<p>#define ACCESS_TOKEN "d6ffe625-7e9f-42cb-8187-49d391107139"</p>
<p>#define SERVER_IP "pg-api.sensorup.com"<br />
#define PORT 80<br />
const int lightSensorPin = A0; // Grove Light Sensor connect to A0<br />
const int INTERVAL = 10; // Interval(second) to post data<br />
unsigned long datastream[] = {DATASTREAM_ID_LIGHT};</p>
<p>void setup()<br />
{<br />
 Serial.begin( 9600 );<br />
 Serial.println("Connect your Linkit One to SensorUp SensorThings Playground.");<br />
 InitLWiFi();<br />
}</p>
<p>void loop()<br />
{<br />
 postToServer(get_sensor_data_light(),datastream[0]);<br />
 delay(INTERVAL*1000);<br />
}</p>
<p>void InitLWiFi()<br />
{<br />
 LWiFi.begin();<br />
 // Keep retrying until connected to AP<br />
 Serial.println("Connecting");<br />
 while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD))) {<br />
 delay(1000);<br />
 }<br />
 Serial.println("Connected");<br />
}</p>
<p>long get_sensor_data_light()<br />
{<br />
 uint16_t light = 0;<br />
 light = analogRead(lightSensorPin);<br />
 return light;<br />
}</p>
<p>void postToServer(long value,unsigned long datastreamId)<br />
{<br />
 Serial.println("Posting value to Sensorup SensorThings Playground...");</p>
<p> String str = "{ ";<br />
 str += " \"result\":";<br />
 str += value;<br />
 str += "}";<br />
 Serial.println(str);</p>
<p> if (client.connect(SERVER_IP, PORT))<br />
 {<br />
 Serial.println("Sensorup SensorThings Playground connected ");<br />
 // Build HTTP POST request<br />
 client.println("POST /st-playground/proxy/v1.0/Datastreams("+String(datastreamId)+")/Observations HTTP/1.1");<br />
 String host = "Host: ";<br />
 host.concat(SERVER_IP);<br />
 client.println(host);<br />
 client.println("Connection: close");<br />
 client.println("Content-Type: application/json");<br />
 String token = "St-P-Access-Token:";<br />
 token += ACCESS_TOKEN;<br />
 client.println(token);<br />
 client.println("Cache-Control: no-cache");<br />
 client.print("Content-Length: ");<br />
 client.print(str.length());<br />
 client.print("\n\n");<br />
 client.print(str);<br />
 while(true)<br />
 {<br />
 // if there are incoming bytes available<br />
 // from the server, read them and print them:<br />
 if (client.available()) {<br />
 char c = client.read();<br />
 Serial.print(c);<br />
 }</p>
<p> // if the server's disconnected, stop the client:<br />
 if (!client.connected()) {<br />
 Serial.println();<br />
 Serial.println("disconnecting.");<br />
 client.stop();<br />
 break;<br />
 }<br />
 }<br />
 }<br />
 else {<br />
 // if you didn't get a connection to the server:<br />
 Serial.println("connection failed");<br />
 }<br />
}<br />
Comments are closed.