In my last post, I introduced Dweet.io and explained how to dweet from your raspberry pi. in this post, I'm going one step ahead and wiring together my Enocean-Py library with dweepy and start dweeting my Enocean sensor data.
Let's start dweeting
The whole idea is this - my raspberry pi, my first python script will first decode a received radio telegram with Enocean-Py and extract the rocker switch state. Then it will dweet this information to the public. On my desktop, I'll be running a second python script which will display as and when a new new dweet is posted by my raspberry pi. This demo utilizes the real time streaming capability of dweepy library and also showcases Enocean-Py library. As a word of caution, Enocean-Py is still under development and I recommend you always to update Enocean-Py before using it.( Details as in this post [FMN#03] : Decoding Enocean Protocol with Enocean Py )
Script for Raspberry pi
I'm assuming that you have already installed dweepy as explained in this post. First create a folder for this demo in your raspberry pi. Say this is '/home/pi/dweet_demo'. Now enter this directory and clone Enocean-Py repository by
git clone https://github.com/v-i-s-h/Enocean-Py.git
Now create a file named 'demo_sender.py' and contents as below :
# demo_sender - a demo for using dweepy and enocean-py for dweeting enocean data. import sys import time import dweepy sys.path.insert( 0, "./Enocean-Py/src/" ); import EO import ESP ''' Function : main Description : main function Arguments : none Returns : none ''' def main(): print "\t\t*************************************************" print "\t\t** Forget Me Not 2014 - Dweeting demo **" print "\t\t** by vish **" print "\t\t*************************************************" ttyPort = "/dev/ttyAMA0" print "\tUsing serial port : " + ttyPort + '\n' hEOGateway = EO.connect( ttyPort ) # better to wait a little for connection to establish time.sleep( 0.100 ) # this is your thing ID, change accordingly thingId = "fmn2014_vish_raspberrypi" # this hash table holds our dweet message myDweet = {} try: while( True ): rawResp = EO.receiveData( hEOGateway ) if rawResp: pkts = ESP.decodeRawResponse( rawResp ) for pkt in pkts: myDweet = {} # clear old data myDweet['sender'] = 'enocean_py demo' # currently no response radio decoding is implemented in Enocean Py # I'm just checking whether the radio is from my rocker switch RORG = F6 # and then blindly checks for the bits set. if pkt['data_recv'][0] == 0xF6: # is this my rocker switch # assuming we got a radio telegram from rocker switch if pkt['data_recv'][1] == 0x70: print "Channel B : OFF ", myDweet['Channel B'] = 'OFF' if pkt['data_recv'][1] == 0x50: print "Channel B : ON ", myDweet['Channel B'] = 'ON' if pkt['data_recv'][1] == 0x30: print "Channel A : OFF ", myDweet['Channel A'] = 'OFF' if pkt['data_recv'][1] == 0x10: print "Channel A : ON ", myDweet['Channel A'] = 'ON' if pkt['data_recv'][1] == 0x00: print "Rocker Released.", myDweet['Rocker0'] = 'Released' dweepy.dweet_for( thingId, myDweet ); print "......dweeted" except KeyboardInterrupt: print "\nExiting Demo" EO.disconnect( hEOGateway ) print "Bye..bye.. :) " if __name__ == "__main__": main()
Once you are done, you can save and close the file. Remember to change the 'thingId' to your thing's id.
Desktop Viewer
Now we'll create a desktop viewer application for viewing the dweets send by out beloved raspberry pi. What we will do is we'll use the realtime streaming capability of dweepy library and create a python script which will decode and show the dweet from raspberry pi.
Off course you need to install dweepy in your PC also.
Now create a new file name 'dweet_viewer.py' and copy paste these contents to the file.
# dweet_viewer.py - a desktop application to view your thing's dweets import json import dweepy def main(): # replace with your thing Id thingId = "fmn2014_vish_raspberrypi" print "\t\t*************************************************" print "\t\t** Forget Me Not 2014 - Dweet Viewer **" print "\t\t** by vish **" print "\t\t*************************************************" for dweetJSON in dweepy.listen_for_dweets_from( thingId ): if type(dweetJSON) is int: continue dweet = json.loads( dweetJSON ) timeStamp = dweet['created'] content = dweet['content'] for items in content.keys(): print "At " + timeStamp + " \"" + items + "\" updated to \"" + content[items] + "\""; if __name__ == "__main__": main()
Save and close the file.
Running the demo
To run the demo, first start your 'dweet_viewer.py' script in your PC and then starting 'demo_sender.py' in your raspberry pi. Now press and release your demo switch and see what happens at both PC and raspberry pi.
Here is a video of demo running.
Happy hacking,
vish