Category Archives: Programming and Tech Stuff

Prevent external HDD drives from sleeping on Mac OSX

I have 4TB of data stored in three external hard-drives attached to my mac. It’s very frustrating when these HDDs enter sleep and the operating system hangs until they wake up, even for actions which do not involve them.

So, putting the side-effect of possible sooner failure due to wear aside, I wrote a little bash script to touch a file on every drive and set this script to run every 5 minutes via cron.

I’ll probably optimize it a little by disabling it every day during daytime hours (when I’m not at home).

This is the bash script

for D in /Volumes/*; do
    if [ -d "${D}" ]; then
        echo "${D}"
		touch "${D}/.touchdrive"
    fi
done

And this is the crontab entry for it

*/5 * * * *   sh /touchDrives.sh > /touchDrivesCron.log

 

How to format C and C++ code in Sublime Text

I am writing some C code on my mac using Sublime Text and I needed a quick way to format the code for better readability.

I found this package for Sublime Text named SublimeAStyleFormatter: https://packagecontrol.io/packages/SublimeAStyleFormatter

I’ve installed it with git by going on the packages folder and cloning the git repo:

cd ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/
git clone git://github.com/timonwong/SublimeAStyleFormatter.git

And that was it! I can format my C code by pressing Ctrl + Alt + F.

Happy owner of a Raspberry Pi 2

I’ve just bought myself a Raspberry Pi 2 computer with several accessories from a store with a presence both online (http://www.jola.ro/) and IRL (“Calea Floreasca nr. 76, Bucharest).

This upgrade model of the B+ has 1GB of RAM and a quad-core 900MHz CPU.

The accessories I’ve bought are: 2.1A power supply, IR blaster, IR sensor (38kHz demodulation chip included), 8GB microSD card for the operating system and storage and connecting wires. The HDMI cable was a gift from the store :)

Raspberry Pi 2 and accessories

Raspberry Pi 2 and accessories

I am really happy and excited with my purchase. So far I’ve downloaded the Raspbian image and wrote in on the card in like thirty minutes with dd command line from my mac. It felt like forever :D

I’ve connected the Pi to an HDMI screen, plugged in the card and an ethernet cable from my router. I’ve also plugged a USB wireless dongle from my Microsoft “All-in-One Media Keyboard” which although doesn’t compare to my Apple Wireless Keyboard and Magic Trackpad, it does fully work with Raspberry Pi (by “fully” meaning both keyboard and touchpad mouse are compatible; don’t know about the media keys, I haven’t tried them).

image4

Raspberry Pi 2 board, manual and box

By default, Pi boots to terminal. If you wish to load the graphical interface you have to type the following command:

startx

I’ve tried the web browser, accessed … er … google, facebook and my site :p and then went straight to terminal. By default, the layout of the keyboard is non-US (I’ve read somewhere on the net it’s UK layout) and some of the symbol keys are messed up, including the pipe symbol which prints a hashtag instead. So you need to issue the following command to have the layout switched to US (if you have a US layout keyboard, of course :P )

setxkbmap us

When the Raspberry config screen first appeared (raspi-config command) I’ve walked through the settings and I have enabled SSH access.

Then I started playing around.  I’ll tell the rest of the story in another post, as it’s somehow a different story from this initial “Hurray! I’ve got a Pi” post, but what I can tell you now is that this model is indeed sensible to flashlight photography, meaning that if you take a photo of it up-close while it’s working and you trigger your camera’s flashlight, the Pi will reboot and enter a non-working state (green stripes on the screen – “Green screen of death” perhaps? :P ) and will require you to hard-reset it (unplug the power supply and plug it back in).

Raspberry Pi 2 USB ports and Ethernet

Raspberry Pi 2 USB ports and Ethernet

Also, it’s good if you update your system with the following set of commands (for example, my next-to-write-about project “IR remote cloner” did not work properly until I’ve upgraded Pi’s firmware with the last command from the set):

sudo apt-get update
sudo apt-get upgrade
sudo rpi-update

That’s all for now! Happy Pi-ing :P

Raspberry Pi GPIO pins

Raspberry Pi GPIO pins

How to keep alive SSH outbound connections in OS X Terminal.app

If you want to keep alive the ssh connections you’ve open in OS X Terminal.app, you can edit the file on your mac located here: /etc/ssh_config and modify the line where you find this instruction: ServerAliveInterval. That tells the number of seconds at which your computer will send a null packet to the server in order to keep the connection alive.

By default, that line is commented and the value is zero anyway. You should uncomment the line and increase the value to something bigger than zero. I’ve set mine to 5.

You can edit the file directly in terminal, with admin permissions like this:

sudo nano /etc/ssh_config

You’ll be asked to enter your admin password. Find this line (as it is in default config):

#	ServerAliveInterval	0

and delete the hashtag at the beginning of line to uncomment it and change the zero value to something bigger. Ctrl + O to save the file and Ctrl + X to exit the nano editor.

Postgres Query with GPS distance calculations without PostGIS

