Category Archives: Uncategorized

Filtering calls with Asterisk

I mentioned on Google+ that I get Asterisk to filter my calls during times when it’s inconvenient to answer the phone and someone asked me to post the details. I’m definitely not an Asterisk expert so there’s probably a better way of doing this.

The PSTN line is answered through a SPA3102 and it is configured not to automatically make the phone on the FXS port ring on incoming calls. There is an option in the advanced settings on the PSTN Line tab on the SPA3102 web config interface that allows you to do this:

Ring Thru Line 1: No

The means that the phone on the FXS port does not ring at all unless the call gets through the filtering in Asterisk and Asterisk tells it to ring.

Below is the relevant excerpt from the extensions.conf file.

; Whitelist various phone numbers
exten => s,n,GotoIf($["${CALLERID(number)}" = "0403XXXXXX"]?ring-all-phones,s,1)

; Check to see if we want to block all calls currently
exten => s,n,GotoIf($[${DB(phonecontrol/state)} = "block"]?out_of_hours,1)

; Check to see if its the right time period to accept calls
exten => s,n,GotoIfTime(9:00-23:00|mon-fri|*|*?ring-all-phones,s,1)
exten => s,n,GotoIfTime(11:00-21:00|sat-sun|*|*?ring-all-phones,s,1)

; Check to see if we want to accept all calls regardless of the time
exten => s,n,GotoIf($[${DB(phonecontrol/state)} = "accept"]?ring-all-phones,s,1)

exten => s,n,Goto(out_of_hours,1)

; Message about not accepting calls
exten => out_of_hours,1,Background(custom/out_of_hours)
exten => out_of_hours,n,WaitExten(5)
exten => out_of_hours,n,Goto(1)

; Ring phone anyway (1)
exten => 1,1,Goto(ring-all-phones,s,1)

; Leave voicemail (2)
exten => 2,1,VoiceMail(3000@default,u)
exten => 2,n,Hangup

The DB entry for phonecontrol/state which is controls whether or not I want to override whether calls are accepted or not is toggled through a web interface.

Controlling the house lighting via MQTT

The lights and some other electrical devices in our new house are controlled by a C-Bus system. Essentially this means that rather than the light switches switching the power to the lights directly, they instead sit on a bus which is connected up to relays which control the power to individual lights. This makes it easy to have smart switches which can control multiple lights and a do a series of tasks (eg dim some lights, pull down a projector screen etc). The most interesting part for me is that when we had the C-Bus system installed is also had an ethernet interface module for the system installed so we can talk to it directly from any of our other computers.

C-Gate is a program which mediates access to the C-Bus interface so multiple programs can access it simultaneously, and fortunately although it was written for windows it’s written in java and runs ok on Linux. The input/output format is not particularly nice for programmatic control, and I ended up writing some scripts that allow for synchronisation of the state between the C-Gate server and an MQTT server.

I already use MQTT as a mechanism to communicate data about power usage in the house. Incidentally I’m also now using the Open Source implementation of MQTT, Mosquitto which for me has been just a drop in replacement of a proprietary version. MQTT can provide a nice uniform interface for apps which insulates them from the details of how data is transferred to and from backend systems. It avoids a bunch of work when the backends change.

I have one perl script which listens for state changes (for example caused by someone pressing a physical light switch) from the C-Bus system and updates the state in MQTT under a simple hierarchy:


lights/<light_num>/state

And another one which listens for changes in a similar hierarchy in MQTT and sends those changes to the C-Bus system:


lights/<light_num>/set_state

The same hierarchy is not used for both to reduce the problem of race conditions and loops occurring. Light numbers are defined in the physical C-Bus setup.

mqtt-cbus

This makes command line control of the lights very straightforward (as long as you know what number a light has been assigned):


mosquitto_pub -h stitch -t lights/<light_num>/set_state -m 255

