Jan 25, 2011

Arduino + Power Switch Tail Fountain Controller

Syncing Things Up

My wife and I recently put in some new landscaping in our front yard, including low-voltage lighting controlled by a transformer/controller housed in our garage. The lighting controller had some nice features: you could turn the lights on at dusk via an optional photoelectric cell, and then turn the lights off with a timer, also an extra option. That was ideal for us, on at sundown, off at 10:00 PM.

At the same time we also purchased a granite, Japanese-style fountain that had embedded LED lights at the fountain throat. This looks dramatic at night as the lights are bright and the water reflects the light and, as our landscaper said, "looks like white fire".

Fountain + LEDs on at Night

We wanted the fountain and lights to sync with the landscape lighting controller, on at dusk, off at 10:00 PM. The fountain had a separate power cord for the lights and another for the pump. The landscape controller already had the timing we wanted, so it would have been ideal to connect it to the fountain/lights. However, neither our landscaper nor us wanted to try jacking the power for the fountain/lights directly into a $350 controller and watch it fry.

We tried using some digital timers, but these had some drawbacks. The cheap ones we looked at used extra-small fonts on their LCD display and a non-intuitive programming/setup process. If you ever had to reprogram the device, it was back to studying the instructions. The one we did buy offered a manual on switch, which we liked. We wanted to be able to turn on the fountain independently of the landscape lights, too. However, with the small font size and a confusing system for indicating when you were on manual, it was a painful device to use.

So, what to do?

Solution #1 - OptoIsolator
I felt confident I could cobble together a solution to the problem using an arduino and some other components. I just had to do some research. The problem definition has three parts:
  1. Power control of the fountain
  2. Detect when the landscape controller is on/off
  3. Be able to turn on the fountain/LEDs independently of the landscape controller
The first thing I found was a Power Switch Tail over at adafruit.com. This is a 120V power source with a relay triggered by a +5v/40mA signal, very easy to use. Adafruit packages the PST with a transistor and resistor so you can wire it right up to a microcontroller.

Power Switch Tail

OK, that takes care of power control. How to signal when the landscape controller was on?
The first thing I experimented with was an optoisolator. Very simply, this is an LED on one side of a gap, with a light detector on the other all encased in a tiny, light-proof shell. When the LED gets a voltage, it goes on, triggering the light detector. It's a way to isolate circuits from each other as you might want to do when one circuit is 12-15V AC (landscape controller) and one is operating on 5V DC (arduino).

I experimented with an optoisolator, hooking it up first to our old landscape controller. OK, seems to work fine. Let's try it out on the real deal, the new controller. Aaaaah! Turn it off, turn it off! A light pall of rubbery-smelling magic smoke appeared. Now I've done it. Check the controller. Whew! Still works....

What I had done was filched a circuit from the web and failed to read it carefully enough. I missed a ground. Oops. Time to review. Get it working, but it's cycling through on/off cycles while the controller remains constantly on. Hmn. Off to various forums for tips on how to use optoisolators. Found out that there are "double" optoisolators made just for AC current, and I had been using a "single" made for DC current. The landscape controller, while low voltage, was AC. The DC optoisolators can work, but you need to add some parts to the circuit. The optoisolators made to work with AC have two LEDs, each one picking up part of the AC cycle. The repeated on/off behavior I was seeing was the unmodified DC optoisolator only one phase of the AC cycle.


Exploiting Simplicity
While doing my research, I saw a forum posting where someone wanted to tell when a circuit powered on. One of the replies was "stick a night light in there and put a photocell on it to tell when the light comes on." Then it hit me: the landscape controller HAD a light inside of it, a power-on light that only operated when power was applied to the lights. Perfect!

Just had to do a little experimentation to calibrate the arduino to the returned photocell values. When I got values that unequivocally indicated the landscape controller was on, I turned on the power switch tail from the arduino.

Photocell Mounted inside Landscape Controller

Fountain Controller I/O: from Photocell; to Power Switch Tail

The only other thing needed was to mount the arduino in a project box. I added an LED to indicate power on for the fountain/LEDs, and an LED to show power on for the landscape controller. Finally, I added a pushbutton to be able to turn the fountain/LEDs on independently of the landscape lights.

Fountain/LED Power On Controller

Now, the Hack-a-Day crowd will argue that using an arduino for this is massive overkill. True!
But you can never have enough overkill. Seriously, if I had better skills I could have just used a 555 timer triggered by the photocell. It would have been much cheaper. However, I had taken too much time figuring out how to get this working, I had a wife to keep happy, and I already had familiarity with arduino and knew I could get it done with that.

Arduino Code:

#include

