Wednesday, August 10, 2011

groovy - a quick way to copy a list of files to the local directory

don't we love these tasks? We get a list of files to look at and they are in a directory with thousands of other files and now we need to copy them one, by one, by one and there is no clear patter, which could ease the pain of doing so.

But like always there is a simple groovy way todo this in a quicker fashion.


def values = [
1333020,
1332872,
1332428,
1332280,
1331984,
1331836,
1331688,
1331540
]

values.each{def value ->
new File("${value}.xml") << new File("/Users/****/Documents/metadata/${value}.xml").text
}


this does simplify your life once in a while to copy a quick list of text files to a different folder.

Tuesday, August 9, 2011

remote ssh forwarding

for some reason I always forget the syntax to forward a port on my server to my local system to work remotely from home on some of my webapps.

Now to not forget it again, I shall write it down on my blog, so I can just look at it...


ssh -L port:remoteServer:port remoteServer


now this let's me access the 'port' on my 'remoteServer' on my local system and work on the webapp. Now I just have to rewrite all the absolute paths to relative paths, so that I can find my css stylesheets too...

Monday, August 8, 2011

grails + jquery + checkboxes

sometimes forms based on checkboxes can be rather annoying. An example would be that this is the return of a standard html checkbox,

html defined checkboxes:

<g:checkBox name="compare" id="1"/>
<g:checkBox name="compare" id="2"/>
<g:checkBox name="compare" id="3"/>


result of the send parameters to the server:



def compareBins = {
println params
}

out:

compare:[on, on, on]


now this is rather useless, we rather would see the id of the checkbox in the result. So let's call jQuery to the rescue and see how it can save out day!



jQuery(document).ready(function() {
$('input:checkbox').each(
function() {
$(this).click(function() {

if (this.checked) {
var value = $(this).attr('id');
$(this).val(value);
}
});
}
);
});



and promptly we have the following result if all 3 boxes are selected


compare:[1,2,3]


or if 1 and 3 are selected


compare:[1,3]


so we can actually tell, which elements where checked instead of getting just 'on'

Thursday, August 4, 2011

Arduino + Ethernet Shield + Temperature 2

After monitoring the thermometer for a couple of hours, it turned out the temperature sensor is a bit off. About 2.5 degrees.
At least compared to me being cold and a good old HG thermometer.

Now this meant we need some improvement in our little thermometer or better, let's find a way to calibrate it.

So looking around in the tool box I quickly discovered a potentiometer and some more cables and thought, we can wire this to another pin and scale it to +/- 5 degree celsius and apply this correction.

This is how it roughly looks now:


And the new code to apply the correction settings is here


#include <SPI.h>
#include <Ethernet.h>


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1, 2 };

Server server(80);

float coreVoltage = 5.0;
float correctionScale = 10; //in degree celsius +/- half scale

void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}

void loop()
{
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println();

// output the value of each analog input pin


//read twice to improve precission
float voltageSensor = analogRead(0);
voltageSensor = analogRead(0) * coreVoltage;

//get temperature of the potentio meter
float voltagePotentioMeter = analogRead(1);
voltagePotentioMeter = analogRead(1)*coreVoltage/1000;

float debugVotlage = voltagePotentioMeter;

//scale it to +/- 1/2 coreVoltage
voltagePotentioMeter = voltagePotentioMeter - (coreVoltage/2);

//calculate the correction value in celsius, based on our scale
float correctionValue = voltagePotentioMeter * correctionScale / coreVoltage ;

voltageSensor /= 1024.0;

//temperature calculation
float temperatureC = (voltageSensor - 0.5) * 100 + correctionValue; //converting from 10 mv per degree wit 500 mV offset
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;


client.print("sensor {");

client.print("temperature_voltage:");
client.print(voltageSensor);
client.print(",");


client.print("temperature_celsius:");


client.print(temperatureC);
client.print(",");

client.print("temperature_fahrenheit:");

client.print(temperatureF);
client.print(",");

client.print("temperature_correction:");

client.print(correctionValue);
client.print(",");

client.print("voltage_potentiometer:");
client.print(debugVotlage);
client.print("}");

break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}



Also since we want to keep track of the correction values, we updated the expected json string a bit to reflect the changes in our implementation.


sensor {temperature_voltage:0.75,temperature_celsius:21.98,temperature_fahrenheit:71.56,temperature_correction:-3.22,voltage_potentiometer:0.89}


I have to say, I'm really start to enjoy playing with this little thing. The next step should be have the transmission of several sensors done wirelessly over XBees and to send them to one central location...

Ardunio + Ethernet shield

After I received the ethernet shield, my first task was it to figure out how to monitor the temperature in my office over the network and possible in a way, which allows me to store this result in a database.

So this got me thinking, JSON is the way togo.

  • easy to create
  • easy to read
  • possible to be understood without an interpreter
  • very lightweight
  • everybody uses it
So let's see how the code would look for the arduino


#include <spi.h>
#include <ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1, 2 };

Server server(80);

void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}

void loop()
{
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println();

// output the value of each analog input pin


//read twice to improve precission
float voltage = analogRead(0) * 5.0;
voltage = analogRead(0) * 5.0;

voltage /= 1024.0;

float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

client.print("sensor {");

client.print("temperature_voltage:");
client.print(voltage);
client.print(",");


client.print("temperature_celsius:");


client.print(temperatureC);
client.print(",");

client.print("temperature_fahrenheit:");

client.print(temperatureF);
client.print("}");


break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}


now this was rather simple and results in the following json string on port 80 of the arduino address


sensor {temperature_voltage:0.74,temperature_celsius:24.22,temperature_fahrenheit:75.59}


Now back to my actual project, trying to monitor the PH value and ammonia value with an arduino and adjust the ph, if needed...

Tuesday, August 2, 2011

MedPlan

Over the last couple of days I was thinking about creating a small grails to show the speed of development with grails. The goal was to create an application in less than 24h, which is able to track your medications and at the same time has some important features.

The original idea came from spending the last 5 days with my wife's sick sister and see how difficult and confusing it was to track all her different medications. Specially since there were 3 people involved doing this and nobody really know, which medications did she already receive.

What does the user want:

  • easily enter a new prescription
  • see which medications have been taken
  • show when to take a medication
  • show the medication's in form of a calendar

What did I want as a developer:
  • easy to use
  • multi user access
  • ajax driven/jquery
  • calendar interface
  • auto complete
  • multi platform support (ipad/windows/linux/osx)
  • able to run without a tomcat installation
  • don't look like a standard grails scaffolding application
  • use as few mouse clicks as possible
how does it look after 16h of work?

weekly overview

prescription overview

adding a prescription

What is missing?

I think at this point in time the application does what it's supposed todo, but some nice features would be:

  • iPad support as a native application
  • internationalization
  • share your prescription plan with another user
  • send out an email, 30 minutes before a medication has to be used
  • soap/rest based access 
Where can I get it?

You can download the application here