Cheap tweeting RFID home security/automation system V1.5



@labosdata

CONCEPT
One of the first things we bought when we founded our hackerspace was a cheep RFID access control system. The original plan was just to connect it to an electric lock on the door and access the space whithout a traditional key. We soon realised that the stuff in our space is not cheap enough to relay on a cheap electromagnetic lock and moved on to other projects. A year later we stumbeled upon the RFID reader and started thinking what can it be usefull for, and EUREKA it was the solution for keeping tabs on enyone not authorized by us that sometimes enters the space with a spare key.
And so we made the door tweet and email us when it’s locked and unlocked with and without rfid authentication. And well, this is a hackerspace, so why stop there?
This is the list of all current uses in ver.1.5
Post lock state on twitter.
Post room temperature on state change on twitter.
Email us on lock state change
Turn on Alarm sound and blink the light violently in the room on unauthorized entering.
Automaticly playing/stoping music playlists in mpd on authorized open/close state.
Automaticly turning lights in the room on/off on open/close state.
Shortly,when a virtual and/or physical trigger is activated it can execute any program/command on any computer it can access via ssh.

Ver.2.0 for the new version we plan on adding the following functions:
Custom tweet/e-mail/playlist for every user(card).
Party switch: Custom: playlist/volume/RGB-led lights profile/tweet party mode.
Adding new triggers/sensors/input devices.
sustav1

PARTS
In the ver.1.5 we used:

HARDWARE
1x Arduino Duemilanove
1x tl-wr703n router
1x AD2000-M RFID access control
1x Always on pc (not necessary for just tweeting/e-mail)
5x 433Mhz controlled RC switches
1x USB-UART TTL adapter
1x Dallas one wire temperature sensor
1x Powered USB 2.0 hub
SOFTWARE:
Homebrew arduino code
OpenWRT on the router
open.sen.se web service, for loging and posting data online
Debian on PC (but you can use any linux)
MPD & MPC for playing music from PC.
Mplayer for the siren sound.
Dropbox for captured intrusions video sharing on twitter
ssh for secure connection between router and pc.

RFID
sustav2
We used the cheap AD2000-M card codes can also be read from pin 16 on the microcontroller(not yet implimented).
The pins we used are the first four connectors as follows:

+12V dc
GND & jumper to next pin & Arduino GND
Jumper(GND)
Wire to Arduino pin A1

ARDUINO
sustav3

The pins on Arduino are connected as follows:
A0 -RFID, pin 4
A1 -microswitch in the lock
GND -RFID GND, micro-switch GND, 433 transmitter GND pin
5V -433 transmitter Vcc pin
D4 -433 transmitter data pin

YOU NEED TO EDIT THE ARDUINO OUTPUT SO THAT IT CORESPONDS TO YOUR FEED NUMBERS REGISTRED AT open.sen.se

ARDUINO CODE

#include
#define PartyPin A2
#define lock A0
#define sens A1
#define close 0
#define open 1
RCSwitch mySwitch = RCSwitch();
String dataString=””;
boolean flagNeovlasten=1;
boolean state = close;
boolean flagSerialSent=0, partySerialSent;
boolean lockState, partyState;
boolean ALARM=0;
boolean PARTY=0;

void setup() {
Serial.begin(115200);
Serial.println(“Starting…\n”);
/mySwitch.enableTransmit(4);
pinMode(PartyPin, INPUT_PULLUP);
pinMode(lock, INPUT_PULLUP);
pinMode(sens, INPUT_PULLUP);
pinMode(13, OUTPUT);

lockState = digitalRead(lock);
partyState = digitalRead(PartyPin);
}
void loop(){
oldloop();

boolean val1 = digitalRead(lock);
delay(5);
boolean val12 = digitalRead(lock);
if (val1 == val12) {
if (val1 != lockState) {
if (val1 == LOW && digitalRead(sens)==close) {
state = !state;
flagSerialSent=0;
}
}
lockState = val1;
}

if(digitalRead(sens) == open && state == close && flagNeovlasten == 1){
Serial.println(44776);
flagNeovlasten=0;
flagSerialSent=0;
ALARM=1;
}
if(digitalRead(sens) == open && state == open && flagSerialSent == 0){
Serial.println(44777);
flagSerialSent=1;
ALARM=0;
}
if(digitalRead(sens) == close && state == close && flagSerialSent == 0){
Serial.println(44778);
OFF();
flagSerialSent=1;
flagNeovlasten=1;
ALARM=0;
}
if(digitalRead(PartyPin)==0 && state == open && partySerialSent == 0){
Serial.println(44779);
partySerialSent=1;
}

if(digitalRead(PartyPin)==1 && state == open && partySerialSent == 1){
Serial.println(44775);
partySerialSent=0;
}

if(ALARM){
mySwitch.send(5592337,24);
delay(100);
mySwitch.send(5592340,24);
delay(100);
}

}

