We’re having some fun these days in the office:
For this solution I used these hardware:
- BeagleBone
- magnetic door open sensor
- a cheap CM102-based sound card from eBay (sold as 3D USB sound card for about $2)
- a USB hub
- a TP-LINK WN722 WiFi dongle.
- 2 active speakers by my colleague makos (passive would have been okay too, as the CM102 drives them pretty loud)
The beer opening sound is here. The sound card and the WiFi dongle is connected to the USB hub, and the hub is to the BeagleBone. The door opener sensor is connected to the BeagleBone’s P8 breakout connector’s third (GPIO1_6) and first (GND) pin.
This script (called ajtonyitas.sh) does the whole thing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #!/bin/bash cd `dirname $0` if [ ! -e /sys/class/gpio/gpio38/value ]; then echo "initializing gpio pin." echo 38 > /sys/class/gpio/export fi while [ 1 ]; do echo "waiting for door opening." while [ "`cat /sys/class/gpio/gpio38/value`" = "0" ]; do sleep 0 done echo "door opened, playing sound." ./upload.sh & play sornyitas_loud.wav &>/dev/null echo "sound playing finished, waiting for door close." while [ "`cat /sys/class/gpio/gpio38/value`" = "1" ]; do sleep 0 done echo "door got closed." done |
As you can see, the echo 38 > /sys/class/gpio/export line initializes the /sys/class/gpio/gpio38/ directory, so we can read the pin’s value. The number 38 is calculated this way: gpmc_ad6 -> gpio1_6 -> 1*32 + 6 = 38. By default this pin is configured for pullup (you can get this information with cat /sys/kernel/debug/omap_mux/gpmc_ad6), so I didn’t have to mess with additional port configuration.
I’ve put this in /lib/systemd/system/ajtonyitas.service:
1 2 3 4 5 6 7 8 9 | [Unit] Description=Ajtonyitas figyelo [Service] ExecStart=/home/root/ajtonyitas/ajtonyitas.sh RemainAfterExit=yes [Install] WantedBy=multi-user.target |
I ran systemctl enable ajtonyitas.service so the ajtonyitas.sh script gets started on system boot.
The upload.sh script downloads 5 frames from a TP-LINK WiFi camera and uploads it to a NAS so we have a photo album of people coming in and going out of the room and smiling because of the sound. :) The .wav file is played by sox. I had to cross compile it, but sox depends on libtool, so I had to compile that too.
For cross compiling libtool, I used this script:
1 2 3 | #!/bin/bash CC=arm-arago-linux-gnueabi-gcc ./configure --prefix=/home/nonoo/libtool --host=arm --disable-static --enable-shared make -j8 install |
And the script for sox:
1 2 3 4 5 6 7 8 9 10 11 12 | #!/bin/bash LDFLAGS="-L/home/nonoo/libtool/lib -lltdl" CC=arm-arago-linux-gnueabi-gcc ./configure --host=arm --prefix=/home/nonoo/sox \ --disable-shared \ --enable-static \ --without-png \ --without-flac \ --without-mad \ --without-pulseaudio \ --without-oggvorbis \ --without-sndfile \ --without-wavpack make -j8 install |
The -j8 switch tells the compiler (I’m using arm-arago-linux-gnueabi-gcc) that it should use 8 threads (I have a 4 core processor with hyperthreading enabled).
I also compiled mpg123 for mp3 playback, but sometimes it stuttered, that’s why I’m using a .wav file and sox.
The webcam image upload.sh script looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #!/bin/sh IP=192.168.18.3 USER= PASS= DIR=/nfs/ajtonyitas TIME=`date +'%Y%m%d_%H%M%S'` usleep 500000 for i in {1..5} do wget http://$USER:$PASS@$IP/jpg/image.jpg -O $DIR/$TIME"_$i".jpg done |
As you can see, it pulls the images from the webcam using wget and then puts them into an NFS mounted directory which is on our NAS. As the BeagleBone shell’s built-in sleep command can’t handle less than 1 second delays, I wrote usleep, which is a little C program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Here’s it’s Makefile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | CC=arm-arago-linux-gnueabi-gcc TARGET := usleep SOURCES := $(shell find . -name '[!.]*.c') MODULES := $(SOURCES:.c=.o) all: $(MODULES) $(CC) -O4 $(MODULES) -o $(TARGET) %.o: %.c *.h $(CC) -O4 -c $*.c -o $@ clean: rm -rf $(MODULES) $(TARGET) |
For the NFS to work on the Bone, I had to compile several libraries: LVM, libevent, libnfsidmap and nfs-utils.
For compiling LVM I used this script:
1 2 3 4 | #!/bin/bash ac_cv_func_malloc_0_nonnull=yes CC=arm-arago-linux-gnueabi-gcc ./configure --host=arm-linux --prefix=/hom e/nonoo/lvm --disable-readline --enable-static_link CC=arm-arago-linux-gnueabi-gcc AR=arm-arago-linux-gnueabi-ar make -j8 install |
For libevent:
1 2 3 | #!/bin/bash CC=arm-arago-linux-gnueabi-gcc ./configure --host=arm-linux-gnueabi --prefix=/home/nonoo/libevent make -j8 install |
For libnfsidmap:
1 2 | #!/bin/bash CC=arm-arago-linux-gnueabi-gcc ./configure --host=arm-linux-gnueabi --prefix=/home/nonoo/libnfsidmap |
And finally for nfs-utils:
1 2 3 | #!/bin/bash CC_FOR_BUILD=arm-arago-linux-gnueabi-gcc CC=arm-arago-linux-gnueabi-gcc PKG_CONFIG_PATH="/home/nonoo/libnfsidmap/lib/pkgconfig" CFLAGS="-I/home/nonoo/libnfsidmap/include -I/home/nonoo/libevent/include -I/home/nonoo/lvm/include" LDFLAGS="-L/home/nonoo/lvm/lib -L/home/nonoo/libevent/lib -ldevmapper" ./configure --host=arm-linux-gnueabi --prefix=/home/nonoo/nfs-tools-arm --disable-nfsdcltrack --disable-gss --enable-mount CC_FOR_BUILD=arm-arago-linux-gnueabi-gcc CC=arm-arago-linux-gnueabi-gcc AR=arm-arago-linux-gnueabi-ar make -j8 install |
So after copying the binaries to the BeagleBone, I could use mount.nfs and add this line to /etc/fstab for automatic mounting of the NFS directory:
1 | 192.168.18.253:/i-data/1c9fdbd6/nfs/ajtonyitas /nfs/ajtonyitas nfs defaults,nolock 0 0 |
Trackback responses to this post
About me
I'm Nonoo. This is my blog about music, sounds, filmmaking, amateur radio, computers, programming, electronics and other things I'm obsessed with.
... »
Trackback URL
No comments yet.