Brad Buskey
Published © GPL3+

Sun and Moon Info

Returns the Sunrise/Sunset and Moon phase info for the current GPS location.

BeginnerShowcase (no instructions)15 minutes1,171
Sun and Moon Info

Things used in this project

Hardware components

Omega2 Plus
Onion Corporation Omega2 Plus
×1
Expansion Dock
Onion Corporation Expansion Dock
×1
OLED Expansion
Onion Corporation OLED Expansion
×1
Onion Corporation GPS Expansion
×1

Story

Read more

Code

Initial Setup

Plain text
There is a few things you will need to do in order for this to all work. First and foremost, you will need to have a Weather Underground API Key in order to utilize this script. It is free for the casual developer and can be obtained from the following location: https://www.wunderground.com/weather/api
opkg update
opkg install python3 python-light python-urllib3 pyOledExp pyOmegaExpansion oled-exp ogps

chmod +x getastro.sh

getastro.sh

SH
This script calls the GPS chip to get the current location, writes that to a file (just like the GetGPS script) and then calls the showastro.py script that does the meat of the work. Do not forget to change this file to be an executable file with the "chmod +x astro.sh" command.
#!/bin/ash

# Get the GPS information and write it to a file.
ubus -S call gps info > /root/gpsastro.txt

# Call the python script to write to the OLED.
python /root/showastro.py

# Exit the script.
exit 0

showastro.py

Python
This script will read GPS info from the chip and using the Lat/Lon and pull the weather data from the nearest weather station and display the requested information on the OLED screen. My own tests have been successful.
#!/usr/bin/env python

#=====================================================
# Pull in the sun and moon infomation from Weather
# Underground based on the current GPS Location and
# display the results on the OLED screen.
#
# Written by: Brad Buskey
# Contact me: deckyon@gmail.com
#=====================================================

# Initialize required libraries
from OmegaExpansion import oledExp
import urllib2
import json
import os

# Initialize and clear the OLED expansion module
oledExp.setVerbosity(0)
oledExp.setTextColumns()
oledExp.driverInit()

# Use your API key here.  Get it from https://www.wunderground.com/weather/api
apikey = ""

#Read in the gps information from the file generated by the shell script.
with file('/root/gpsastro.txt', 'r') as gpsfile:
	gpsinfo = json.load(gpsfile)

# Check for the existence of the GPS signal.
if gpsinfo.has_key('signal'):
	# If the GPS chip could not get a signal, then display that and quit.
	oledExp.setCursor(3, 0)
	oledExp.write("No GPS signal found")
	oledExp.setCursor(5, 0)
	oledExp.write("Please try later!")
	exit()
elif gpsinfo.has_key('latitude'):
	# If the location information exists, grab the required lat and lon for the script to continue.
	lat=gpsinfo['latitude']
	lon=gpsinfo['longitude']

# Pull in the data from the Weather Underground website
GetURL = "http://api.wunderground.com/api/"+apikey+"/astronomy/q/"+lat+","+lon+".json"
astrodata = urllib2.urlopen(GetURL)
astrodict = astrodata.read()
astroinfo = json.loads(astrodict)

# Pull in the information from the URL to put together the output
srh = astroinfo['moon_phase']['sunrise']['hour']
srm = astroinfo['moon_phase']['sunrise']['minute']
ssh = astroinfo['moon_phase']['sunset']['hour']
ssm = astroinfo['moon_phase']['sunset']['minute']
ill = astroinfo['moon_phase']['percentIlluminated']
pha = astroinfo['moon_phase']['phaseofMoon']
hem = astroinfo['moon_phase']['hemisphere']

sunrise = "Sunrise    : "+srh+":"+srm
sunset = "Sunset     : "+ssh+":"+ssm
moonlite = "Moonlight  : "+ill+" %"
hemisphere = "Hemisphere : "+hem

# Start writing the data to the OLED display.
oledExp.setCursor(0, 0)
oledExp.write("=====================")
oledExp.setCursor(1, 0)
oledExp.write("= Sun and Moon Info =")
oledExp.setCursor(2, 0)
oledExp.write("=====================")
oledExp.setCursor(3, 0)
oledExp.write(sunrise)
oledExp.setCursor(4, 0)
oledExp.write(sunset)
oledExp.setCursor(5, 0)
oledExp.write(moonlite)
oledExp.setCursor(6, 0)
oledExp.write(hemisphere)
oledExp.setCursor(7, 0)
oledExp.write(pha)

# Exit the script cleanly.
exit()

astroinfo.txt

Plain text
This is an example of what should be pulled down from Weather Underground.
{
  "response": {
  "version":"0.1",
  "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
  "features": {
  "astronomy": 1
  }
	}
		,	"moon_phase": {
		"percentIlluminated":"10",
		"ageOfMoon":"3",
		"phaseofMoon":"Waxing Crescent",
		"hemisphere":"North",
		"current_time": {
		"hour":"21",
		"minute":"20"
		},
		"sunrise": {
		"hour":"7",
		"minute":"49"
		},
		"sunset": {
		"hour":"18",
		"minute":"03"
		},
		"moonrise": {
		"hour":"9",
		"minute":"24"
		},
		"moonset": {
		"hour":"21",
		"minute":"03"
		}
	},
	"sun_phase": {
		"sunrise": {
		"hour":"7",
		"minute":"49"
		},
		"sunset": {
		"hour":"18",
		"minute":"03"
		}
	}
}

gpsastro.txt

Plain text
This is the file created by the Shell script and read by the Python script. This is just an example of when there is a good GPS signal and it can get a lock and return the location.
{"age":6357,"latitude":"38.1234","longitude":"-80.1234","elevation":"1234.5","course":"12.34","speed":"N"}

gpsastro.txt (No Signal)

Plain text
This is the output if there is no GPS signal. This is the final piece for me to do for this project to work completely. The script will check for this instance of the file, as well as an empty gpsastro.txt
{"signal":"false"}

Credits

Brad Buskey

Brad Buskey

6 projects • 14 followers
Just getting started.. Currently have the Onion Omega 2+, Raspberry Pi 3 B +, Raspberry Pi Zero W. Working in Python.

Comments