but I wanted something a bit more user-friendly. So using a bit of javascript, php and a very useful, but slightly hacked version of phpMQTT, I put together a dodgy web page which shows the state of all the lights and exhaust fans in the house as well as allowing us to control them.

iphone_lights

So what’s next on the list to work on?

  • Display the state of the lights and allow control of them through an image of the floorplan of the house
  • Add other inputs such as water and gas usage, which computers are currently on and being used, alarm sensors etc into MQTT
  • Add temperature and humidity sensors in all the rooms in the house as well as outside
  • Experiment with little agent programs that sit around monitoring the data from the MQTT server and try to do smart things – eg warn us when we leave lights or appliances on, perhaps even proactively turn them off, warn us when there has been an unusual pattern of electricity/gas/water usage, open windows when its too hot inside and the temperature outside has dropped below the inside temperature, etc

Wireless Ambient Orb

I’ve been tracking our household electricity usage live for a while. We have an LCD display but its not something that we remember to check very often to make sure that everything that should be turned off is off.

I noticed some cheap rgb led strips on deal extreme and thought I’d make my own ambient orb. I dug out an old arduino I wasn’t using and found some information from this site on how to control the strip using a darlington array. I added a perl script to bridge between the microbroker which receives the power usage information and translates it to a color for the ambient orb to display.

Ambient Orb

At what is our normal minimum power usage the orb glows blue and as the power usage increases turns green, yellow, orange, and then red. This makes it pretty easy to see at a glance when leaving the house or going to bed if the household power usage is about right. After a bit of testing I added purple at the end for when Kelly turns on the kettle and the toaster at the same time :-)

I’ve been interested in playing with xbees for a while, so rather than get a 802.11b wifi shield for communication I bought an arduino xbee shield and a couple of xbees. It turned out pretty easy to setup the xbees and I think I’ll end up with a little mesh network at home with both sensors and display devices like ambient orbs.

DSC_0586

I found some really cheap giant usb driven plastic keys on ebay. It just lights up with a white color when pressed but was easy to disassemble and put the led strips and arduino inside instead.

DSC_0611

DSC_0613

DSC_0616

The white plastic does a better job of diffusing the led light than in the photo above.

Now Kelly wants an orb of her own, so I’m helping her make a smaller and cheaper version using an Arduino Pro Mini 328 instead of an Arduino Duemilanove.

Seedlings!

About a week after planting the seeds we have little seedlings appearing :-) All the cucumber seeds have sprouted as well as a couple of the tomato plants. So far no sign of life from the eggplant or cherry tomato plant seeds.

Apparently its a bit too cold to plant the seedlings yet and warming the soil a bit can also help. So I’ve put down some black plastic where we’re planning on planting the seedlings when they’re ready. I dug some organic fertiliser into the ground and we picked up some pea straw for mulch so are already once the seedlings have matured enough to go outside.

Practicing photography

The weather forecast for Saturday was clear and sunny so Kelly and I decided to take Alyssa out to see if we could get some good high resolution outdoor photos of her. Most of the photos we have of her are low resolution ones taken with our iPhones. So I got out my D70s and 50mm f/1.8 portrait lens and we headed out to Tusmore Park near to where I grew up. Its a really nice green grassy park with a good playground, creek and tall trees.

By the time we had arrived at the park Alyssa had fallen asleep in her car seat and we lay her down on the grass until she woke up. Although I took almost 200 photos my favourite photo of the set was taken right near the beginning when she was still asleep on the grass.

dsc_0019

I like it so much I’m thinking of getting a large canvas print done. When she woke up and realised she was in the park with a playground she was very happy! The lens has such a narrow depth of field that taking photos of her in focus while on the swing was quite difficult.

dsc_0122

Same problem with the slide, although this action shot is not framed well, I love the expression on her face it captured.

dsc_0122

I think this one would have been really nice if I’d rotated the camera 90 degrees like the one after it. I think she’s lit really well in these two photos and it might have been because of the light coloured pool floor reflecting light up from below her.

dsc_0157

