The Things Uno Workshop / Ulm Digital
This workshop will guide you through working with The Things Uno to send sensor data over The Things Network to an application.
Pre-requisites
- The Things Uno
- Micro-USB cable
- Sensors, jumpers and optional breadboard as provided:
- Computer running Windows 7 or higher, Mac OS X or Linux
- Wifi for your laptop.
- The Things Network coverage.
- A pre-configured Node-RED server.
- Go to https://docs.google.com/spreadsheets/d/1uMzCxTNxh69-df0xIY2Pbcqg7AAfcITcAoNxY8ZX0WQ/edit?usp=sharing
- Claim the first available URL by adding your name
Connect to The Things Uno
Set up the Arduino Software (IDE) and connect to your Uno.
-
Download and install the latest version of the Arduino Software.
-
Navigate to Sketch > Include Library > Manage Libraries....
-
Search for TheThingsNetwork and click the result to select it.
-
Click the Install button which should appear:
-
Connect the The Things Uno to your computer using the Micro-USB cable.
-
Select Tools > Board > Arduino Leonardo
-
Select Tools > Port > the port that identifies as Arduino Leonardo:
For Windows, see Getting Started with the Arduino Leonardo and Micro on installing drivers and finding the COM port to select.
Register with The Things Network
Managed your applications and devices via The Things Network Console.
Create an Account
To use the console, you need an account.
- Create an account.
- Select Console from the top menu.
- Click Authorize ttn-dashboard-preview.
- From the top right menu, select your name and then Settings from the dropdown menu to change the default Handler if the one currently selected is not where you'll be deploying your devices.
Add an Application
Add your first The Things Network Application.
-
In the console, click add application.
- For Application ID, choose a unique ID of lower case, alphanumeric characters and nonconsecutive
-and_(e.g.hello-world). - For Application Description, enter anything you like (e.g.
Hello, World!).
- For Application ID, choose a unique ID of lower case, alphanumeric characters and nonconsecutive
-
Click Add application to finish.
You will be redirected to the newly added application, where you can find the generated Application EUI and default Access Key which we'll need later.
If the Application ID is already taken, you will end up at the Applications overview with the following error. Simply go back and try another ID.
Register the Device
The Things Network supports the two LoRaWAN mechanisms to register devices: Over The Air Activation (OTAA) and Activation By Personalization (ABP). In this workshop, we will use ABP.
In production, you'll want to use OTAA, which is the default. This is more reliable because the activation will be confirmed and more secure because the session keys will be negotiated with every activation. ABP is useful for workshops because you don't have to wait for a downlink window to become available to confirm the activation.
-
On the Application screen, scroll down to the Devices box and click register device.
- For Device ID, choose a - for this application - unique ID of lower case, alphanumeric characters and nonconsecutive
-and_(e.g.my-uno). - For Device EUI, click the randomize link.
- For Device ID, choose a - for this application - unique ID of lower case, alphanumeric characters and nonconsecutive
-
Click Register.
You will be redirected to the newly registered device.
-
On the device screen, select Settings from the top right menu.
-
Change Activation method to ABP.
-
Uncheck Frame counter checks.
Note: This allows you to restart your device for development purposes without the routing services keeping track of the frame counter. This does make your application vulnerable for replay attacks, e.g. sending messages with a frame counter equal or lower than the latest received. Please do not disable it in production.
-
-
Click Save to finish.
You will be redirected to the device, where you can find the Device Address, Network Session Key and App Session Key that we'll need next.
Send a Message
Activate your device and send an hello world to verify it works.
Configure
-
In the Arduino IDE, select File > Examples > TheThingsNetwork > Workshop.
-
Set the values for
devAddr,nwkSKeyandappSKeyusing the information from the device in the console. Use the π buttons next to fields to copy their (hidden) value.- For
devAddruse the Device Address. - For
nwkSKeyuse the Network Session Key. - For
appSKeyuse App Session Key.
- For
-
Change the line for
freqPlanwith:const ttn_fp_t freqPlan = TTN_FP_EU868;
If you use a device with the RN2903 LoRa module, then use
TTN_FP_US915instead.
Upload
-
Select Sketch > Upload
Ctrl/β Uto upload the sketch.Wait for the status bar to say Done uploading.
-
Select Tools > Serial Monitor
Ctrl/β Shift Mto open the Serial Monitor.Soon, you should see something like this:
Sending: mac tx uncnf 1 010203 Airtime added: 1.25 s Total Airtime: 876.11 s Successful transmission
Monitor
From the device or application in the console, select Data in the top right menu. You should soon see the messages come in. Click on the blue βΆ to see all data:
As you can see you are sending 3 bytes. In the sketch you have uploaded you can find we do this in the loop() function:
void loop() {
// Create a buffer with three bytes
byte payload[3] = { 0x01, 0x02, 0x03 };
// Send it to the network
ttn.sendBytes(payload, sizeof(payload));
// Wait 10 seconds
delay(10000);
}Send Sensor Data
Instead of sending 3 bytes, we're going to send real sensor data. But first, we need to connect our sensors. In this workshop, we will use a light and a temperature sensor.
Connect the Sensors
With a Grove shield
Use the Grove cables to connect the temperature and the button or water sensor:
- Connect the temperature sensor to
A2. - Connect the button or water sensor to
D2.
Without a Grove shield
Use the Grove to 4-pin Male cables to connect the temperature and the button or water sensor:
-
Connect the black
GND(ground) to one of the 3GNDon the Uno. -
Connect the red
VCC(voltage) to either the3v3or5Von the Uno (both sensors can take both voltages). -
Connect the yellow
SIG(signal) to the Uno:- For the temperature sensor use an analog input:
A2. - For the button or water sensor use a digital input:
2from the group labeled as Digital.
- For the temperature sensor use an analog input:
Read the Sensors
Now that the sensors are connected, we have to write some code in the sketch to read their values.
-
Replace your
loop()function with the following code:// See http://www.seeedstudio.com/wiki/Grove_-_Temperature_Sensor float getCelcius(int pin) { int a = analogRead(pin); float resistance = (1023.0 - a) * 10000 / a; return 1 / (log(resistance/10000)/3975 + 1 / 298.15) - 273.15; } bool wasPressedOrWet = false; void loop() { // Read digital sensor bool pressedOrWet = (digitalRead(2) == LOW); // State unchanged if (pressedOrWet == wasPressedOrWet) { return; } wasPressedOrWet = pressedOrWet; // Not pressed or wet if (!pressedOrWet) { return; } // Read the temperature float celcius = getCelcius(A2); // Log the value debugSerial.print("Temperature: "); debugSerial.println(celcius); // Encode float as int (20.98 becomes 2098) int16_t celciusInt = round(celcius * 100); // Encode int as bytes byte payload[2]; payload[0] = highByte(celciusInt); payload[1] = lowByte(celciusInt); ttn.sendBytes(payload, sizeof(payload)); }
-
Select Sketch > Upload
Ctrl/β U. -
Select Tools > Serial Monitor
Ctrl/β Shift M.When you press the button or place your finger on the water sensor you should see something like:
Temperature: 20.98 Sending: mac tx uncnf 1 0832 Airtime added: 1.25 s Total Airtime: 2.51 s Successful transmission -
Switch back to the Data screen in the console to verify you see the payload (here:
0832) come in when you press the button.
Decode the Payload
The Things Network allows you to decode bytes to a meaningful data structure before passing it on to your application.
We will only use the decoder in this workshop. You can also use a converter to combine values or convert units and a validator to drop invalid payloads.
-
From the application in the console, select Payload Functions from the top right menu.
-
Leave decoder selected and copy-paste the following JavaScript code:
function Decoder(bytes, port) { // Decode an uplink message from a buffer // (array) of bytes to an object of fields. var decoded = {}; // Decode bytes to int var celciusInt = (bytes[0] << 8) | bytes[1]; // Decode int to float decoded.celcius = celciusInt / 100; return decoded; }
-
Enter the bytes you saw in the Serial Monitor (e.g.
0832in the Payload input and click Test.You should get an object with the temperature in celcius. For
0832this would be:{ "celcius": 20.98 } -
Click Save payload functions.
-
Select Data from the top right menu to see how the next payloads will be decoded:
Process Sensor Data
We will use Node-RED to get the data from The Things Network and process it.
Node-RED allows you to build all kinds of flows with basic business logic. You can add switches, triggers, custom functions and install thousands of nodes with additional functionality, for example storing data in a database.
Retrieve Data
-
Go to https://docs.google.com/spreadsheets/d/1uMzCxTNxh69-df0xIY2Pbcqg7AAfcITcAoNxY8ZX0WQ/edit?usp=sharing to find the first available Node-RED instance
-
Add your name to claim the Node-RED instance and go to the URL.
-
From the input category in the toolbox on the left, drag a new ttn message node to your flow.
-
Double-click the node.
-
Click the
βοΈto Add new ttn app....Copy-paste the following information from the console:
- For App ID, copy Application ID from the Application Overview box.
- For Access Key, scroll down to the Access Keys and click π to copy the default key.
- For Region or Broker, scroll back again to use Handler Status from the Application Overview box. Only copy the last bit following
ttn-handler-(e.g.eu).
-
Click Add.
-
Click Done.
-
From the output category, drag a new debug node to the flow and drag the output of the ttn message node to the input of the debug node to connect them.
-
Click Deploy and monitor the debug tab on the right for incoming messages.
Push to IFTTT
A common use case is to invoke a HTTP request to an external web service of your application. To complete the end-to-end workshop, we're going to use If This Then That (IFTTT) to connect to APIs.
Create the IFTTT Applet
Let's start on IFTTT.
-
Go to IFTTT and create an account or login.
-
Select New Applet from your account menu.
-
Click this to Choose Trigger Channel.
- Search for
maker. - Click the Maker channel.
The first time you'll need to click Connect, then Done in the popup that opens and finally Continue to the next step.
- Search for
-
Click Receive a web request.
- For Event Name, let's enter
workshop.
- For Event Name, let's enter
-
Click that to configure an action, e.g. post a tweet on Twitter, e-mail or a notification to your phone.
Use the fields
value1andvalue2as ingredient. For example, a tweet could be:The temperature is: {{value1}} #thethingsnetwork -
Click Create action.
-
Click Finish.
-
Your key is the last part of the URL (after
/use/)
Update the Node-RED flow
-
In Node-RED, drop a new function on the flow from the function category of the toolbox.
-
Drag a wire from the output of the ttn message node to the input of the new node.
-
Double click the new node to edit it.
-
Enter a Name like
create request. -
As the actual Function IFTTT expects a payload with
value[1-3]. Use the following function to pass the temperature asvalue1:return { payload: { value1: msg.payload.celcius } };
This should look something like:
-
Drag a http request node from the same function category.
-
Drag a wire from the output of the create request node to the input of the http request node.
-
Double click the new node to edit it.
-
As Method select POST.
-
For URL enter
https://maker.ifttt.com/trigger/{event}/with/key/{key}.- Replace
{event}with the Event Nameworkshopwe used at IFTTT. - Replace
{key}with the key you found at the Maker Channel.
- Replace
-
Click Done.
-
Click Deploy.
-
Now use the button or water sensor to trigger the action you have configured on IFTTT.
OK. Done. What's Next?
π Congratulations! You just learned how to create an account, an application, register a device, send data from a device, decode it, get it in Node-RED, process it and push it to IFTTT to connect to the world of APIs.
Node-RED can be used to build complex applications too. You can store data in a database, query data on an interval, add all kinds of business rules and invoke any web service.
From this starting point, you can start building a real world application. Here are some useful links:
- Set up Node-RED and install the TTN node locally.
- Use Node-RED to store data in a time series database, for example InfluxDB via the InfluxDB node.
- Install additional nodes for Node-RED.
- Receive and process data on any platform using MQTT.
- Visualize your data, for example with Grafana which works good with InfluxDB.
- Create your own charts and maps, e.g. combine our Socket.io example with Flot or Google Maps API.
- Send messages back to the device.
- Integrate with IoT cloud platforms like Azure IoT Hub and AWS IoT.
Bonus: Play with iTalk
- Get one of the 4 iTalks
- Import the flow at https://gist.github.com/FokkeZB/ce8b912ceafa6c9b1db645cf99f06ab0
- Filter on your iTalk (see the number on the box):
italk-95,italk-9e,italk-a6,italk-a7,italk-a8 - Use an MQTT output node to send the data to MyDevices as described here