void serialEvent(){
dataString=””;
while(Serial.available() > 0){
char c = Serial.read();
dataString += c;
delay(10);
}
if(dataString.startsWith(“ON”)) ON();
else if(dataString.startsWith(“OFF”)) OFF();
else if(dataString.startsWith(“ALARM1”)) ALARM=1;
else if(dataString.startsWith(“ALARM0”)) ALARM=0;
else if(dataString.startsWith(“UNLOCK”)) state=open;
else if(dataString.startsWith(“LOCK”)) state=close;
else if(dataString.startsWith(“A”))mySwitch.send(5588305,24);
else if(dataString.startsWith(“a”))mySwitch.send(5588308,24);
else if(dataString.startsWith(“B”))mySwitch.send(5591377,24);
else if(dataString.startsWith(“b”))mySwitch.send(5591380,24);
else if(dataString.startsWith(“C”))mySwitch.send(5592145,24);
else if(dataString.startsWith(“c”))mySwitch.send(5592148,24);
else if(dataString.startsWith(“D”))mySwitch.send(5592337,24);
else if(dataString.startsWith(“d”))mySwitch.send(5592340,24);
else if(dataString.startsWith(“E”))mySwitch.send(1394001,24);
else if(dataString.startsWith(“e”))mySwitch.send(1394004,24);
}
void ON(){
mySwitch.send(5588305,24);
mySwitch.send(5591377,24);
mySwitch.send(5592145,24);
mySwitch.send(5592337,24);
mySwitch.send(1394001,24);
}
void OFF(){
mySwitch.send(5588308,24);
mySwitch.send(5591380,24);
mySwitch.send(5592148,24);
mySwitch.send(5592340,24);
mySwitch.send(1394004,24);
}

TPLINK TLWR703N & OPENWRT

Don’t forget to assign static ip addresses to the router and any pc you want to control.

First thing you need to do is to install usb basic support and then add an extroot to your device, and install usb video support , mjpg-streamer. This will turn your webcam into a cheap IPcam.

Next you need to install drivers for the Arduino with

opkg install kmod-usb-serial-ftdi

And if you are using Duemilanove or Arduino Uno

opkg install kmod-usb-acm

If you are using PL2303 based USB/serial adapters

opkg install kmod-usb-serial-pl2303

If you are using CP201x based USB/serial adapters

opkg install kmod-usb-serial-cp210x

Insert your Arduino and verify that its recognised with

dmesg | grep -i usb

you should get a response that looks like FTDI USB Serial now attached to /dev/ttyUSB0, and if it is you can try it by opening serial communication:

cat /dev/ttyUSB0 &

and send a command manually with:

echo -n “ON” > /dev/ttyUSB0

That should turn all of your 433 RC switches on.

Repeate check for your USB-UART TTL adapter with your dalas one wire sensor, it should be on /dev/ttyUSB1 or something similar.

Add aliases for the two devices so that they don’t mix up on reboot.

Edit /etc/rc.local so that it opens two way communication with the Arduino on every reboot:

vi /etc/rc.local

Press i for insert and add:

cat /dev/arduino > /tmp/serial.txt &
exit 0

You also need to enable ssh public key communication between the router and the always on pc, or any other pc via dropbear so the script can send ssh commands.

Save and exit with esc , ZZ.

Next we are going to make our service named secsysd, so open secsysd with:

vi /etc/init.d/secsysd

Enter i for insert,
Review the script, enter custom ip address feeds,apykay & paths.Paste it and save it with esc, ZZ.

secsysd:

#!/bin/sh /etc/rc.common

START=91

