vrijdag 27 september 2013

A soft reset button for a raspberri pi

scenario

i have a raspberry pi that i want to be able to reboot cleanly without ssh-ing into it


sollution

we're going to use a GPIO pin with a button wired up like this

picture from adafruit's website
3.3 volt is pin 1 (the pin nearest to the port where you plug in the power addaptorGND  is pin 6 (2 down and one over from pin 1)
and i'll be using gpi 23 .. that's pin 16 (nr 8 on the same line as pin 6)

My buton is actually a different setup from the on in this picture (mine breaks contacet when pressed



next we make a script
$ sudo nano /etc/softresetbutton.py




#!/usr/bin/env python
from time import sleep
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
while True:
        if ( GPIO.input(23)== True ):
             # tue or false here depends on your button type
             # when in doubt replace 'reboot &' in the following line with 'echo "pushed" &'
                os.system('reboot &')
        sleep(0.1);



next we make this script executable
$sudo chmod +x /etc/softresetbutton.py

and test-run it
$sudo /etc/softresetbutton.py

push the button to test it
works ok ??
now we need to make it run at startup

$sudo nano /etc/rc.local

and add the line
/etc/softresetbutton.py &
just above the " exit 0 "


reboot and you'll have a soft-rest button

if you would prefer a "clean halt" button .. replace the reboot command in the script with  halt

woensdag 25 september 2013

Stupid but sometimes usefull IP-tables trick

OK so you want to secure your off-site server using IP tables .. this is always a good thing to do


on of the things that i highly recommend is to restrict the incomming IP's to  whatever port you are using for management so that you can only access it from your networks

but if you start messing with the iptables for port 22 (for example) and you make a tiny tiny slip-up you might lose connection yourself. if the machine is sitting next to you (under your desk or in your on-site server-rack, yu can walk over hook up a screen and keyboard and fix it .... but if it's in a datacenter somewhere... yu've got a problem. (with a physical machine all you can do is get an engineer to help you out (expensive) or hard reboot the machine (not nice))

the trick is .. let's say we're starting at 2 in the afternoon, and we think we might have 15 minutes of work.
so we'll shedule a restore f the current situation in 20 minutes
as root (obviously) do

$iptables-save > /home/user/iptables.conf
$at 14:20
at>iptables-restore < /home/user/iptables.conf
   ctrl-D to save the job and quit

now check your job is scheduled
$atq
1       Wed Sep 25 14:20:00 2013 a root


start messing with your iptables
if something goes wrong .. wait for the scheduled restore

if nothing goes wrong (congrats)
you can remve the job with
$atrm 1

woensdag 10 april 2013

Raspberry PI Checkin devices

the brief

We have a project at work called the "uitpas" it's sort of a point card for cultural events. (more info about the uitpas can be found at http://www.uitpas.be/ ) when you have a certain ammount of points, you can exchange it for ceratin prizes (movie tickets, hiking maps ... )

There are a lot of events however where people either buy their tickets before hand or where no tickets are needed. So  there was a need for a check-in device that could opperate without a person manning it. 

The Uitpas cards are rfi cards (DESfire mifare to be exact) 

the sollution  

hardware 

ingredients 
  * Raspberry pi (orignally i used the 256mb kind, currently we are using the the 512mb) 
  ** 4 gig SD card 
  ** case for the raspberry pi  (i chose this one because it still allows me access if one side is glued to the backplate of our setup)
  * powerd usb hub 
  * a small screen that we got from adafruit http://www.adafruit.com/products/946 
   * short mini usb cables
Optional
  * sme small wifi adaptors (i got an edimax one from amazon , but i guess any would do .. check the raspberry pi wifi compatibility page for details)

testing setup

you can see everything (exept the screen) hooked to the left here. In this example it was hooked up via HDMI to a PC screen because that was easier to debug on (A 4 inch screen makes the console hard to read)

The raspberry pi is hooked up to the USB hub twice, once as it's host, and once in order to be fed some yummy yummy electricity
the reason for hooking up like this is that the raspberry pi does not supply enough amps on it's usb ports to power the RFID reader.




the card reader required these packages to be added

libccid : PC/SC driver for USB CCID smart card readers
libpcsclite1:armhf : Middleware to access a smart card using PC/SC (library)
pcscd : Middleware to access a smart card using PC/SC (daemon side)


we have a software partner ( lodgon ) that writes all the clients and they supplied us with a java application that reads the card does the oauth handshake with the server and writes directly to the frame buffer.

in order to run that software the PI user has to lgin automatically at boot-time

to do this you have to edit  /etc/inittab  end replace the line
1:2345:respawn:/sbin/getty --noclear 38400 tty1
with
1:2345:respawn:/bin/login -f pi tty1
/dev/tty1 2>&1

Autorunning the java application is done by adding the command that starts it to the end of the /home/pi/.bashrc file.


Accessing them remotely

They willl be dropped at locatios where they will not have a public IP adress. but i still need to be able to access them remotely. mainly for trouble-shoting, but also for monitoring them and seeing that they do OK.
i could set up VPN tunnels but that's a hasslte to maintain on my end and can cause "issues". So i requested a bunch of IPv6 tunnels from the nice people at Sixxs.
Because the raspberry pi does not have a battery for it's onboard clock the tunnels did not start reliably at bootup
so i have a script that runs at bootup that checks wether or not the tunnel is up. (by calling ifconfig and checking if the sixxs interface is present) .. if it is not i runt "ntpd -qg" and restart the tunnel. it's a little dirty, but it works.

The casing 

We have a previous version that's running an arduino ... the new Raspberry pi version should be recognizable and be able to use the "pedestals" of the arduino version, but obviously it should be bigger (more stuff inside, bigger screen ). and it should also solve a few "usability problems". 
I feel compelled to write here that the "industrial design" on the arduino version was done by an external partner .. all i did was the electronics. and they placed the rfid antenna behind 1 cm of plexy and a working 16*2 lcd screen .. so it does not read to well , and to add to the troubles your card blocks the screen if you present it to the reader. 

siblings
the small one on the right is the old version 
the one on the right is the new version. 



metal piece of the casing
so we made some basic design (in cardboard) had some meetings and found someone to build us a piece of metal as a frame. we also found someone to print and cut the frontplate as well as bend and cut a piece of plastic to cover the back.




screen and rfid reader inside


using some double-sided 3M tape to paste it all together. ANd adding some short USB cables where possible to keep it from becomming a big cable-knot.
All the electronics inside

So We've had about 10 of them in the field now and they seem to be performing well.
one Check-in device in the public library in Lede





donderdag 28 februari 2013

watching a logfile and doing stuff

I needed to watcha logfile and trigger an action if a specific line appeared in the logs


    #!/bin/sh
    #tail the file you want to watch
    tail -fn0 /var/log/logfile | while read line ; do
            echo "$line" | grep 'line to react to' | grep -v 'things to block the reaction' 
            if [ $? = 0 ]
            then
                    # do things
                    echo "just saw $line"
                    mpg123 triomph.mp3
            fi
    done


save this as script.sh
make it exectuable (chmod +x script.sh) 
and let it run in the background ./script.sh & 




maandag 7 mei 2012

Moving mysql data folder


check your mysql setup , and maybe take a backup

stop mysql
$/etc/init.d/mysql stop

move the data folder

$mv /var/lib/mysql /var/db/

create a symlink so everything can find teh new data folder

$ln -s /var/db/msql /var/lib/mysql


edit the apparmor file

$nano /etc/apparmor.d/usr.sbin.mysqld

find the  2 entries that point to /var/lib/mysql and change the appropriately (in my case /var/db/mysql)

restart apparmor
$/etc/init.d/apparmor restart 

start mysql
/etc/init.d/mysql start 


check if everything is ok 













maandag 5 maart 2012

Mobistar USB modem on debian

Hi


I got a hold of a Mobistar USB modem for mobile internet
thought it would make a great addition to my Thikpad x200S running Debian unstable
installing it was easy, but i thought i'd document the steps anyway since i also haven't written anything here for a while .

plugging it in an doing ls-usb
it identifies as a huawei e173
Bus 001 Device 022: ID 12d1:1c0b Huawei Technologies Co., Ltd. E173 (modem off)

this is the built in storage device

i googled a bit and found instructions here
http://sussexcomputerworks.co.uk/Linux/kubuntu-1004-and-the-t-mobile-broadband-615-usb-device.html


long story short
install usb-modeswitch and wvdial
$apt-get install usb-modeswitch wvdial

make a config file
nano /etc/usb_modeswitch.d/12d1:1c0b


and add the following to it

########################################################
# Huawei E173s

DefaultVendor= 0x12d1
DefaultProduct= 0x1c0b

TargetVendor= 0x12d1
TargetProduct= 0x1c05

CheckSuccess=20

MessageEndpoint= 0x0f
MessageContent= "55534243123456780000000000000011062000000100000000000000000000"





then do the necessary modprobes

$ usb-modeswitch -c /etc/usb_modeswitch.d/12d1:1c0b
$ modprobe usbserial vendor 0x2d1 product 0x1c05


i also had to unplug the usb device and plug it back in


now if i do

$wvdialconf

wvdial finds my modem and creates a /etc/wvdial.conf file
edit this file to add your login and password for the sim card

i don't have an extra active sim here at the moment ... will p update this post when i've tested with that too

Greetz

WOuter

vrijdag 29 juli 2011

restoring couchdb database from backup .couch file

hi

at my work we're using couchdb running on ubuntu server 10.04 to log some stuff
the db was growing near the disk space, and the person responsible was absent (due to the holiday season).
so i shut down couchdb.. moved the .couch file to a diferent location and recreated the database as an empty db so the logging of data would continue.

when the person returned from holiday they needed the data, so they first took what they needed from the new db file.
After this they asked to restore the big DB file.



make sure in the new instance of couch the appropriate database has been created

shut down couchdb (/etc/init.d/couchdb stop )

restore the file to it's original location (if there is data in the new file you might want to make a backup of that first)
make sure the .couch file has the correct owner & group .. (in my case both couchdb, look at the new file to know for sure)
restart couchdb (/etc/init.d/couchdb start)


should be all done


Greetz

Wouter