Archive

Archive for the ‘Utilities And Other Useful Things’ Category

Arduino Project 5: Internet Control without Ethernet Shield

February 18, 2013 22 comments

I’m getting ready to start an Arduino home automation project, so I started looking at ways to interface with an Arduino across the internet. That way I’ll be able to control all of the lights, locks, etc. anywhere I have internet access.

Now, the obvious answer was to buy an Ethernet shield, but I already run a home web server so that seemed unnecessary. I saw a few solutions using Processing or Python scripts, but that seemed unnecessarily complicated. It took a fair amount of digging and brainstorming, but I’ve found an ultra-easy, ultra-flexible, and ultra-fast solution. Ultra.

There are a few major assumptions here.

  1. You have a computer running a web server that is accessible from the open internet, using a static IP address or a Dynamic DNS name. Your computer is either directly connected to your ISP and using a public IP address or your router is set up for port forwarding. If you don’t have this set up yet, just Google it – there are a lot of tutorials that explain how to set up a home web server.
  2. Your home web server has PHP. If not, this will still work, you’ll just have to re-write it in Java or whatever server-side language you’re using.
  3. The Arduino will be plugged into the web server via USB.

Here’s the basic concept: the Arduino can’t read files from the server via the USB serial connection, so the server will have to “push” the message. The server side code (PHP, Java, whatever you choose) cannot talk directly to the serial COM port, so we need a local script on the web server that can talk to the serial (USB) port. Last, the Arduino sketch has to be written so it can “catch” and process the message.

In this example, we’ll just create a barebones web page for controls that will turn an LED on and off via the Arduino. Let’s start with the sketch. The sketch is just listening on the serial connection for a 1 or 0. If it receives a 1 it will turn the LED on, if it receives a 0 it will turn the LED off.


/*
Alex Glover
February 2013
*/

void setup() {
Serial.begin(9600);
//set the LED pin to OUTPUT
pinMode(13, OUTPUT);
}

void loop() {
//wait until the serial connection is open
while (Serial.available() ==0);

//read from the serial connection; the - '0' is to cast the values as the int and not the ASCII code
int val = Serial.read() - '0';

//print to the console for testing
Serial.println(val);

//if we've recieved a '1', turn on the LED and print a message
if(val==1){
Serial.println("Received a 1");
digitalWrite(13, HIGH);
}
//if we've recieved a '0', turn off the LED and print a message
if(val==0){
Serial.println("Received a 0");
digitalWrite(13, LOW);
}
}

Pretty straightforward, right? OK, now we need two scripts, one to send a ‘1’ and one to send a ‘0’. In Windows, simply create a text file (call it whatever you want), and give it a .bat extension. In my setup, my files are called serial_out_0.bat and serial_out_1.bat. Each script has only one line of code.


ECHO 0 > COM3:

and


ECHO 1 > COM3:

Note that you might have to change the COM designation. You can check which COM your Arduino is connected to by looking in the Arduino IDE under Tools –> Serial Port. If you’re not using Windows, you should be able to do this pretty easily in a shell script. At this point you can test to see if the batch scripts will turn the LED off and on. Also ensure that the web server will be able to execute these scripts (don’t assign user-specific privileges or save them in protected directories). If you’re unsure, just put these scripts in the same web root directory where you’ll host your web page.

Easy right? Alright, the last piece is the web form. All you need are buttons within a form that will then execute the batch scripts we wrote earlier. Easiest solution is to use ‘submit’ buttons and then check for which post variables are set. The rest of the code is very straightforward so I’ll let it speak for itself.

<html>
<head>
<?php
if(isset($_POST[‘submitOn’])) {
exec(‘C:\xampp\htdocs\Home_Automation\serial_out_1.bat’);
}
else if(isset($_POST[‘submitOff’])) {
exec(‘C:\xampp\htdocs\Home_Automation\serial_out_0.bat’);
}
?>
</head>
<body>
<form action=”control.php” method=”post”>
<input type=”submit” name=”submitOn” value=”Submit On”>
<input type=”submit” name=”submitOff” value=”Submit Off”>
</form>
</body>

