Author Archives: Gary Sone

Bash profile specific ‘on login script’

Edit/Create file

nano ~/.bash_profile

Runs this example script every time you login – handy if you have many SD cards.

Example file contents

date
echo ''
echo ---------------------------------
echo '| RaspberryPI - Weather Station |'
echo ---------------------------------
echo 'Login: '$USER
echo ''
uptime
echo ''
who

Connecting to a Raspberry Pi via USB/TTL-Serial with Ubuntu Linux

HT – http://martin.leyrer.priv.at/y2013/m03/Connecting_to_a_Raspberry_Pi_via_USB_TTL-Serial_with_Ubuntu_Linux.html

ls -l /dev/ttyUSB0 #verify device is attached
sudo adduser [the user you want to add] dialout

REBOOT

sudo reboot now

CONNECT VIA TTL SESSION

screen /dev/ttyUSB0 115200

From Linux (Ubuntu) you can remote forward the x-windows GUI apps

ssh -YC user@ip

then run a gui app, ie: lxpanel, lxsession, wpa_gui

My #RaspberryPi #Python code for tweeting the MetOffice weather for #BrizeNorton #Stornoway #BurnhamOnSea & #Newquay

I have been developing this python code and I found a great way of sharing my code but not my API keys! I also made a Python ‘function’ so that new locations can simply be added by defining a name and the longitude and latitude coordinates (the Met office API does the hard work is finding the nearest weather station to these coordinates) and calling this function with the 3 parameters.

For updates see this repository – https://github.com/soneups/MetOffice

#!/usr/bin/env python
# Tweet some weather data!

import time
import sys
import metoffer
from twython import Twython

# Define location of api files - recorded in a file - avoids a GitHub slurp for API keys!
keys_file='/home/pi/piweather/metoffice/MetOffer-1.2/keys'
metkeys_file='/home/pi/piweather/metoffice/MetOffer-1.2/apikeysmetoffice.key'

# Read API keys from file
with open(keys_file) as f:
    CONSUMER_KEY = f.readline().strip("\n")
    CONSUMER_SECRET = f.readline().strip("\n")
    ACCESS_KEY = f.readline().strip("\n")
    ACCESS_SECRET = f.readline().strip("\n")

with open(metkeys_file) as f:
    met_api_key = f.readline().strip("\n")
	
# Setup API calls
api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)
met = metoffer.MetOffer(met_api_key)
 
# get local weather
def LookupObs( locationdescription, longitude, latitude ):
   weather_observations = met.nearest_loc_obs(float(longitude),float(latitude)) 
   weather_report = metoffer.parse_val(weather_observations)
   weather_data = weather_report.data[len(weather_report.data)-1]
   wind_info = weather_data['Wind Speed']
   wind_direction = weather_data['Wind Direction']
   mph=int(wind_info[0])
   kph=int(mph*1.609344)
   tmpf=int(weather_data['Temperature'][0])*9/5+32
   print (time.strftime("%d/%m/%Y"))+"@"+(time.strftime("%H:%M:%S")) + " - " +locationdescription+": "+str((weather_data['Temperature'][0]))+"c/"+str(tmpf)+"f, Wind speed: "+str(wind_info[0])+wind_info[1]+" "+wind_direction[0]
   api.update_status(status=str(time.strftime("%d/%m/%Y"))+"@"+str(time.strftime("%H:%M:%S")) + " - " +locationdescription+": "+str((weather_data['Temperature'][0]))+"c/"+str(tmpf)+"f, Wind speed: "+str(wind_info[0])+wind_info[1]+" "+wind_direction[0])
   return;

LookupObs( locationdescription="#BrizeNorton", longitude=51.7616, latitude=-1.5779 );   #Brize
LookupObs( locationdescription="#Stornoway", longitude=58.1011, latitude=-4.9899 );     #Stornoway
LookupObs( locationdescription="#Burnhamonsea", longitude=51.2430, latitude=-2.9920 );  #BOS
LookupObs( locationdescription="#Newquay", longitude=50.3995, latitude=-5.1379 );       #Newquay

And we are back!

Learnt the hard way with hosting providers, self hosted blogs and back ups (or the lack of them!) posts will be restored* and ‘normal’ service should resume! happy new year!

*from posts cached in the IOS WordPress app.