Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Sunrise / Sunset calculation
andrew@mythic:~$ cat bin/
# calculate sunrise/sunset times for location
# Andrew Elwell <> 2013-09-02
import ephem
import ConfigParser
import paho.mqtt.client as mqtt
config = "/home/andrew/solarmon.cfg"
settings = ConfigParser.RawConfigParser()
home = ephem.Observer() = settings.get('general', 'lat')
home.lon = settings.get('general', 'lon')
#print, 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.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
lat = 51.50081
lon = -0.14258
broker =
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment