About these ads

Nugget Post: Android Process Dialog

A lesson that took me an hour to figure out: android progress dialogues will only be displayed on the UI if called/shown in a thread or async task. Basically when try to call a progress dialog directly from a fragment, android would simply not show the dialog.

Example of the android progress dialog (indeterminate)

Example of the android progress dialog (indeterminate)

So the code would look something like this:

 

The code shows how to attach a listener that will trigger a sequence of events when a button (uploadButton) is clicked. The sequence is:

  • Initialize the progessDialog (so we can refer to the variable across threads)
  • Start a new thread
  • Within the thread, prepare the look and feel of the progress dialog and display it in the UI thread
  • Do your long task (uploadFile)
  • Dismiss the progress dialog in the UI thread
About these ads

Nugget Post: Android threads and array adapters

An important point of note when using array adapters in android is that the notifyDataSetChanged method can only be called in the main UI thread. So when developing multithread android apps, it’s important that the method is called within a runOnUiThread or similar. The notifyDataSetChanged is used to inform an array adapter that it’s data source has been updated, so it should refresh it’s linked UI element (usually a listview or similar). So an android developer should have something like this in an android fragment:

 

getActivity().runOnUiThread(new Runnable() {

public void run() {
adapter.add(argument.toString());
}
});

 

Note how in the above example I am using the add method, whose implementation includes the notifyDataSetChanged method, and hence must be included in the runOnUiThread method.

Raspberry Pi : Home Control Center Part 2

In this second part of my series, I’ll go into configuring your PI to have:

  • A “security mode” that will record about 30-60 seconds of video everytime movement is sensed wherever a raspberry pi is installed
  • Display the local temperature, humidity security monitor arming on a simple web UI

In this photo, reposted from a previous article, you can see both the PIR (infrared sensor), and camera that will be used in this post.. note how tiny they are! For scale reference, that blue square just under the red LED of the camera on the right is an SD card….

... and showing network information

… and showing network information

First off, have a quick read through the following from adafruit to get acquainted with the PIR sensor:

http://learn.adafruit.com/adafruits-raspberry-pi-lesson-12-sensing-movement/overview

The article explores how to wire up the PIR sensor. We next have to get the raspi cam setup so we can take videos whenever movement is sensed by the PIR. The hardware I am using can be found here:

http://www.raspberrypi.org/camera

First some groundwork:

vi /etc/default/tmpfs

We will store the video file temporarily in /tmp, and then transfer this via FTP to a storage server (a QNAP in my case). To make things really fast, and save on the SD card life of the raspiPI, we use tmpfs, which by default is already installed on raspbian. This will ensure the data is stored in memory, making it quite fast. To store the /tmp file in memory via tmpfs, find the following line and uncomment it:

RAMTMP=yes

Next we install the python framework called “flask“. I find this framework extremely easy to use, and it’s fully featured, so it’s a winning combo in my opinion:

pip install flask

Last, we install a python based program that basically starts / stops arbitrary scripts of your choosing, and can even restart them when they die (this is very similar to the “forever” program used by node.js developers). The program is called “supervisor” and is installed with a simple:

apt-get install supvervisor

Let’s quickly build the webinterface using flask…

Note how in lines 32-33 in the above program, I am retrieving values from a pickle file wherein I’ve stored the sensor readings from the previous article to avoid over-taxing the sensor circuit. The script is quite easy. One function “index” serves a simple homepage, the other, “toggle” uses supervisor to turn the PIR sensor program on or off. The index.html file itself is nothing special:

<html>
<head>
<script src=”http://code.jquery.com/jquery-1.11.0.min.js”></script&gt;
<script src=”{{ url_for(‘static’, filename=’js/raphael.2.1.0.min.js’) }}”></script>
<script src=”{{ url_for(‘static’, filename=’js/justgage.1.0.1.min.js’) }}”></script>
</head>
<body>

<table>
<tbody>
<tr>
<td><div id=”tempGauge” style=”height:200px; width:250px”></div></td>
<td><div id=”humidGauge” style=”height:200px; width:250px”></div></td>
</tr>
</tbody>
</table>

<h1>Status: <a href=”/toggle”>{{status}}</a>
<h1><a href=”./securityFeedOn”>View Live Feed</a></h1>

<script>
var g = new JustGage({
id: “tempGauge”,
value: {{ temp }},
min: 10,
max: 40,
title: “Temperature *C”
});
</script>

<script>
var g = new JustGage({
id: “humidGauge”,
value: {{ humid }},
min: 0,
max: 100,
title: “Humidity %”
});
</script>

</body>

</html>

It shows a page similar to this (the graphics come courtesy of http://justgage.com/):

Selection_147The “unarmed” link will toggle the security system to start sensing movement. Going back to the flask python script, if you notice, on lines 19 and 22, I call on supervisor to start or stop the program called “pir”. This “pir” reference comes from the supervisor configuration file (/etc/supervisor/supervisord.conf) where I added a section similar to the following:

[program:flask]
command=python /home/pi/flask/main.py

[program:pir]
command=python /home/pi/PIR.py
autostart=false

The first program defined (“flask”) points to the flask script I present previously, and is started automatically on bootup. The second program is the “pir” program I reference in the flask script. Here’s the full PIR.py script:

I’ve inserted comments into the script which hopefully makes it clear enough to the reader what’s happening. In essence the script triggers an email alert, a 10-second video clip, which is then sent via FTP to a server.

That’s it!! try it out :) In the next article, we’ll setup a real-time feed from the camera using mjpg-streamer, stay tuned….

