Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sunrise / Sunset calculation
andrew@mythic:~$ cat bin/get_sunrise.py
#!/usr/bin/python
# calculate sunrise/sunset times for location
# Andrew Elwell <Andrew.Elwell@gmail.com> 2013-09-02
import ephem
import ConfigParser
import paho.mqtt.client as mqtt
config = "/home/andrew/solarmon.cfg"
settings = ConfigParser.RawConfigParser()
settings.read(config)
home = ephem.Observer()
home.lat = settings.get('general', 'lat')
home.lon = settings.get('general', 'lon')
#print home.lat, home.lon
sunrise = ephem.localtime(home.next_rising(ephem.Sun()))
sunset = ephem.localtime(home.next_setting(ephem.Sun()))
#print "sunrise: %s, sunset: %s" % (sunrise, sunset)
# lets publish to broker
broker = settings.get('mosquitto', 'broker')
client = mqtt.Client()
client.connect(broker)
client.publish("sunrise", sunrise.strftime("%Y-%m-%d %H:%M"), qos=0, retain=True)
client.publish("sunset", sunset.strftime("%Y-%m-%d %H:%M"), qos=0, retain=True)
; config file for home
[general]
lat = 51.50081
lon = -0.14258
[mosquitto]
broker = 192.0.2.10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.