Skip to content

Instantly share code, notes, and snippets.

@Elwell
Created May 22, 2015 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Elwell/8cc09d26cb1a0ece3a1a to your computer and use it in GitHub Desktop.
Save Elwell/8cc09d26cb1a0ece3a1a to your computer and use it in GitHub Desktop.
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