Sodaq ONE with TTN downlink LED control

To be able to test the dual channel up and downlink gateway (see The Things Network dual-channel up&downlink gateway ) we needed to have a node available to demonstrate the downlink messages coming through.

sodaq-one-base-and-antenna

For this, we used a Sodaq ONE which has nice multicolored LEDs we can use to test if the gateway is functioning properly.

I created the following sketch that, depending on the message you send, cycles through the sent bytes and switches between the RED, GREEN and BLUE LED.  Be sure to enter your proper ABP values, from the TTN console application registered device, as indicated in the source below to make it work.

The dual channel up/downlink supports OTAA also, but this might take a long time to connect as we cannot use all TTN channels.

#include "TheThingsNetwork.h"
#include "Arduino.h"
// ABP
const byte devAddr[4] = { /* your devAddr here */ };
const byte appSKey[16] = { /* your appskey here */ };
const byte nwkSKey[16] = { /* your nwkskey here */ };

#define debugSerial SerialUSB
#define loraSerial Serial1

#define debugPrintLn(...) { if (debugSerial) debugSerial.println(__VA_ARGS__); }
#define debugPrint(...) { if (debugSerial) debugSerial.print(__VA_ARGS__); }

TheThingsNetwork ttn;
void RED() {
    digitalWrite(LED_RED, LOW);
    digitalWrite(LED_GREEN, HIGH);
    digitalWrite(LED_BLUE, HIGH);
}

void GREEN() {
    digitalWrite(LED_RED, HIGH);
    digitalWrite(LED_GREEN, LOW);
    digitalWrite(LED_BLUE, HIGH);
}

void BLUE() {
    digitalWrite(LED_RED, HIGH);
    digitalWrite(LED_GREEN, HIGH);
    digitalWrite(LED_BLUE, LOW);
}

void ALL_OFF() {
    digitalWrite(LED_RED, HIGH);
    digitalWrite(LED_GREEN, HIGH);
    digitalWrite(LED_BLUE, HIGH);
}

void setup()
{
  debugSerial.begin(115200);
  loraSerial.begin(57600);
  //LEDs setup
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);
  GREEN();
  delay(1000);
  ttn.init(loraSerial, debugSerial); //Initializing...
  ttn.reset(false,7,1);

  ttn.personalize(devAddr, nwkSKey, appSKey);
  RED();
  delay(6000);
  ttn.showStatus();
  debugPrintLn("Setup for The Things Network complete");

  delay(1000);

  ALL_OFF();

}

void loop() {
  // Send a byte
  byte buf[1];
  buf[0] = 20;
  int downlinkBytes = ttn.sendBytes(buf, 1);

  if (downlinkBytes > 0) {
    debugPrintLn("Received " + String(downlinkBytes) + " bytes");
    // Print the received bytes
    for (int i = 0; i < downlinkBytes; i++) {
      debugPrint(String(ttn.downlink[i]) + " ");
      if ( ttn.downlink[i] == 1 ) {
        RED();
        delay(2000);
      }
      else if ( ttn.downlink[i] == 2 ) {
        GREEN();
        delay(2000);
      } else if ( ttn.downlink[i] == 3 ) {
        BLUE();
        delay(2000);
      } else if ( ttn.downlink[i] == 0 ) {
        ALL_OFF();
       delay(2000);
      }
    }
    debugPrintLn();
    }
  ttn.showStatus();
  delay(20000);
  ALL_OFF();
}

Once you loaded the sketch into the Sodaq it will start sending a single byte uplink which you can follow in your console.

Now send the downlink to the device from the console by logging into the console, selecting your created application and the Sodaq device and scroll down to the DOWNLINK section:

ttn-console-downlink

Enter bytes 01 (for RED), 02 (for GREEN), 03 (for BLUE) or 00 (for ALL OFF)  in the Payload field. If you enter sequence the LEDs will switch every 2 seconds.

Remark: DOWNLINK messages are only send upon receiving a UPLINK message, so do not be alarmed if it takes some time before the actual message comes through…

Advertenties

3 reacties op ‘Sodaq ONE with TTN downlink LED control

  1. Hi Hans,
    I’m new to LoRa and TTN so I’m trying all kinds of example codes.
    As I have a Sodaq One I thought “Lets try this” .. but sadly it does not compile. There are probably changes in the “TheThingsNetwork” library that does not longer comply with this code.

    * TheThingsNetwork ttn(loraSerial, debugSerial, TTN_FP_EU868);

    Sadly I can’t get past the ttn.downlink[x] and ttn.uplink[x] statements.

    Can you please elaborate?

    Regards,

    Willem

    Like

  2. I have created a new sketch that works with the latest ttn library, it is using OTAA:
    #include

    // Set your AppEUI and AppKey
    const char *appEui = “”;
    const char *appKey = “”;

    #define loraSerial Serial1
    #define debugSerial SerialUSB

    // Replace REPLACE_ME with TTN_FP_EU868 or TTN_FP_US915
    #define freqPlan TTN_FP_EU868

    TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
    void RED() {
    digitalWrite(LED_RED, LOW);
    digitalWrite(LED_GREEN, HIGH);
    digitalWrite(LED_BLUE, HIGH);
    }

    void GREEN() {
    digitalWrite(LED_RED, HIGH);
    digitalWrite(LED_GREEN, LOW);
    digitalWrite(LED_BLUE, HIGH);
    }

    void BLUE() {
    digitalWrite(LED_RED, HIGH);
    digitalWrite(LED_GREEN, HIGH);
    digitalWrite(LED_BLUE, LOW);
    }

    void ALL_OFF() {
    digitalWrite(LED_RED, HIGH);
    digitalWrite(LED_GREEN, HIGH);
    digitalWrite(LED_BLUE, HIGH);
    }
    void setup()
    {
    loraSerial.begin(57600);
    debugSerial.begin(115200);

    //LEDs setup
    pinMode(LED_RED, OUTPUT);
    pinMode(LED_GREEN, OUTPUT);
    pinMode(LED_BLUE, OUTPUT);
    GREEN();
    delay(1000);

    // Set callback for incoming messages
    ttn.onMessage(message);

    RED();
    delay(6000);

    debugSerial.println(“– STATUS”);
    ttn.showStatus();

    debugSerial.println(“– JOIN”);
    ttn.join(appEui, appKey);

    ALL_OFF();
    }

    void loop()
    {
    debugSerial.println(“– LOOP”);

    // Send single byte to poll for incoming messages
    ttn.poll();

    delay(10000);
    }

    void message(const uint8_t *payload, size_t size, port_t port)
    {
    debugSerial.println(“– MESSAGE”);
    debugSerial.print(“Received ” + String(size) + ” bytes on port ” + String(port) + “:”);

    for (int i = 0; i < size; i++)
    {
    debugSerial.print(" " + String(payload[i]));
    if ( String(payload[i]) == "1" ) {
    RED();
    delay(2000);
    }
    else if ( String(payload[i]) == "2" ) {
    GREEN();
    delay(2000);
    } else if ( String(payload[i]) == "3" ) {
    BLUE();
    delay(2000);
    } else if ( String(payload[i]) == "0" ) {
    ALL_OFF();
    }
    }

    debugSerial.println();
    ttn.showStatus();
    delay(20000);
    ALL_OFF();
    }

    Liked by 1 persoon

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit /  Bijwerken )

Google photo

Je reageert onder je Google account. Log uit /  Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )

Verbinden met %s