Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Saturday, January 14, 2012

Remote Control Sprinklers: The Irrduino Project

So... I connected my lawn to the internet.



More specifically, I built an internet-based control for the thing that most contributes to the on-going health of my lawn. That is, the sprinklers.

I call this project Irrduino, because at the core of it is an Arduino microcontroller that lets me remotely control the irrigation zones at my house. Irrduino communicates via Ethernet and standard html requests and responses (specifically a REST interface with JSON responses, for you web geeks out there) which means I can control my sprinklers from anywhere on the planet with a web browser and an internet connection, or any smartphone with the same.

Why the heck would I want to do that?

Well, first and foremost, because I can. I wanted to see if I could do it. Beyond that, there are some very practical reasons for wanting to do this, which should be readily understood by anyone who has ever had to do maintenance on sprinklers. They need to be tested regularly, and when you test sprinklers, it’s best to be able to chose exactly where you are standing when the water comes on. So being able to turn them on from my phone is very handy for testing. But that’s not the best part.

The best part, by far, is being across the street at my neighbor’s house and seeing the neighborhood cat preparing to use my lawn as a a toilet, pulling out my phone, pushing a button and watching the feline fur fly.

Hardware


I started with the Android Accessory Development Kit (ADK) as my Arduino platform. In retrospect, this board is massive overkill for the project, but it’s nice to have a extra headroom just in case. I knew early on that I didn’t want to have the ADK plugged into my phone so I invested in an Ethernet Shield so I could connect it to my home network. After it arrived, I spent about a week trying to figure out why it didn’t work, until I discovered that the shield needs to be plugged into the Arduino ISCP header pins, which don’t exist, as such, on the ADK. Luckily those connections can be made separately, and once I figured that out, it was off to the races with a teeny, tiny little web server.

Next, I needed a way to control the higher voltage circuits of the sprinkler value solenoids. Sprinkler systems typically run at 24 volts and somewhere between 700 and 1000 milliamps. After a bit of research on the topic, I figured out the electronic component I needed to control these higher power circuits was called a relay. Being rather new to electronics engineering, I settled on something that was prebuilt and designed to work with Arduino. For that reason, I bought a few of Seeed Studio’s Relay Shields, waited for them to show up, then hooked them up and once again spent another a week trying to figure out why they didn’t work.

Turns out the Relay Shields are a bit power hungry. Running them off the 5-ish volts of USB power that easily runs the ADK and the Ethernet Shield, is not enough to run the Relay Shields. It’s a bit annoying, but easily solved with 9 volt wall adapter plug connected to the ADK, and then connecting the Vin power pin and neighboring ground to a power bus connected to the power inputs for the Relay Shields.


Wiring


After bringing all this hardware together, I started wiring it all up and writing the Arduino control software. The first task (after figuring out that the the shields needed 9 volts of power) was connecting the control wires to the Relay Shields signal inputs. After a quite a bit of experimentation, I settled on using standard Arduino pins 2 though 9 for controlling the first two shields, and pins 11 and 12 for the third shield.

There were a couple of key learnings from the wire-up process:

  • Don’t Use Pins 0, 1, 10, 13: These pins receive quite a bit of signalling for other purposes, so don’t try to use these as output controls for the relays. Pin 13 on the ADK, in particular, gets flashed quite a bit when you upload new programs, which I spend a lot of time doing. Once you have a program that you are happy with and don’t need to update, it may be OK to use it.
  • NC, NO: What’s that? On the Seeed Studios Relay shield the big, green screw terminals are labeled NC#, COM#, NO#. This stands for Normally Open (NO), common wire (COM) and Normally Closed (NC). For use with a sprinkler system, the control circuits are usually disconnected (Open) and only connected when the sprinkler valves are open, so I used the NO terminals to connect the sprinkler valve circuits.

Software


There are several separate software projects that contribute to the functionality of Irrduino:

  • IrrduinoController - This software runs on the ADK / Arduino microcontroller and is the core of the Irrduino project. It runs a small web server, handles the http REST requests, activates and deactivates the relays to turn the sprinkler values on and off.
  • IrrduinoRemote - This Android application is what I'm using in the video to activate the IrrduinoController. There is not much to this application, because there doesn't need to be. I wrote the original, functional version in about 3 days.
  • IrrduinoServer - This Google App Engine server application acts as a reporting backend and alternative controller it was written in Python by my partner in crime on this project, JJ Behrens.

