Step 9: Serial communication

To make the pet door controller communicate with the OpenWrt router you must connect the controller's TX pin to the router's RX pin and the controller's GND to router's GND (In our situation the communication is one way, from the controller to the router).

First of all, comment (add # in front of them) the lines in /etc/inittab :

tts/0::askfirst:/bin/ash --login
ttyS0::askfirst:/bin/ash --login
tty1::askfirst:/bin/ash --login

this will disable login console terminal on serial port, so we can use it for back and forth communication with the arduino based controller.

Both the router's serial port and controller's serial port must have same speed to be able to communicate. The router's serial speed to is set to 9600 by executing:

root@OpenWrt:~# stty 9600 < /dev/tts/0

For each RFID tag read by the pet dor controller, the controller will write to the serial the RFID tag with .txt at the end of line, for example, if the tag is 01068E2081, and the pet went out the door (breaking the IR beam) the controller is going to write to the serial 01068E2081-out.txt and if the pet went in the door (door opened by the RFID tag), the controller is going to send over the serial 01068E2081-in.txt

On the router, to read what's coming over serial communication from the controller, your code should look something like this:

# Tell the AVR we're ready to start doing stuff
echo "start" > /dev/tts/0
while true # loop forever
  inputline="" # clear input
  # Loop until we get a valid command from arduino
  # the line should be the text file we have to open to get
  # a random message
  until inputline=$(echo $inputline | grep -e ".txt")
     inputline=$(head -n 1 < /dev/tts/0)

by looking for .txt we will know when we got a new command from the pet door controller.

After we got a new command, for example 01068E2081-out.txt, we are going to read a random line from the file with the same name:

LINES=`wc -l $inputline | awk '{ print ($inputline + 1) }'`
RANDSEED=`date '+%S%M%I'`
LINE=`cat $inputline | awk -v COUNT=$LINES -v SEED=$RANDSEED 'BEGIN { srand(SEED); i=int(rand()*COUNT) } FNR==i { print $0 }'`

take a picture from the Webcam:

ln -s /dev/v4l/video1 /dev/video1 # create /dev/video1
spcacat -d /dev/video1 -g -f jpg -p 10000 -o > /dev/zero # take a picture every 3 sec

and upload them to twitter (twitpic):

# tell curl to send a multipart form to twitpic
# save returned XML in a variable
RET=$(curl -s -S -F "$myname" -F "$pword" -F "$cargo" -F "$tweet" $target)
echo "$RET"

Note: In this project the communication is just one way, the OpenWrt router just receives messages from arduino based controller. If you want to communicate both ways, in your bash script on the router use following command to send messages to arduino based conntroller:

echo "test message sent from OpenWrt router to arduino" > /dev/tts/0