</html>

You’ll have to change the paths to correspond with the location of your scripts, but otherwise that’s it. Add some CSS if you want to add some polish to your controls. This is a very simple example, but you should be able to adapt this code to any project.

WordPress: Twenty Eleven Child Theme with Galleria Slider

January 28, 2013 Leave a comment

OK – I’ve been spending the last few hours getting acquainted with the WordPress backend (that sounds more risque than I had intended) and making my first custom ‘child’ theme.

If you have an interest in making custom themes and you’re just getting started, I’ve got a few very generic tips:

  • Don’t underestimate how complex some of the themes and frameworks can be
  • Start by creating a child theme or modifying an existing theme
  • Check out this tutorial on PressCoders

The tutorial on PressCoders is very clear, so I won’t re-invent the wheel and write a tutorial on making child themes. Instead, I wanted to share the child theme that I made. Basically, I’ve taken the Twenty Eleven theme and replaced the static header image with a dynamic image slider (using Galleria). You can check out a live demo, otherwise here’s a quick screenshot:

alexdglover_twentyeleven_slider

Anyways, as simple as it is, I’ve learned a lot from the exercise. If you want to use the theme or take a look at the code, here’s the package.

Thanks for reading.

Bootable USB with UNetbootin

January 14, 2013 Leave a comment

OK, so you have a netbook or ultrabook with no optical drive. Or maybe your optical drive is dead. Or maybe you’re smart enough to not use optical drives in general. Whatever the situation is, you want to run or install something from a bootable USB drive.

There are lots of solutions out there, but nothing beats the Universal Netboot Installer, or UNetbootin. UNetbootin has a simple GUI and never gives any grief. You can use your own ISO that you’ve downloaded or use one of the 42 ISOs UNetbootin provides, including BackTrack, CentOS, Ubuntu, as well as several rescue and recovery utilities.

Using UNetbootin is stupid easy. Grab a USB flash drive with enough storage to hold your ISO. Better yet – pick up a 16GB flash drive for $10 and it will be big enough to hold any ISO you could ever want. Got your USB flash drive? Let’s get started.

  1. Plug your USB flash drive into your computer. Note the drive assignment; in Windows, it will probably be D:\ or E:\, or some other letter. For the Unix users, your drive designation will probably be sda, sdb, sdc… sdx. Remember the drive assignment.
  2. Go to UNetbootin’s page and download the correct version for your OS.
  3. If you have a particular ISO you want to use, download it. Otherwise if you want to use one of the included ISOs, skip this step.
  4. Open UNetbootin. If you want to use one of the ISOs they provide, check the “Distribution” radio button and choose an ISO from the dropdown

    Thanks to UNetbootin for this image.

    If you want to use your own ISO, click the “Diskimage” radio button, click the browse button ” … ” and find your ISO that you downloaded.

  5. Change the drive to correspond with your USB flash drive. If you can’t find your drive, make sure it’s mounted and then restart UNetbootin.
  6. Click OK. If you chose a distribution provided by UNetbootin, it will take a fair amount of time to download the ISO before it can write to your USB drive. This is normal.
  7. Once the USB has been formatted and the ISO written to it, UNetbootin will restart your computer in an attempt to boot from the USB drive. If it doesn’t work, go into your BIOS settings and try changing the boot order.

Donezo. Hope you found this useful.

Monitoring Disks in RAID Array in Windows 7

December 5, 2012 2 comments

