This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Lorawan - The Things Network - Dragino sensor PS-LB
#1
Hi. 
I had a need for a remote sensor to monitor the waterlevel in a well in the forest 200m from house uphill with terrain in the way from the house, and ended out with a lorawan sensor from Dragino (PS-LB model)
Have it communicating to the things network now and gets the payload decoded by a code i got from draginos websites in the TTN console. But in MQTT explorer it seems like every package is formatted in a .json format in one topic and not with the decoded data as separate topics to subscribe to. 
here is a copy paste the end topic (v3/sensor-well-level@ttn/devices/eui-xxxxxxxxxxxxxxxx/up) from MQTT explorer.

Code:
{
  "end_device_ids": {
    "device_id": "eui-xxxxxxxxxxxxxxxx",
    "application_ids": {
      "application_id": "sensor-well-level"
    },
    "dev_eui": " xxxxxxxxxxxxxxxx ",
    "join_eui": " xxxxxxxxxxxxxxxx ",
    "dev_addr": "xxxxxxxx"
  },
  "correlation_ids": [
    "gs:uplink:01HH0xxxxxxxRX9DMMXWT5XMN"
  ],
  "received_at": "2023-12-06T23:01:24.067287960Z",
  "uplink_message": {
    "session_key_id": "AYwxx9WcxkxxxFh5xxeYZg==",
    "f_port": 2,
    "f_cnt": 573,
    "frm_payload": "DO4ABSM+AAAA",
    "decoded_payload": {
      "Bat_V": 3.31,
      "Exti_pin_level": "Low",
      "Exti_status": "False",
      "IDC_intput_mA": 9.022,
      "IN1_pin_level": "Low",
      "IN2_pin_level": "Low",
      "Probe_mod": 0,
      "VDC_intput_V": 0,
      "Water_deep_cm": 156.938
    },
    "rx_metadata": [
      {
        "gateway_ids": {
          "gateway_id": "tos-loragw-03-bjolab-net",
          "eui": "3133303719004900"
        },
        "time": "2023-12-06T23:01:23.822356Z",
        "timestamp": 106957004,
        "rssi": -104,
        "channel_rssi": -104,
        "snr": 6.5,
        "location": {
          "latitude": 69.655554,
          "longitude": 18.919952,
          "altitude": 80,
          "source": "SOURCE_REGISTRY"
        },
        "uplink_token": "CiYKJAoYdG9zLWxvcmxxxxxxYWItbmV0EggxMzA3xxxxxxxxxw/rDqwYQ1u6zmAMg4LmJuY6A+wE=",
        "channel_index": 6,
        "received_at": "2023-12-06T23:01:23.856487766Z"
      },
      {
        "gateway_ids": {
          "gateway_id": "tos-loragw-02-bjolab-net",
          "eui": "313330371A006500"
        },
        "time": "2023-12-06T23:01:23.823376Z",
        "timestamp": 1786967500,
        "rssi": -103,
        "channel_rssi": -103,
        "snr": 3,
        "location": {
          "latitude": 69.661456,
          "longitude": 18.93894,
          "altitude": 111,
          "source": "SOURCE_REGISTRY"
        },
        "uplink_token": "CiYKJAoYdG9zLWxxxxxxxxxYWItbmV0EggxMzA3GgBlAxxxxxxxxxxMCMP6w6sGEKKwzZsDIODJwPyAjYcC",
        "channel_index": 6,
        "received_at": "2023-12-06T23:01:23.863197218Z"
      },
      {
        "gateway_ids": {
          "gateway_id": "tos-loragw-01-bjolab-net",
          "eui": "3133303722003D00"
        },
        "time": "2023-12-06T23:00:42.527271Z",
        "timestamp": 1961777188,
        "rssi": -111,
        "channel_rssi": -111,
        "snr": -7,
        "location": {
          "latitude": 69.67219385058512,
          "longitude": 18.96223068237305,
          "altitude": 110,
          "source": "SOURCE_REGISTRY"
        },
        "uplink_token": "CiYKJAoYdG9zLxxxxxxxxxxItbmV0EggxMzA3IgA9ABCkxxxxxxxxxMP6w6sGEIxxxxxwDIKCZoZiMkocC",
        "channel_index": 6,
        "received_at": "2023-12-06T23:01:23.864463746Z"
      }
    ],
    "settings": {
      "data_rate": {
        "lora": {
          "bandwidth": 125000,
          "spreading_factor": 7,
          "coding_rate": "4/5"
        }
      },
      "frequency": "867700000",
      "timestamp": 106957004,
      "time": "2023-12-06T23:01:23.822356Z"
    },
    "received_at": "2023-12-06T23:01:23.857527261Z",
    "consumed_airtime": "0.056576s",
    "locations": {
      "user": {
        "latitude": 69.691434,
        "longitude": 18.7124573,
        "altitude": 47,
        "source": "SOURCE_REGISTRY"
      }
    },
    "network_ids": {
      "net_id": "000013",
      "ns_id": "EC65xxxxxxxx0181",
      "tenant_id": "ttn",
      "cluster_id": "eu1",
      "cluster_address": "eu1.cloud.thethings.network"
    }
  }
}

