Sending daily live data to your phone automatically
Tags: python, raspberrypi, tutorial
Published: 2017-07-30 16:02:11

---
title: Sending daily live data to your phone automatically
published: true
description: Daily push messages including live data for your phone
tags: python,raspberrypi, tutorial
---
###Introduction
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:
```python
from pushbullet import Pushbullet
import requests
```

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.

```python

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
```


###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](https://developer.yahoo.com/weather/documentation.html).


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

```python
def getWeather(woeid):
    apiUrl = "https://query.yahooapis.com/v1/public/yql?format=json&"
    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
```
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.

```python
def pushMessage(device,msg):
    pushTitle = "Weather Update"
    push = device.push_note(pushTitle, msg)
    print("Message was sent!")
```
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

```python
# 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()
pushMessage(device,msg)
```

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:

```python
print(pb.devices)
```

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](https://github.com/Lars124/MorningPi)