I have some 433mHz sockets around the house. The newer models the range is good working across adjacent floors and through walls. The older models normally only work in the same room as the tranmitter, unless a modified antena made of copper wire is used to increase the effective range of the transmitter.
I've got Kodi on all the TV around the house and TV Headend for blended FreeView / FreeSAT / Networked DVR / Automated download server and link the sockets to the AV room amp and speakers was simple: key mappings in Kodi to run a shell script, shell scripts to wget the 8266 web server connected to a 433mHz transmitter.
My first version of the code had the 433mHz transmitter hardcoded to control specific sockets but given the range of the newer sockets I think a generic web server on the ESP8266 than just gets given codes to broadcast via a web API might be simpler as
- All the ESP8266 units linked to 433mHz controllers can run the same code.
- A single shell script can call the transmitter anywhere in the house. Useful for commands to turn on lighting etc
- All the net sockets have unique codes factory set so this makes them individually addressable even when there is a power failure.
- The ESP8266 servers will be dumb, no time, no time zone, no daylight saving/summer time no sunset or sunrise. That can call be run on an existing ARM-based server running on an Orange Pi
How to get sunset and sunrise? Sure we could invoke a web service or cloud offering but I'd like something simple and robust. Linux server can tell the time and handle seasonal time shifts perfectly so we just need some help to get sunset and sunrise. There are many options one of them is a python module called Astal: http://astral.readthedocs.io/en/latest/index.html
On Armbian, I logged in and...
First checked that my timezone was set up correctly: sudo dpkg-reconfigure tzdata
Then I installed the packages for Python
root@orangepione:~# pip install astral
Collecting astral
Downloading astral-1.4-py2.py3-none-any.whl
Collecting pytz (from astral)
Downloading pytz-2016.10-py2.py3-none-any.whl (483kB)
Installing collected packages: pytz, astral
Successfully installed astral-1.4 pytz-2016.10
root@orangepione:~# pip install tzlocal
Collecting tzlocal
Downloading tzlocal-1.3.tar.gz
Requirement already satisfied: pytz in /usr/local/lib/python2.7/dist-packages (from tzlocal)
Installing collected packages: tzlocal
Running setup.py install for tzlocal ... done
Successfully installed tzlocal-1.3
To test everything works, create a file with the code below
sudo nano sun.py
import datetime
from astral import Astral
import time
from datetime import datetime
import pytz # $ pip install pytz
from tzlocal import get_localzone # $ pip install tzlocal
# get local timezone
local_tz = get_localzone()
# test it
# utc_now, now = datetime.utcnow(), datetime.now()
ts = time.time()
utc_now, now = datetime.utcfromtimestamp(ts), datetime.fromtimestamp(ts)
local_now = utc_now.replace(tzinfo=pytz.utc).astimezone(local_tz) # utc -> local
assert local_now.replace(tzinfo=None) == now
print (local_now)
print (utc_now)
city_name = 'London'
a = Astral()
a.solar_depression = 'civil'
city = a[city_name]
print('Information for %s/%s\n' % (city_name, city.region))
#Information for London/England
timezone = city.timezone
print('Timezone: %s' % timezone)
#Timezone: Europe/London
print('Latitude: %.02f; Longitude: %.02f\n' % (city.latitude, city.longitude))
#Latitude: 51.60; Longitude: 0.08
sun = city.sun(date=local_now, local=True)
print('Dawn: %s' % str(sun['dawn']))
print('Sunrise: %s' % str(sun['sunrise']))
print('Noon: %s' % str(sun['noon']))
print('Sunset: %s' % str(sun['sunset']))
print('Dusk: %s' % str(sun['dusk']))
sudo python sun.py
The output should be correct for your locale / timezone on the first line the second is UTC and then locale information followed by sun set / sun rise calculations.
2017-03-28 01:51:14.448518+01:00
2017-03-28 00:51:14.448518
Information for London/England
Timezone: Europe/London
Latitude: 51.50; Longitude: -0.12
Dawn: 2017-03-28 06:11:23+01:00
Sunrise: 2017-03-28 06:45:03+01:00
Noon: 2017-03-28 13:05:34+01:00
Sunset: 2017-03-28 19:26:05+01:00
Dusk: 2017-03-28 19:59:44+01:00
For later work, it will be useful to have a lightweight messaging server so we can install that now, so we have all the parts ready to set up some basic home automation.
For example, I currently have time switches that turn the lights on and off but these need to be manually adjusted for summer/winter as well as changes in the local timezone twice a year.
I want to replace the time switches with RF plugs. Control the plugs via an ESP8266 and while I could use NTP and code the logic on the ESP I think a better approach would be to drive the ESP8266 via web service / MQTT as all the new RF sockets are uniquely coded I think payload for the message could just be the plug message to power on or power off the socket.