I need to get subscribed to new updates (each 20 minutes) for the data of interest, (se below snippet of the above where i add double ## in front to mark the interest) and send it to group adresses in my LM, one value to KNX ( "Water_deep_cm" ).

Someone here who could help as i see this involves .json and doble encoding and all this [ and { parts making me as a newbe lua guy confused how to sort this out.
MQTT server is 
eu1.cloud.thethings.network:1883 and i have to supply a username and a password i get from TTN to connect as a client.

Code:
##"received_at": "2023-12-06T23:01:24.067287960Z",
  "uplink_message": {
    "session_key_id": "AYwxx9WcxkxxxFh5xxeYZg==",
    "f_port": 2,
    "f_cnt": 573,
    "frm_payload": "DO4ABSM+AAAA",
    "decoded_payload": {
      ##"Bat_V": 3.31,
      "Exti_pin_level": "Low",
      "Exti_status": "False",
      ##"IDC_intput_mA": 9.022,
      "IN1_pin_level": "Low",
      "IN2_pin_level": "Low",
      "Probe_mod": 0,
      "VDC_intput_V": 0,
      ##"Water_deep_cm": 156.938
    },
Reply
#2
Create a resident script with non-zero sleep time. Modify username, password and topicname variables as needed. Change group addresses in the ON_MESSAGE callback.

Code:
broker = 'eu1.cloud.thethings.network'
port = 8883
username = 'PUT_USER_HERE@ttn'
password = 'PUT_API_KEY_HERE'
topicname = 'v3/sensor-well-level@ttn/devices/eui-xxxxxxxxxxxxxxxx/up'

require('json')
mqtt = require('mosquitto')
client = mqtt.new()

client.ON_CONNECT = function(status, rc, msg)
  if status then
    log('mqtt connected')
    client:subscribe(topicname)
  else
    log('mqtt connect failed ' .. tostring(msg))
    client:disconnect()
  end
end

client.ON_MESSAGE = function(mid, topic, payload)
  local data = json.pdecode(payload)

  if type(data) ~= 'table' then
    return
  end

  local datetime = data.received_at
  local sensor = data.uplink_message.decoded_payload

  grp.checkwrite('1/1/1', datetime)

  grp.checkwrite('1/1/2', sensor.Bat_V)
  grp.checkwrite('1/1/3', sensor.IDC_intput_mA)
  grp.checkwrite('1/1/4', sensor.Water_deep_cm)
end

client.ON_ERROR = log

client:login_set(username, password)
client:tls_insecure_set(true)
status, rc, msg = client:connect(broker, port)

if status then
  client:loop_forever()
else
  log('connect failed: ' .. tostring(msg))
end
Reply
#3
(07.12.2023, 08:52)admin Wrote: Create a resident script with non-zero sleep time. Modify username, password and topicname variables as needed. Change group addresses in the ON_MESSAGE callback.

Code:
broker = 'eu1.cloud.thethings.network'
port = 8883
username = 'PUT_USER_HERE@ttn'
password = 'PUT_API_KEY_HERE'
topicname = 'v3/sensor-well-level@ttn/devices/eui-xxxxxxxxxxxxxxxx/up'

require('json')
mqtt = require('mosquitto')
client = mqtt.new()

client.ON_CONNECT = function(status, rc, msg)
  if status then
    log('mqtt connected')
    client:subscribe(topicname)
  else
    log('mqtt connect failed ' .. tostring(msg))
    client:disconnect()
  end
end

client.ON_MESSAGE = function(mid, topic, payload)
  local data = json.pdecode(payload)

  if type(data) ~= 'table' then
    return
  end

  local datetime = data.received_at
  local sensor = data.uplink_message.decoded_payload

  grp.checkwrite('1/1/1', datetime)

  grp.checkwrite('1/1/2', sensor.Bat_V)
  grp.checkwrite('1/1/3', sensor.IDC_intput_mA)
  grp.checkwrite('1/1/4', sensor.Water_deep_cm)
end

client.ON_ERROR = log

client:login_set(username, password)
client:tls_insecure_set(true)
status, rc, msg = client:connect(broker, port)

if status then
  client:loop_forever()
else
  log('connect failed: ' .. tostring(msg))
end

Hello there

Apology, for my ignorance. I'm trying to get a temperature value from a LoRaWan sensor. I use this script, as I also use the the things network, and while I seem to connect to MQTT, I never get any values. I run this resident script with a 60 seconds delay. I use a virtual variable. Now, there I wasn't sure either. Is the result a text or 2 byte value?

I see no errors but also no values.

Your help is much appreciated.

Martin
Reply
#4
Your payload might be different then in this sample, try adding this in the client.ON_MESSAGE function:
Code:
local data = json.pdecode(payload)
log(data) -- what values does the payload has?
Reply
#5
Use MQTT Explorer to see the topics and values.
------------------------------
Ctrl+F5
Reply
#6
Script sleep time should not be zero. Sleep time only affects the reconnect period because normally script does not return due to loop_forever() call.
Reply
#7
(10.07.2025, 07:26)admin Wrote: Script sleep time should not be zero. Sleep time only affects the reconnect period because normally script does not return due to loop_forever() call.

Thanks Admin and Daniel for your inputs. I will give it another try this weekend. Can I just ask you to confirm the resident script with a sleep time of 60 seconds would do?

Thank you Erwin van der Zwart, I'm already logging the data but no data show in the log. But I'll test some more and report.

Attached Files Thumbnail(s)
   
Reply
#8
60 seconds sleep time is ok. The first thing you should check is the topic name.
Reply
#9
(11.07.2025, 09:06)admin Wrote: 60 seconds sleep time is ok. The first thing you should check is the topic name.

Hello there

Thank you for all your inputs and your tireless support. I  checked the topic name and that seemed to match the one from TTN. I then copy and pasted the code into a new resident script, and suddenly the on.message part of the LUA script was performed and the data retrieved. 

So, I don't really know what happened. But I really appreciate this forum. I've run two logic machine for some time now, and I really rely on them. Because they run so well, I barely need to code in Lua. That's why I turn to this forum for code snippets and support.

I will run some test and if I find the root of the original problem, I will post it. 

Cheers!
Martin
Reply


Forum Jump: