This project show you how to make a weather bot, using Python to get real-time weather data. For IoT purpose, we use DFrobot Lattepanda and its Windows 10 OS, but since it is python, you can run this project on other operation system like MAC OSX and any distribution of Linux. Please follow steps below to finish this project.
Dark Sky weather APIRegister a new Dark Sky API account and get a weather API key. For free use, the first 1,000 forecasts per day are free, then $0.0001 per forecast.
Dark Sky is an easy-to-use weather API. According to its home page, it has features below:
- Hourly and daily forecasts for the next week.
- Minute-by-minute “hyperlocal” rain forecasts.
- Governmental severe weather alerts.
- Observations going back decades.
- Dozens of languages and measurement units.
- Easy to understand documentation.
Please download Python 2.7 from https://www.python.org/downloads/windows/, and do not forget to add Python path(C:\Python27) to your system variable.
Press ”win+R” and input “cmd” to launch it. As for Windows 10, we recommend you to use powershell.
Install python moduleInstall following python module using command below. There are four of them:
- SpeechRecognition (for recognize audio input from microphone)
- pyaudio (play speech audio file)
- python-forecastio (weather api)
- gtts (Google text to speech engine)
pip install SpeechRecognition pyaudio python-forecastio gtts
Download project source codeOpen project GitHub from https://github.com/YuanYouYuan/weather-bot, and click [Clone or download] at top-right corner, then click Download ZIP. Please unzip the downloaded file.
For MAC/Linux user or Windows user who had already installed Git tool, just use Git tool to clone this project to your computer.
Please download MPV player from https://mpv.srsfckn.biz/. You can use another favorite audio player, but must modify the python code at STEP 6.
Unzip and clone/move the mpv.exe to our unzipped project folder.
(7z file archiver: http://www.developershome.com/7-zip/)
Run projectPlease open project folder and execute weather-test.py in Python IDLE, or simply open cmd and input:
python weather-test.py
(MAC/ Linux user please use the same command in your terminal).
Please modify api_key in your python code to the api key you got from https://darksky.net/dev/.
# This version is for windows, and there must be a mpv application in this work directory
import forecastio #import weather module
from gtts import gTTS #import google tts engine
import subprocessapi_key = 'Enter your api key' #your api key
# the following coordinate is Taipei/Taiwan
lat = 25.0391667 #set latitude and longitude interested
lng = 121.525
lang = 'EN' #language,
file_name = 'weather.mp3'
player = './mpv' #set mpv as audio player
forecast = forecastio.load_forecast(api_key, lat, lng)
by_hour = forecast.hourly()
for data in by_hour.data:
#get current time and temperature
text = 'now is' + str(data.time) + 'the current temperature is' + str(data.temperature) + 'degree celsius'
print(text)
tts = gTTS(text, lang)
tts.save(file_name)
subprocess.call([player, file_name])
#start a subprocess to play audio file
YouTubeExtensionPlease check GitHub of forecast.io, which has detailed documentation and applications. For example, you can get current temperature, weather of today, weather of specified location and rainfall probability. Just try it and have fun.
Comments