dsc_0160

I think this one is pretty cute as we didn’t realise she was able to climb up steps that high:

dsc_0169

Although it would have been much nicer if I’d framed it like the following which shows how tall the trees in the background are.

dsc_0172

It turned out to be a lot cloudier than we expected so the light wasn’t as nice as we were hoping for. I’m really pleased with how some of the photos turned out and I learned quite a bit, so next time there is bright sunny day on the weekend we’ll be out at a park again.

Fixed voip call dropouts

All of our outgoing phone calls go via a local asterisk server connected to Internode’s voip service NodePhone. One problem we’ve had is that muting our phone line for long periods of time, which I like to do during conference calls, would lead to the call being disconnected. I should complain more often on twitter as someone suggested I look into RTP keep alive.

By default asterisk does not terminate calls if there is no RTP activity, but neither does it send keep alive packets to the voip service provider if no packets are received when someone mutes their line. Adding the following to the general section of sip.conf fixed the call dropout problem:

[general]
rtpkeepalive=30

GNOME MQTT Panel Applet 0.1

We’ve been tracking our electricity usage for a few weeks now and have a web page of graphs which make it easier to work out when we’re using a lot of electricity.

Last 24 hours power usage
Last 24 hours power usage

I have a perl script running on one machine which reads the CurrentCost data from a serial port and publishes the temperature and instantaneous power consumption into an MQTT microbroker. On the webserver machine another perl script is subscribed to the temperature and power consumption topics and uses rrdtool to record the data and create not so pretty graphs.

I also wanted to be able to keep an eye on the current power usage so I put together a little gnome applet that connects to an MQTT server and subscribes to a single topic. You can run multiple applets connecting to different brokers or subscribing to different topics. Conceptually data moves from the current cost around the system like this:

mqtt-current-cost-diagram-export

One of the big advantages of using the broker is it neatly abstracts the data producers like the CurrentCost or a temperature sensor connected via an arduino from the consumers that process, record and render the data. Its easy to slip in additional sensors or replace existing ones with something which is quite different without having to make any changes on the processing and user interface side. When our new house gets built and we have a C-Bus system controlling the electrical system, state information from it will be added to the broker so it can be monitored easily.

I’ve never written a gnome applet before nor even done much gtk programming before so its still a bit rough around the edges. But it currently looks like this on a panel:

Temperature and Power Usage
Temperature and Power Usage
Preferences dialog
Preferences dialog

Tooltip with status information
Tooltip with status information

I still need to work on having the preferences saved between sessions.

Monitoring power usage with CurrentCost

Last year i was very lucky to be given a CurrentCost power meter. Unfortunately the electrical board in the townhouse we were living in didn’t really have room to fit the clamp meter and as it had 3 phase power and we only had one clamp we couldn’t really use it properly. Yesterday I finally got around to installing it in the house we’re currently living in.

CurrentCost Meter (photo by Roo Reynolds)
CurrentCost Meter (photo by Roo Reynolds)

There is a wireless connection between the meter and the clamp which goes around the active cable coming out of the normal electricity meter.

Measuring Clamp (photo by PixelFrenzy)
Measuring Clamp (photo by PixelFrenzy)

Unlike most power meters available in Australia it is possible to connect a serial cable to display to get a live reading of the power used downloaded to a computer. Using a microbroker, rrdtool and a bit of perl as glue we know have 24/7 logging of electricity usage at about 30 second intervals from which we can create pretty graphs.

Breakfast
Breakfast power usage

Unlike our last house we have electric cooktops in the current place and you can definitely tell when I started cooking breakfast for Kelly on Sunday in the graph above. It probably cost us about 20 cents in electricity.

Our idle power usage is around 550W which is pretty high and we’ll hopefully be reducing it a bit once we start identifying exactly what is using the power. Looking at a snapshot of power usage overnight its pretty clear when the fridge/freezer thermostat kicks in though.

Fridge Power Usage
Fridge Power Usage