Raspberry Pi Project 6: Temperature Twitter Weather Station

This project takes regular temperature measurements and Tweets them via your Raspberry Pi

This project uses the Python plugin Tweepy to integrate your Raspberry Pi with Twitter's API to automatically tweet the temperature every 30 minutes.

You will need a Twitter account for this project. If you do not already have one, you can create one at https://www.twitter.com Your Twitter account will also need to be linked to your mobile phone before you can access the API.

The project uses the same circuit that we made in tutorial 6:

Raspberry Pi Project 6: Temperature Twitter Weather Station

Open IDLE on your Raspberry Pi (Menu > Programming > Python 2 (IDLE)) and open a new project (File > New File).  Then type the following:

import tweepy, time, sys
import RPi.GPIO as GPIO
import time
from array import *
import datetime

waittime=1800 #interval time in seconds
SensorPin=17
TriggerPin=27
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
cap=0.000001
adj=2.130620985
location="Nottingham" 

CONSUMER_KEY = 'XXXX'#keep the quotes, replace this with your consumer key
CONSUMER_SECRET = 'XXXXXX'#keep the quotes, replace this with your consumer secret key
ACCESS_TOKEN = 'XXXXX'#keep the quotes, replace this with your access token
ACCESS_TOKEN_SECRET = 'XXXXX'#keep the quotes, replace this with your access token secret

def Tweet(s):
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    api = tweepy.API(auth)
    api.update_status(s)

def measureresistance(mpin,tpin):
    GPIO.setup(mpin, GPIO.OUT)
    GPIO.setup(tpin, GPIO.OUT)
    GPIO.output(mpin, False)
    GPIO.output(tpin, False)
    time.sleep(0.2)
    GPIO.setup(mpin, GPIO.IN)
    time.sleep(0.2)
    GPIO.output(tpin, True)
    starttime=time.time()
    endtime=time.time()
    while (GPIO.input(mpin) == GPIO.LOW):
        endtime=time.time()
    return endtime-starttime


