This is my own spin on the Arduino Networked Lamp. I live in California and maybe you've heard of the drought we are experiencing. Due to this drought, California has reigned in its water use and Brown has placed some restrictions on water use. I've recently moved into a house that has sprinklers going off for 10 minutes, 3 days a week. Sometimes I find this a bit excessive and recently, the sprinklers went off on a day where it rained, making those 10 minutes of watering a real waste. To cut down on water use, I wanted to build a machine that could communicate with the internet, find out if it will rain in some period of time and have a motor manually shut off the water accordingly. If it seems that it won't rain, then the motor should turn on water access again.
Naturally, I am using my beloved Arduino to receive information from the internet and translate the forecasted precipitation chances into a signal to send to a motor that will shut off or turn water back on. Right now I'm in the stages of finding the right tools and start making some skeleton code. There are several problems that need to be addressed:
For the first issue listed, I tried to use Processing code again like I did in the Arduino Networked Lamp tutorial. All was going well to set up communication between the Arduino and the computer however, I kept running into the same bug: the Processing program could not reach the right serial port for the Arduino, so that was a bust. Even after many modifications and lots of frustrations, this method was probably not the best to use. Another thing to keep in mind is that I also had make calls to some kind of weather feed off the internet and interpret that information efficiently.
After some google-ing, I came across a Python weather API I could use. According to Wikipedia an API, which stands for Application Programming Interface, is a set of routines, protocols, and tools for building software. It is a set of rules and functions that allow communications between different applications and make it easier for software engineers to develop new software. In this case the API I found enables me to request weather.com forecasts and current weather with a simple function call pywapi.get_weather_from_weather_com() from a library called pywapi imported into a python shell. With this library, I don't need to write every line of code that it takes to set up communications to the internet, request data from the servers at weather.com, and then display it in a format that is easy to code with.
Great! I found an easy way to get my forecasted precipitation for a particular upcoming week. But how can I get this weather API to work with the Arduino? After digging around some more, I found another great API floating out in the internet. This one is called the Python Arduino Command API (https://github.com/thearn/Python-Arduino-Command-API). This API allows you to communicate to a connected Arduino and allows a user to code sketches for the Arduino in python, with very similar function calls to the original Arduino language you would use in the Arduino IDE. This now lets me use the weather API for python earlier and translate it to a signal to send to the Arduino easily, all while using my terminal in a python shell.
I need to also make calls periodically and automatically to weather.com and get the forecasted precipitation. I want it to update at the same time everyday so I always know that the sensor is up to date. Scouring through stackoverflow, I found some snippet of code that takes advantage of some python modules such as threading and datetime.
Here is the really rough code I have written so far. I am starting to write some skeletons to some functions that I want to use to get my end result. I will be refining them and making a complete script soon. I will update and share how I progress in this project.
Naturally, I am using my beloved Arduino to receive information from the internet and translate the forecasted precipitation chances into a signal to send to a motor that will shut off or turn water back on. Right now I'm in the stages of finding the right tools and start making some skeleton code. There are several problems that need to be addressed:
- Need to find a way for Arduino to receive information from the internet periodically
- Need to figure out how to get the Arduino to update the forecasts every so often so that the measurements stay accurate
- Find a good way to extract the weather information and make it into a signal the motor can easily and accurately interpret and execute the action desired
For the first issue listed, I tried to use Processing code again like I did in the Arduino Networked Lamp tutorial. All was going well to set up communication between the Arduino and the computer however, I kept running into the same bug: the Processing program could not reach the right serial port for the Arduino, so that was a bust. Even after many modifications and lots of frustrations, this method was probably not the best to use. Another thing to keep in mind is that I also had make calls to some kind of weather feed off the internet and interpret that information efficiently.
After some google-ing, I came across a Python weather API I could use. According to Wikipedia an API, which stands for Application Programming Interface, is a set of routines, protocols, and tools for building software. It is a set of rules and functions that allow communications between different applications and make it easier for software engineers to develop new software. In this case the API I found enables me to request weather.com forecasts and current weather with a simple function call pywapi.get_weather_from_weather_com() from a library called pywapi imported into a python shell. With this library, I don't need to write every line of code that it takes to set up communications to the internet, request data from the servers at weather.com, and then display it in a format that is easy to code with.
Great! I found an easy way to get my forecasted precipitation for a particular upcoming week. But how can I get this weather API to work with the Arduino? After digging around some more, I found another great API floating out in the internet. This one is called the Python Arduino Command API (https://github.com/thearn/Python-Arduino-Command-API). This API allows you to communicate to a connected Arduino and allows a user to code sketches for the Arduino in python, with very similar function calls to the original Arduino language you would use in the Arduino IDE. This now lets me use the weather API for python earlier and translate it to a signal to send to the Arduino easily, all while using my terminal in a python shell.
I need to also make calls periodically and automatically to weather.com and get the forecasted precipitation. I want it to update at the same time everyday so I always know that the sensor is up to date. Scouring through stackoverflow, I found some snippet of code that takes advantage of some python modules such as threading and datetime.
Here is the really rough code I have written so far. I am starting to write some skeletons to some functions that I want to use to get my end result. I will be refining them and making a complete script soon. I will update and share how I progress in this project.
#sprinkler prototype code
#code to make script run once every day
from datetime import datetime
from threading import Timer
x=datetime.today()
y = (x + timedelta(days=1)).replace(hour=2, minute=0, second=0)
delta_t = y - x
secs=delta_t.seconds+1
def hello_world():
print "hello world"
#...
t = Timer(secs, hello_world)
t.start()
import pywapi
from Arduino import Arduino
#Will return a giant convoluted dictionary of info
weather_com_results = pywapi.get_weather_from_weather_com()
def will_it_rain_forecast(weather_com_results):
list = []
#weather.com only forecasts five days in advance
for i in range(0,5):
list.append(weather_com_results["forecasts"][i]["day"]["chance_precip"])
#storing values in a list to use later
return list
#This will check if at any point in the forecasted chance of precipitation there
#is a day that it will rain. It there is, it will change the message of the
#indicator
def rain_check(list):
indicator = 'it will not rain'
for i in list:
if int(i)>0:
indicator = 'it will rain'
return indicator
#Before you water, make sure rain is not predicted in the next few days
#Might need to make this less general and calculate exactly what day is
#predicted to rain, and shut off sprinkler a couple days before that day
#also, after it rains want to make sure sprinkler stays shut for 48 hours
#after rainfall.