Giter Site home page Giter Site logo

dersimn / owrtwifi2mqtt Goto Github PK

View Code? Open in Web Editor NEW
74.0 20.0 24.0 40 KB

Using your OpenWRT Router's Wifi to detect if a person's smartphone is still in/near the apartment and publish via MQTT

Shell 63.68% JavaScript 36.32%
openhab openhab2 openwrt mqtt presence mqtt-smarthome

owrtwifi2mqtt's Introduction

A simple shell script to detect presence of Wifi devices (smartphones, tablets, Amazon Dash Buttons, ..) and post the results via MQTT. This information can be processed in Homeautomation Systems like OpenHAB to turn down the heating when everyone left the appartment.

Installation

Install the MQTT client

Install the packages

  • mosquitto-client
  • coreutils-nohup

with either luci or opkg.

Get the script

Download

opkg update && opkg install libustream-openssl
wget -O /usr/bin/presence_report https://raw.githubusercontent.com/dersimn/owrtwifi2mqtt/master/presence_report && chmod u+x /usr/bin/presence_report

Copy with SCP

Use SCP to copy the presence_report script to /usr/bin/presence_report on the target device. Call chmod u+x /usr/bin/presence_report to allow script execution.

Add the script to rc.local

Place the following lines

nohup /usr/bin/presence_report event 192.168.1.2 >/dev/null 2>&1 &
nohup /usr/bin/presence_report lastseen 192.168.1.2 >/dev/null 2>&1 &

inside the /etc/rc.local file before the exit 0. You can to this via command-line or via LuCI in System -> Startup -> Local Startup. The script will be executed after reboot.

If you are running more than one OpenWRT Router and want to collect data from both, you can specify an own base topic for each with:

MQTT_BASETOPIC="owrtwifi2" nohup /usr/bin/presence_report event 192.168.1.2 >/dev/null 2>&1 &
MQTT_BASETOPIC="owrtwifi2" nohup /usr/bin/presence_report lastseen 192.168.1.2 >/dev/null 2>&1 &

ENV variables for configuration are:

  • MQTT_BASETOPIC: owrtwifi
  • MQTT_STATUS_TOPIC: $MQTT_BASETOPIC/status/mac-
  • MQTT_MAINTENANCE_TOPIC: $MQTT_BASETOPIC/maintenance
  • MQTT_USER
  • MQTT_PASSWORD
  • MQTT_ID: $MQTT_BASETOPIC

Usage

After installation the following topics will be published for each WiFi device, using the lowercase MAC address:

owrtwifi/status/mac-00-00-00-00-00-00/lastseen/iso8601

Payload contains the timestamp when the device was seen in an ISO 8601 (and OpenHAB) compatible format, like this: 2017-08-25T19:29:57+0200

owrtwifi/status/mac-00-00-00-00-00-00/lastseen/epoch

Unix epoch in seconds

owrtwifi/status/mac-00-00-00-00-00-00/event

Message will be new or del and is sent right after the device connected/disconnected to/from WiFi.

Additionally the DHCP name (if available) from /tmp/dhcp.leases will be published to

owrtwifi/status/mac-00-00-00-00-00-00/dhcp-name

Credits

Original idea and script from Jupiter "belikh" Belic. Suggestion to use iw event from Tom "tomdee" Denham, merging these two scripts into one for easier installation by afreof.
This script follows Oliver "owagner" Wagner's architectural proposal for an mqtt-smarthome.

owrtwifi2mqtt's People

Contributors

afreof avatar cirolaferrara avatar dersimn avatar zanattabruno avatar zuernc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

owrtwifi2mqtt's Issues

Opkg upgrade all package break something here

I am on openwrt 19.07.1 and just upgrade the packages with opkg.

But this brake the scripts ..I do not know which package do that.

I refreshed my router with 19.07.1 again and the script is working again.

No MQTT publications after upgrade to "presence_report"

After upgrading from "presence_event.sh" and "presence_lastseen.sh" to the latest script MQTT publication of MAC addresses stopped working.

Inserted my MQTT-Servers IP in "presence_report", did chmod x+u the script and changed rc.local according to readme.

Any idea what I'm missing? Do I need to make some other changes to the "presence_report" script?

Sorry to bother you again and thanks for your help!

OpenHAB rule not working after persistence reset

Due to current implementation of this line in wifi_detection.rules, persistence detection is not working under the following circumstances:

  • When Persistence of the items Person_X_Wifi_LastSeen_Seconds is not set to restore on startup and one or more people are not at home when OpenHAB is started
  • Persistence cache got wiped while one or more people were not at home.

Log will show the error: Error during the execution of rule Wifi Detection: java.lang.Number. This is because line throws an error if Person_X_Wifi_LastSeen_Seconds is undefined (for e.g. after a restart or wipe of persistence cache) the remaining rule functions are not executed and the wifi detection can't work properly.

Detection in sample rule false fully sets away status

When presence is detected via event channel, the LastSeen script might delete the status when timing is bad:

  1. Event fires, presence ON
  2. Cron rule triggers, calculates LastSeen >4mins turns presence OFF
  3. Router sends updated LastSeen message
  4. Cron rule triggers, calculates LastSeen <4mins turns presence ON

Subscription not working, please help troubleshooting

Sorry, very new to MQTT.

I followed your instructions and everything seems to have worked fine: "mosquitto_sub -t '$SYS/#' -v" shows traffic.

Simple MQTT-sub/pub-trials work in Shell and MQTTLens but I can't get neither to receive messages when I subscribe to "owrtwifi/status/mac-[Devices-MAC-address]/lastseen".

Any idea what I'm doing wrong? Do I need to make changes to "presence_event.sh" and "presence_lastseen.sh" besides setting my MQTT-Servers IP?

Thanks for your help!

rules syntax throws error in openhab log

I'm continuely getting an error in the openhab log.

2017-07-23 18:43:30.021 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule Wifi Detection: java.lang.Number

Ive copied the rules from code and everything seems to be working apart from the rules.

any ideas?
P.S I'm really impressed with presence idea you've created its the reason I researched openwrt and bought a router that was compatible.

iw not found

Hi, I try to run the script but I always get this:
line 16: iw: not found
where can I find this iw or maybe switch iw command to something else?? like arp??
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.