The IrrduinoController software presents REST-like interface for client applications like the IrrduinoRemote app, the IrrduinoServer, or anyone with a web browser. The software call for turning on a sprinkler valve drives digital output HIGH, thereby activating a relay, closing a circuit for one of the valve pumps and turning on the water. Once you know which sprinkler zone to activate and the digital pin associated with it, all you need to do is call:

// turn on selected zone
digitalWrite(pin, HIGH);

More challenging is receiving and interpreting a REST command to turn on a zone, and running a sprinkler zone for a specified amount of time without blocking the processing thread. Luckily, I had some help and inspiration for interpreting REST commands from Jason Gullickson’s excellent RESTduino project. For the non-blocking timer, the Arduino forums were quite helpful in suggesting setting a end time with the millis() function and running a check function on each loop.

There was a lot of trial and error and rework for the Arduino code, and I’m already planning a major refactor to make extension of the Irrduino code. I’ll let the code speak for itself, with the caveat that it is not particularly elegant but, it works.


Irrduino 2.0


As I was working on this project, it became clear to me that the Irrduino system could easily do more than just remotely activate my sprinklers; it could actually run them, full time, and thus allow me to replace my commercial controller with a completely open source, open hardware solution that would give me way more control and flexibility in how I watered my landscape. I could give it a regular schedule to run, and then give it enough smarts to adjust watering based on seasonal timing, predicted weather, actual rain (rain sensor), or do even smarter things like automatic soak-wait-soak scheduling, water budgeting and more.

For Irrduino 2.0, I plan to add timed scheduling, and after that the sky's the limit.

Friday, November 4, 2011

Irrduino Project Open Sourced!

As of today, I have found a co-conspirator in my remote control irrigation project, which I have christened, Irrduino. (irrigation + Arduino = Irrduino! Get it?)

Due to this 100% expansion in support staff for this project, I have moved my code out onto code.google.com so my new colleague can help me extend it:


http://code.google.com/p/irrduino/


This code is probably not useful to anyone except me, and possibly other folks who are already working on using Arduino to control their sprinkers (of which there are a few), but it is exciting (and a little scary) to put it out there in the world.

Monday, August 8, 2011

Deleting Files from Android SDCard

Today I needed to delete a bunch of files off of my phone because some rogue test program I was running generated several hundred image files in the root of my SDCard. Here is program I wrote to get rid of them:

package com.example.android.filemanagement;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class FileCleanupActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       Button b = (Button) findViewById(R.id.go);
        b.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {

				// Delete those files!
		        Iterator it = listFiles().iterator();
		        while (it.hasNext()){
		        	it.next().delete();
		        }
				
			}
        });
        
        TextView tv = (TextView) findViewById(R.id.file_list);
        
        StringBuffer sb = new StringBuffer();
        Iterator it = listFiles().iterator();
        while (it.hasNext()){
        	sb.append(it.next().getName() + "\n");
        }
        
        tv.setText(sb.toString());
    }
    
    private static List listFiles(){
    	
    	File[] files = Environment.getExternalStorageDirectory()
    					.listFiles(new ExtensionFilter("jpg"));
    	
    	return Arrays.asList(files);
    }
}

class ExtensionFilter implements FilenameFilter {

	private String ext;
	
	public ExtensionFilter(String ext){
		this.ext = "."+ext;
	}
	
	@Override
	public boolean accept(File dir, String filename) {
		return filename.endsWith(ext);
	}
	
}

Friday, August 5, 2011

Uris and Intents and Cameras, Oh My! - Android Development

For the last couple of days, I've been hacking on the Android camera API functions. Specifically, I've been trying to invoke the camera with an Intent (new Intent(MediaStore.ACTION_IMAGE_CAPTURE);), rather than building out my own preview and capture function.

The main stumbling block has been how to pass file location to the Intent so that the Camera function saves images where I want them to go, rather than the default location (/mnt/sdcard/DCIM/Camera/ I believe). I've looked all over the internet for how to do this properly. After getting some expert advice, this is the best solution I found:

