Google Analytics

Wednesday, October 26, 2016

Wish you bed had LEDs that turned on when you got up so you didn't stub your toes on your dresser?

Duh.



Here's how I did that.

A rough diagram explaining the components.

My bill of materials

  • Pi Zero (With USB Wifi dongle and MicroSD card)
  • 2 PIR sensors
  • 2 momentary push buttons 
  • 5v 5A power supply
  • Strip of Neopixels (150/5meters)

The premise is simple.  Detect motion, use math to figure out if it's dark outside, and if so, turn the lights on until there is no motion for about 5 minutes.  2 buttons have been added so you can turn it on and off on demand.  

You can find a the code and documentation here: Dhowdy on GitHub



And, of course, a video of the LEDs turning off


Please post any questions below!  Enjoy!

Monday, May 23, 2016

Update to VM_URL_Passer

Adding a simple bit of authentication to the VM URL Passer scripts.

Edit: Updated code can always be found on my github: https://github.com/dhowdy/VM-URL-Passer/


I got tired of network scans causing my script to open new Chrome windows, so I'm adding a bit of authentication.  Keep in mind that all of the data is still passed in cleartext, so this is not making it more secure to people wanting to force you to check out a website with a horrible photo but rather causing innocuous port scanners to not cause random popups.  

The updated client and server is as follows:

VM_URL_Passer_Server.pl

#!/usr/bin/perl
use IO::Socket::INET;

# auto-flush on socket
$| = 1;

# creating a listening socket
my $socket = new IO::Socket::INET (
    LocalHost => '0.0.0.0',
    LocalPort => '7777',
    Proto => 'tcp',
    Listen => 5,
    Reuse => 1
);
die "cannot create socket $!\n" unless $socket;
print "Running on port 7777\n";

while(1)
{
    # waiting for a new client connection
    my $client_socket = $socket->accept();

    # get information about a newly connected client
    my $client_address = $client_socket->peerhost();
    my $client_port = $client_socket->peerport();
    #print "connection from $client_address:$client_port\n";

    # read up to 1024 characters from the connected client
    my $data = ""; 
    $client_socket->recv($data, 4096);
    my @datastring = split('~~',$data);
    if ($datastring[0] eq "myweakasspassword"){
         print "received data: $datastring[1]\n";
         system("'/usr/bin/google-chrome' \"$datastring[1]\"");
    }else{
         print "Wrong password.";
    }   

    # write response data to the connected client
    $data = "ok";
    $client_socket->send($data);

    # notify client that response has been sent
    shutdown($client_socket, 1); 
}

$socket->close();

url_tcp_client.pl

#!/usr/bin/perl
use IO::Socket::INET;

# auto-flush on socket
$| = 1;

# create a connecting socket
my $socket = new IO::Socket::INET (
    PeerHost => '192.168.100.100',#IP of the host machine
    PeerPort => '7777',
    Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";

# data to send to a server
my $req = "$ARGV[0]";
my $pass = "myweakasspassword";
my $data = "$pass\~\~$req";
my $size = $socket->send($data);
#print "sent data of length $size\n";

# notify server that request has been sent
shutdown($socket, 1);

# receive a response of up to 1024 characters from server
#my $response = "";
#$socket->recv($response, 1024);
#print "received response: $response\n";


$socket->close();


The only real change is that the client now sends a "password" to the server separated by two tilde (~~).  The server splits the string into an array on "~~" and treats the first part as a password and the second as the actual data.  

Again: this isn't secure, but it doesn't really need to be.  Just a quick hack to make VMs a little bit more usable.

Wednesday, October 21, 2015

Opening a garage door from the Internet

Who wouldn't want to be able to control their garage door over the Internet?  No one, that's who.

I've recently mounted a 7" Android tablet in my wife's car to control navigation, music, realtime ODB II data, and various other tasks.  My wife then asked if she could control the garage door with the tablet.  "Well..... you can, but I have to build something to do that."  "Ok, then build it."

The requirements of the build are as follows:
  • It must give the status of the door (open/closed/unknown)
  • It must be able to toggle the status of the door
  • The WAF must be very high
  • It must work with at least Android, but all platforms would be ideal 
  • It must be secure since it's Internet facing
  • It must be reliable and therefore simple

The obvious place for me to start is with a Raspberry Pi - it's cheap, has network connectivity, runs Linux, and I have a spare sitting around the house waiting for a project.

It also seemed logical to make this as a web app instead of a native Android app so 0) it would be instantaneously up to date, 1) it wouldn't cause any battery drain from having to check the status every 30 seconds to stay up to date, and 2) for me, a web app is much easier to develop.

