Giter Club home page Giter Club logo

tsb_prototype's Introduction

Sensor data > Node > Gateway > TTN > Webapp

We're sending sensory data with a LoRaWAN node through a TTN gateway in order to connect a simple webservice with our own TTN API

Pre-requisites

  1. Arduino Mega 2560 (2x Serial (debug, com))
  2. Dragino LoRa GPS Shield + Dragino LoRa Bee v1.0
  3. USB cable
  4. Running IDE (Arduino, Atom, ..)
  5. Sensors, jumpers and optional breadboard (in this case we're using a proximity sensor)
  6. Computer running Windows 7 or higher, Mac OS X or Linux
  7. A running TTN Gateway

Installing the library

In order to use the .ino you'll need this library:

  • install it using the Arduino Library manager ("Sketch" -> "Include Library" -> "Manage Libraries..."), or
  • download a zipfile from github using the "Download ZIP" button and install it using the IDE ("Sketch" -> "Include Library" -> "Add .ZIP Library..."
  • clone this git repository into your sketchbook/libraries folder.

For more info, see https://www.arduino.cc/en/Guide/Libraries

> On Windows, you might need to [install drivers](https://www.arduino.cc/en/Guide/ArduinoLeonardoMicro#toc2).

Register with The Things Network

Manage your applications and devices via The Things Network Console.

Create an Account

To use the console, you need an account.

  1. Create an account.
  2. Select Console from the top menu.

Add an Application in the Console

Add your first The Things Network Application.

  1. In the console, click add application.

    • For Application ID, choose a unique ID of lower case, alphanumeric characters and nonconsecutive - and _ (e.g. hi-world).
    • For Application Description, enter anything you like (e.g. Hi, World!).

    Add Application

  2. 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.

    ID exists

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.

  1. 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 button.

    Register Device (OTAA)

  2. Click Register.

    You will be redirected to the newly registered device.

  3. On the device screen, select Settings from the top right menu.

    switch-abp

    • You can give your device a description like My Uno - Workshop

    • Change Activation method to ABP.

    • Uncheck Frame counter checks at the bottom of the page.

      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.

  4. 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.

    device-info

Send a Message

Activate your device and send your first byte to verify that it works.

Configure

  1. In the Arduino IDE, select File > Examples > TheThingsNetwork > SendABP.

  2. Set the values for devAddr, nwkSKey and appSKey using the information from the device in the console. Use the ๐Ÿ“‹ buttons next to fields to copy their (hidden) value.

    • For devAddr use the Device Address.
    • For nwkSKey use the Network Session Key.
    • For appSKey use App Session Key.
  3. Change the line #define freqPlan REPLACE_ME to:

    #define freqPlan TTN_FP_EU868
    

    If you use a device with the RN2903 LoRa module, then use TTN_FP_US915 instead.

Upload

  1. Select Sketch > Upload Ctrl/โŒ˜ U to upload the sketch.

    Wait for the status bar to say Done uploading.

  2. Select Tools > Serial Monitor Ctrl/โŒ˜ Shift M to open the Serial Monitor.

    Soon, you should see something like this:

    Sending: mac tx uncnf 1 010203
    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:

messages-test

As you can see you are sending 1 byte. In the sketch you have uploaded you can find we do this in the loop() function:

void loop() {
  byte payload[1];
  payload[0] = (digitalRead(LED_BUILTIN) == HIGH) ? 1 : 0;

  // Send it off
  ttn.sendBytes(payload, sizeof(payload));
}

Send Sensor Data

Instead of sending 1 byte, we're going to send real sensor data. But first, we need to connect our sensors. In this workshop, we will use a temperature sensor.

Connect the Sensors

Use the Grove to 4-pin Male cables to connect the temperature and the button or water sensor:

  1. Connect the black GND (ground) to one of the 3 GND on the Uno.

  2. Connect the red VCC (voltage) to the 3v3 and 5V on the Uno (connect the temperature sensor to the 5V).

  3. 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: 2 from the group labeled as Digital.

Read the Sensors

Now that the sensors are connected, we have to write some code in the sketch to read their values.

  1. 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));
    }
  2. Select Sketch > Upload Ctrl/โŒ˜ U.

  3. 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: 18.58
    Sending: mac tx uncnf 1 0742
    Successful transmission
    
  4. Switch back to the Data screen in the console to verify you see the payload (here: 0742) come in when you press the button.

Decode the Payload in the Console

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.

  1. From the application in the Console, select Payload Functions from the top right menu.

  2. 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;
    }
  3. Enter the bytes you saw in the Serial Monitor (e.g. 0832 in the Payload input and click Test.

    You should get an object with the temperature in celcius. For 0832 this would be:

    {
      "celcius": 20.98
    }
  4. Click Save payload functions.

  5. Select Data from the top right menu to see how the next payloads will be decoded:

    decoded-payloads

Process Sensor Data

A common use case is to invoke an HTTP request to an external web service. for this workshop we are going to process the sensor data and send it to IFTTT (If This Then That) to trigger an event of your own choice.

IFTTT is a free web-based service that you can use to create simple conditional statements, called applets. An applet is triggered by changes that occur within other web services such as Gmail, Facebook, Instagram, or The Things Network.

Create the IFTTT Applet

Let's start on IFTTT.

  1. Go to IFTTT and create an account or login.

  2. Select New Applet from your account menu.

  3. Click This to Choose Trigger Channel.

    1. Search for maker.
    2. 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.

  4. Click Receive a web request.

    • For Event Name, let's enter workshop.
  5. Click That to configure an action, e.g. post a tweet on Twitter, e-mail or a notification to your phone.

    Use the field Value1 as ingredient. For example, a tweet could be:

    The temperature is: {{Value1}} #thethingsnetwork
    
  6. Click Create action.

  7. Click Finish. Good job! You created the Applet on IFTTT. The only thing you have to do now it connect The Things Network to your Applet and trigger the event with the sensor data.

Connect The Things Network to IFTTT

  1. Go back to your application in the Console and click on Integrations.

    integrations

  2. Add as a new integration the IFTTT Maker.

    IFTTT_maker

  3. Think of a fancy Process ID, like temperature-tweet and fill in the Event Name you just created on IFTTT.

  4. To find your secret Key, go to ifttt.com/maker and then Settings. Your key is the last part of the URL (after /use/)

  5. As Value 1 write celcius Make sure you don't accidentally add a space before or after celcius

  6. Click on Add Integration to finalize the integration.

The moment of truth

It's time for a live demonstration. It's important to gather a small audience which you can impress with your end-to-end IoT application.

Now, use the button or water sensor to trigger the action you have configured on IFTTT.

Bonus Exercise

You can even go one level further. Maybe you only want to activate the IFTTT event when the temperature is above or below a certain degree. You can enable or disable the trigger in the Decoder of the Payload Fuctions (remember where to find this?).

For doing so, you need to add the code before the return decoded;

  decoded.trigger = decoded.celcius > 20;

You can replace the > 20 with any value that you want to set as the minimal temperature to activate the trigger.

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, process it and push it to IFTTT to connect to the world of APIs.

From this starting point, you can start building a real world application. Here are some useful links:

tsb_prototype's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.