int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
//int optoPin = 9; // input from optoisloator/transformer
int pwrLED = 13; // Power on/off indicator light 0 = OFF; 1 = ON
int fountLED = 7; // Fountain on/off indicator light 0 = OFF; 1 = ON
int pushbutton = 5; // PushButton switch: Normally Open (0), when closed sends HIGH (1)
int transistor = 3; // Transistor 2N3904 or similar NPN attached through 10K-22K resistor
int pwr = 1; // Transformer State: Logic LOW (0) indicates ON; Logic HIGH (1) indicates OFF!
int prev_pwr = 1; // Previous power state, to detect change in power. Same logic as pwr
int fountain = 0; // Is the fountain on/off? 0 = OFF; 1 = ON
int pb = 0; // PushButton State: 0 = OFF; 1 = ON
int prev_pb = 0; // Previous PushButton State, to detect change in on/off

// Instantiate a Debounce object with a 20 millisecond debounce time
Debounce debouncer = Debounce( 20 , pushbutton);

void setup()
{

// pinMode(optoPin, INPUT);
// digitalWrite(optoPin, HIGH); // need to set pull-up resistor or values float!
pinMode(pwrLED, OUTPUT);
pinMode(fountLED, OUTPUT);
pinMode(transistor, OUTPUT);
Serial.begin(9600);
Serial.print("Starting...\n");

}


void loop()
{
//pwr = digitalRead(optoPin);

photocellReading = analogRead(photocellPin);

if (photocellReading >= 510)
pwr = 0; // POWER ON
else
pwr = 1; // POWER OFF


// Update the debouncer
debouncer.update();

// Get the update value
pb = debouncer.read();

if (pwr) // Logic 1 = POWER OFF
if (pb) // Pushbutton was pressed, Logic 1
if (prev_pb) // If it was ON, turn it OFF
{fountUpd(0);
prev_pb = 0;
}
else
{fountUpd(1); // If it was OFF, turn it ON
prev_pb = 1;
}
delay(100);

Serial.print("Pushbutton value is: ");
Serial.print(pb);
Serial.print("\n");


Serial.print("Power Setting = ");
Serial.print(pwr);
Serial.print("\n");

if (pwr != prev_pwr) // A change in power state occured
{prev_pwr = pwr;
if (!pwr) // Power was turned on
{fountUpd(1);
Serial.print("Power Detected!\n");
digitalWrite(pwrLED, HIGH);
}
else // Power was turned off
{fountUpd(0);
Serial.print("NO POWER!\n");
digitalWrite(pwrLED, LOW);
}
}
//delay(2000);

}
void fountUpd(int state) {
fountain = state;
digitalWrite(fountLED, state); // turn on fountain status LED
digitalWrite(transistor, state); // activate transistor for Power Switch Tail
}

Here's another example of this idea of "Exploiting Simplicity" : turning on/off a computer remotely via arduino hooked up to the computer power switch, grounding it out appropriately. Video by Mr. Hasselhoff of www.ZomgStuff.net



Update:
Checked into a 555 timer circuit that could do the same thing as the Arduino gadget I built:


The circuit on the left will use the presence of light as a trigger for the speaker to make a buzzing sound. As long as the photocell detects sufficient light, with sensitivity controlled by the 100k ohm potentiometer, the speaker will buzz. Using information from the circuit on the right, if you remove the 10u cap and speaker at pin 3 of the 555 in the Light Detector circuit and substitute in a 2.2k ohm resistor and an NPN transistor to drive your load, this modified circuit will work to trigger the Power Switch Tail. I tested this with a 2N3904 and it worked fine.

Jan 19, 2011

Unetbootin: Bootable Lunux on a USB Stick

Used Unetbootin to create a bootable USB flash drive of Jolicloud, a cloud-based version of Ubuntu. Jolicloud has its own USB creator app, but on Jolicloud 1.1 that version stops with the message "No drive selected". Unetbootin works well enough, but even that took me three tries. I was writing over a Ubuntu 8.10 install on the flash drive. What finally worked was using fdisk to remove the existing partition (vfat) and create an ext3 partition, followed by mkfs.ext3 /dev/sdb1 in this case.
See the docs at:
http://unetbootin.sourceforge.net/

Jan 13, 2011

Python+Arduino = Simple Twitter Search Client

There are lots of twitter/arduino mashup examples out on the web. Here's one that's pretty easy to put together.




Requirements:
  • computer running python + Twython, a python add-in for twitter
  • 20x4 serial-enabled LCD
  • an arduino or arduino clone
This was developed in Ubuntu 10.04 (Linux). I leave it as an exercise to the interested student to make this work on Windows or Mac.