My garage door (like most) has a simple mechanism to trigger opening and closing the door.  Simply short two pins via a push button or, in this case, a relay.  The problem is that a Pi can't activate a relay directly with GPIO.  Well, not safely anyhow.  So we need to a very small amount of power to trigger an NPN transistor in order to trip the relay.  Since I'm using a 5V relay, I can safely pull that voltage from the 5V pin on the Pi.  The diode is there to prevent damage to the Pi when the field from the relay's coil collapses.

The electronics are pretty straightforward. A transistor activates a relay to open and close the door and 2 magnetic switches report the door's status back to the Pi.


The 10K resistors are there as pull up resistors.  Basically it makes GPIO pins 23 and 24 read as "high" unless the switch is activated.  Once the switch is activated the voltage goes to ground and the pin goes "low".

If you look at other projects on this page, you'll notice that I'm a big fan of using Cat5.  It's great: it's cheap, reliable, and able to carry signals over long distances without degrading.  In this case I ran 1 line of Cat5 to the garage and split 3 pairs off of it. The blue pair go to the garage door, the green pair go to the "opened" switch, and the orange pair go to the "closed" switch.

I decided to use 2 different switches instead of one because "not closed" or "not open" is not good enough for this project.  I wanted 2 switches to be in agreement as to the state of the door.  These are cheap door switches from Amazon - about $7 for a pack of 5 but they work great.


The activation circuit.  The blue and blue/white wires go to the garage door.


The switch to confirm that the door is opened

The switch to confirm that the door is closed

I simply soldered the resistors inline instead of bothering with another PCB.
Heatshrink tubing is awesome.
The finished hardware on a shelf in the basement (where no one can see it).
Now on to the software!

I used Inkscape to generate the SVG that I used to display the status of the door.  This project was largely an exercise for me to learn a little about CSS and SVGs.  This is the first thing like this that I've done from scratch and I know that there's room for improvement.  Please share ideas in the comments section if you have any constructive insight!

The idea is that the SVG will have either opaque or transparent doors depending on the status of the magnetic switches when the page loads.  When clicked, the SVG will have an animation of the doors either going up or down.  Once the animation is complete (which gives the door plenty of time to open or close) the page will refresh showing the status of the door as a confirmation. 



The code can be found on my GitHub account here: https://github.com/dhowdy/GarageDoor.

Security is accomplished via Apache with ModSSL and basic authentication.  The guys at DuckDNS and PortForward have everything that you need to access your Raspberry Pi from the Internet.  


Lastly, I have to say thank you to to Kevin Sangeelee for his great write-up on using a Pi's GPIO to activate a relay, Justin McCandless for patching jQuery's GLARING lack of SVG class support, and to Nate Krofft for giving me a huge hand with some of the CSS.  And, since I know that I'm going to get a lot of this... I know that I'm not the first person to do this; I simply wanted to share *my* way of doing this with everyone in case it can be helpful to someone else.

Thanks for reading!


Sunday, February 22, 2015

Building a clock to help kids tell time

My daughter is about 2 1/2.  She loves learning about colors, shapes, animals, dinosaurs, and all sorts of other things.  However, she's still too young to understand how to read a clock.  But now is a good time to start teaching her.

We wanted a clock that would change colors based on what it was time to do: play quietly, go to bed, get up and play, pick up toys, and so on.  Here is the result:



Alarm clock next to the baby monitors and entertainment center.



To start, I purchased a wife-approved, battery-driven clock from a local retailer for about $10.  This one was perfect for me: it has plenty of room inside, a nice, big face, some weight to it to hold it in place, and, to top it all off, it matched the wall.

OK, enough introduction, let's get down to it.  I've got a Pi running a baby monitor (another project, another write-up, but the LEDs in the lower left corner are a super bright infrared spotlight for night vision on the cameras), so I'll be driving it from that. And I've got some ShiftBrites leftover from version 1 of my DIY Ambilight (scroll down for more info on that project).  Perfect.  Let's build.

