Zum Inhalt springen

Postbox Sensor with LoraWan and mqtt

Door Sensor LDS01 from Dragino

Get the sensor LDS01 LoRaWAN Door Sensor from Dragino – i have purchased it at Antratek . They have a lot of additional interesting LoraWan stuff as well.

Configuration of the LDS01 for TTNv2.0

For the purpose of this tutorial its assumed you susccessfully have added the application and the device to The Thingsnetwork on TTNv2.0. For the application the integration „Data Storage“ needs to be enabled. The Payload format needs to be added in the application screen like this (taken from Dragino manual):

<YOURKEY>: Your Application key
<YOURAPPNAME>: Name of your application
function Decoder(bytes, port) {
  // Decode an uplink message from a buffer
  // (array) of bytes to an object of fields.
  var value=(bytes[0]<<8 | bytes[1])&0x3FFF;
  var bat=value/1000;//Battery,units:V
  
  var door_open_status=bytes[0]&0x80?1:0;//1:open,0:close
  var water_leak_status=bytes[0]&0x40?1:0;
  
  var mod=bytes[2];
  var alarm=bytes[9]&0x01;
  
  if(mod==1){
    var open_times=bytes[3]<<16 | bytes[4]<<8 | bytes[5];
    var open_duration=bytes[6]<<16 | bytes[7]<<8 | bytes[8];//units:min
    return {
      BAT_V:bat,
      MOD:mod,
      DOOR_OPEN_STATUS:door_open_status,
      DOOR_OPEN_TIMES:open_times,
      LAST_DOOR_OPEN_DURATION:open_duration,
      ALARM:alarm
    };
  }
  else if(mod==2)
  {
  var leak_times=bytes[3]<<16 | bytes[4]<<8 | bytes[5];
  var leak_duration=bytes[6]<<16 | bytes[7]<<8 | bytes[8];//units:min
  return {
      BAT_V:bat,
      MOD:mod,
      WATER_LEAK_STATUS:water_leak_status,
      WATER_LEAK_TIMES:leak_times,
      LAST_WATER_LEAK_DURATION:leak_duration
  };
  }
  else if(mod==3)
  {
  return {
      BAT_V:bat,
      MOD:mod,
      DOOR_OPEN_STATUS:door_open_status,
      WATER_LEAK_STATUS:water_leak_status,
      ALARM:alarm
  };
  }
  else{
  return {
      BAT_V:bat,
      MOD:mod,
  };
  }
}

Prerequesites & installation

  • Raspbian OS installed on a Raspberry PI
  • Home directory of the user pi existing
  • Install software required by executing following commands:
apt-get install curl mosquitto-clients grep
mkdir /home/pi/smarthome/
mkdir /home/pi/smarthome/messwerte
  • Copy script into letterbox.sh and enable execution
vi /home/pi/smarthome/letterbox.sh
chmod +x /home/pi/smarthome/letterbox.sh

Check the status remotely

As real live notification is not essential a delay of a few minutes is acceptable in this case. Therefore we are checking the status every 10 minutes by downloading the status of the sensor in a JSON file.

#! /bin/bash
# get the data from the thingsnetwork App and store JSON file
# we have a crontab entry for calling this script every 10 minutes
# with last=15m we get all data from last 15 minutes 
curl -X GET --header 'Accept: application/json' --header 'Authorization: key ttn-account-<YOURKEY>' 'https://<YOURAPPNAME>.data.thethingsnetwork.org/api/v2/query?last=15m' > /home/pi/smarthome/messwerte/post.json

Analyze the JSON file

# Populate Variables from JSON file with grep and tail
POST0=$(grep -Po '"'"DOOR_OPEN_STATUS"'"\s*:\s*\K([^,]*)' /home/pi/smarthome/messwerte/post.json)
# get last entry for door status
POST1=$(grep -Po '"'"DOOR_OPEN_STATUS"'"\s*:\s*\K([^,]*)' /home/pi/smarthome/messwerte/post.json| tail -1)
# get doorcounts recorded
STATUS=$(cat /home/pi/smarthome/messwerte/postbox)
# generate variable for door counts from JSON
OPENTIMES=$(grep -Po '"'"DOOR_OPEN_TIMES"'"\s*:\s*\K([^,]*)' /home/pi/smarthome/messwerte/post.json| tail -1)
OPENTIMES1=$(grep -Po '"'"DOOR_OPEN_TIMES"'"\s*:\s*\K([^,]*)' /home/pi/smarthome/messwerte/post.json| tail -1)
# get duration from last open state
DURATION=$(grep -Po '"'"LAST_DOOR_OPEN_DURATION"'"\s*:\s*\K([^,]*)' /home/pi/smarthome/messwerte/post.json| tail -1)
# get battery status from sensor
BAT=$(grep -Po '"'"BAT_V"'"\s*:\s*\K([^,]*)' /home/pi/smarthome/messwerte/post.json| tail -1)

Send notification with Telegram

Telegram has a bot functionality which is a good solution to send messages related to smart home and IOT. In this case we use Telegram to send messages, alarms and status updates on mobile phones with Telegram messenger installed.

# Send message via telegram (you could also email)
  curl -X POST 'https://api.telegram.org/<YOURBOTID>/sendMessage?chat_id=-<YOURCHATID>&text=<b>Postbox delivery</b>%0A Duration open: '$DURATION' %0A Message time: '$TIME'%0A&parse_mode=html'

Update your smart home platform with MQTT