start() {

#serial.txt is writen from rc local
# >>> /etc/rc.local cat /dev/arduino > /tmp/serial.txt
#variable 1: tail -n 2 /tmp/serial.txt | cut -f2 > /tmp/row.txt & sleep 2

#open.sen.se
apikey= #your apikey

#FEEDS, enter yours:
OPEN=44777
CLOSED=44778
INTRUDER=44776

SERVERIP= #your always on pc local ip address
ROUTERIP= #This routers static ip goes here
#replece feeds with yours from open.sen.se
VARO=”EMPTY”

while [ 1 ]
do

TXTSIZE=$(ls -al /tmp/serial.txt | cut -f 5 -d ‘ ‘) #(limiter 5 polje je varijabla)
LIMIT=100000

VARN=$(tail -n 2 /tmp/serial.txt | cut -f2)

if [ “$VARN” != “$VARO” ]
then
TEMP=$(digitemp_DS9097 -a -q -o”%.2C”)
echo $VARN
curl ‘http://api.sen.se/events/?sense_key=’$apikey -X POST -H “Content-type: application/json” -d ‘[{“feed_id”: ‘$VARN’,”value”: ‘$TEMP’}]’

if [ “$VARN” -eq “$INTRUDER”]

then echo INTRUDER
ssh -f root@$SERVERIP -i /etc/dropbear/dropbear_rsa_host_key “sh -c ‘nohup wget -O Dropbox/video.mjpg http://$ROUTERIP:8080/?action=stream > /dev/null 2>1 &'”
ssh -f root@$SERVERIP -i /etc/dropbear/dropbear_rsa_host_key “sh -c ‘nohup mplayer /home/labos/intruder.mp3 > /dev/null 2>1 &'”
fi

if [ “$VARN” -eq “$CLOSED” ]
then
echo CLOSED
ssh -f root@$SERVERIP -i /etc/dropbear/dropbear_rsa_host_key “sh -c ‘nohup killall mplayer & nohup mpc stop > /dev/null 2>1 &'”
ssh -f root@$SERVERIP -i /etc/dropbear/dropbear_rsa_host_key “sh -c ‘nohup killall wget && nohup ffmpeg -framerate 5 -i Dropbox/video.mjpg Dropbox/video/upad_$(date +”%d.%m.%H:%M:%S”).mp4 > /dev/null 2>1 &'”
fi

if [ “$VARN” -eq “$OPEN” ]
then
echo OPEN
echo -n “ON” > /dev/arduino
ssh -f root@$SERVERIP -i /etc/dropbear/dropbear_rsa_host_key “sh -c ‘nohup killall wget & nohup killall mplayer > /dev/null 2>1 &'”

ssh -f root@$SERVERIP -i /etc/dropbear/dropbear_rsa_host_key “sh -c ‘nohup mpc play > /dev/null 2>1 &'”
fi

fi

if [ “$TXTSIZE” -gt “$LIMIT” ]
then
rm /tmp/serial.txt
fi

VARO=$VARN

done
}

Enable and start the service with:

/etc/init.d/secsysd enable && /etc/init.d/secsysd start

OPEN.SEN.SE
sustav 5
If you ask open.sen.se for an invite , they will send it to you in a day or two.

login
click:

Channels>add channel>Arduino & other…>Get started>
>Name your device:open (you’ll need two more devices closed, unauthorized)
> Device is: Arduino based
>Description: lock-open-feed

>Sending data: http posting
>Save and continue

>Name this feed: Door open
>Which way does your feed go: Input
>What type of data will be flowing in this Feed: Other…
>Save feed
>Finish
Note and save the Input feed number. For exsample mine now is: #52060

Then click:
Applications>Messengers & Notifiers and install Tweet it
> 1 Choose data source: open
> 2 Create a tweet template:
“%DATE %TIME Door unlocked, temperature upon unlock was %VALUE °C!”

> 3 Save settings under the name: door unlocked

> Save and continue
> Complete installation
>Connect to twitter #you need to be logged in to twitter on the brawser.

in popup click:
>Authorize app

Do that for closed,unauthorised and any custom feeds you need.And that’s it!

433Mhz RC switches
sustav4

We used the model:RCS1000N RC switches from brennenstuhl,so if you want to be sure that they work with the arduino code buy this model, otherwise edit their codes in the arduino code.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s