Finally, aftertrying for a long time, I’ve managed to get the payload function working in The Things network console. In my previous post of setup the Lora Gateway, I was able to receive already sensor data, but, as you know, it’s not human readable data.
I’ve a couple of TTGO, SX1276 nodes with and without display. This post is going about the one with the OLED display. With display you can easily monitor the value of the HC-SR04 sensor without having a working connection to any platform.
This post is not about activating the LoRa board in the TTN console, but only as a reference to me, how I was able to get the human readable data from the LoRa board visible in the console.
For the Arduino environment, I’m using the sourcecode from Matthijs Kooijman: https://github.com/matthijskooijman/arduino-lmic/tree/master/examples. In my case I’m using the “ttn-abp” file.
After I activated the board in the TTN console, I’ve connected the HC-SR04 sensor to the TTGO board..
Connecting HC-SR04 to TTGO board
Pin HC-SR04 | Pin TTGO board |
Vcc | 5volt |
Trig | 17 |
Echo | 23 |
GND | GND |
Changing sourcode in Arduino for sending data to TTN:
Add 2 new libraries, one for the display, one for the sensor (starting line 35) | #include <U8x8lib.h> #include <hcsr04.h> |
Define sensor pinouts (starting line 37) | #define TRIG_PIN 17 #define ECHO_PIN 23 HCSR04 hcsr04(TRIG_PIN, ECHO_PIN, 20, 4000); |
Define array for variables (Starting line 68) | byte mydata[2]; |
In the “void do_send(osjob_t* j)” function, add in in the “else” statement the following code | LMIC_setTxData2(1, mydata, sizeof(mydata), 0); |
In the “void loop” function, add the following lines of code | int distance = hcsr04.distanceInMillimeters(); mydata[0] = highByte(distance); mydata[1] = lowByte(distance); |
Upload the sketch to the Lora board
in the TTN application, reset the frame counter first:
In the application, go to tab “Payload Format”. Add the following code to the “decoder” tab:
function Decoder(bytes, port) {
// Decode an uplink message from a buffer
// (array) of bytes to an object of fields.
var decoded = {};decoded.distance = (bytes[0] << 8) + bytes[1];
return decoded;
}
Which will be visible like:
Save the change at the bottom of the page.
Go to tab “Data”. If everything is done correctly, the decoded payload data is visible here at every data upload.
Open one of the entries, and you’ll see something like:
In the download file below, the whole source code is inclusive:
- extra code for having the HC-SR04 libraries going to work
- TTN data variables
- Set TTN data upload every 15 seconds (line: 75: “const unsigned TX_INTERVAL = 15;”)
- Updating OLED every 500ms
Be aware the application keys has to be set manually based on the created Application in the TTN console.
Downloads:
TTGO-OLED-HCSR04.txt (11.3 KiB, 1,853 hits)
Related Posts
June 11, 2016
Mailbox sensor
June 4, 2016
Fibaro Flood sensor
October 2, 2015
Where do you get the library “U8x8lib” from? i can not find it
I have TTGO Lora32 V2 board. What pin should I use for Trig and Echo? Any idea?
Thanks
Patrice