Getting a random string from MySQL for Arduino

When I modded Thomas Taylor’s Tweeting Dryer project to add a washing machine I also wanted to change the way the phrases used by the washer and dryer were implemented. In the original project the strings were hardcoded as a set of global variables. To add a new phrase to the washer or dryer the code needed to be changed in three places then uploaded to the Arduino. I didn’t want to recompile for a string change, so I decided to make my tweeting machines pull their random tweets from a MySQL database on the web.

A usb plug with a forbidden sign

Note: for details and code see my earlier post: Tweeting washer and dryer for Arduino with Ethernet.

Below is a snippet of the code before my change.

#define NUM_MSGS 2 // change this
prog_char msg_0[] PROGMEM = "Hey, I can't fold these things by myself.";
prog_char msg_1[] PROGMEM = "Come and get me while I'm all warm.";
// add a line here
PROGMEM const char* messages[] =
{
msg_0,
msg_1
// Add a line here
};

To add a new string you need to 1) update the #define to reflect the new message count 2) add the string as a new prog_char 3) add to the messages array. Just doing that much work to add a single string is irritating… and you still need to plug your Arduino into your PC to upload your changes (and I bet your development machine isn’t in the laundry room).

Moving the strings from code to MySQL was accomplished with a new function: strFromMySQL. The new function takes a string as input (“washer” or “dryer” in my case) and returns a random quote as a string. The function calls a PHP file which in turn requests a single, random row from a database of quotes. When the string is passed back to the Arduino by the PHP pages the Arduino uses the text finder library to parse the random quote string from the HTTP response.

Setup requires three things:

  1. Create one or more MySQL databases to host your quotes
  2. Modify and upload the PHP files
  3. Update the code with your server name and any needed changes to the path

The database schema is simple, you need tables for washer and dryer quotes and each of those tables need a column to hold the text. To use the code with minimal modifications create two tables, “Dryer” and “Washer”. In each table create a tinytext column named “thequote”. Fill in a few rows of data, using the strings from the original dryer code is a great place to start.

Once you’ve created your database download and modify the PHP files: strFromMySQL. You’ll need to update the conec.php file, adding the information for your MySQL database created in step 1. At this time you can also take a quick look into the washer and dryer PHP files to see what’s going on. The magic here is really not that magical at all, it’s just sorting the database in a random order and limiting the results to a single row.

select * from Washer order by rand() limit 1

Once you’ve uploaded the PHP files to your server it’s a good time to do a quick sanity test to make sure things are working correctly. Open up a web browser and open either your washer or dryer PHP page from the server. If all is working correctly you should see a blank page in your browser. It’s blank because the PHP is designed to return a minimal result so the Arduino has less work. The next step to testing is to right click in the page and view the source (e.g. “view page source” in Chrome or “view source” in IE). When you view the source you should see a quote in angle brackets (e.g. “<Hey, I can’t fold these things by myself.>”). If you don’t see a quote, circle back and check your settings in conec.php and check your MySQL settings. Also make sure PHP is correctly configured on your host and can successfully talk to MySQL.

Now that the PHP pages uploaded and talking to MySQL it’s time to customize the Arduino code.

  1. Turn the on pulling string from the web: set WEBMSG to true (1) on line 6
  2. Add the host name of your string server (the PHP pages, not the MySQL db): update stringServer on line 106
  3. Update strFromMySQL to use the path to your PHP files: change the YOUR_PATH_HERE string to match what is between your host name and, for example, washer.php (line 640). If the URL for your washer.php file is  http://example.com/arduino/strings/washer.php then line 640 should be:
    client.println(“GET /arduino/strings/”+ theDevice+”.php HTTP/1.1″);

You should be ready to go at this point. Recompile the code and upload to your Arduino.

With the strings and the code separated you are now free to add new tweets to your washer and dryer without coding and without fear of running out of memory. Any time you want to add a new string, just go to your database and add a new row, the new string is instantly added into the random queue.

How did this work out for you? Do you have any clever new tweets for a washer or dryer? Share your results in the comments below.

Tweeting washer and dryer for Arduino with Ethernet

My tweeting washer and dryer have been running great for a bit now and I’ve done some code cleanup to make it less embarrassing to share. The code attached to the post  is designed to use an Arduino Ethernet shield, not the WiFly module from the original example. If you already changed your tweeting dryer to use an Ethernet shield you can probably just download this code and upload it with little-to-no modification.

Download the Arduino code, circuit diagram and PHP files from my Git repo: WasherDryerMessenger

I’ve added a number of things to my code which weren’t in the original. I have, however, put them behind configs so I believe you should be able to use the code without hardware changes.

Here are two relevant configs at the top of the code:

  1. #define WASHER 0: In the sample code I have tweeting for the washing machine turned off. If you add another current sensor to your hardware you can turn this on to get tweets from your washer too.
  2. #define WEBMSG 0: This setting enables pulling the pithy tweets from a database on the web. Before enabling this there is some setup required, see: Getting a random string from MySQL for Arduino

If you want to have a tweeting washer as well as dryer, the hardware change you’ll need to make is to duplicate the current sensing circuit already in place for the dryer and add a third LED. In the diagram below left I have recreated the original circuit from Thomas’s Dryer Messenger in breadboard format (click for full size). On the right I show the added washer circuit (note: Ethernet shield not shown for simplicity).

Breadboard circuit diagram  Circuit diagram for washer and dryer

When you finish the modified circuit you can test it by plugging the dryer into the new transformer jack and updating the dryer pin setting in the code. Only after you’ve tested the hardware should you plug in both appliances and turn on the washer tweeting using the WASHER define (see the configuration notes earlier in this post). If you want the same account to tweet both washer and dryer status you can duplicate your dryer OAuth token for the washer on line 103. I elected to have separate handles for my washer and dryer and obtained a second OAuth token from Arduino-tweet.appspot.com.

At this point you should have a tweeting washer and dryer but you’ll notice the tweets are the same every time and rather boring. To add variety back to your tweets you should follow my instructions for enabling the WEBMSG for getting random tweets from a database on the web: Getting a random string from MySQL for Arduino.

Have you added a washer to your setup? Share your washer’s twitter handle in the comments below.

Tweeting dryer, now with washer too

Hallelujah! Today I finally finished adding washer monitoring functionality to the tweeting dryer setup I built earlier (see: Getting started with Arduino). Say hello to Gus!

A picture of a washing machine

My implementation was built up from the excellent work by Thomas Taylor for his article in Make, The Dryer Messenger. It provided a great starting point for this project but I wanted a bit more… I wanted to have my washer tweet and I wanted the washer/dryer quotes to be pulled from the web. Should be simple enough, right? I was thinking that after I added an additional connection to the hardware configuration that it should be as easy as duplicating the dryer code and renaming some functions. However, nothing is never that easy. My optimistic guess was that I’d add 100 lines of code and in the end it was more like 300. It was a fantastic learning experience.

A quick overview of my upgrades:

  • Added code to automatically pick a baseline for the current draw of each device.
    The original example required you to play with the values a bit to define the “off” value for the dryer. I added a loop to take the state of both washer and dryer at startup and set the baseline values automatically.
  • Made it so both devices would pull a random string from a MySQL database.
    By moving the strings of the Arduino and onto the interwebs I made it so I can go to the web any time I feel like adding a new pithy quote. Before, if you wanted to add a new string you needed to change the code in a couple places, recompile and re-upload to the hardware.
  • Various changes to make the code ready to monitor two devices.
    There were some minor changes I needed to make to the main loop (move the status LEDs into washer or dryer specific functions) and fix the “takeCurrentMeasurement” function (it had the dryer pin hardcoded as A0).

 

I’ll share out my code, an updated circuit design as well as a quick write up on how I’m pulling random strings from the web. Right now, however, I need to go do a load of laundry.

Getting started with Arduino

I’ve been reading about Arduino in Make magazine for years now and decided to jump in. I picked up a starter kit from Adafruit along with Make’s excellent “Getting Started with Arduino” book. The tutorials were a perfect introduction to electronics and provided easy code to get started modifying. Anyone with even the most basic understanding of computer programming can be up and creating in no time flat.

WP_20130419_001

One thing I’ve been wanting to do for a long time is build a weather station for logging temperature, humidity, etc. The parts for Arduino are easy to come by and there are a ton of examples out there to get you started. That was, however, until I came across Make’s really cool Dryer Messenger project. Built with an Arduino, a network adapter and current sensor, this nifty gadget makes your dryer send out tweets when it finishes a load. Yep, no more forgetting your load of laundry only to find it the next day as a mass of un-ironable wrinkles.

WP_20130427_007 WP_20130427_005

Armed with Make’s excellent instructions, a metal project box from Radio Shack and a non-invasive current sensor I set to work recreating the Dryer Messenger. I am using an Ethernet shield instead of the wireless option used in the article, so I had to change the code a little bit… but that was simple given the sample code.

WP_20130427_004

It took a couple of weekends to get it all together and running, but that was more a function of life getting in the way of fun. Now we can say that we are part of the IOT (Internet of Things) revolution. Our friendly dryer, Sam, is now up and running with his very own Twitter account: @3rdPlaceDryer.

Easy visualizations for your Arduino data

Over the years I’ve had several different time & temperature weather stations which gave the current outdoor temperature and humidity but were a bit thin on capturing historic data. I’ve considered buying a fancier weather station which connects to a PC for logging… but those are pricy and require a PC be connected and on. An Arduino, on the other hand, is wicked cheap and tons of people use them as the brains behind their weather stations (IOW: lots of example code is out there to be had).

It’s fairly common to log data from an Arduino to an SD card, but that would require periodically pulling the card and downloading the data for review. I knew I didn’t want that level of effort so went looking for a way to push the data up to some place on the interwebs. I had all but given up on finding an easy solution when I came across COSM.

Graph of temperature data from xlively
Screenshot of graph from the dashboard

COSM is a nifty web service which allows developers to push data to the cloud and visualize the result. The service is free to use for basic logging and visualizations but their true raison d’être is a cloud service for the mythical coming of the IoT (internet of things). Hardware developers can use COSM to onboard and manage devices in their product line. I’m unlikely to ever need their cool pay services, but I do appreciate the ability to put meteorological data in the side bar of my website.

COSM has a chunk of sample code that makes it really easy to get started pushing data to them from an Arduino. Because you can push the data directly from your Arduino (provided you have a network interface of some sort) you don’t need to be running a power-hungry PC 24/7 to indulge your inner data nerd. Keeping an eye on your data is easy too with nice data presentation via their dashboard (picture above) or you can embed live, auto updating graphs using nifty image tags. Here are a couple of graphs, first for temperature and second for air pressure:

  

Now, if I can just figure out a way to use the anemometer from the cheap weather station I bought off Woot…

[Edit: as of May 14, 2013, COSM has changed its name to Xively]