Amateur weather station with Twitter bot

We invested in a personal/amateur weather station. This is a result of my kid’s increasing interest in weather and wanting to push data to sites like Weather Underground. We decided to purchase the Ambient Weather WS-2902B WiFi Smart Weather Station which has several sensors including wind direction, wind speed, rainfall, solar radiation, and humidity. It communicates over 900MHz to the base station which can connect to WiFi and push data to other sites.

Configuration was quick and easy. We are now pushing data to:

For some additional fun, I decided to create a Twitter bot that runs on Amazon Web Services Lambda. It periodically queries the Ambient Weather API to fetch my weather station’s data. It then tweets the data on @WanamassaW.

Some useful Python libraries for creating the Twitter bot:

  • Tweepy – lets you interact directly with the Twitter API
  • Ambient API – lets you query the current weather data through Ambient Weather

Here’s a quick prototype of using the libraries:

import tweepy
import datetime
from ambient_api.ambientapi import AmbientAPI
import time

consumer_key = '***'
consumer_secret = '***'

access_token = '***
access_token_secret = '***'

api = 0
ambient_api = 0
data = 0

def degToCompass(num):
    val=int((num/22.5)+.5)
    arr=["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"]
    return(arr[(val % 16)])

def get_current():
    current = "Current weather:\n"
    current += 'Temperature: ' + str(data['tempf']) + 'F\n'
    current += 'Feels like : ' + str(data['feelsLike']) + 'F\n'
    current += 'Humdity: ' + str(data['humidity']) + '\n'
    current += 'Wind direction: ' + degToCompass(data['winddir']) + '\n'
    current += 'Wind speed: ' + str(data['windspeedmph']) + ' MPH\n'

    return current


def publictweet():
    if datetime.date.today().weekday() == 0:
        tweettopublish = 'Hi everyone, today is Monday.   #Monday '
    if datetime.date.today().weekday() == 1:
        tweettopublish = 'Enjoy your Tuesday.  #Tuesday'
    if datetime.date.today().weekday() == 2:
        tweettopublish = 'Third week of the Week. #Wednesday'
    if datetime.date.today().weekday() == 3:
        tweettopublish = 'Thursday. I cannot wait for the Weekend'
    if datetime.date.today().weekday() == 4:
        tweettopublish = 'Friday...Finally'
    if datetime.date.today().weekday() == 5:
        tweettopublish = 'Great it is Saturday #weekend #Saturday'
    if datetime.date.today().weekday() == 6:
        tweettopublish = 'Sunday...#Weekend #enjoy '
    tweettopublish += '\n' + get_current()
    api.update_status(tweettopublish)
    print(tweettopublish)

def lambda_handler(event, context):
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    global api
    api = tweepy.API(auth)
    
    global ambient_api
    ambient_api = AmbientAPI()
    devices = ambient_api.get_devices()
    device = devices[0]
    time.sleep(1)
    
    global data
    data = device.get_data()[0]
    
    publictweet()
    return {
        "":""
    }

Leave a Reply