Monday, November 1, 2010


This is a wireless thermometer. It transmits the temperature in the small greenhouse on my balcony to my computer. From there, I can email myself if the temperature in the greenhouse approaches freezing or just post the temperature on the web for fun.

There are three components here:

  1. An Arduino.
  2. An XBee wireless module.
  3. A Temperature sensor. 
The arduino is programmed to read the voltage from the temperature sensor, convert it to a text string, and then send it over the XBee transmitter to the XBee receiver which is connected to my computer's serial port. 

This is the arduino code:
#include <NewSoftSerial.h>

NewSoftSerial mySerial
= NewSoftSerial(2, 3);

void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
Serial.begin(
9600);

Serial.println(
"Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println(
"Hello, world");
}

void loop() {
float voltage = 5.0*analogRead(0)/1024;
float temperatureC = (voltage - 0.5) * 100;
float temperatureF = (temperatureC * 9 / 5) + 32;

Serial.print(temperatureF); Serial.println(
" degrees F ");

mySerial.print(temperatureF); mySerial.println(
" degrees F ");

delay(
1000);
}

The SoftSerial library is very useful. It enables serial communications on any two digital io pins.

The following powershell code reads the temperature from the XBee module and uploads it to a WebService:

$portName = "COM4"
$url = "http://ogresoft.com/Temperature/Home/SetTemperature?Temperature="
$delay = 60

$serialPort = New-Object "System.IO.Ports.SerialPort"
$serialPort.PortName = $portName
$serialPort.Open()

while($true) {
$temperature = $serialPort.ReadLine()
Write-Host $temperature
$request = [System.Net.HttpWebRequest]::Create($url + [DateTime]::Now.ToString() + " " + $temperature)
$response = $request.GetResponse()
Write-Host ([DateTime]::Now.ToString() + " " + $response.StatusCode)
Sleep $delay
}


No comments:

Post a Comment