Raspberry Pi : Home Control Center Part 1

In this series i’ll document how I put together a lot of people’s hard work into a home control center that is based off the raspberry pi, both for my future reference and for anyone out there who needs to build something similar. By the end of it, we should be able to have the following features:

  • Monitor temperature and humidity across various points in the house – wirelessly
  • Connect to your network via wireless
  • Display essential info on an OLED screen
  • Have a “webcam” mode that will allow you to stream real-time video of a room in your house wherever a raspberry pi is installed
  • Have a “security mode” that will record about 30-60 seconds of video everytime movement is sensed wherever a raspberry pi is installed
  • Trigger alarms whenever movement is sensed or doors open/close – wirelessly
  • Have an easy to use WEB UI that also works on your mobile phone
  • Can control lights on sensing movement

That’s quite a list which I hope to eventually complete and document. In this 1st part, I’ll focus on:

  • Connect to the network via  wireless
  • Display IP address, local temperature and humidity on an OLED screen

Most of the heavy lifting has been done by the awesome folks over at www.adafruit.com , in most cases I was just gluing everything together…. This guide assumes a fresh raspbian install (preferably via NOOBS). Without further ado

  • Connect to the network via  wireless

Really simple and run of the mill – if you have the right hardware. This point is key… I’ve had other wireless cards and they involved compiling drivers, not working, rinse, repeat. Save yourself a lot of hassle and just get a wireless USB that works out of the box. The adafruit one is perfect:

http://www.adafruit.com/products/814

Simply switch off your raspberry pi, plug it in and wait for a bootup and run an ifconfig to make sure wlan0 is listed. Then it’s the usual connect-your-linux-to-wlan procedure… In my case, I am connecting to a WPA2, pre shared key wireless lan. First add the following to your /etc/network/interfaces file:

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface wlan0 inet static
    address 192.168.1.245
    netmask 255.255.255.0
    network 192.168.1.0
    gateway 192.168.1.1
The above will give a static IP to your Pi, amend as necessary. And also in your /etc/wpa_supplicant/wpa_supplicant.conf file:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
 ssid="YOUR_WLAN_SSID"
 proto=RSN
 key_mgmt=WPA-PSK
 pairwise=CCMP TKIP
 group=CCMP TKIP
 psk="YOUR_PASSWORD"
}

Once again amend as necessary. I also added the following to the /etc/rc.local file to make sure the wireless connected on bootup:

ifdown --force wlan0
ifup --force wlan0

That’s it for wireless.

  • Display IP address, local temperature and humidity on an OLED screen

The OLED screen purchase and setup were a breeze thanks to adafruit. Visit and follow the tutorial here: http://learn.adafruit.com/adafruit-oled-displays-for-raspberry-pi.

The author of this post already includes how to display IP addresses on the OLED screen in one of his python examples so you’re halfway there. Adding the temperature and humidity are equally easy, again due to adafruit’s amazing tutorials. In this case, follow the one here: http://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/overview

I obviously did deviate here since I wanted to place the information on the OLED screen rather than google docs. So in my case, after performing this step:

$ git clone git://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
$ cd Adafruit-Raspberry-Pi-Python-Code
$ cd Adafruit_DHT_Driver

I compiled and installed the python library for the DHT driver as follows:

cd ../Adafruit_DHT_Driver_Python
python setup.py build

This results in a dhtreader.so file in the build directory which we can now use directly in python. Next to code:

  • Create a directory for the project. In my case:
mkdir /home/pi/raspiCC
  • Create a new python file and copy/paste the following:

If you notice, in the above code I’ve placed the main logic in a for loop with specific sleep periods. This was done so the whole script executes for a total of just under 60 seconds. This way, we can schedule a cron job that runs every minute and will continuously cycle between IP and environment information. This is quite simply done via first entering

sudo su -
crontab -e


Then entering the following crontab entry:

* *  *   *   *     /home/pi/raspiCC/daveOLED.py

The above will run our script every minute and refresh our OLED display. Here are the results so far:

Raspi PI OLED screen showing temperature and humidity

Raspi PI OLED screen showing temperature and humidity

... and showing network information

… and showing network information

Note the camera and infrared sensor in the last photo, which is coming up next… connecting the raspi cam to record about 30-60 seconds of video everytime movement is sensed near your pi :)

Android NFC access card reader demo

What if your company/client has rolled out an access card system… You know, the sort that you swipe your card across a reader, it beeps green and doors open… and you’d like the same functionality for your mobile apps? Something like, the user swipes their card, and an app opens, detects the user, and displays the appropriate data according to who the user is. By extension, you can build an app that requires two factor authentication, e.g. a PIN number (something you know), and the access card (something you have). This proof of concept is something I built to show off that android phones and apps can actually support such scenarios through NFC.

Watch a video of it in action here:

http://www.youtube.com/watch?v=LYIEctwCzuc

And you can find the source code here:

https://github.com/dvas0004/droidNFCdemo/blob/master/src/me/davidvassallo/nfc/MainActivity.java

References:

 

Follow

Get every new post delivered to your Inbox.

Join 138 other followers

%d bloggers like this: