This wiki has been archived and made read-only.
For up-to-date information about TkkrLab and it's projects please visit our main website at tkkrlab.nl.

Tweetswitch

From

Revision as of 23:32, 2 June 2013 by Zeno (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Aim:

To tweet a message on twitter if a switch is turned on of (in our case the space is open/closed).

Bonus : switch on/of external device, in our case our public access point in our space.

PS: We use now the DoorAccess system with extra functions to control the according twiiter/irc/etc.

Materials

  • Server where the software runs
    • Com port on server

For the bonus part

  • Any access point on 12V (if different change relais)
  • Eletronics parts
    • Serial D9 female
    • R1 = 1K resistor
    • T1 = BC547 Transistor
    • K1 = Relais 12V (depending on external device)
    • D1 = Diode 4101

Software


Steps

Build the hardware, for testing you can connect a switch on the comport pin 9 to 4 (closed) or 9 to 3 (open) like this:

Photo of simple version

IMG 0618.JPG


Schematic & photo of build of bonus part

Tweetswitch.png

Er zit een klein foutje in het schema, waar pin 3 van de COM-poort staat moet pin 5 zijn.

Tweetwitch electronics.JPG

Sofware

get code and compile setSerial


create script monserial


Serial monitor script - monserial.

#!/bin/sh
getstatus()
{
    oldstatus=$status
    status=`/root/setserial /dev/ttyS0 RI`
}

statuschange()
{
    if [ "$status" = 0 ]
#    if test "$status" == "0"
    then
        echo opened
        python tweet.py "We are open"
        curl "http://pagetoupdatestatus"
     else
        echo closed
        python tweet.py "We are closed"
        curl "http://pagetoupdatestatus"
     fi
}

getstatus
statuschange
while :
do
    getstatus
    if [ "$status" != "$oldstatus" ]
    then
        statuschange
    fi
    sleep 2
done

Do the steps of twitter API to get the autorisation codes. (4 codes in total)

get the python packages oauth2 and python-twitter

build and install these

copy/create the script below

Twitter Script - tweet.py

import twitter
import sys
import datetime

twit = twitter.Api(consumer_key='key_from_twitter',
consumer_secret='from_twitter', access_token_key='from_aouth_script', access_token_secret='from_aouth_script')

now = datetime.datetime.now()
timestamp = now.strftime("%d-%m-%Y %H:%M")

text = sys.argv[1] +' '+ timestamp

status = twit.PostUpdate(text)

You can now start the monserial script that wil tweet if the switch is used.

Good luck with building.

This project has been build by

Millibit for the bash script

Bigred for compiling SetSerial and all the hardware

Zeno for the twitter python script