private static Uri getImageFileUri(){

     // Create a storage directory for the images
     // To be safe(er), you should check that the SDCard is mounted
     // using Environment.getExternalStorageState() before doing this
     
     File imagePath = new File(Environment.getExternalStoragePublicDirectory(
                                Environment.DIRECTORY_PICTURES), "CameraTest");
     if (! imagePath.exists()){
      if (! imagePath.mkdirs()){
       Log.d("CameraTestIntent", "failed to create directory");
       return null;
      }
     }
     
     // Create an image file name
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
     image = new File(image.getPath() + File.separator +
                        "IMG_"+ timeStamp + ".jpg");

     // Create an File Uri
     return Uri.fromFile(image);
    }

The key appears to be not to use ContentResolver (e.g.,
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);) to generate a Uri, because this means the Camera app will just save the image in the default location with a default name.

The above solution saves the image in a shared location so other applications (like Gallery) can access it. Files saved to this location also will not be deleted when your application is uninstalled. If you want your image files to be not easily visible to the user and removed when your app is uninstalled, call your application's Context object to create the image path:

     File imagePath = context.getExternalFilesDirectory(
                         Environment.DIRECTORY_PICTURES);

You don't need the "CameraTest" bit in this case, because the imagePath will be specific to your application: /mnt/sdcard/Android/data/<app_package>/Pictures/

Friday, February 11, 2011

Customized ListView Data Display in Android

ListViews are very handy user interface tools on the Android platform. We see them all the time in applications: phone number lists, contact name lists, shopping lists, all kinds of lists. The example Notepad application for Android, arguably the second app that all fledgling developers build is—you guessed it—a list.

Android provides some easy to use tools for mapping data from your application’s database into a ListView. However, you can quickly reach the limit of the basic tools as soon as you try to move past simply displaying data as it appears in your database. In this article, I discuss how to go beyond basic display of data in ListViews.

Lists, Cursors and the SimpleCursorAdapter

The SimpleCursorAdapter provides a simple way to map database fields into an entry in a List. All you have to do is provide a Cursor to your database table with the fields you want to display, map the fields to ids in your list item layout xml file and you are done:

// Get all of the items from the database and create a list
       Cursor cursor = mDbHelper.fetchAllItems();
       startManagingCursor(cursor);
       
       // Create a mapping between database columns and layout fields
       String[] from = new String[] { DbAdapter.COL_TITLE };
       int[] to = new int[] {R.id.text1};
       
       // Create a cursor adapter and set it to display using a row layout
       SimpleCursorAdapter notes = 
              new SimpleCursorAdapter(this, R.layout.list_item, cursor,
                                        from, to);

       setListAdapter(notes); // assumes this is a ListArray class

This approach works great for a basic list of text items or numbers, but what if you want to do something beyond just displaying database data as-is? Consider the following list of things you might want to do when displaying your data in a List:
  • Format the data for display
  • Optionally display data for each list item (skip display of some items)
  • Combine data from multiple data fields into a single display field
SimpleCursorAdapter does not allow for these variations, but hey, what did you expect? It is called SimpleCursorAdapter for a reason and for basic mapping it’s a perfect solution.

ComplexCursorAdapter?

So what is the next step up? No, not ComplexCursorAdapter; I made that up. The next step is to extend either the CursorAdapter or ResourceCursorAdapter classes. For this tutorial, I’ll focus on the use of ResourceCursorAdapter, which provides the ability to use XML-based layout resources for your list items.

Getting Started

This tutorial assumes you have already successfully created a few Android applications. So, if you have not done that yet, get Eclipse and the Android SDK, build Hello Android and the Notepad example applications and come back.

Create a new Android project with a ListActivity as the primary Activity for the application. If you want to follow along with my example project, you must choose Android SDK version 2.2 (8) or higher as your build target. After the Android Project Wizard creates the template Activity class, modify it to extend ListActivity (and make sure to update your imports):

public class CustomDataList extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Next, modify the default main.xml layout to include a ListView. (See the example project for an example.) If you are using your own layout, make sure your ListView includes the default id for lists:

    <ListView 
              android:id="@android:id/list" ...

Create a layout xml for your list items under the ProjectName > res > layout. Make sure your layout has at least two TextView fields and make a note of their ids. If you need something to get started, check out the list_item_with_description.xml in the example project.

In your ListActivity class, create a new inner class that extends ResourceCursorAdapter. This inner class is the custom CursorAdapter we will use to manage how data is bound to a list item:

    private class MyListAdapter extends ResourceCursorAdapter {
        
        public MyListAdapter(Context context, Cursor cursor) {
            super(context, R.layout.list_item_with_description, cursor);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // TODO Auto-generated method stub
        }
    }

The first method is the constructor, where we declare which list item layout to use. The bindView method is where the action happens. In this method, we take control over how data from our database gets populated into each list item.

Got Data?

In order to do data binding, we need data. So, use the DbAdapter class in the example project for sample data (recommended) or use a database adapter from one of your applications. If you use your own database adapter, make sure you have a method that returns a Cursor object with three or more data fields. In the example DbAdapter class, this method is called fetchListItems().

Connecting the Custom ListAdapter

Now we have a database and method to get a Cursor to our data, so it is time to connect the data pipes. In your ListActivity class, add the following to the onCreate() method:

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        DbAdapter dbHelper = new DbAdapter(this);
        dbHelper.open();
        
        // Get a Cursor for the list items
        Cursor listCursor = dbHelper.fetchListItems();
        startManagingCursor(listCursor);
        
        // set the custom list adapter
        setListAdapter(new MyListAdapter(this, listCursor));
    }


These statements create a connection to your database, get a Cursor to your list data and then set the customized ListAdapter, which tells the ListView how to render your data. Actually, we have not defined how the data is rendered for each list item yet; That is our next step.

Note: If you are getting a “method setListAdapter() is undefined” error, make sure the enclosing class extends ListActivity. This method is not available in a regular Activity class.

Binding it Your Way

With the implementation so far, no data is shown in our list! Now it is time to fix that problem by defining a data binding in the bindView() method of our custom ResourceCursorAdapter inner class. Here is how we update bindView() to render the data from the example database:

@Override
public void bindView(View view, Context context, Cursor cursor) {

       TextView title = (TextView) view.findViewById(R.id.item_title);
       title.setText(cursor.getString(
                           cursor.getColumnIndex(DbAdapter.COL_TITLE)));
       
       TextView details = (TextView) view.findViewById(
                                            R.id.item_details);
       StringBuffer detailsText = new StringBuffer();
       
       int price = cursor.getInt(cursor.getColumnIndex(DbAdapter.COL_PRICE));

       if (price > 0){
              detailsText.append("$"+price+".00");
       } else {
              detailsText.append("Price Unavailable");
       }
       String description = cursor.getString(cursor.getColumnIndex(
                                             DbAdapter.COL_DESCRIPTION));
       if (description != null && description.length() > 0){
              detailsText.append(", "+description);
       }
       details.setText(detailsText.toString());
}

As you can see from this example, while we can still do a simple mapping of an item title data to a TextView, we also have the opportunity to do apply any rendering logic we want to the data retrieved from our database. A word of caution, however, is in order. Since the bindView() code is executed fairly frequently, it is a good idea to keep your rendering logic to a minimum, otherwise your users may experience significant pauses as your list renders.

Code Download


Here is the example project for this tutorial. You need Eclipse and the Android SDK with platform version 2.2 (8) to be able to run this project.

CustomDataList_CodeProject.zip

Conclusion

I hope you have found this article to be a useful next step in the evolution of your understanding of working with ListViews, Cursors and databases in Android. Please add your comments and questions below. Happy programming!

Wednesday, February 9, 2011

Managing Date and Time Data in Android Apps

The Android platform offers many well-defined options for managing various types of data in your applications. However, date and time values, such as in January 1, 2011 at 12:00pm, are an example of a data type on Android where you have many options, but the best path for storing and displaying them is not clear. This article discusses my investigation into managing date and time values for one of my Android applications, my conclusions and the approach I finally implemented.

Date and Times in SQLite

SQLite, the embedded database library in the Android platform, does not have a native date and time storage type (see SQLite docs), and instead suggests the use of TEXT, REAL or INTEGER data types. The library also has functions for working with dates and times, but I prefer to do my date and time calculations in Java, so these offerings were not of much use to me and did not figure into my decision about how to store this type of data.

Text or Numbers?

So, what data type to use? Text strings or number values? Text strings are convenient for display, but they are a pain when it comes time to do calculations (is this date more recent than that date?). In addition, if I ever want to do localized date display (European Day, Month Year format versus US Month / Day / Year), having dates preformatted for one locale means quite a bit of work to transform it into other locales. So, text strings get tossed out as a storage type!