def restotemp(rest):
    temp=[0]*151
    res=[0]*151
    temp[0]=-40
    temp[1]=-39
    temp[2]=-38
    temp[3]=-37
    temp[4]=-36
    temp[5]=-35
    temp[6]=-34
    temp[7]=-33
    temp[8]=-32
    temp[9]=-31
    temp[10]=-30
    temp[11]=-29
    temp[12]=-28
    temp[13]=-27
    temp[14]=-26
    temp[15]=-25
    temp[16]=-24
    temp[17]=-23
    temp[18]=-22
    temp[19]=-21
    temp[20]=-20
    temp[21]=-19
    temp[22]=-18
    temp[23]=-17
    temp[24]=-16
    temp[25]=-15
    temp[26]=-14
    temp[27]=-13
    temp[28]=-12
    temp[29]=-11
    temp[30]=-10
    temp[31]=-9
    temp[32]=-8
    temp[33]=-7
    temp[34]=-6
    temp[35]=-5
    temp[36]=-4
    temp[37]=-3
    temp[38]=-2
    temp[39]=-1
    temp[40]=0
    temp[41]=1
    temp[42]=2
    temp[43]=3
    temp[44]=4
    temp[45]=5
    temp[46]=6
    temp[47]=7
    temp[48]=8
    temp[49]=9
    temp[50]=10
    temp[51]=11
    temp[52]=12
    temp[53]=13
    temp[54]=14
    temp[55]=15
    temp[56]=16
    temp[57]=17
    temp[58]=18
    temp[59]=19
    temp[60]=20
    temp[61]=21
    temp[62]=22
    temp[63]=23
    temp[64]=24
    temp[65]=25
    temp[66]=26
    temp[67]=27
    temp[68]=28
    temp[69]=29
    temp[70]=30
    temp[71]=31
    temp[72]=32
    temp[73]=33
    temp[74]=34
    temp[75]=35
    temp[76]=36
    temp[77]=37
    temp[78]=38
    temp[79]=39
    temp[80]=40
    temp[81]=41
    temp[82]=42
    temp[83]=43
    temp[84]=44
    temp[85]=45
    temp[86]=46
    temp[87]=47
    temp[88]=48
    temp[89]=49
    temp[90]=50
    temp[91]=51
    temp[92]=52
    temp[93]=53
    temp[94]=54
    temp[95]=55
    temp[96]=56
    temp[97]=57
    temp[98]=58
    temp[99]=59
    temp[100]=60
    temp[101]=61
    temp[102]=62
    temp[103]=63
    temp[104]=64
    temp[105]=65
    temp[106]=66
    temp[107]=67
    temp[108]=68
    temp[109]=69
    temp[110]=70
    temp[111]=71
    temp[112]=72
    temp[113]=73
    temp[114]=74
    temp[115]=75
    temp[116]=76
    temp[117]=77
    temp[118]=78
    temp[119]=79
    temp[120]=80
    temp[121]=81
    temp[122]=82
    temp[123]=83
    temp[124]=84
    temp[125]=85
    temp[126]=86
    temp[127]=87
    temp[128]=88
    temp[129]=89
    temp[130]=90
    temp[131]=91
    temp[132]=92
    temp[133]=93
    temp[134]=94
    temp[135]=95
    temp[136]=96
    temp[137]=97
    temp[138]=98
    temp[139]=99
    temp[140]=100
    temp[141]=101
    temp[142]=102
    temp[143]=103
    temp[144]=104
    temp[145]=105
    temp[146]=106
    temp[147]=107
    temp[148]=108
    temp[149]=109
    temp[150]=110
    res[0]=190556
    res[1]=183413
    res[2]=175674
    res[3]=167646
    res[4]=159564
    res[5]=151597
    res[6]=143862
    res[7]=136436
    res[8]=129364

    res[9]=122667
    res[10]=116351
    res[11]=110409
    res[12]=104827
    res[13]=99584
    res[14]=94660
    res[15]=90032
    res[16]=85677
    res[17]=81574
    res[18]=77703
    res[19]=74044
    res[20]=70581
    res[21]=67298
    res[22]=64183
    res[23]=61223
    res[24]=58408
    res[25]=55728
    res[26]=53176
    res[27]=50745
    res[28]=48429
    res[29]=46222
    res[30]=44120
    res[31]=42118
    res[32]=40212
    res[33]=38398
    res[34]=36674
    res[35]=35036
    res[36]=33480
    res[37]=32003
    res[38]=30602
    res[39]=29275
    res[40]=28017
    res[41]=26825
    res[42]=25697
    res[43]=24629
    res[44]=23617
    res[45]=22659
    res[46]=21752
    res[47]=20891
    res[48]=20074
    res[49]=19298
    res[50]=18560
    res[51]=18481
    res[52]=18148
    res[53]=17631
    res[54]=16991
    res[55]=16279
    res[56]=15535
    res[57]=14786
    res[58]=14055
    res[59]=13353
    res[60]=12690
    res[61]=12068
    res[62]=11490
    res[63]=10953
    res[64]=10458
    res[65]=10000
    res[66]=9576
    res[67]=9183
    res[68]=8818
    res[69]=8478
    res[70]=8160
    res[71]=7860
    res[72]=7578
    res[73]=7310
    res[74]=7056
    res[75]=6813
    res[76]=6580
    res[77]=6357
    res[78]=6141
    res[79]=5934
    res[80]=5734
    res[81]=5540
    res[82]=5353
    res[83]=5172
    res[84]=4997
    res[85]=4828
    res[86]=4665
    res[87]=4507
    res[88]=4354
    res[89]=4207
    res[90]=4065
    res[91]=3927
    res[92]=3793
    res[93]=3663
    res[94]=3537
    res[95]=3414
    res[96]=3293
    res[97]=3175
    res[98]=3057
    res[99]=2941
    res[100]=2825
    res[101]=2776
    res[102]=2717
    res[103]=2652
    res[104]=2581
    res[105]=2507
    res[106]=2431
    res[107]=2355
    res[108]=2280
    res[109]=2206
    res[110]=2135
    res[111]=2066
    res[112]=2000
    res[113]=1937
    res[114]=1878
    res[115]=1822
    res[116]=1769
    res[117]=1719
    res[118]=1672
    res[119]=1628
    res[120]=1586
    res[121]=1545
    res[122]=1507
    res[123]=1470
    res[124]=1435
    res[125]=1400
    res[126]=1366
    res[127]=1333
    res[128]=1300
    res[129]=1268
    res[130]=1236
    res[131]=1203
    res[132]=1171
    res[133]=1139
    res[134]=1106
    res[135]=1074
    res[136]=1042
    res[137]=1010
    res[138]=978
    res[139]=948
    res[140]=918
    res[141]=888
    res[142]=861
    res[143]=834
    res[144]=809
    res[145]=787
    res[146]=766
    res[147]=748
    res[148]=733
    res[149]=721
    res[150]=713
    op=0
    for ii in range(0,150):
        if rest

Save your project as tweettemp.py (File > Save As) in your Documents folder.

Creating a Twitter App

Sign in to Twitter and then go to https://apps.twitter.com

Click the "Create New App" button.

Provide a unique name for your application (this can be anything e.g. "Pi Temperature 3165a"), a brief description (e.g. "This app will tweet the temperature") and your website address (If you don't have one, just add http://www.craftykits.net).

Tick to confirm that you agree to the Developer Agreement and then click "Create App".

Since the app name needs to be unique, this may take a few attempts.

On the next screen, go to the "Keys and Access Tokens" tab and find the Consumer Key and Consumer Secret (These will be a jumble of letters and numbers). Copy these keys to lines 16 and 17 respectively between the two quotation marks.

scroll to the bottom of the page. Click the "Create my access token button". Twitter will then provide you with an Access Token and an Access Token Secret. Copy these to lines 18 and 19 respectively between the two quotation marks.

Change the location on line 14 to your city, town or village.

Now open Terminal (Menu > Accessories > Terminal) and type the following command:

python tweettemp.py