Twitter Water Sensor Wireless

The detector will be placed in the basement while an internet connected computer upstairs will be used to send the tweet. A pair of XBee wireless modules are used to communicate between the basement and computer.

Configuring the modules

X-CTU configuration interface

Download the X-CTU software from the digi.com website and use the XBIB development board, program and configure the modules.

For the PC connected module, select the Coordinator API firmware. Restore defaults settings. Set a PAN to ensure the sensor device pairs with this PC receiver module if running multiple networks. Check the box to Always Update Firmware and write the changes. Once programmed, remove module and insert the second device.

Select the End Device API firmware for the sensor module. Set the PAN to the same as the as the coordinator module. We will attach the sensor directly to the module inputs, so we configure the module to poll the input periodically and send the data to the coordinator. Set the DIO4 Configuration to be a Digital Input. Then under the I/O Sampling group, set the sample period IR to 0x1388 for a 5 second update rate. Write this firmware and settings to the module.

The modules are now programmed to read DIO4 from the end device module every 5 seconds and send that to the coordinator module.

Refreshing iOS view immediately

Invalidating a region on a window will cause the region to be redrawn on the next drawRect which will not occur immediately. To force the redraw immediately, set the image by using performSelectorOnMainThread and wait for call to return.

[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:UIGraphicsGetImageFromCurrentImageContext() waitUntilDone:YES];

Parsing Misterhouse Serial Data

The sensors transmit data in the form “Address,Port,Value” A typical input would be “A1,T1,30.0” for module 1, temperature sensor 1, and 30 deg C.

In this case only temp data is read by this. split is used to segment the three elements, the temperature is converted to Fahrenheit, and the result stored in a hash.

$xbee_serial = new Serial_Item(undef, undef, 'serial1');
if (my $data = said $xbee_serial)
{
	my @values = split(',', $data);
	my $temp = $values[2] * 9 / 5 + 32;
	$xbee_data{ $values[0].$values[1] } = $temp;
}

SQLite result count

To fetch the number of SQLite results before iterating over sqlite3_step calls, first run a query on the count. Such as this statement to count the number of ‘x’ items in table. The first result column will contain the count.

SELECT count( index ) FROM table WHERE item=x