CakePHP and MySQL Spatial Extension
Just some notes on how one could use the MySQL spatial extension in CakePHP. Let's say I have the following table:
CREATE TABLE IF NOT EXISTS `reports` ( `id` int(11) NOT NULL AUTO_INCREMENT, `created` datetime DEFAULT NULL, `modified` datetime DEFAULT NULL, `position` point NOT NULL, `shortdesc` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), SPATIAL KEY `position` (`position`), ) ENGINE=MyISAM
Store a Point:
In the View do something like this, instead of a field for position:
echo $this->Form->input('lat');
echo $this->Form->input('lng');
In your Models beforeSave you could do something like this:
public function beforeSave($options = array()) {
if (isset($this->data['Report']['lat']) && isset($this->data['Report']['lng'])) {
$db = ConnectionManager::getDataSource('default');
$this->data['Report']['position'] = $db->expression("GeomFromText('POINT(" . $this->data['Report']['lat'] . " " . $this->data['Report']['lng'] . ")')");
}
unset($this->data['Report']['lat']);
unset($this->data['Report']['lng']);
return true;
}
This saves your lat, lon point to the database.
Retreive a (human readable) Point:
You retreive the position value as a strange blob, so we need to use the ASTEXT() method. In your Model add the following virtualField:
public $virtualFields = array( 'position' => 'ASTEXT(Report.position)' );
The virtualField overrides the real field, the outputs should look like this:
'Report' => array( 'id' => '2', 'created' => '2013-05-08 16:24:46', 'modified' => '2013-05-08 16:24:46', 'shortdesc' => 'Schlesische Straße 27B, 10997 Berlin, Deutschland', 'position' => 'POINT(52.4986476 13.4483252)' ),
If you need the blob output of position, you can choose any other name for the virtalField. Okay, so now you have lat and lon in one field and can parse it with something like the following. You can do it in afterFind for convenience
preg_match_all('![-+]?\d+\.?\d*!', 'POINT(52.4985982 13.449118799999951)', $matches);
array( (int) 0 => array( (int) 0 => '52.4985982', (int) 1 => '13.449118799999951' ) )
Please remember these are just notes and snippets, because I haven't found anything like this with a quick google search and maybe I can spare someone some time. If you have a better solution please share in the comments.
Hackberry A10: Wifi with Debian
The Hackberry A10 has a built in wifi chip which is deactivated by default if you use the Debian Wheezy image provided by Jas-Hacks (I wrote an article about how to run Debian on your Hackberry). Im going to explain what I did to get wifi running and make the Hackberry automatically obtain an IP-Address after boot. Again this is basically a summary of bits and pieces I found either on Jas-Hacks blog or in the miniand forums. Also I followed this (german) tutorial on wpa-supplicant.
Try it manually
Boot up your Hackberry A10 with your Debian SD Card inserted and connect to it via SSH.
First of all we manually load the wifi card module
sudo modprobe 8192cu
lsmod should now list the module and iwconfig should list your wifi card. It's probably called "wlan1".
lsmod sudo iwconfig
Fire up the card and check if it's listed in ifconfig now.
sudo ifconfig wlan1 up sudo ifconfig
You should now be able to scan for surrounding networks.
sudo iwlist wlan1 scan
Configure wpa-supplicant
I recommend using wpa-supplicant to connect to wifi networks. I'm not going into detail what wpa-supplicant is, go search on your own. ;)
wpa-supplicant should already be installed, we only have to add a config file. The first lines are basic configuration, after that you can add multiple "network" blocks, for each network you want to use your board with. For example network blocks see for instance this file. The iwlist command earlier should have presented you with your networks configuration details like WPA/WPA2, TKIP/CCMP, ...
sudo vim /etc/wpa_supplicant/wpa_supplicant.conf
My config looks like this:
ctrl_interface=/var/run/wpa_supplicant
eapol_version=1
ap_scan=1
network={
ssid="networkname"
scan_ssid=1
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
group=TKIP
psk="ibetyouwouldliketoknow"
}
Since this file holds a lot of sensitive information, chmod it to 0600.
sudo chmod 0600 /etc/wpa_supplicant/wpa_supplicant.conf
Now let's do a manual test of the configuration
sudo wpa_supplicant -i wlan1 -D wext -c /etc/wpa_supplicant/wpa_supplicant.conf -d
Open up another shell and check what iwconfig displays. Something like the following should be great.
wlan1 IEEE 802.11bgn ...
Now we can connect to the specified network by
sudo wpa_supplicant -i wlan1 -D wext -c /etc/wpa_supplicant/wpa_supplicant.conf -B
I checked via ifconfig if I got an IP-address, which I didn't, so I manually called dhclient.
sudo ifconfig #check for IP sudo dhclient wlan1 sudo ifconfig #check again
If all that worked, you should now be connected to your network via wifi.
Make this permanent
What I wanted, and you do probably too, is for the Hackberry to connect to the network automatically after boot, since, yeah, the thing is still headless and we want to connect via ssh.
To load the wifi-card module on startup uncomment the "8192cu" line in /etc/modules
sudo vim /etc/modules
Also modify the wifi part in /etc/network/interfaces to use wpa-supplicant.
sudo vim /etc/network/interfaces
Mine looks like this:
auto wlan1
iface wlan1 inet dhcp
wpa-driver wext
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
Unplug the ethernet cable and reboot your device. In your router check if the Hackberry is connected. Mine unfortunately wasn't so I had to do the following. Plugin ethernet again, connect via ssh and insert the following ifdown/ifup commands into /etc/rc.local before "exit 0". These commands are executed after every boot.
... ifdown wlan1 ifup wlan1 exit 0
Now I did another reboot (with ethernet unplugged) and the Hackberry showed up in my routers DHCP table and I could SSH onto the board. YEAH!
Hackberry A10: Starting with Debian
Wow, this is a coincidence! I decided to finally write another blog entry today and had a look how much time passed since my last posts. It was exactly one year ago.
What happened in the time? Not much, but as mosts geeks out there I ordered myself a Raspberry Pi. While waiting in the ridiculously long waiting line, I read up on some of the other "developer boards" popping up at the moment. The Hackberry A10 Dev Board sold by miniand caught my eye with built in wireless, its 1.2GHz Allwinner CPU and 1GB Memory, so I went ahead and bought one of those as well. The guys at miniand let me pay via Paypal (even though it's not an official payment method on their site) and shipped really fast. Thanks!

You can read up on the details of the board on their homepage. The device runs Android out of the box but to really put the hack in hackberry I want to run a debian on it. Of course I'm not the only one, so the great Jas Hacks already offers a nice Headless Debian Wheezy Image. So basically all the credit goes to him. I just want to leave a little write-up of how to install the image and the first steps in the new debian for low-mid skilled linux users like me. If I do something terribly wrong, please tell me in the comments. ;)
What do you need?
- SD Card with at least 4GB capacity
- SD Card reader
- Ethernet Cable
Let's begin:
Download the Jas Hacks Image from this Blog Entry and upack the thing.
wget http://dl.miniand.com/jas-hacks/debian_wheezy_3.0.36.img.gz gunzip debian_wheezy_3.0.36.img.gz
Also get the right updated uboot (example for 1Gb Board)
wget http://dl.miniand.com/jas-hacks/uboot/1gb/sunxi-spl.bin wget http://dl.miniand.com/jas-hacks/uboot/1gb/u-boot.bin
Insert the SD Card into the card, find out the name of the card. Be careful! Chosing the wrong device may destroy your system. On my system it's called "/dev/mmcblk" so make sure to replace it in the following commands. Copy the image on the card using dd
sudo dd bs=4M if=debian_wheezy_3.0.36.img of=/dev/mmcblk0
This may take some time. To see the progress open another shell, find out the pid of the dd process and run the foolowing kill command. The progress should pop up in the dd shell.
pgrep -l '^dd$' sudo kill -USR1 <pid>
When it's done do a sync
sudo sync
dd the new uboot to the SD Card
sudo dd if=sunxi-spl.bin of=/dev/mmcblk0 bs=1024 seek=8 sudo dd if=u-boot.bin of=/dev/mmcblk0 bs=1024 seek=32 sudo sync
If your card is larger than 4GB you can extend the ext partition on the card. I use gparted for this task. Now you can insert the SD Card into your hackberry. Plug in the ethernet cable and the power supply. The board should now boot debian. Since it's a headless image, it's configured to fire up ssh. Find out the IP of the device (probably via your routers webinterface) and ssh into it
ssh root@192.168.0.42 (pw: password)
Now I'm going to write down a few things I do on a fresh install
Change the root password
passwd
Change the hostname (in this example hackberry)
hostname hackberry echo hackberry > /etc/hostname
Change locale and timezone
dpkg-reconfigure tzdata aptitude install locales dpkg-reconfigure locales
select some utf-8 (e.g. en_US.UTF-8 UTF-8) by pressing the space bar
Set the clock:
aptitude install ntp ntpd -q -g hwclock --systohc
Do a full upgrade
aptitude update aptitude upgrade
Add a new user (muxe in this example)
adduser muxe aptitude install sudo visudo
Insert "muxe ALL=(ALL:ALL) ALL" below the root line
Backup
When you are finished setting up your basic system I recommend doing a backup of the card. So in case you somehow fuck up your system you have a kind of savegame. shutdown the hackberry and insert the card to your cardreader. dd the other way round.
sudo dd if=/dev/mmcblk0 of=my_hackberry.img bs=4M
What I'm planing on blogging in the future on this topic: I'm currently building my own kernel with webcam support. This takes so long, it's the main reason I even had the time to write all this down. ;) When the webcam is running, I'll hopefully tell you how I did it and show you some stuff you can do with tools like motion. Also some stuff of the board is not very well documented. I had to read through a lot of different forum topics to gather those informations. When my composite cable arrives, I plan on writing something about different ways to output video with the Hackberry A10.
Todos: Wow this looks ugly. Guess I'll have to edit the css a bit and insert codeboxes, but now I'm too lazy. Have a great day and leave comments.
Croogo Socialprivacy Plugin
I wrote a small plugin for the cakePHP CMS Croogo. It displays share buttons for the social networks Facebook, Twitter and Google+ below each blog entry. It uses socialshareprivacy script by Heise, which hides social media buttons behind a second click to prevent auto-loading these buttons and thereby auto-transmitting data to third parties by default.

You can find more info on the used script here. The download of the plugin can be found on my github.
To install this plugin put the socialprivacy folder into your /app/plugins/ folder on your webserver and activate the plugin in the admin area.
The script can only be used once per page, so it is only displayed in details of a blog entry and not on the main page (where more than one entry is shown). You can probably see a demo of the plugin right below this blog entry.
cakePHP QR Code Helper
I wrote this little Helper to simplify the creation of QR Codes in a cakePHP app. It uses the Google Chart API, therefore Codes are generated on the fly and not saved on the server.
It can be found in my GIT repository.
Includes functions to create QR codes for:
- free text
- contacts
- email sending
- calendar events
- Geolocations
- Android Market Searches
- MMS sending
- SMS sending
- Telephone calls
- URLs
Some samples
echo $this->Qrcode->text('muXe rocks!');
would create this QR code with the content "muXe rocks!"
And this:
echo $this->Qrcode->event( array( 'summary' => 'Highnachten', 'start' => '20101224T180000', 'end' => '20101224T235900', 'location' => 'Berlin', 'description' => 'Time to celebrate!' ), array( 'size' => '500x500', ) );
would create the following QR code, which contains an iCal event.