Sunday, June 6, 2021

Building a Tweeting Weather Station - Hooking Things up to Twitter with Python

 

The Twitter account is live!


Last week I put together a fairly simple weather station that gathered the temperature, humidity, and barometric pressure and stored the data that the station was gathering into a text file.

The ultimate goal in all of this was to have the data from the weather station end up being fed to Twitter so that I can see what the weather conditions like at home from almost anywhere. 

To do this required doing a little bit of magic involving using some Python code to create a bot to feed my data and setting up an API on a Twitter account to allow Twitter to talk to the bot. 

To be honest, I am not very familiar with Python and I would be in a very bad way if you asked to me write a Python program from scratch. But thankfully Python has a very robust user community and I find that reading pre-written Python code to be very easy to do. So while I am not a Python programmer, I should be able to pretty easily change anything that's pre-existing to do what I wanted it to do. 

But before I started looking into code to send my weather data, I need to get some API codes from Twitter that I can plug into the program. 

To do this I needed to first set up a Twitter developer account. 

First I created a new Twitter account called North Perth Weather and logged into Twitter as that account.

To turn my account into a developer account I then clicked on the Settings and Privacy tab for my Twitter account and clicked on the additional resources tab. 

This in turn opened up another tab that had "Developers" as an option, which I clicked on that in order to turn my normal Twitter account into a developer one. 

To request a Developer account, I needed to fill in a form on what I planned to do with my developer account and submitted it for approval. Within a day I got an email back from Twitter that my account was good to go. 

Once I got my approval, I went to developer.twitter.com, navigated to Project & Apps Overview, and clicked on the button labeled "Create a Standalone App" 

There I created a new app called Atwood Weather, made sure that the App Permissions were set to Read, Write, and Direct Messages, and clicked complete. 

Setting up the App on Twitter

Next, I clicked on the Keys and Tokens tab and copied down the API Key, API Secret Key, Access Token, and the Access Token Secret key that was displayed. 

This bit of information will be critical for my bot to communicate with Twitter. 

With my Twitter account now set up and with now having the means to communicate with Twitter, the next thing to do was to set up a bot to send my weather to Twitter. 

The first thing I needed to do was to actually install Python onto my PC in order to run the programs.

To do this I went to www.python.org/downloads, downloaded and installed the most recent version of Python onto my computer.

As I mentioned earlier, I found that Python had a very robust user community, and what I actually had found was that it was very much a community that treated building applications like Lego blocks - you can put together a bunch of Python applications together to make an application to meet your purposes. 

Doing some searching around I found an application called tweet.py which I found at github.com/langphil/Twitter-Bot

This application exactly fitted my needs perfectly. This program reads and tweets a line of a text file and sets a counter to tell the program to read the next line of the text file when the application runs again. 

This is perfect since my Putty log outputs weather data line by line in the log file, so the fact that the Tweet program can read line by line is critical. 

In keeping with the Lego brick concept, this application needs to have another python application, called Tweepy (code for this is found at github.com/tweepy/tweepy) which is a common code library for doing Twitter API calls in Python. 

Since it was the foundation for the Tweet program, I first downloaded and installed Tweepy as instructed by the instructions found on the Github site. 

Once that was done, I then downloaded and installed the Tweet.py application into the same folder that I had installed Tweepy. 

Next, I went back to my Putty settings for the Arduino connection and I changed the location for the weather.txt log file to also be in the same folder as Tweepy.

With all the software components now installed the final step that was needed was to connect the weather.txt file to Twitter by using the Tweet.py program. 

To make that happen I needed to make a couple of small updates to the Tweet.py program. Pulling up the tweet.py file in a text editor, I saw the following:

import tweepy, time, sys, os
CONSUMER_KEY = 'key'
CONSUMER_SECRET = 'secret'
ACCESS_KEY = 'key'
ACCESS_SECRET = 'secret'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
filename=open('weather.txt','r')
text=filename.readlines()
filename.close()

As per instructions, I filled out the Consumer and Access keys and secrets that I had gotten earlier from my Twitter account. 

To tell Tweet.py to look at the weather.txt file, I set the filename parm to open weather.txt. 

After that, I save the file and I ran the program by typing tweet.py at my computer's command line. 

A black box quickly opened and closed on my desktop, so I wasn't able to determine if it worked or not, but a quick peek at my Twitter feed confirmed that my weather data did come across - Success!

Now that I can now tweet my weather data, the last step was to automate the feed. To do this I used the Windows Scheduler to set up a schedule where the Tweet.py program would be executed every 6 hours. I did this by setting up 4 daily scheduled tasks (for 12 am, 12pm, 6am, and 6pm) to execute tweet.py. 

Setting up the schedule

Setting up the schedule with 4 daily tasks

Setting up the schedule

I now have my weather data going to my Twitter account for all to see. So far it's been working without any issues to date. 

The only wrinkle now is that the sensors are sitting inside my house. While it is nice to know that my house isn't too hot or cold, it isn't really really useful. 

The next step will be to put the sensors outside, where the real weather is. 

No comments:

Post a Comment