MQTT connected light switch with geofencing
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
assets
.gitignore
README.md
enocean.py
mqttplug.ini.sample
mqttplug.py
mqttplugd
nodered.flow
smartplug.py

README.md

The Butta La Pasta Light Switch

Script that controls the operation of a Wifi connected mains plug switch using MQTT messages.

Pasta

In Italy we love to prepare pasta for lunch. Pasta with tomato, ragu, aglio olio e peperoncino, pasta with pesto, with cacio e pepe, and of course risotto.

risotto

Teenagers here love to eat pasta at lunch. As soon as they come back from school they would like to have lunch.

The Problem

Pasta takes around 10 minutes to cook in boiling water. To be more precise, between 8 and 13 (please too early better than too late!). Spaghetti takes less, fusilli more, but pasta is really good if coming directly out of boiling water, mixed and served.

So how can I start cooking exactly a few minutes before they come and sit down for lunch? Of course an option would be to tell "hey, do you know what? Pasta will be ready in a quarter of an hour, please wait!" but we're engineers and we prefer to take the long ride.

The Solution

A light in my kitchen that lights up as soon as my son approaces home. Let's say when he's closer than 100mt. So I'm using:

  • 1 Raspberry board
  • 1 Wifi connected plug
  • Riso 1Kg
  • Salsiccia 1
  • Radicchio two... no sorry, this is for preparing risotto!

and managed to put everything together and prepared this nice example of an automation based on MQTT, Bluewind's Things messaging broker, the well done OwnTracks smartphone app, a simple NodeRED flow running on a AWS EC2 machine, a very simple Python script running on a Raspberry inside my house. As a load switch I'm using a Smart Plug model SP-1101W from Edimax but the same should apply to any other similar product that exposes an open API.

plug

I'm using a clever and clean Python gist found on Github, and of course the Eclipse Paho MQTT libraries.

So the starting point is the OwnTracks application installed on a smartphone; following clear indications, I configured OwnTracks so that it sends messages (events) to an MQTT broker: the Things messaging server is a good companion but for this simple task any other can do the job. I also created a geofencing region around my house, with a radius of 100mt. OwnTracks will send an "event" message to the broker at each entering / leaving the region.

owntracks

My son carries the smartphone and events are consumed first by the NodeRED flow, that in turn generates new simple MQTT messages consumed by the Python script.

nodered

Why should I pass through NodeRED and then Raspberry? Because I need to perform simple operations on the messages (like being sure I'm not switching on the light at night) that are better handled by NodeRED and then I also need to access the SmartPlug from inside my home LAN, I don't want to expose my light switch to the Internet. And of course, SmartPlugs and similar devices will grow here...

New feature: EnOcean temperature sensor

Well someone in my family started using greenhouses seriously during this winter and soon came the need for having temperature inside under control.

In our labs at Bluewind we're using a lot EnOcean technology for demos and product development; I decided to add temperature measurement connecting an EnOcean USB300 dongle to my Raspberry and one of their magic small STM330 RF boards powered by an enclosed solar cell inside the greenhouse.

The sensor just needs to be put under light and soon will start advertising ambient temperature every 20 minutes or so; after a few hours it will also be able to continue advertising for at least six days (!) in the dark. EnOcean sensors work at 433 / 868 MHz and in my experience can easily traverse a couple of walls and more than 10 meters in house.

I will not go into more details here: just have a look at my Python code for an example of how to speak to EnOcean, and have fun reading temperatures encoded in Json messages sent under a configured topic via MQTT.

Installation and usage

After having installed the SmartPlug following indications from Edimax, you will need to discover its IP address (with a sniffer, or by checking the DHCP requests) and then install and configure the simple Python script in your Raspberry.

## install prerequisites ##
# in Ubuntu/Debian
user@host:~$ sudo apt-get install python-pip
user@host:~$ pip install paho-mqtt
#
## clone this and configure for your targets from sample ##
user@host:~$ git clone <this repo> && cd mqtt-plug
user@host:~/mqtt-plug$ cp mqttplug.ini.sample mqttplug.ini && vi mqttplug.ini
#
## run  ##
user@host:~/mqtt-plug$ python ./mqttplug.py

Of course you will also want to configure this as a service running at startup, I'm not giving details here about this as several different strategies exist (init, supervisor, etc.).

You will also copy and run the provided NodeRED flow and adapt to your needs, connect to the same MQTT broker, add any other special automation for your light.

That's all: have a nice lunch!

Reference

Contact