Thursday, November 6, 2014

ZWay in C++: wire Door sensor with Power switch

This post is after my previous post: "ZWay in C++: how to send command". This post will show you how I wired two ZWave devices in C++ without using the default ZWay web service. However, since the goal of my program is not to make complete ZWay alternative, there is things that I rely on the ZWay web service. I did at least two steps with the web interface: inclusion of devices and setting "associate". I will skip the inclusion part because it will be device dependent.

"Association" is one of ZWave command that is not explained well in the ZWay Dev document. Until I understand how it works, I was very puzzled by the behavior. The door/window sensor I bought had "Association" command implemented. What it does is that when the sensor detected door or window being open or close, it send event to devices that are "associated". It seems that it send BASIC_SET command to the associated devices but I am not sure exactly what it does.

I had to set RaZberry device as an associated device in the door/window sensor so that whenever door/window is open/close, I get an event right away. What confused me was that when I put the power switch as an associated device along with RaZberry device, the sensor turned on/off the power switch all ways. Even when my program didn't send command, the power switch was turned on/off whenever the sensor detects the event. It was not what I wanted because I didn't want to turn on the light during the day time. I learned that I should not put the power switch as an associated device if I want to have full control. But if you want to turn on/off all the time whenever the sensor status changes, this may be good enough for your need.

The program above is the final program I wanted. It turns on/off the power switch when the sensor is triggered but only from 12AM to 7AM; meaning when I am supposed to be sleeping. Also it turns off after 30 minutes to cool down the light.
The police beacon light I bought came with a warning note that it may cause fire if I use it two hours consecutively. I am not sure how serious the warning is but I didn't want to take a chance. This whole setup is to increase the security not reduce. lol

The event from ZWay system is enqueued through ThreadMessageQueue I showed a few month ago. But it is little modified. I deleted the copy constructor to be safe and I implemented "Clear()" method to empty the queue. Interestingly std::queue does not have "clear()" method, because queue is not an actual container but a wrapper to any container and somehow it doesn't bother how to clear. The clear is done by swapping with an empty queue; I am not sure if it is a proper way tho. lol.

The class, SensorMonitorFunctor, is for a functor that will be called from ZWay system when the sensor is triggered. As I mentioned, the call-back function cannot do much due to some restrictions that are not well explained in the document. All it does is to enqueue a command to the thread safe queue and that's it. Note that the ThreadMessageQueue should not be passed in as a value. lol. It should be a pointer or reference.

The class, MyZWayMonitor, may seem complex and long but basically it is to register a call-back function to ZWay system with a function call, "zway_data_add_callback()". But I needed to implement the destructor that remove the call-back when the call back argument object is not valid anymore. The object memory pointer is passed in as the last argument of the function call, zway_data_add_callback. If the object is destroyed somehow, ZWay system call the call-back function, the call-back function, which is MyZWayMonitor::TypelessCB() in this case, will use the invalid pointer in the line: "monitor->CB( Overload< ReturnType >(), data );". So when the object of MyZWayMonitor gets destroyed, I needed to unregister the call-back by "zway_data_remove_callback()".

Like other classes, MyZWayMonitor intends to do strong-typed style with templates.

Once I get it compiled, I run it as a background process all the time.
To get it running every time RasberryPi gets rebooted, I also put it in a file /etc/rc.local.

1 comment:

  1. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..
    Home Automation in Chennai
    smart home in Chennai
    Home security in Chennai
    Burglar alarm in Chennai
    Door sensors Chennai


About Me

My photo
Tomorrow may not come, so I want to do my best now.