First, I needed a power supply.  Since we have an excess of USB ports, that's what we'll use.  I've already built a power supply based on the LM317T and it's been running for 7 or 8 years now.  Using this basic schematic, I built the power supply to take a 5V input:
(credit to http://kb.kaminskiengineering.com/ for the picture)




(I know, my workbench is cluttered.  I do a lot of work on it.)
This power supply is going to supply ~3V to the clock internals and 5V to the ShiftBrite.

Now we need software to test with.  After I could not get this tutorial to work, I found Hive13's project on GitHub.  After playing with it for a bit, I compiled the C program and was getting good tests.  However, I could not figure out a dead simple way to to just set certain colors and the program looped until interrupted - all of this is great for what Hive13 and likely most people want to do with a ShiftBrite on a Pi, but I needed something different.  I forked his GitHub repo and made a few changes, namely the ability to push red, green, blue, purple, yellow, and cyan from the command line and immediately exit.  I also added an "install script" as well as a "crazyclock" script to cycle through the colors.  It's all kind of hackish, but since the code was written for only my specific example, I don't care - but sharing that code back to the community is still the right thing to do.  

Essentially, I just added 6 command line arguments so instead of only having "-c [0-255]" emitting a white color with the given intensity, I've added the following code:

printf("  -R: Set to constant color Red\n");
printf("  -G: Set to constant color Green\n");
printf("  -B: Set to constant color Blue\n");
printf("  -P: Set to constant color Purple\n");
printf("  -Y: Set to constant color Yellow\n");
printf("  -C: Set to constant color Cyan\n");
.
.
.
} else if (opt->mode == RED && opt->constant_value_red >= 0) {
img[0] = opt->constant_value_red;
shiftbrite_refresh();
return 0;
} else if (opt->mode == GREEN && opt->constant_value_green >= 0) {
img[1] = opt->constant_value_green;
shiftbrite_refresh();
        return 0;
} else if (opt->mode == BLUE && opt->constant_value_blue >= 0) {
        img[2] = opt->constant_value_blue;
        shiftbrite_refresh();
        return 0;
} else if (opt->mode == YELLOW && opt->constant_value_yellow >= 0) {
        img[1] = opt->constant_value_yellow;
img[0] = opt->constant_value_yellow;
shiftbrite_refresh();
        return 0;
} else if (opt->mode == PURPLE && opt->constant_value_purple >= 0) {
        img[2] = opt->constant_value_purple;
        img[0] = opt->constant_value_purple;
        shiftbrite_refresh();
        return 0;
 } else if (opt->mode == CYAN && opt->constant_value_cyan >= 0) {
        img[2] = opt->constant_value_cyan;
        img[1] = opt->constant_value_cyan;
        shiftbrite_refresh();
        return 0;

All of this is on GitHub, but essentially calling the binary with "-R 255" will turn the LED red at full brightness and immediately exit.  Simple and straightforward.

It's also worth mentioning that since this box is secure and I didn't want to mess with the proper permissions to grant other users the ability to use SPI, I just changed the setuid attribute of the binary to always run as root.  Again, this would should never fly in a production environment but in this case it's nothing to worry about.  

SPI pins on the Pi.  
Next is the case mods.  There was not enough room to put the ShiftBrite, so my Dremel helped me make it fit. 

 
Essentially from here, I just hooked the Vout pin of the LM317T to the clock's battery terminal lines and attached the ShiftBrite directly to the 5V line coming off of the USB cable.  Then I crammed everything as neatly back in to the case as I could.  I had to cut some pins and use some hot glue to insulate some things, but it worked.


There is a USB cable running in to the back for the power supply and a piece of Cat5e running out the back for data from the Pi.
The clock installed next to the baby monitor cameras.


The last thing is setting the clock color automatically.  I found a nifty little project called Minicron. Essentially, it's a client/server setup that modifies your cron jobs and sends the output back to the server.  It's simple, straightforward to set up, and perfect for what I need (given my lack of web development skills).  I installed it on my web server and on this Pi.  Then I just configure what colors I want the clock to be and when.


Additionally, I've configured ConnectBot on my wife's phone to have different connections that will just log in using a public key and execute "/usr/local/bin/clock -G 255" for play time or "/usr/local/bin/clock -R 75" to turn the clock red at reduced brightness for nap.  The cron job will take care of almost everything, but she needed the ability to change it on demand.  

So, that's pretty much it.  I'll be happy to answer any questions below.  



UPDATE: I realize that I forgot to mention that it is important to keep the clock and Pi synchronized.  Since I use Raspbian on the Pi, here's how I ensure it has correct time by using a Stratum 1 time server in my home town.

apt-get install ntpdate
service ntp stop
update-rc.d ntp disable
echo -e '#!/bin/sh \n/usr/sbin/ntpdate 0.ntp.mst.edu' > /etc/cron.daily/ntpdate
chmod +x /etc/cron.daily/ntpdate
service cron restart

This will get the Pi to update its time every day.  If you notice a skew over the course of a day (since the Pis lack a realtime clock) then you can just move this script to the /etc/cron.hourly folder.  If you do so, please us a different time server than this one.



Friday, February 21, 2014

Making VMs play nicely with hosts (by passing URLs)

I've recently moved to Linux (currently Mint) as my primary desktop at work and I need to run a Windows VM in order to use Outlook.  I'm running Outlook 2013 in Unity mode and kept getting aggravated that when I clicked links it would open the link in Chrome on the VM and not in Chrome on my host OS.

 When VMWare Tools installed, it created a registered application named "Default Host Application" that is apparently used for VMware Fusion on OS X and is useless on a Linux or Windows host.  VMware Tools would give the following error:

Make sure the virtual machine's configuration allows the guest to open host applications.

After searching high and low for a solution other than to make it go away, I gave up and wrote my own.  Here's the idea:

Set up a listener on the host OS that will just listen for a client and pass anything that it gets to Chrome so it will open a new tab.  Now we'll set up a client on the VM to forward any URLs that it receives to the listener on the host.  And finally, we'll configure our client on the VM to be the default for all HTTP/HTTPS URLs.  Pretty simple, right?  Oh, and we'll use Perl so it will work with Windows, OS X, or Linux on the host OR the guest.

Let's get started by installing Perl on your Windows VM.  OS X and Linux should have it already.

So we'll start with the server code.  I borrowed the majority of this form a few sites online and tweaked it to my requirements.

VM_URL_Passer_Server.pl
#!/usr/bin/perl
use IO::Socket::INET;

# auto-flush on socket
$| = 1;

# creating a listening socket to listen to port 7777 on all IP addresses.
my $socket = new IO::Socket::INET (
    LocalHost => '0.0.0.0',
    LocalPort => '7777',
    Proto => 'tcp',
    Listen => 5,
    Reuse => 1
);
die "cannot create socket $!\n" unless $socket;
print "Running on port 7777\n";

while(1)
{
    # waiting for a new client connection
    my $client_socket = $socket->accept();

    # get information about a newly connected client
    my $client_address = $client_socket->peerhost();
    my $client_port = $client_socket->peerport();
    #print "connection from $client_address:$client_port\n";

    # read up to 1024 characters from the connected client
    my $data = "";
    $client_socket->recv($data, 4096);
    #print "received data: $data\n";
    
    #Put your path to Chrome here!!#
    system("'/usr/bin/google-chrome' \"$data\"");

    # write response data to the connected client
    $data = "ok";
    $client_socket->send($data);

    # notify client that response has been sent
    shutdown($client_socket, 1);
}

$socket->close();

Simply change line 34 to point to Chrome.app on OS X or Chrome.exe on Windows.  Running this script will invoke Perl and it will sit listening for connections.

url_tcp_client.pl
#!/usr/bin/perl
use IO::Socket::INET;

# auto-flush on socket
$| = 1;

# create a connecting socket
my $socket = new IO::Socket::INET (
    PeerHost => '255.255.255.255',
    PeerPort => '7777',
    Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";

# data to send to a server
my $req = "$ARGV[0]";
my $size = $socket->send($req);
#print "sent data of length $size\n";

# notify server that request has been sent
shutdown($socket, 1);

# receive a response of up to 1024 characters from server
#my $response = "";
#$socket->recv($response, 1024);
#print "received response: $response\n";


$socket->close();

Running this script on your guest VM with any arguments will pass the string to the host and attempt to open it with Chrome.  Simply replace 255.255.255.255 on line 9 with the IP address of your host and you should be in business.  I've left some debugging code in there so if you have problems you can uncomment it and try again.


Finally, we need to register our client script as the default HTTP, HTTPS, and FTP handler in our Windows VM.  Start by saving this to install.reg, modifying it to point to the location that you've saved the client script (I used the root of the C: drive) as well as the path to Perl, and importing it.  It is safe, but you should know what you are doing if you are modifying the registry.

Install.reg
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\dhowdy\VMURLPasser\Capabilities]
"ApplicationDescription"="VM URL Passer is a Perl TCP client that sends all clicked URLs to the specified TCP Server in order to open links clicked in a VM to the host."
"ApplicationIcon"="C:\url_tcp_client.pl,0"
"ApplicationName"="VM URL Passer"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VMURLPasser\shell\open\command]
@="\"C:\\Perl64\\bin\\perl.EXE\" \"C:\\url_tcp_client.pl\" \"%1\""


[HKEY_LOCAL_MACHINE\SOFTWARE\dhowdy\VMURLPasser\Capabilities\URLAssociations]
"ftp"="VMURLPasser"
"http"="VMURLPasser"
"https"="VMURLPasser"
"irc"="VMURLPasser"
"mailto"="VMURLPasser"
"mms"="VMURLPasser"
"news"="VMURLPasser"
"nntp"="VMURLPasser"
"sms"="VMURLPasser"
"smsto"="VMURLPasser"
"tel"="VMURLPasser"
"urn"="VMURLPasser"
"webcal"="VMURLPasser"


[HKEY_LOCAL_MACHINE\SOFTWARE\RegisteredApplications]
"VM_URL_Passer"="Software\\dhowdy\\VMURLPasser\\Capabilities"


[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VMURLPasser]
@="VM URL Passer Document"
"URL Protocol"=""



[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VMURLPasser\shell]


[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VMURLPasser\shell\open]

Now that our program is "installed" we need to tell Windows to use it.  Open Control Panel > Default Programs > Set Default Programs.  Find "VM URL Passer" in the list, click "Choose defaults for this program" and check the protocols you wish to have passed to Chrome on your host.  I chose most of them, but the important ones are FTP, HTTP, and HTTPS.



Click "Save" and you should be good to go!  Try clicking a link in Outlook on your guest VM or just open Start > run > type 'http://dhowdy.blogspot.com' and press enter.  It should open in Chrome on your host!


