Getting data from LoRa - TTN network

Post Reply
costo
Posts: 57
Joined: Tuesday 11 August 2015 17:35
Target OS: Raspberry Pi
Domoticz version: stable
Location: Netherlands
Contact:

Getting data from LoRa - TTN network

Post by costo » Friday 16 March 2018 17:12

I'm trying to extract data from a node that is communicating with TheThingsNetwork over LoRaWan.
In a terminal window I can sent this command:

curl -X GET --header 'Accept: application/json' --header 'Authorization: MyKey' 'MyURL'

where MyKey is a long private key for this particular node and MyURL is on .data.thethingsnetwork.org/etc
I get a response between [ ] brackets which contain several strings separated by comma's:
each line is like this:

{"device_id":"costonode0","hum":55,"raw":"3x4gGw==","temp":21.9375,"time":"2018-03-16T13:50:23.154294105Z","volt":3227},

So I want to write a script either in BASH or LUA which analyses the strings and with JSON returns the time, hum(idity) , temp(erature) and voltage of my battery (in mV). Ofcourse this node can generate other data so the script needs to be a little configurable.
I am not a very good programmer, I have a little experience with LUA so I ask here for some help and clues how to write this script, either in LUA or another language.
truth is hatespeech to those who hate the truth.

costo
Posts: 57
Joined: Tuesday 11 August 2015 17:35
Target OS: Raspberry Pi
Domoticz version: stable
Location: Netherlands
Contact:

Re: Getting data from LoRa - TTN network

Post by costo » Saturday 17 March 2018 12:46

I made this LUA script but apparently there is something wrong in the formatting of the curl string , i get this error:

-- 2018-03-17 11:32:00.391 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_lora.lua: /home/pi/domoticz/scripts/lua/script_time_lora.lua:14: ')' expected near 'Accept'

Code: Select all

    local idxHum  = 644  
    local idxTemp = 645  
    local idxVolt = 643  
    local hum = ' '
    local temp = ' '
    local volt = ' '

    commandArray = {}

    time = os.date("*t")
    if  ((time.min % 5)==0)  then         -- Run every 5 minutes.

        json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
        local jsondata = assert(io.popen('curl -X GET --header 'Accept: application/json' --header 'Authorization: key ttn-account-v2.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 'https://costonode100.data.thethingsnetwork.org/api/v2/query/costonode0?last=5m'))

        local jsondevices = jsondata:read('*all')
        jsondata:close()

        local jsonLoRa = json:decode(jsondevices)
        hum = jsonLoRa.hum
        temp = jsonLoRa.temp
        volt = jsonLoRa.volt

        commandArray[1] = {['UpdateDevice'] = idxHum .. '|0|' .. hum}
        commandArray[2] = {['UpdateDevice'] = idxTemp .. '|0|' .. temp}
        commandArray[3] = {['UpdateDevice'] = idxVolt .. '|0|' .. volt}
    end
    
return commandArray
truth is hatespeech to those who hate the truth.

costo
Posts: 57
Joined: Tuesday 11 August 2015 17:35
Target OS: Raspberry Pi
Domoticz version: stable
Location: Netherlands
Contact:

Re: Getting data from LoRa - TTN network

Post by costo » Saturday 17 March 2018 21:21

Does someone see the error ?
I cannot figure it out what is going wrong.
I get error in line 23: attempt to index local 'jsonLoRa' (a nil value)
So maybe the json does not execute well or maybe there is an error in the Curl string.
I am out of options.
Can someone who understands LUA better than me help me here a bit please. ?

Code: Select all

    local idxHum  = 644  
    local idxTemp = 645  
    local idxVolt = 643  
    local hum = ''
    local temp = ''
    local volt = ''
    local accessKey = 'ttn-account-v2.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    local urlTTN    = 'https://costonode100.data.thethingsnetwork.org/api/v2/query/costonode0?last=5m'
	
commandArray = {}

   -- time = os.date("*t")
   -- if  ((time.min % 5)==0)  then         -- Run every 5 minutes.

        json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()

	local config = assert(io.popen("curl -X GET -H \'Accept: application/json\' -H \'Authorization: ..'accessKey'..\' \'..'urlTTN'..\'"))
        local devices = config:read('*all')
        config:close()
        print(output)
        local jsonLoRa = json:decode(devices)
	
        hum = jsonLoRa.hum
        temp = jsonLoRa.temp
        volt = jsonLoRa.volt

        commandArray[1] = {['UpdateDevice'] = idxHum .. '|0|' .. hum}
        commandArray[2] = {['UpdateDevice'] = idxTemp .. '|0|' .. temp}
        commandArray[3] = {['UpdateDevice'] = idxVolt .. '|0|' .. volt}
   -- end

return commandArray
truth is hatespeech to those who hate the truth.

costo
Posts: 57
Joined: Tuesday 11 August 2015 17:35
Target OS: Raspberry Pi
Domoticz version: stable
Location: Netherlands
Contact:

Re: Getting data from LoRa - TTN network

Post by costo » Sunday 18 March 2018 2:16

I found out that the problem is in the square brackets.
ttn server returns something like this:

[{"device_id":"costonode0","hum":50,"raw":"3RogEg==","temp":21.8125,"time":"2018-03-17T23:52:14.248591862Z","volt":3218}]
or
[{"device_id":"costonode0","hum":50,"raw":"3RogEg==","temp":21.8125,"time":"2018-03-17T23:52:14.248591862Z","volt":3218},{"device_id":"costonode0","hum":50,"raw":"3hogEg==","temp":21.875,"time":"2018-03-17T23:55:56.381884587Z","volt":3218}]

data for the last 5 minutes usually returns one json object and sometimes two.
look like local jsonLoRa = json:decode(devices) gives an error (nil value) because of the square brackets .

tried to remove the square brackets with this:
x = string.gsub(devices,"[{","{")
devices = string.gsub(x,"}]","}")
that was not working.

someone has a clue for me ?
truth is hatespeech to those who hate the truth.

alexsh1
Posts: 275
Joined: Wednesday 30 September 2015 11:50
Target OS: Raspberry Pi
Domoticz version: v3.8975
Location: United Kingdom
Contact:

Re: Getting data from LoRa - TTN network

Post by alexsh1 » Saturday 05 May 2018 10:14

costo wrote:
Sunday 18 March 2018 2:16

someone has a clue for me ?
I am afraid you are on your own.
I have just started to looking into LoraWAN/TTN

User avatar
gizmocuz
Posts: 8685
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi
Domoticz version: beta
Location: Top of the world
Contact:

Re: Getting data from LoRa - TTN network

Post by gizmocuz » Sunday 26 August 2018 9:51

Hi,

I just finished the first implementation of a hardware class (available in current beta, 9946 and up) that connects to the TTN MQTT broker and is able to work with devices that use CayenneLPP

"The Things Network (MQTT/CayenneLPP) with LAN interface"

First try to get yourself connected via a commandline MQTT client to test if everything works, then you can add this hardware
(Do not forget to enable 'accept new hardware' in the Domoticz settings)

Hope all works well , tested here with a temp/hum/baro sensor (BME 280), but separate should also work, as well as the other sensors.
Currently it is not possible to 'send' data (like send a switch ON/OFF command)

If you use the same channel for your temp,humidity,baro sensor, domoticz will combine them into 1 sensor, same for temp+hum, or temp+baro

In the Domoticz github repo you can find a class to encode to CayenneLPP in C++ (folder hardware/CayenneLPP)
Quality outlives Quantity!

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests