How to turn on the light of the office with your WP7 mobile phone

I looked forward to the release of “Mango” the new version of the O.S. for my Windows Phone 7 smartphone, and here it is at last! There are plenty of new features introduced by this new version now in beta, but always available for developers who want to take advantage in developing solutions that use the new features provided by the Development Team.

My main interest was the introduction of Sockets. Actually, ( and I think this is the strength of .NET Framework) I hadn’t to learn anything new but only implement also for WP7 the functionalities that I already used in other types of applications (Win forms, WPF, Micro Framework and Silverlight). The functionalities exposed by the most complete version of the framework (currently Version 4.0) are not always implemented in their entirety also for the reduced framework intended for different platforms than PC, but generally they don’t differ much from the reference version.

In the demo version I have implemented the use of Sockets to send commands ModBus RTU standard to a PLC home automation, which allow me to modify the register which controls the relay turning on the light of the office. In order to study the various standard please refer to the following references:

PLC –> http://en.wikipedia.org/wiki/Programmable_logic_controller

ModBus RTU Protocol –> http://en.wikipedia.org/wiki/Modbus; www.modbus.org

To put it simply what I have to do is to build an Array of Bytes of data in the ModBus RTU standard and send it to the PLC via Socket by indicating what is the status of the register set. The digital relays have been set to work like simple buttons ( press – turn on – press – turn off – press – turn on – press – turn off - ) I will always indicate to the register of the digital relay to set its own state to True (the same as saying “press the button”) because the PLC is programmed to automatically reset the status of the digital relay that has raised its switching on bit, lowering it after it has executed the command, just like a physical button that returns to the off position.

Otherwise, I would have to manually send a second ModBus command to reset the bit of the digital relay. Naturally, my home automation system involves a few devices connected to each other as the following layout:

Presentazione standard1en

The use of this type of Hardware involves the sending of the ModBus command using the UDP protocol so that the use of the sockets is limited only to the sending of the Array of Byte created by my class for the management of the ModBus protocol (not shown here) through a Socket object:

    Private Sub SendPacket(PLCIPAddress As String, Port As Integer)         
           Dim Client As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)         
           Dim Packet As Byte() = ModBus.GetModBusBuffer(…)         
           If Client IsNot Nothing Then             
                  Dim socketEventArg As New SocketAsyncEventArgs()             
                  Dim Addr As IPAddress = IPAddress.Parse(PLCIPAddress)             
                  socketEventArg.RemoteEndPoint = New IPEndPoint(Addr, Port)             
                  AddHandler socketEventArg.Completed, AddressOf SendComplete             
                  socketEventArg.SetBuffer(Packet, 0, Packet.Length)             
                  Client.SendToAsync(socketEventArg)        
           End If     
    End Sub     
    Private Sub SendComplete(sender As Object, e As SocketAsyncEventArgs)         
           If e.SocketError <> SocketError.Success Then
            ‘Gestione eccezioni di comunicazione         
           End If
     End Sub

Obviously, everything is seen in a very simple way because we can introduce several different implementations. First of all, the reading of the status of a register, or to check whether the light is on or off; to put a dimmer and to adjust the intensity of the light through a slider; to manage the thermostat in order to raise or to lower the temperature of the room; etc..etc..

Probably there will be other posts about the building automation, but for now I hope you get the idea of how it may be easy to develop apparently complex applications and how high are the business opportunity created by Microsoft technologies.

Do you fancy seeing the program in action?

How to turn on office lights with WP7
Alberto

Print | posted on sabato 27 agosto 2011 0.51

Feedback

No comments posted yet.

Your comment:





 
Please add 8 and 5 and type the answer here:

Copyright © Alberto De Luca

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski