Wednesday, 2 July 2014

The Pi-Mote 433MHz control unit for the RaspberryPi

This controller allows a user to remotely control power sockets via a simple program.


Having just received my kit from Energenie, this post is basically an introduction to using the system, driven by a Gambas program.


I've also included information on the serial data format, which was captured using the method described in an earlier post.


What's in the kit?


The Energenie kit ENER002-2Pi includes the tiny Pi-Mote module and 2 radio controlled UK (13Amp) sockets. The Pi-Mote very neatly connects directly to the 26way GPIO connector.

There is a sample Python program available from the Energenie website, but I decided to create my own program in Gambas, as the program details are very simple.

As I had my Velleman receiver laying around from last week, still wired for testing, I thought I'd capture the data stream to get a better idea what was going on.

The data stream is similar to the Velleman, except its a 24bit system with an extra bit, which I assume is some kind of stop-bit.

The documentation includes a table showing how the 4 bit code can be used to control 4 power sockets.



Each socket seems to be identified by the last nibble of the data command (i.e. the last 4 bits, not including the "stop" bit), as can be seen by this data capture.

Upper trace is socket 1 ON command: 0010-1111-0011-0010-0001-1111-0
Lower trace is socket 1 OFF command: 0010-1111-0011-0010-0001-1110-0

It looks like the Pi-Mote only gives you control over these 4 bits. It would be nice to be able to change some of the other nibbles in order to use Pi-Mote with other devices.

Once the command code is written to Pi-Mote, it appears to just generate it continually, because when the command is sent (i.e. the modulator is enabled) the pulse train contains a number of commands where the first and/or last are often truncated.

In this example, the first command in the stream has lost the first 10 bits. The second command is complete.

In the sample program there is a delay of 250ms after the modulator control bit is enabled. This ensures that a number of complete commands (i.e. all 24+1bits) will be received by the sockets.
  
From my rough measurements using Audacity it looks like the "1" pulse is about 600us wide, while the "0" appears to be about 225us wide.

Obviously the sockets are all the same. You configure a socket to be "socket 1" by pressing the button until the unit flashes, then send a socket 1 command from Pi-Mote. That's all there is to it.

When time permits, I plan to get a cheap transmitter module and see if I can control both the Velleman and the Energenie devices from the same Pi + transmitter setup.

Gambas with wiringPi


My Gambas program just has 5 check boxes and uses the wiringPi library.


This is the code, which must be run with root permissions:-

' Gambas class file

Library "/usr/local/lib/libwiringPi"

Public Extern wiringPiSetup() As Integer        'Initialises wiringPi & assumes the calling program uses virtual Pin numbers
Public Extern pinMode(pin As Integer, pud As Integer)   'sets Pin mode to either INPUT, OUTPUT, (or if applicable; PWM_OUTPUT or GPIO_CLOCK)

Public Extern digitalWrite(pin As Integer, value As Integer)


Public Const PIN_OUTPUT As Integer = 1
Public Const PIN_INPUT As Integer = 0

Public Const MODULATOR_ENABLE As Integer = 6
Public Const MODE_SELECTOR As Integer = 5
Public Const AS_KEYING As Boolean = False

Public Const D0 As Integer = 0   'RPi D0 To Pi-Mote K0
Public Const D1 As Integer = 3   'RPi D1 To Pi-Mote K1
Public Const D2 As Integer = 4   'RPi D2 To Pi-Mote K2
Public Const D3 As Integer = 2   'RPi D3 To Pi-Mote K3

Public Sub _new()

End

Public Sub Form_Open()

  wiringPiSetup()
  'config I/O modes
  pinMode(D0, PIN_OUTPUT)
  pinMode(D1, PIN_OUTPUT)
  pinMode(D2, PIN_OUTPUT)
  pinMode(D3, PIN_OUTPUT)
  pinMode(MODE_SELECTOR, PIN_OUTPUT)
  pinMode(MODULATOR_ENABLE, PIN_OUTPUT)
  digitalWrite(MODE_SELECTOR, 0)       '0 = on/off keying (ASK)
  digitalWrite(MODULATOR_ENABLE, 0)
  digitalWrite(D3, 0)
  digitalWrite(D2, 0)
  digitalWrite(D1, 0)
  digitalWrite(D0, 0)

Catch
  Me.Text = "Failed to setup wiringPi. This program must be run as root"
End

Public Sub chkPower1_Click()

  If chkPower1.Value = True Then
    digitalWrite(D3, 1)
    digitalWrite(D2, 1)
    digitalWrite(D1, 1)
    digitalWrite(D0, 1)
  Else
    digitalWrite(D3, 0)
    digitalWrite(D2, 1)
    digitalWrite(D1, 1)
    digitalWrite(D0, 1)
  Endif
  SendCommand()
End


Public Sub chkPowerAll_Click()

  If chkPowerAll.Value = True Then
    chkPower1.Value = True
    chkPower2.Value = True
    chkPower3.Value = True
    chkPower4.Value = True   
  Else
    chkPower1.Value = False
    chkPower2.Value = False
    chkPower3.Value = False
    chkPower4.Value = False   
  Endif
End

Public Function SendCommand() As Boolean
 
  Wait 0.1
  digitalWrite(MODULATOR_ENABLE, 1)
  Wait 0.25
  digitalWrite(MODULATOR_ENABLE, 0)

End 


...the code for chkPower2, 3 & 4 is missing, but is very similar to chkPower1_Click.

No comments:

Post a Comment