# Arduino Uno / Dragino - Radar Senor Data to TTN

**URL:** <https://www.thethingsnetwork.org/forum/t/arduino-uno-dragino-radar-senor-data-to-ttn/43827>\
**Category:** Dragino\
**Created:** [February 5, 2021, 3:24pm UTC](https://www.thethingsnetwork.org/forum/t/arduino-uno-dragino-radar-senor-data-to-ttn/43827 "2021-02-05T15:24:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kabcom](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/kabcom/32/35697_2.png) [@kabcom](https://www.thethingsnetwork.org/forum/u/kabcom)\
**Post date:** [February 5, 2021, 3:24pm UTC](https://www.thethingsnetwork.org/forum/t/arduino-uno-dragino-radar-senor-data-to-ttn/43827/1 "2021-02-05T15:24:26Z")

</div>

Dear forum participants,

Currently I am testing with an  
Arduino UNO, Dragino Shield v1.4,  
to transfer the distance values of a radar sensor into the TTN.

The radar sensor is connected via Pin3 and Pin4 (at the plugged Dragino Shield).  
(3,4 pin selection was made because these are not occupied by Dragino Lora Shield and the sensor data can be displayed in the serial monitor).

To test the Dragino/Uno connection to TTN,I used a “HelloWorld”-Sketch -\>Works!

To test the sensor and display the distance values in the Serial Monitor I used another test script. -\>Works!

Now I would have to combine the two sketches to send the distance values to TTN.  
Can you give me a few tips in this regard?

Lora-TTN-HelloWorld:

```
#include <lmic.h>
#include <hal/hal.h>

#ifdef CREDENTIALS
static const u1_t NWKSKEY[16] = NWKSKEY1;
static const u1_t APPSKEY[16] = APPSKEY1;
static const u4_t DEVADDR = DEVADDR1;
#else
static const u1_t NWKSKEY[16] = { 0xXX, 0xXX, 0xXX, 0xXX,0xXX, 0xXX, 0xXX, 0xXX,0xXX, 0xXX, 0xXX, 0xXX,0xXX, 0xXX, 0xXX, 0xXX,};
static const u1_t APPSKEY[16] = { 0xXX, 0xXX, 0xXX, 0xXX,0xXX, 0xXX, 0xXX, 0xXX,0xXX, 0xXX, 0xXX, 0xXX,0xXX, 0xXX, 0xXX, 0xXX,};
static const u4_t DEVADDR = 0x12345678;

#endif

// These callbacks are only used in over-the-air activation, so they are
// left empty here (we cannot leave them out completely unless
// DISABLE_JOIN is set in config.h, otherwise the linker will complain).
void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }

static osjob_t sendjob;

// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 60;

// Pin mapping Dragino Shield
const lmic_pinmap lmic_pins = {
    .nss = 10,
    .rxtx = LMIC_UNUSED_PIN,
    .rst = 9,
    .dio = {2, 6, 7},
};
void onEvent (ev_t ev) {
    if (ev == EV_TXCOMPLETE) {
        Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
        // Schedule next transmission
        os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
    }
}

void do_send(osjob_t* j){
    // Payload to send (uplink)
    static uint8_t message[] = "HelloWorldDragino";

    // Check if there is not a current TX/RX job running
    if (LMIC.opmode & OP_TXRXPEND) {
        Serial.println(F("OP_TXRXPEND, not sending"));
    } else {
        // Prepare upstream data transmission at the next possible time.
        LMIC_setTxData2(1, message, sizeof(message)-1, 0);
        Serial.println(F("Sending uplink packet..."));
    }
    // Next TX is scheduled after TX_COMPLETE event.
}

void setup() {
    Serial.begin(115200);
    Serial.println(F("Starting..."));

    // LMIC init
    os_init();

    // Reset the MAC state. Session and pending data transfers will be discarded.
    LMIC_reset();

    // Set static session parameters.
    LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);

    // Disable link check validation
    LMIC_setLinkCheckMode(0);

    // TTN uses SF9 for its RX2 window.
    LMIC.dn2Dr = DR_SF9;

    // Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
    LMIC_setDrTxpow(DR_SF12,14);

    // Start job
    do_send(&sendjob);
}

void loop() {
    os_runloop_once();
}

```

Radar-Sensor SoftSerial-Test:

```
#include <SoftwareSerial.h>
SoftwareSerial RadarSerial(4, 3);
void setup() {
 Serial.begin(19200);
 // set the data rate for the SoftwareSerial port
 RadarSerial.begin(19200);
}
void loop() { // run over and over
 if (RadarSerial.available()) {
   Serial.write(RadarSerial.read());
 }
 if (Serial.available()) {
   RadarSerial.write(Serial.read());
 }
}

```

Thank you very much in advance

---

<div class="post-metadata">

**Author:** ![descartes](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/descartes/32/47220_2.png) [@descartes](https://www.thethingsnetwork.org/forum/u/descartes)\
**Post date:** [February 5, 2021, 7:31pm UTC](https://www.thethingsnetwork.org/forum/t/arduino-uno-dragino-radar-senor-data-to-ttn/43827/2 "2021-02-05T19:31:05Z")

</div>

Can you show us what RadarSerial.read() returns so we can give you pointers on how to format the payload.

And what do you send to RadarSerial.write() ??

---

<div class="post-metadata">

**Author:** ![kabcom](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/kabcom/32/35697_2.png) [@kabcom](https://www.thethingsnetwork.org/forum/u/kabcom)\
**Post date:** [February 6, 2021, 9:08am UTC](https://www.thethingsnetwork.org/forum/t/arduino-uno-dragino-radar-senor-data-to-ttn/43827/3 "2021-02-06T09:08:08Z")

</div>

Hello!

Thanks for pointing this out to me.  
I forgot to specify the output.  
“Serial.read” does not work,therefore I use “Serial.write”, or “Serial.println”  
to display the distance value in a Serial Monitor.

Using,  
` RadarSerial.write()`  
the distance value in the Serial Monitor is output in readable format.

Serial.write - Output Example (2.2 Meter):  
`2.2`

Using,  
`RadarSerial.println()`  
the distance value in the Serial Monitor is output in ASCII format

Serial.println - Output Example (2.2 Meter):  
`50` (2)  
`46` (.)  
`50` (2)  
`13` (\r carriage return)  
`10` (\n newline)

---

<div class="post-metadata">

**Author:** ![Jonthan](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/jonthan/32/35966_2.png) [@Jonthan](https://www.thethingsnetwork.org/forum/u/Jonthan)\
**Post date:** [February 7, 2021, 9:35am UTC](https://www.thethingsnetwork.org/forum/t/arduino-uno-dragino-radar-senor-data-to-ttn/43827/4 "2021-02-07T09:35:14Z")

</div>

I’m a bit new but this looks like “hello world”. it is sending the variabe called “message” but that is defined as “HelloWorldDragino” Prehaps it would be easire to start with a sketch that is sending sensor data?

here i have two sensors and thier values are the payload. the variables are humidity and temperature

// Split both words (16 bits) into 2 bytes of 8  
byte payload[4];  
payload[0] = highByte(humidity);  
payload[1] = lowByte(humidity);  
payload[2] = highByte(temperature);  
payload[3] = lowByte(temperature);  
ttn.sendBytes(payload, sizeof(payload));

So you need to read the radar and then put that value into a variable, in the code you posted it just pushes it out on seriel. You could read it once or a few times and average it then send it out. You need some long pause in this loop though so that you are not constantly sending over the air waves like serial does over the usb cable.

See here… I read my sensor and put those values into the variables… the (false) is just something about the type of data i am getting false means I get celcuis.

uint16\_t humidity = dht.readHumidity(false);  
uint16\_t temperature = dht.readTemperature(false);

---

<div class="post-metadata">

**Author:** ![kabcom](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/kabcom/32/35697_2.png) [@kabcom](https://www.thethingsnetwork.org/forum/u/kabcom)\
**Post date:** [February 7, 2021, 5:09pm UTC](https://www.thethingsnetwork.org/forum/t/arduino-uno-dragino-radar-senor-data-to-ttn/43827/5 "2021-02-07T17:09:50Z")

</div>

Thank you for your suggestion.

I have found at: [https://www.thethingsnetwork.org/docs/devices/bytes.html](https://www.thethingsnetwork.org/docs/devices/bytes.html)

how to deal with decimals.  
I think that would be more appropriate (“2.2” for example) in my case or?

```
byte payload[1];
payload[0] = round(RadarSerial * 100);
ttn.sendBytes(payload);
```

---

<div class="post-metadata">

**Author:** ![kersing](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/kersing/32/44777_2.png) [@kersing](https://www.thethingsnetwork.org/forum/u/kersing)\
**Post date:** [February 9, 2021, 7:46pm UTC](https://www.thethingsnetwork.org/forum/t/arduino-uno-dragino-radar-senor-data-to-ttn/43827/7 "2021-02-09T19:46:05Z")

</div>

> [@Jonthan](#):
>
> I’m just going to paste in my sketch. hope thats alright

No it isn’t, please use 10 additional seconds to [format](https://www.thethingsnetwork.org/forum/t/how-to-format-your-forum-post-howto/8735) your post.

---

<div class="post-metadata">

**Author:** ![Jonthan](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/jonthan/32/35966_2.png) [@Jonthan](https://www.thethingsnetwork.org/forum/u/Jonthan)\
**Post date:** [February 9, 2021, 8:06pm UTC](https://www.thethingsnetwork.org/forum/t/arduino-uno-dragino-radar-senor-data-to-ttn/43827/8 "2021-02-09T20:06:34Z")

</div>

To deal with decimals you can do \*100 on the device side and then /100 on the other end.

---

<div class="post-metadata">

**Author:** ![kabcom](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/kabcom/32/35697_2.png) [@kabcom](https://www.thethingsnetwork.org/forum/u/kabcom)\
**Post date:** [February 15, 2021, 1:34pm UTC](https://www.thethingsnetwork.org/forum/t/arduino-uno-dragino-radar-senor-data-to-ttn/43827/9 "2021-02-15T13:34:42Z")

</div>

In your example you use the  
`#include <TheThingsNetwork.h>`  
library.

But for the Dragino-Shield I have to use the LMIC library  
`#include <lmic.h>`  
`#include <hal/hal.h>`

right?

---

<div class="post-metadata">

**Author:** ![descartes](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/descartes/32/47220_2.png) [@descartes](https://www.thethingsnetwork.org/forum/u/descartes)\
**Post date:** [February 15, 2021, 2:43pm UTC](https://www.thethingsnetwork.org/forum/t/arduino-uno-dragino-radar-senor-data-to-ttn/43827/10 "2021-02-15T14:43:00Z")

</div>

Yes, the first is for the TTN Node, the second is for devices that don’t have a pre-built module inside, just a radio chip.

---

<div class="post-metadata">

**Author:** ![Jonthan](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/jonthan/32/35966_2.png) [@Jonthan](https://www.thethingsnetwork.org/forum/u/Jonthan)\
**Post date:** [February 15, 2021, 7:02pm UTC](https://www.thethingsnetwork.org/forum/t/arduino-uno-dragino-radar-senor-data-to-ttn/43827/11 "2021-02-15T19:02:22Z")

</div>

You need to store the value and send it. The hello world one just sends “HelloWorldDragino” So instead of sending the exact text between the “” signs you should send the distance value stored in the variable.
