Exercises

RF transmitter and Alexa

1. RF remote controller (4 channels)

Make a 433 MHz RF remote control to turn on / off 4 radio controlled sockets.

Required material: an RF transmitter.

Transmitter connections: Vcc: 3.3V, Gnd: GND, data: d2.

Sketch

--- Pin connections
rf :: d2   

--- Commands
start = [s1=0,s2=0,s3=0,s4=0]  
sock1=[rfch='1000010000',rf=s1]   
sock2=[rfch='1000001000',rf=s2]   
sock3=[rfch='1000000100',rf=s3]   
sock4=[rfch='1000000010',rf=s4]   

--- Rules
!s1 -> sock1
!s2 -> sock2
!s3 -> sock3
!s4 -> sock4
 s1 -> sock1
 s2 -> sock2
 s3 -> sock3
 s4 -> sock4

Meaning of the variables

  • s1: state of the socket 1
  • s2: state of the socket 2
  • s3: state of the socket 3
  • s4: state of the socket 4

Use

Setting s1=1 turns on socket 1. Setting s1=0 turns off socket 1

Note

Since the action of the two rules

!s1 -> sock1
 s1 -> sock1

is the same you are tempted to do the OR of the predicates

 !s1 | s1 -> sock1

But it will never work because x! | x is always true (tautology) so it will never vary. Because rules and events are activated when the predicate becomes true, this will never happen.

2. Alexa and RF remote controller

In Exercise 1, add a voice user interface (VUI) to control:

  1. Heater
  2. Lamp
  3. Printer
  4. Television.

Configuration

voxkey = "xf73702aa-f9de-43cb-B25D-9693884a3c25"

Sketch

--- Events
Heater = Voxdev=='5bf52e06861813419eab830e'   
Lamp = Voxdev=='5beddc335f94a3052ae72811'
Printer = Voxdev=='5bf4b3e9eb220f3c039cf752' 
Television = Voxdev=='5bf52d59861813419eab830d'

ON = Voxval=='ON'
OFF = Voxval=='OFF'

--- Commands
start = [v=0,mqtt=0,wifi=1] 

--- Rules
Wificon -> vox=1
ON -> v=1
OFF -> v=0
Heater -> [rfch='1000010000',rf=v] ##
Lamp -> [rfch='1000001000',rf=v] ##
Printer -> [rfch='1000000100',rf=v] ## 
Television -> [rfch='1000000010',rf=v] ## 

To track all messages sent by Alexa add:

--- Rules
Voxcom -> [Voxdev,Voxcom,Voxval]

Note 1: if you use Google Home the ON and OFF definitions OFF become:

--- Events
ON = true<<Voxval
OFF = false<<Voxval

Note 2: if you use Alexa and Google Home at the same time the ON and OFF definitions OFF become:

--- Events
ON = Voxval=='ON' | true<<Voxval
OFF = Voxval=='OFF' | false<<Voxval

Note 2: you can compare a part of the device code (Voxdev) using the '== *' "ends with" relationship.

--- Events
Heater = Voxdev==*'830e'   
Lamp = Voxdev==*'2811'
Printer = Voxdev==*'f752' 
Television = Voxdev==*'830d'  

Use with Alexa

Saying "Alexa, turn on the Lamp", Alexa sends this message to Smick

{"Voxdev": "'5beddc335f94a3052ae72811'", "Voxcom": "'setPowerState'", "Voxval": "'ON'"} 

Saying "Alexa, turn off the Lamp", Alexa sends this message to Smick

{"Voxdev": "'5beddc335f94a3052ae72811'", "Voxcom": "'setPowerState'", "Voxval": "'OFF'"}

Lamp is an event defined as:

Lamp = Voxdev=="5beddc335f94a3052ae72811"   

or in short form

Lamp = Voxdev==*"2811"   

Events ON and OFF are so defined:

--- Events
ON = Voxval=='ON'
OFF = Voxval'OFF'

and they set or reset the variable v

ON -> v=1
OFF -> v=0

This simple rule:

Lamp -> [rfch='1000001000',rf=v] ##

will be activated when the message is received and with the command [rfch='1000001000',rf=v] you send turn-on RF code if v = 1 or send turn-off RF code if v = 0.

Use with Google Home

Saying "Ok Google, turn on the Lamp", Google Home sends this message to Smick

{"Voxdev": '5beddc335f94a3052ae72811', Voxcom='action.devices.commands.OnOff',Voxval='{"on":true}'} 

Saying "Alexa, turn off the Lamp", Alexa sends this message to Smick

{"Voxdev": '5beddc335f94a3052ae72811', Voxcom='action.devices.commands.OnOff',Voxval='{"on":false}'} 

Lamp is an event defined as:

Lamp = Voxdev=="5beddc335f94a3052ae72811"   

or in short form

Lamp = Voxdev==*"2811"   

Events ON and OFF are so defined:

--- Events
ON = true<<Voxval
OFF = false<<Voxval

and they set or reset the variable v

ON -> v=1
OFF -> v=0

This simple rule:

Lamp -> [rfch='1000001000',rf=v] ##

will be activated when the message is received and with the command [rfch='1000001000',rf=v] you send turn-on RF code if v = 1 or send turn-off RF code if v = 0.