How to get free weather updates via SMS?

A python script to send weather updates or similar data via SMS using twilio.

14th Jul, 2015

The Driving Force View on Github

On a visit to my hometown, I came across an issue faced by my uncle. Farmers in India need weather updates in real time while government structures fail to do so. In a light mood, he asked me to find a solution to the problem, for instance I can call him to tell weather updates in case of an abrupt change.

Providing updates on call isn’t feasible for a man who sleeps at 3 am and wakes up late afternoon. But I found a workaround.

The Elucidation

A python script to send weather updates or similar data via SMS using twilio.

Requirements

  • Python: If you know programming and have no idea what python is, man! you need some books ‘on your face’. In short, it’s a high-level programming language. A Beginner’s Python Installing Tutorial

  • pip: It is a package management system used to install and manage software packages written in Python. We require this to install twilio.
    Installation:

    1
    $ sudo easy_install pip
  • Twilio: This will help us to send free SMS.
    Installation:

    1
    $ sudo pip install twilio

How do we do it?

  1. Get an account on twilio. Click here to create and account.
  2. Verify the phone number on twilio to recieve SMS.
  3. Use Open Weather Map API to retrieve live weather updates and forecast weather data. Note: This code retrieves the forecast of the next day at noon. You can change it in line number 8.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    while True:
    data = json.load(urllib2.urlopen('http://api.openweathermap.org/data/2.5/weather?q=Delhi,India'))
    forec = json.load(urllib2.urlopen('http://api.openweathermap.org/data/2.5/forecast?q=Delhi,India'))
    t = float(float(data['main']['temp']) - 272.15)
    fmt = "%d-%m-%Y, %H:%M:%S"
    now_time = datetime.now(timezone('Asia/Kolkata'))
    msg = 'Station: ' + data['name'] + ' ' + now_time.strftime(fmt) + ': Weather: ' +
    data['weather'][0]['description'] + ', Temp: ' + str(float(t)) + 'C'
    fmt = "%d"
    forec_date = '2015-07-' + str(int(now_time.strftime(fmt)) + 1) +' 12:00:00'
    for x in forec['list']:
    if(str(forec_date) in str(x['dt_txt'])):
    t = float(float(x['main']['temp']) - 272.15)
    msg += ' and ' + x['dt_txt'] + 'Weather:' + x['weather'][0]['description'] + ', Temp: ' + str(float(t)) + 'C'

    print msg
    print 'SMS ID: ', sendSMS(msg)
    print '------------------------------------------------------------------------'
    time.sleep(180)
  4. Function to send SMS. Note: You need to replace, ‘ACCOUNT_SID’, ‘AUTH_TOKEN’, ‘to’ and ‘from’ with twilio account id, twilio authentication token, the mobile number and your twilio number, respectively in the code below.
    1
    2
    3
    4
    5
    6
    def sendSMS(body):
    ACCOUNT_SID = "AC2a7f100XXXXXXXXXXXXXXXXXXXXXXXXX"
    AUTH_TOKEN = "77b9a6e4XXXXXXXXXXXXXXXXXXXXXXXX"
    client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
    mes = client.messages.create(to="+91XXXXXXXXXX", from_="+14XXXXXXXXX", body=body)
    print mes
  5. Save the code in a file as ‘**sms-weather.py**’ and run it in python.

Now grab a coffee or cold drink and get free weather updates at an interval of 3 minutes.