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


Forum Jump: