Skip to content

Python script for Raspberry Pi that uses web Sunrise and Sunset times to turn on/off a relay controlled light or any other GPIO device

Notifications You must be signed in to change notification settings

teepeepro/raspi-sun-switch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Raspi-Sun-Switch

Python script for Raspberry Pi that uses web Sunrise and Sunset times to turn on/off a relay controlled light or any other GPIO device, making a nifty sun-driven IoT device.

Using this script, you can change the settings on the GPIO pins of a raspberry pi (on or off), based on the times of the sunset or sunrise anywhere in the world.

Before you get started, make sure you have the following installed on your Raspberry Pi or in the case of a weather API, set up an account. All instructions are written more or less for Raspian, or Debian based OSes.

  • Python 3 for overall code (Note- should be installed on Rasbian OS, but use sudo apt-get install python3 to check or instal)
  • A free weather API - in this case, OpenWeatherMap.com
  • Easy instructions on how to obtain OpenWeatherMap.com API code.

  • and the Rpi.GPIO python library (Note- It is installed by default on Rasbian OS) to install, simple run the following on the command line
    sudo apt-get install python-dev python-rpi.gpio
  • Manipulating Script for your needs

    This section describes how to change parts of the script to modify for your needs

    • First, take a look at the Get_Sun_Times function, and change the city code (look up city code here) and place your API key in the apikey variable. def get_sun_times(): city = "3451190" #Default is Rio de Janeiro, BR apikey = "000000"
    • Next, make sure your GPIO pins are correctly assigned. Since this is set up as an array, you can put multiple pins here, for multiple relays and lights that you wish to control # init list with pin numbers, add for more relays pinList = [22]
    • Finally, you can adjust how long after sunset or sunrise the lamp or relay turns on and off, respectively. The value is in seconds, so adjust accordingly, or leave the defaults as is. #Define your times to wait after and before Sunset and Sunrise waitTimeAfterSunset = 1800 waitTimeBeforeSunrise = 19800
    • The rest of the code is free to manipulate based on your needs and should be pretty readable with basic python or programming knowledge. Have fun!

About

Python script for Raspberry Pi that uses web Sunrise and Sunset times to turn on/off a relay controlled light or any other GPIO device

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages