DEV Community

Lars Wächter
Lars Wächter

Posted on

Sending daily live data to your phone automatically


Today I'd like to show you an excerpt of a small RaspberryPi / Python project I worked on recently.

The main idea behind this was to send daily push notifications with live data to my phone. For example: Weather or traffic information.

So I thought an easy and efficient way to accomplish this was by writing a small python programm that was executed on a Raspberry Pi.

To do that repeatedly I created a cronjob on my Pi that triggers the python programm at a certain time daily.

Now I show you the basic structure of the python programm. In this case we are sending live weather information to our phone. Let's start!

Importing the required modules:

from pushbullet import Pushbullet
import requests
Enter fullscreen mode Exit fullscreen mode

We use the python Pushbullet module for sending the notitifactions to our phone and the requests module for making HTTP requests.

Creating the Weather Class:

The next step is to create a class for the weather data. Here we deal with the following weather information:

  • Location
  • Min Temperature
  • Max Temperature
  • Summary

For each one of these we create a property in our Weather class.
Moreover, we create a init method to set the properties and one to return all these information in a string.

Surely you can add more data later.

class Weather:
    location = ""
    tempMin  = ""
    tempMax = ""
    summary = ""

    def __init__(self,location,tempMin,tempMax,summary):
        self.location = location
        self.tempMin = tempMin
        self.tempMax = tempMax
        self.summary = summary

    def getWeatherString(self):
        return "Today's weather in " + self.location + ": \nTemperature: " + self.tempMin + " - " + self.tempMax + u'\u2103' + "\nSummary: " + self.summary
Enter fullscreen mode Exit fullscreen mode

Receiving the weather data:

For receiving the weather data we call the Yahoo Weather API. The usage is for free and we don't need an API key.

For detailed API information take a look at the Docu.

Next, we create a function that sends the HTTP request and deals with the data.

def getWeather(woeid):
    apiUrl = ""
    apiQuery = {"q": "select location,item from weather.forecast where u='c' AND woeid=" + woeid}

    res = requests.get(apiUrl, params=apiQuery).json()

    location = res['query']['results']['channel']['location']['city']
    weatherToday = res['query']['results']['channel']['item']['forecast'][0]

    tempMin = weatherToday['low']
    tempMax = weatherToday['high']
    summary = weatherToday['text']

    weather = Weather(location,tempMin,tempMax,summary)

    return weather
Enter fullscreen mode Exit fullscreen mode

The procedure of this function should be obvious. Firstly we set the API Url and the query string for selecting the data we'd like to have. Our "woeid" Parameter is used to indicate the location for the weather forecast as a WOEID.

The API returns many information. In our case we just filter the essentials.

The "weatherToday" variable includes today's weather from our json data. If you want to get tomorrow's weather easily change the array index to 1.

Check the docu (link above) to find out the WOEID of your target city and the more.

Sending the data to your phone:

Now we know how to get our data. The next step is to send it to our phone. As I said already above we'll do this with Pushbullet.

Let's create a new function that sends our data to the target device.

def pushMessage(device,msg):
    pushTitle = "Weather Update"
    push = device.push_note(pushTitle, msg)
    print("Message was sent!")
Enter fullscreen mode Exit fullscreen mode

Our function takes two arguments. "device" is our target device and "msg" is our data / message. "push_note" is a method from the pushbullet module. It sends the data to the target device.

Last steps:

We are almost done. There are just few more things to do. :)

What's left to do:

  • Set our WOEID that identifies our target city for the weather
  • Set our Pushbullet API Key
  • Set our Pushbullet target device
  • Call our functions we created above
# Your Pushbullet API Key
pb = Pushbullet("your_api_key")

# Target Device Name
device = pb.get_device('your_device_name')

# Yahoo WOEID for location identification
woeid = "your_woeid"

msg = getWeather(woeid).getWeatherString()
Enter fullscreen mode Exit fullscreen mode

Just ask Google how to get your own Pushbullet API key. As you can see we call our "getWeather" function with the corresponding WOEID and get the output string with "getWeatherString". Finally, we call the "pushMessage" function with our target device and message.

To find out your device name you can do this:

Enter fullscreen mode Exit fullscreen mode

That's it, we are done! Every time we execute our programm we get a pushbullet notification to our phone with live weather data. Of course, you can expand your programm to get more data. For example:

  • Traffic data
  • Tweets
  • News

and much more.

If you're using a Raspberry Pi you might want to add a cronjob that executes the programm for example from every monday to friday at 7 am.

I also created a GitHub Repo including the source code from here.
Check it out: MorningPi

Top comments (4)

lukaszkuczynski profile image

Hello! Great job, I really like the idea of using PushBullet, what are other "push" options for mobile devices?
You can also check out my project of using RPi to parse RSS (later on indexed on Elastic) on my github repo

diegolago profile image
Diego Lago

What is the pip/pip3 package I have to install to import Pushbullet class?


larswaechter profile image
Lars Wächter

You have to install the Pushbullet package via pip: pip install

More infos here:

diegolago profile image
Diego Lago

Thanks very much!