That leaves storing dates and times as numbers. The big question there, is how precise do these dates and times need be? The SQLite INTEGER type can handle values up to 8 bytes (64 bits) in size. Using Java’s long value for time in milliseconds from January 1, 1970 might be convenient. However, I tossed out this idea because of concerns about the size of the numbers generated by this method and the fact that they are completely unreadable as dates without programming assistance. My application only needed precision down to seconds and I wanted the numbers to be as human-readable as possible without actually being formatted strings.

What’s Your Number?

With all this in mind, I decided to define a date and time number format that was closely related to printed dates and times, but that could be easily sorted into more recent and less recent. The format I defined starts with a numbered year, then month, then day of month, hours in 24 hour-format, minutes and then seconds.

Example Date/Time:   10/29/2007, 12:06am, 21 seconds
Date/Time Number:    20071029000621

Dates and times stored in this format can be interpreted by humans with a little instruction and the values will sort nicely, with higher values being the most recent.

Note: If you need to do length calculations in a specific unit (how many days between date X and date Y) this format isn’t much help, but unless you save your date and time values in that specific unit (e.g., days) doing those types of calculations is always going to be a bit of a pain.

The Implementation

With this design in mind, I created the following functions to help me convert between the Calendar objects and the long values I was going to retrieve out of my SQLite database:

    public static final String DATE_FORMAT = "yyyyMMddHHmmss";
    private static final SimpleDateFormat dateFormat = new
       SimpleDateFormat(DATE_FORMAT);
    
    public static long formatDateAsLong(Calendar cal){
       return Long.parseLong(dateFormat.format(cal.getTime()));
    }
    
    public static Calendar getCalendarFromFormattedLong(long l){
       try {
                     Calendar c = Calendar.getInstance();
                     c.setTime(dateFormat.parse(String.valueOf(l)));
                     return c;
                     
              } catch (ParseException e) {
                     return null;
              }
    }

This implementation uses Java’s SimpleDateFormat object to convert Calendar objects formatted long dates and formatted long dates back to Calendar objects.

Note: For those of you who might be wondering why I’m using a Calendar object as the primary Java data type for my date/times (rather than the Date object), it is because the implementation of Android’s DatePickerDialog is more closely aligned with the Calendar object.

In the database adapter for my application, the create and insert functions that include date and time information take a Calendar object and convert to a long just prior to inserting it into the SQLite database:

    public long updateDateRecord(long id, Calendar calendar){
        ContentValues values = new ContentValues();

        // Here is where we apply the Data DateFormat:
        values.put(COL_DATE, DateDataFormat
           .formatDateAsLong(calendar));
        
        return mDb.update(TABLE_DATES, values, COL_ID + "=" + id, null);
    }

On the user interface side, I retrieve the date and time value as a long, convert it to a Calendar object and then format the date into a string for display:

    private void populateDate(){
       String date = null;
       if (mListCursor != null && mListCursor.getCount() > 0){
              
              dateId = mListCursor.getLong(
                    mListCursor.getColumnIndex(DbAdapter.COL_ID));
              long d = mListCursor.getLong(
                    mListCursor.getColumnIndex(DbAdapter.COL_DATE));

              if (d > 0){
                  mDate = DateDataFormat
                      .getCalendarFromFormattedLong(d);
                           
                     date = DateDisplayFormat.getFormattedDate(mDate);
              }             
       }
       if (date != null){
              mDateText.setText(date);
       }
    }

Code Download

To review a complete, example implementation of this approach in an Android project, please download and import the following Android Eclipse project:

DateData_CodeProject.zip

You must have Eclipse and the Android SDK including platform version 1.5 (3) installed to compile and run this project.

Roads not Taken

Here are some things that were not considered in this approach, but might be relevant to your application:

  • Time Zones – All the date and times I was recording for my application were in relation to one user in a single time zone, so they did not figure into my implementation. Folks working with dates and times that need to be coordinated across time zones would need to enhance my approach using an implementation of Coordinated Universal Time (UTC).
  • High-Precision Times – All the dates and times for my application would differ by at least a second or more, so there was no reason for me to include millisecond precision.

Conclusion

This example implementation is not meant to be the ultimate solution for saving and retrieving date and time data in Android. As noted above, your application may have different needs which are not considered by this methodology, which you should seriously consider before adopting this approach.

I hope this article helps you more easily navigate the decisions of date and time storage on the Android platform and gets you another step further in completing your app. Happy programming!