For many folks whose motherboards don’t support RAID or just want a simple, easy to implement RAID solution, software RAID is often the best solution. In Windows 7, Microsoft finally got around to providing out-of-the-box software RAID capabilities (apparently you could do it in Windows XP, but not without some real leg work). Only problem is Microsoft didn’t include a notification or alarm mechanism to deal with disk failures. The only notification is a passive entry in the Event Log, which is not obvious for basic users and not really convenient for anyone. This effectively defeats the purpose of RAID as a mechanism for data redundancy, as your disks could fail one by one until all of your data was unrecoverable, and you as the user wouldn’t realize until it was too late.

Click to see my solution

Easily Create a Store Locator

November 28, 2012 Leave a comment

I had initially set out to build my own custom store locator (just for the learning experience) and post a how-to article for it. I quickly found that this has been done many times before, and Google has a very good tutorial if you want to build your own.

But what if you don’t have a database behind your website? What if you don’t have any IT staff to support a custom store locator? What if you need an easy solution?

Check out BatchGeo, a free (with ads) hosted store locator service. All you need is a spreadsheet of your locations that you want to map and whatever secondary information you want to display. BatchGeo takes care of mapping the addresses, so you don’t have to determine the latitude and longitude values for each address (like in the Google tutorial). You can also group your locations arbitrarily, such as by location type in my demo, by simply adding another column of data.

alexdglover_batchgeo_map_demo

Once you supply your information and click “Map Now,” the map will be generated for you to preview. If all is well, just click Save & Continue. You’ll be asked to provide an email address and some basic information about the map you generated, including whether you want it to be public or unlisted. Then just click Save Map.

Finally, BatchGeo will email you the new map URL, as well as some basic iframe code to embed your map on your website. The map has great functionality, allowing you to search geographically (by city, zip code, whatever) or by secondary content (store name, group name, even phone number). Content display is great. Markers are lettered to correspond to the location listing below (as we’ve come to expect from Google maps). Markers are also color coded if you used grouping. Info windows cleanly display formatted secondary information such as web site and phone number.

If you want to get rid of the embedded advertisements, unfortunately it’s going to cost you $99.00 per month. If you can bear the ads, it’s an awesome free service.

Prevent SQL Injection Attacks in PHP Applications

November 26, 2012 2 comments

Ok, some quick background before we get started. Let’s say you have a search bar in your PHP-based web site to help people find items, which ties back to your database. Behind your search bar is some code and a query, something like

$query = "SELECT * FROM ITEMS_TABLE WHERE ITEM_NAME LIKE" . $mySearchBarString .";";

So a user provides some string, like ‘Xbox,’ and it finds all items with ‘Xbox’ in the name. Lovely. But what if the user enters this:

blah; SET @tables = NULL;
SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @tables FROM information_schema.tables;
SET @tables = CONCAT('DROP TABLE ', @tables);
PREPARE stmt1 FROM @tables;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

For those who can’t read SQL, here’s the short version – an attacker just dropped ALL of your databases, in all schemas. Whoops. This is your face right now:

We’re not going to let this happen to us, because losing and replacing that data seems like a lot of work, and we all have better things to do than restore backups and try to explain data loss to end users.

Let’s review some options

Deleting Files Based on System Timestamp

November 10, 2012 Leave a comment

Recently ran into an issue at work where a single database was creating A LOT of trace files. 54 gigabytes of trace files to be specific. Well, this was a problem because the system disk was only 56GB, preventing me from building new databases. No problem, its a dev environment, I’ll just delete the trace files that are older than 3 days. Except that the trace file naming convention wasn’t consistent, so there was no way for me to delete the oldest files based only on file name.

What about the system timestamp on each file? It seemed like a good solution, but I didn’t want to write a shell script to do something so minor. Then I got lucky on Google, and found this useful little gem:

find /full/path/to/directory -type f -mtime +3 -exec rm -f {} \;

I’ll translate – it will find all files in a given directory that have system timestamps from 3 days ago or greater. Clarifying note – the system timestamp reflects when a file was created OR when it was last modified. For each file that it finds that matches that criteria, it will execute ‘rm -f {file name}’ and delete the file!

But wait, there’s more…

%d bloggers like this: