Introduction
Since abandoning the Notify My Android setup, I decided to investigate a few more openHAB items before my first order shows up. I worked with the twitter action, which obviously allows openHAB to tweet for you. I created a second twitter account for my openHAB system so it wouldn't get confusing. I followed the guide at https://github.com/openhab/openhab/wiki/Twitter-Action and modified the instructions to accommodate working with the Pi.
Working with the Twitter Action
You’ve got to time this one correctly or it won’t work. Go through all the steps before you start because the entire process cannot take longer than 5 minutes. Upon starting, openHAB will create a URL that is use to bind or authenticate your twitter account to openHAB. This URL process expires in 5 minutes.
Step 1 - Modify openhab.cfg
Open openhab.cfg and modify the section for twitter action (don't forget to remove the #'s):
########################### Twitter Action configuration ############################## # # The ConsumerKey, ConsumerSecret combination (optional, defaults to official Twitter-App # Key-Secret-Combination) twitter:key=IPqVDkyMvhblm7pobBMYw twitter:secret=2HatstDfLbz236WCXyf8lKCk985HdaK5zbXFrcJ2BM # Flag to enable/disable the Twitter client (optional, defaults to 'false') twitter:enabled=true
I used the key and secret above because otherwise it didn't work. I tried leaving them blank like the wiki suggested but that always resulted in a failed authentication. I found the key and secret in openHAB's Twitter.java:
/** the configured ConsumerKey (optional, defaults to the official Twitter-App key 'IPqVDkyMvhblm7pobBMYw') */ private static String consumerKey = "IPqVDkyMvhblm7pobBMYw"; /** the configured ConsumerSecret (optional, defaults to official Twitter-App secret '2HatstDfLbz236WCXyf8lKCk985HdaK5zbXFrcJ2BM') */ private static String consumerSecret = "2HatstDfLbz236WCXyf8lKCk985HdaK5zbXFrcJ2BM";
Step 2 - Authenticate openHAB for Twitter
Start openHAB:
pi@raspberrypi ~/openHAB $ ./start.sh
This is where it gets tricky. If you aren't echoing your openHAB messages to the terminal, you will miss this message:
21:13:50.197 INFO o.o.a.t.i.TwitterActionService[:123]- ################################################################################################ 21:13:50.213 INFO o.o.a.t.i.TwitterActionService[:124]- # Twitter-Integration: U S E R I N T E R A C T I O N R E Q U I R E D !! 21:13:50.220 INFO o.o.a.t.i.TwitterActionService[:125]- # 1. Open URL 'https://api.twitter.com/oauth/authorize?oauth_token=5ojKAILHvaxoHVN3bdqqbNfnaTeeAJbK' 21:13:50.252 INFO o.o.a.t.i.TwitterActionService[:126]- # 2. Grant openHAB access to your Twitter account 21:13:50.256 INFO o.o.a.t.i.TwitterActionService[:127]- # 3. Create an empty file 'twitter.pin' in your openHAB home directory 21:13:50.264 INFO o.o.a.t.i.TwitterActionService[:128]- # 4. Add the line 'pin=<authpin>' to the twitter.pin file 21:13:50.275 INFO o.o.a.t.i.TwitterActionService[:129]- # 5. openHAB will automatically detect the file and complete the authentication process 21:13:50.302 INFO o.o.a.t.i.TwitterActionService[:130]- # NOTE: You will only have 5 mins before openHAB gives up waiting for the pin!!! 21:13:50.316 INFO o.o.a.t.i.TwitterActionService[:131]- ################################################################################################
Your openHAB startup should PAUSE at this point. It will wait 5 minutes for you to complete the authentication. If it goes beyond this point the timer has stopped and you need to try again.
If you are not watching the messages, wait a minute and open the openhab.log file in the logs directory of the openHAB install directory. You should see the banner printed above in the log. Copy the link from your log and load it in a web browser. You will be taken to a twitter/openHAB authentication page where you need to login with your twitter credentials. Once you do that, you will see something like the following:
The blacked out portion will show an authentication number.
Step 3 - Set Authentication Pin
To set the pin, open a second terminal window, navigate to the openHAB install directory and type the following:
pi@raspberrypi ~/openHAB $ nano twitter.pin
This will open the nano text editor with a blank screen. Type:
auth=<your pin>
where <your pin> is the number from the website above. After you've done that, hit CTRL-X and say yes to the saving. Then just hit enter to confirm the file name (twitter.pin). If you've completed everything correctly and in the 5 minute window, twitter will authenticate:
21:46:31.492 INFO o.o.a.t.i.TwitterActionService[:107]- TwitterAction has been successfully authenticated > awaiting your Tweets!
I wrote a simple rule to test this:
rule "Rocker Pressed" when Item Rocker1A changed from OFF to ON then sendTweet('Rocker 1A ON') end
And, the result was:
Comments:
This sounds easy, but it's hard to get it all done in 5 minutes on the Raspberry Pi because it's relatively slow. How I eventually did it was by streamlining the process.
1. Do the nano twitter.pin part and have a terminal window waiting for you to enter the authentication pin. Don't save the file until after you put in the pin.
2. Have a browser open and waiting to accept a URL. Also, know your account login credentials.
3. Have a second terminal window for starting/stopping openHAB.
4. Have openHAB outputting its information directly to the terminal window.
Good Luck!
Top Comments