Robodia Technology
Published © GPL3+

Raspberry Pi as a tweeting weather station!

Get all the weather updates of your room in your Twitter feeds anywhere across the globe.

IntermediateFull instructions provided5 hours2,141
Raspberry Pi as a tweeting weather station!

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Adafruit MCP3008
×1
Light dependent resistor
×1
LM35
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian
Python

Story

Read more

Schematics

Breadboard Diagram

This is a breadboard schematic to assemble your circuit with all the components and Raspberry Pi.

Schematic Diagram

Screenshot

Screenshot -2

Code

Untitled file

Python
import spidev
import time
from twython import Twython

DEBUG = 1

spi = spidev.SpiDev()
spi.open(0,0)

# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum):
        if ((adcnum > 7) or (adcnum < 0)):
                return -1
	r = spi.xfer2([1,(8+adcnum)<<4,0])
	adcout = ((r[1]&3) << 8) + r[2]
	return adcout

# LDR  connected to Channel - 1 of the ADC Chip MCP3008
# LM35 connected to Channel - 2 of the ADC Chip MCP3008
LDR  = 1
LM35 = 2

i = 0

# Twitter App Credentials.
# This is My Twitter App Credentials, You need to write your Own Twitter App's Credentials.
APP_KEY='6OMHjSfJ1iDCMGxCL'
APP_SECRET='duAp9NPDCUtpYTj1d5RYcXFxRkj1oanQP9WEQ4W8p4iG'
OAUTH_TOKEN='53864802693121-Il2nAh2yWASot7DJDvDQUBBfu5E'
OAUTH_TOKEN_SECRET='NKOzrVsvhQcVz5q9iBwovwCwFXbj31LDAjYEFJ'

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

#Special Character Degree SIgn for Temperature
deg = u'\N{DEGREE SIGN}'

while i < 1:
#while 1:
        
        # read the analog pin               
        LDR_Value = readadc(LDR)
        time.sleep(0.5)
        Light = ( LDR_Value / 10 )
        
        Temp_Value = readadc(LM35)
        volts = (Temp_Value * 3.3) / 1024
        Temp = volts / (10.0 / 1000)        

        #Get Current System Date and Time
        DateTime = time.strftime('%m/%d/%Y %H:%M:%S')
        
        if LDR_Value >= 1000:
                Light = 100
        
        if DEBUG:
                print "------------------------------------------------------------"
                print 'It is ' + str(DateTime) + '.'                
                print "Light_Value :- "
                print "%4d/10 => %3d " % (LDR_Value, Light) + '%'
                print "Temp Value:- "
                print "%4d/1023 => %5.3f V => %4.1f " % (Temp_Value, volts, Temp ) + deg + 'C.'
                print "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
				                
        if Temp > 50:
			print "sending to twitter.........................................."
			Temp = '{:.1f}'.format(Temp)
			MSG = '#EMERGENCY!Fire!Your House at #Bhopal_INDIA got Fire!' + 'The Temp is ' + str(Temp) + deg + 'C.' +\
                      'Immediately Call #FireBrigade: 101' + ' #TweetsFromRaspberryPi.'
		
        elif Temp <= 50:
			print "sending to twitter.........................................."
			Temp = '{:.1f}'.format(Temp)
			MSG = '#Bhopal_INDIA\'s #Weather_bot here! It is ' + DateTime + '.' + \
                      'Your House is getting ' + str(Light) + '% #SUN_LIGHT,' + \
                      'and #Temp is ' + str(Temp) + deg + 'C.' +\
                      '#TweetsFromRaspberryPi'	

        #twitter.update_status(status=MSG)
        print "Weather Info Sent to Twitter!"		
	    #Sent information only one time.
        i = i + 1
        print "Delay for 10 Sec"
        # do nothing for 10 Sec
	    time.sleep(10)

Credits

Robodia Technology

Robodia Technology

3 projects • 69 followers
At its core, Robodia Technology Solutions is an electronics lab focusing on various exciting new technologies benefitting from the thriving open source hardware

Comments