Let’s consider the following scenario:
You have a Postgres Database and an addresses table which stores items with GPS latitude and longitude values. You want to select all the items located inside a circular area around a GPS point you have.

You can write your SQL query without using PostGIS extension.
Let’s define the following data:

1. table: streets
id | name | latitude | longitude

2. GPS_CENTER_LATITUDE, GPS_CENTER_LONGITUDE = coordinates of the center area point around which you wanna search streets

3. AREARADIUS = the area’s radius expressed in meters

select 
	count(*) 
from 
	(
		select 
			(acos(sin(radians(s.latitude)) * sin(radians(GPS_CENTER_LATITUDE)) + cos(radians(s.latitude)) * cos(radians(GPS_CENTER_LATITUDE)) * cos(radians(s.longitude-GPS_CENTER_LONGITUDE))) * 6371 * 1000) computedDistance, 
			s.name
		from 
			streets s 
	) 
as tempQuery 
where 
	computedDistance < AREARADIUS;

Running Multiple Simultaneous Headless Watir Scripts in Different Displays in Ruby

When running multiple headless Watir browser scripts in parallel, in Ruby, if you do not specify a different display number, every script will try to access the same browser window and will occasionally cause an error related to not being able to access a port.

The solution is simple, you need to provide a display number for your headless code, replacing a code like this:

Headless.ly do
    begin
	
    end
end 

with this version:

Headless.ly(:display => 3) do
    begin
	
    end
end 

where the number given for display must be different for every simultaneous headless script you’ll run.

WordPress search and replace serialized strings in database

Every now and then I take little projects which involve developing a website in WordPress.
Naturally I install it on a test development machine, do all the stuff required and then move the thing to live server.

The moving part is sometimes painful especially if the site was populated with real data (articles) during testing phase by the client and he doesn’t want to loose the data already entered.

Since WP serializes it’s settings and then saves them into database, a quick search and replace for URLs and other variables which usually change when the site is moved onto other server and domain it’s not productive.

There are some WordPress plugins which help do the search and replace part, but I usually don’t like relying on plugins. And if the installation is dead, I cannot access the plugin anyway, so it’s pointless.

I found a little piece of software which does provide a tremendous help when moving a wordpress site to another hosting or domain and which does not rely on plugins.

The best thing it can do is it can search and replace a string in the entire database, unserializing strings when it has to and serializing them back.

It’s a PHP script you just drop into a secretly named folder on the same level with /wp-admin and the rest and then you navigate with the browser to that folder’s path and the script will try to automatically connect to WordPress’ database and initialize.

You can find the script here: https://interconnectit.com/products/search-and-replace-for-wordpress-databases/, many thanks to it’s creators! :)

Find if a point is inside a polygon with native PostgreSQL

There is the big and exhaustive PostGIS which can be installed into your PostgreSQL database and will provide you with lots of geometry functions and types you can use for your complicated geometry problems.

But what if you need, like I needed, to find out wether a point is inside a described polygon or not and you don’t want to go into all the trouble of installing PostGIS?

Well, good news. As I was happy to learn, PostgreSQL has native support for some geometry types and operators.

For instance, it supports the type polygon, which has the following syntax:


( (x1,y1) , (x2,y2), ... , (xn,yn))

It also supports the following operator which means “contains”

@>

So, let’s put this two together and make a functional example:

-- postgresql demo for point-in-polygon with native support

create table test_polygon
("id" serial,
"data" polygon NOT NULL
PRIMARY KEY ("id));

insert into test_polygon (data) values ('((1,1),(12,1),(12,12),(1,12))');

-- example of query which will validate a point as being inside the polygon
select * from test_polygon where data @> '(3,4)';

-- example of query which will invalidate a point as being inside the polygon
select * from test_polygon where data @> '(24,14)';

The code is simple and powerful. If you combine it with an online tool for generating a list of coordinates for a polygon drew on a map, you can easily create systems which will compute if a GPS point is inside that map polygon.

As for PostgreSQL’s native support for geometry stuff, (which I believe is great), you can read more on the manual here: http://www.postgresql.org/docs/9.3/static/datatype-geometric.html and here: http://www.postgresql.org/docs/8.2/static/functions-geometry.html.

 

Putty automatic reconnect after internet interruption

I love working on my servers via SSH by using Putty. I think it’s a great SSH client.

However there’s one thing that annoys me the most: when my internet connection gets interrupted or when after I wake up my computer from sleep, all my Putty instances get disconnected. I have to log in again to all of them, make the initial commands in each of them, etc, etc.

I’ve recently discovered another Putty-fork client, called Putty Tray which you can find here: https://puttytray.goeswhere.com/.

The best new thing added by Putty Tray to the old and best classic Putty are two new options in the Conenction tab, targeting automatic reconnecting after interruptions: “attempt to reconnect on network failure” and “attempt to reconnect on system wake-up”. You can see them on the screenshot below:

putty-tray

 

Page 1 of 3123