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);
}
}
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:
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:
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:
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/
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/
Monday, August 1, 2011
Web Relay Test Arduino Sketch
I wrote this Arduino sketch to test firing the relays on 2 of my Seeed Studio Relay Shields. Shield 1 is connected to digital pins 2 to 5 and Shield #2 is connected to digital pins 6 to 9.
This sketch enables all those pins for output and then turns them on and off for half a second (500 milliseconds) in sequence.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x50, 0xA0 }; //physical mac address
byte ip[] = { 192, 168, 1, 177 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
Server server(80); //server port
byte sampledata=50; //some sample data - outputs 2 (ascii = 50 DEC)
String readString = String(30); //string for fetching data from address
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
//Set pins 2 through 9 to output
for (int i = 2; i <= 9; i++){
pinMode(i, OUTPUT);
}
//enable serial data print
Serial.begin(9600);
}
void loop(){
// Create a client connection
Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 30) {
//store characters to string
readString.concat(c);
}
//output chars to serial port
Serial.print(c);
//if HTTP request has ended
if (c == '\n') {
if(readString.indexOf("test=all") > -1) {
httpReply(client);
// cycle through pins 2 to 9
for (int i = 2; i <= 9; i++){
Serial.print("Writing pin ");
Serial.print(i);
Serial.println(" HIGH");
digitalWrite(i, HIGH); // set the LED on
delay(500);
Serial.print("Writing pin ");
Serial.print(i);
Serial.println(" LOW");
digitalWrite(i, LOW);
delay(500);
}
}
//clearing string for next read
readString="";
//stopping client
client.stop();
}
}
}
}
}
void httpReply(Client client){
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("{\"status\" : \"executing\" , \"cmd\" : \"test=all\"}");
client.stop();
}
This code is loosely derived from code by Mareika Giacobbi of nerdydog.it for his Domotic Home project.
ADK + Ethernet Shield + 2 Relay Shields = Almost Working Prototype
Here is the current iteration of my Arduino Irrigation Controller rig:
I now have 2 of the 3 Relay Shields connected to the ADK with Ethernet. Did I say connected? It's a bit fragile to say the least. Don't breathe on it! Those green wires going into the middle of the Relay PCBs are basically held their by the tension of the wires wanting to straighten out.
I really wish the Seeeduino guys had put on those extra long headers and pins on these Relay Shields like on the Arduino Ethernet Shield. It's basically impossible to attach them to the Arduino board or the Ethernet Shield because these huge freakin' pins from the screw terminals extend 2mm out the bottom of the board (and cause shorts against tall components, if you're not careful).
Now I'm waiting on some parts from AdaFruit so I can connect the 3rd Relay Shield and create a more reliable connections to the first two boards. But, where am I going to put Relay Shield #3? Hmmm...
One other thing that confuses/frustrated me with the Relay Shields is that they cannot run off USB power (5v), you have to plug them into their own 9v power supply, or plug the Arduino into a 9v+ power supply and route the Arduino's 9v pin (not the 5v pin) and ground to the Relay Shields. That's a bit annoying. The important thing, though, is that it works. On to the next stage.
Pin 0 and 1: Don't Use for Output!
Oh, and one other thing: If you are thinking of using digital pin 1 and pin 2 to control the Relay Shields: Don't. I tried using them in an earlier version of this prototype and saw some worrying behavior. Namely, when uploading a sketch to the Arduino board, pins 1 and 2 flash on and off very quickly which turns on the connected relays on and off very quickly and makes them rather hot. I didn't have a catastrophic failure; the relays still work, but that'll be the last time I try using pin 1 and 2 for anything other than TX and RX.Thursday, July 28, 2011
ADK / Ethernet Shield Problem Solved!
Fixed! A quick trip to Radio Shack for some hookup wire (22 gauge, solid) and I manually wired the ICSP pins from the Ethernet Shield and now I have a web server that's just a bit bigger than a deck of cards.

Here is the ISCP pin mapping to the ADK pins for future reference:
Only pins 1,3 and 5 need to be connected (orange wires in the picture). The Ethernet shield still works if you leave pins 2, 5 and 6 disconnected (red, yellow and green wires in picture).

Here is the ISCP pin mapping to the ADK pins for future reference:
(MISO) 50 <-- 1 o o 2 --> +5V
(SOCK) 52 <-- 3 o o 4 --> 51 (MOSI)
RESET <-- 5 o o 6 --> GND
Only pins 1,3 and 5 need to be connected (orange wires in the picture). The Ethernet shield still works if you leave pins 2, 5 and 6 disconnected (red, yellow and green wires in picture).
Android ADK and Arduino Ethernet Shield Woes
I think I figured out why my Arduino Ethernet shield won't play nicely with my Android ADK Arduino board.
You see the black, six-pin header connector on the right side of the blue board (the Ethernet Shield)? That's the the ICSP bus in Arduino-speak. That's supposed to connect into a matching set of pins on the white Arduino board so the Ethernet shield can to its "talkin' to the interweb" thing. Do you see matching pins on the ADK board? Yeah, neither do I.

Turns out this is a bit of a problem, because the Arduino Ethernet Shield documentation states:
Like I said, bit of a problem. Looks like there are exposed copper pads on the bottom of the ADK board for the ICSP connections, so I could solder in some connections if absolutely necessary. Those pads (MISO1, MOSI1, SOCK1) are clearly wired to 50,51 and 52 as described in the Arduino Mega 2560 schematics.
Time for some hand wiring...
You see the black, six-pin header connector on the right side of the blue board (the Ethernet Shield)? That's the the ICSP bus in Arduino-speak. That's supposed to connect into a matching set of pins on the white Arduino board so the Ethernet shield can to its "talkin' to the interweb" thing. Do you see matching pins on the ADK board? Yeah, neither do I.

Turns out this is a bit of a problem, because the Arduino Ethernet Shield documentation states:
Arduino communicates with both the W5100 and SD card using the SPI bus (through the ICSP header).
Like I said, bit of a problem. Looks like there are exposed copper pads on the bottom of the ADK board for the ICSP connections, so I could solder in some connections if absolutely necessary. Those pads (MISO1, MOSI1, SOCK1) are clearly wired to 50,51 and 52 as described in the Arduino Mega 2560 schematics.
Time for some hand wiring...
Friday, July 22, 2011
Arduino Irrigation Controller Projects Across the Web
I've done a bunch of research on this type of project in the last few weeks. Here are some interesting links from around the web that I find useful and/or interesting:
Arduino Irrigation Projects
Seems like all these guys built custom hardware, which is cool, but I'm going to use mostly off the shelf parts (for the first iteration, anyway).
Supporting Technology Projects
Arduino Irrigation Projects
- viknet project - webbased Jquery Scheduler for home automation, sprinkler, and remote access
- Kevin Colyar's project - iPhone-based Remote Control of Irrigation valves using Arduino, Sinatra server and iPhone (very close to what I'm planning for my first itteration.)
- Hypnopompia project - DYI Arduino board that uses a telnet control system over ethernet
- Jason F. Ball irrigation controllers Version 1, Version 2 and Version 3 - Custom DYI boards based on Arduino (Dude is building himself a reflow oven from a toaster over so he can cook his SMDs onto his board, holy crap!)
- drj113 project (via Instructables) - DYI Arduino board with an ethernet jack and 6 valve controllers
- Gerry Duprey project (10 zone and 27 zone controller design) - Seems like a very robust hardware design, but it's PIC16F877P based, not Arduino.
- M.H.Kabir project - SMS-controlled Wireless Irrigation System
Seems like all these guys built custom hardware, which is cool, but I'm going to use mostly off the shelf parts (for the first iteration, anyway).
Supporting Technology Projects
- DomoticHome project : not actually controlling irrigation, but using Ethernet shield and with a Android app client
- RESTduino - A nice REST interface for Arduino. Very cool.
Subscribe to:
Posts (Atom)