Steps:
  1. Install python. You can do this from the Ubuntu Software Center.
  2. Install twython. Here's the source on github.
  3. Hook up your LCD to the arduino. Serial LCDs only need +5V, GND and digital signal to TX (outbound to LCD from arduino).
Notes:
  • I used the Modern Device LCD117 serial kit hooked up to a 20x4 LCD from an ebay seller. Another option is this one from sparkfun.com.
  • I used a seeedstudio seeeduino, an arduino clone. Serial TX to the arduino(the RX pin on the arduino) will cause a real arduino to auto-reset. The seeeduino has a switch for manual reset, which avoids the problem. Here's the comments from the arduino.cc site:

    This setup has other implications. When the Duemilanove is connected to either a computer running Mac OS X or Linux, it resets each time a connection is made to it from software (via USB). For the following half-second or so, the bootloader is running on the Duemilanove. While it is programmed to ignore malformed data (i.e. anything besides an upload of new code), it will intercept the first few bytes of data sent to the board after a connection is opened. If a sketch running on the board receives one-time configuration or other data when it first starts, make sure that the software with which it communicates waits a second after opening the connection and before sending this data.

    The Duemilanove contains a trace that can be cut to disable the auto-reset. The pads on either side of the trace can be soldered together to re-enable it. It's labeled "RESET-EN". You may also be able to disable the auto-reset by connecting a 110 ohm resistor from 5V to the reset line; see this forum thread for details.

Python code:
#!/usr/bin/env python
import feedparser
import time
import serial
from sys import stdout
ser = serial.Serial('/dev/ttyUSB0', 9600)

from twython import Twython
twitter = Twython()
while(1):
  time.sleep(5)
  search_results = twitter.searchTwitter(q="%23arduino",lang="en",rpp="1")
    # use '%23' to prefix hash tag
  for tweet in search_results["results"]:
      #print tweet["created_at"]
      #print tweet["from_user"]
      #print tweet["text"]
      to_arduino = tweet["from_user"]+": "+tweet["text"]
      #print to_arduino
      ser.write(to_arduino[0:80].encode('utf-8'))
      time.sleep(5)
      ser.write(to_arduino[80:159].encode('utf-8'))
      time.sleep(5)
#end of script

Arduino code:
/***************************************************************************
* Twitter search client. Can be modified to display other
* RSS feeds by changing the source python script.
* Expects data input to serial (RX on arduino) to be formatted
* for your display. This example used 20x4 LCD (20 chars x 4 lines).
*
* Tested to work with Modern Device LCD117 Serial Kit (phanderson product)
*
* This is really just a general serial receive and print routine
*
* thisoldgeek@gmail.com
* 01/13/2011
****************************************************************************/



byte incomingFeeds;
int i = 0;
char feed[180]; // current feed from RSS

void setup()
{Serial.begin(9600);
delay(100);
Serial.print("?c0"); // set no cursor
Serial.print("?f"); // clear screen
}

void loop()
{
i = 0;

while(Serial.available()) // are there any bytes on the serial port ???
{
incomingFeeds = Serial.read(); // one instance of feed
feed[i]=(byte(incomingFeeds)); // store the feed chars in an array
i++;
}
feed[i]='\0'; // need to end the string

if (strlen(feed) >= 1)
{ Serial.print("?f"); // clear screen
Serial.print(feed); // print all 4 lines.
//May need a different print rtn for other serial LCD's
}
delay(1000); // needs a delay or drops most of the characters from the sender
}

Jan 8, 2011

cupsd not starting? Try this...

My cupsd wasn't starting on boot, I'd always have to do:
sudo service cups restart
Make sure the only thing in /etc/network/interfaces is the following two lines:
auto lo
iface lo inet loopback

I had the definition of eth0 in there, turns out it was actually causing a problem. Eth0 also wasn't showing up in the network manager notification area from nm-applet, so I commented out the /etc/network/interfaces lines related to eth0 and set up an entry in network manager for the specific MAC address of the on-board ethernet with the same parameters that I had in /etc/network/interfaces.
Rebooted, seems to work.


Jan 4, 2011

Ubuntu 10.04 Update Flatlined (temporarily) my Ethernet Card

On my Biostar TH55B HD motherboard, I have on-board Ethernet connectivity. Did a kernel update on Jan 03, 2011 to the following:
 2.6.32-27-generic #49-Ubuntu SMP Thu Dec 2 00:51:09 UTC 2010 x86_64 GNU/Linux
The update wanted a restart, so I did. No connectivity after that.
Hmn....
Well, let's see if it's hardware or software. This box dual-boots Windows 7 and Ubuntu 10.04 64 bit LTS. Boot Windows 7, LAN connectivity works just fine. OK, let's go back to Ubuntu and start troubleshooting.
Works fine in Ubuntu now. Go figure.