Disclaimer:  This was a quick and dirty hack and there is no security at all.  Anyone on your network can send anything to your browser if they find that you have that port open.  Use this with caution and if anyone has suggestions for improvement, feel free to post below!

Tuesday, October 11, 2011

Updated DIY Arduino Ambilight

I have updated my DIY Arduino Ambilight!

After using my DIY Ambilight for a few days I got tired of plugging in and unplugging the power supply to the ShiftBrite shield, so I finally broke down and added a switch to turn it on and off.  Now that I've been using it for a few weeks, I'm tired of having to turn it on and off manually every time I want to watch TV.

I noticed that my Samsung TV had a serial interface in the form of the EX-Link connector to see if I could electrically tell if the TV was on or not.  No luck.  I tested the USB ports next and found that when the display is off, the USB interfaces have about .15V and just over 5V when the display is on.  Bingo.  Now to get a relay.


This relay requires 3.5V to activate and less than .25V to deactivate. 




My next step was to add a relay to my ShiftBrite shield and cut an extra USB extension cable and wire the +5V wire (red) and the ground (black) wire to the coil of a relay.  My DIY ShiftBrite shield is now wired as the above illustration indicates with the USB +5V & Ground lines coming from the TV.  Since the coil in the relay will cause a brief voltage spike that could damage your TV's USB port, it is important to make sure that the protection diode is added.  Just add it 'backwards' across your relay coil so the energy stays in the coil and doesn't go back into the USB port.


Here is what my finished ShiftBrite shield looks like (the protection diode is on the bottom out of view):

















Since most LCD TVs now have USB ports, I figured that this build could end up helping someone else.  I certainly hope so!

Friday, October 7, 2011

Field Day - Shooting a 1 lb propane tank with an AR 15.

Field Day Autumn 2011

 We try to hold Field Day at least twice a year for our Desktop Engineering students.  This year Field Day was on 10/07/11.  We decided to try to shoot a propane tank this time.  Here is what happens when you shoot a 1 LB propane tank with an AR15 (.223) from about 35 yards.  I was standing about 15 yards off to the side filming this.  I'm glad Bryan is such a good shot.