MQTT (Message Queuing Telemetry Transport) is a lightweight and publish-subscribe network protocol. This protocal can transport messages between devices or servers. MQTT is a quite good protocoll if you want to connect with remote locations where the bandwidth is limited or only small amounts of data needs to be transmitted.

You can install a MQTT broker on your a server to manage the data. There are also solutions available to store the data in a database like MariaDB.

<YOURBROKERHOST>: Host which runs mqtt broker
<YOURBROKERUSER>: Login for mqtt user
<YOURBROKERPW>: Password for mqtt user 
# populate to mosquitto (e.g. for home assistant)
   mosquitto_pub -h <YOURBROKERHOST> -p 1883 -u <YOURBROKERUSER> -P <YOURBROKERPW> -m $POST1 -t sensor/postbox/open
   mosquitto_pub -h <YOURBROKERHOST> -p 1883 -u <YOURBROKERUSER> -P <YOURBROKERPW> -m $OPENTIMES1 -t sensor/postbox/count
   mosquitto_pub -h <YOURBROKERHOST> -p 1883 -u <YOURBROKERUSER> -P <YOURBROKERPW> -m $BAT -t sensor/postbox/bat
   mosquitto_pub -h <YOURBROKERHOST> -p 1883 -u <YOURBROKERUSER> -P <YOURBROKERPW> -m $DURATION -t sensor/postbox/duration

Full bash script for postbox sensor

Create shell script letterbox.sh and make it executable by chmod +x letterbox.sh. This script contains all necessary parts to get the data from The Things Network, store it locally and send Telegram message and mqtt messages to the mqtt broker.

#! /bin/bash
# get the data from the thingsnetwork App and store JSON file
# we have a crontab entry for calling this script every 10 minutes
# with last=15m we get all data from last 15 minutes 
curl -X GET --header 'Accept: application/json' --header 'Authorization: key ttn-account-<YOURKEY>' 'https://<YOUAPPNAME>.data.thethingsnetwork.org/api/v2/query?last=15m' > /home/pi/smarthome/messwerte/post.json
sleep 1
# Populate Variables from JSON file with grep and tail
POST0=$(grep -Po '"'"DOOR_OPEN_STATUS"'"\s*:\s*\K([^,]*)' /home/pi/smarthome/messwerte/post.json)
# get last entry for door status
POST1=$(grep -Po '"'"DOOR_OPEN_STATUS"'"\s*:\s*\K([^,]*)' /home/pi/smarthome/messwerte/post.json| tail -1)
# get doorcounts recorded
STATUS=$(cat /home/pi/smarthome/messwerte/postbox)
# generate variable for door counts from JSON
OPENTIMES=$(grep -Po '"'"DOOR_OPEN_TIMES"'"\s*:\s*\K([^,]*)' /home/pi/smarthome/messwerte/post.json| tail -1)
OPENTIMES1=$(grep -Po '"'"DOOR_OPEN_TIMES"'"\s*:\s*\K([^,]*)' /home/pi/smarthome/messwerte/post.json| tail -1)
# get duration from last open state
DURATION=$(grep -Po '"'"LAST_DOOR_OPEN_DURATION"'"\s*:\s*\K([^,]*)' /home/pi/smarthome/messwerte/post.json| tail -1)
# get battery status from sensor
BAT=$(grep -Po '"'"BAT_V"'"\s*:\s*\K([^,]*)' /home/pi/smarthome/messwerte/post.json| tail -1)
#get actual time
TIME=$(date +%T)

if [ $STATUS -lt $OPENTIMES ]
then
# Send message via telegram (you could also email)
  curl -X POST 'https://api.telegram.org/<YOURBOTID>/sendMessage?chat_id=-<YOURCHATID>&text=<b>Postbox delivery</b>%0A Duration open: '$DURATION' %0A Message time: '$TIME'%0A&parse_mode=html'
# write doorcounts
   echo $OPENTIMES > /home/pi/smarthome/messwerte/postbox
# populate to mosquitto (e.g. for home assistant)
   mosquitto_pub -h <YOURBROKERHOST> -p 1883 -u <YOURBROKERUSER> -P <YOURBROKERPW> -m $POST1 -t sensor/postbox/open
   mosquitto_pub -h <YOURBROKERHOST> -p 1883 -u <YOURBROKERUSER> -P <YOURBROKERPW> -m $OPENTIMES1 -t sensor/postbox/count
   mosquitto_pub -h <YOURBROKERHOST> -p 1883 -u <YOURBROKERUSER> -P <YOURBROKERPW> -m $BAT -t sensor/postbox/bat
   mosquitto_pub -h <YOURBROKERHOST> -p 1883 -u <YOURBROKERUSER> -P <YOURBROKERPW> -m $DURATION -t sensor/postbox/duration
# we still might want to update mosquitto / smarthome 
else
  echo No post delivery > /dev/null
  mosquitto_pub -h <YOURBROKERHOST> -p 1883 -u <YOURBROKERUSER> -P <YOURBROKERPW> -m $POST1 -t sensor/postbox/open
  mosquitto_pub -h <YOURBROKERHOST> -p 1883 -u <YOURBROKERUSER> -P <YOURBROKERPW> -m $OPENTIMES1 -t sensor/postbox/count
  mosquitto_pub -h <YOURBROKERHOST> -p 1883 -u <YOURBROKERUSER> -P <YOURBROKERPW> -m $BAT -t sensor/postbox/bat
  mosquitto_pub -h <YOURBROKERHOST> -p 1883 -u <YOURBROKERUSER> -P <YOURBROKERPW> -m $DURATION -t sensor/postbox/duration
fi