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.
--- 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
Setting s1=1 turns on socket 1. Setting s1=0 turns off socket 1
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.
In Exercise 1, add a voice user interface (VUI) to control:
voxkey = "xf73702aa-f9de-43cb-B25D-9693884a3c25"
--- 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'
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.
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.
RF codes associated with the 20 buttons:
RGB LED strip with 5050 5V LED chip.
Connections: 5V :: 5V, G::d3, R::d2, B::d1.
--- Pin connections
b :: d1
g :: d3
r :: d2
--- Commands
blue = [b=1023-102*$]
green = [g=1023-102*$]
off = [r=1,g=1,b=1]
on = [r=0,g=0,b=0]
red = [r=1023-102*$]
Try:
red=10 --- max value
red=0 --- min value
Simple device to demonstrate interaction with IoT MQTT panel app. The device uses: an LED, an LDR and a push button.
--- Pin connections
But :: D3
led :: d7
vcc :: d5
--- Variables
br = 3
--- Commands
start = [vcc=1,wifi=1]
analog = [< '{"a":' + A0 + '}']
button = [< 'button='+$]
ledb = [led=$*leds] --- led brightness: 0,..,1023
leds = [led=ledb*$] --- led status: 0|1
--- Events
Click = But==0
--- Rules
Wificon -> mqtt=1
Mqttcon -> [button=0,analog#1]
Click -> button=!button
br==0 -> ledb=0
br==1 -> ledb=4
br==2 -> ledb=16
br==3 -> ledb=64
br==4 -> ledb=256
br==5 -> ledb=1023
Nota: the light sensor can also be used to check that the LED is really on.
type: gauge panel; topic sub: out
min: 0, max: 1023
Color sectors: gray, brown, yellow [0,350,700,1023]
JSON payload: $.a
NOTE: every second Smick sends
< {"a":<A0>}
See analog command.
type: multi-state indicator; topic sub: out
payload/label: Conn/on, Disc/off
type: button panel; topic pub: in
payload: mqtt=0,reset##1
type: switch panel; topic pub: in
payload on: leds=1, payload off: leds=0
type: led indicator; topic sub: out
payload on: button=1, payload off: button=0
type: slider panel; topic pub: in
min: 0, max: 5
JSON payload: br=<slider-payload>
Connect RGB LED and the matrix 3-buttons RGB.
Red::!D2, Yellow::!D3, Green::!D4, Ground::GND
--- Pin connections
G :: !D4
R :: !D2
Y :: !D3
b :: d8
g :: d7
gnd :: d6
r :: d5
--- Commands
loop = [r=R|Y,g=G|Y]
start = [gnd=0,status=0,loop*]
stop = [r=0,g=0,b=0]
S :: A0
G :: d0 (gnd)
V :: d5 (vcc)
--- Pin connections
Light :: A0
gnd :: d0
vcc :: d5
--- Commands
start = [gnd=0,vcc=1,Light #1]
Illuminando con una luce a LED in penombra
value distance (cm)
1023 3
540 5
250 8
128 10
64 20
32 25
16 36
8 64
Macro regola: al raddoppiare della distanza il valore si dimezza.
Per un semplice sensore di luminosità è preferibile un LDR.
V :: d6 (vcc)
S :: D7
G :: d8 (gnd)
--- Pin connections
Touch :: D7
gnd :: d8
vcc :: d6
--- Commands
start = [gnd=0,vcc=1]
--- Events
Click = Touch
Avvicinando qualsiasi cosa (3mm) si genera un evento Click. Il sensore ha anche un LED rosso che si accende al touch.
LDR: RST, A0
RFTX: rf :: d6, vcc :: d7, gnd :: d8
BUZZER: buz :: d2, GND
where msg=
"dark alarm <pp>%" | "light alarm <pp>%" | "alarm off"
--- Pin connections
buz :: d2
gnd :: d8
rf :: d6
vcc :: d7
--- Variables
ch = '10000'
darkAlarm = 1
darkThreshold = 22
ldr = 100
lightAlarm = 1
lightThreshold = 71
msg = 'alarm off'
--- Commands
beep = [buz=0,buz=!buz ^6]
alarm = [beep #2*$,[var,<alarm]##]
light = [ldr=+(A0/10.23/'.'),json,< ldr]
log = [msg=$,json,< log]
rf1 = [rfch=ch+'10000',rf=$,var,< rf1]
rf2 = [rfch=ch+'01000',rf=$,var,< rf2]
rf3 = [rfch=ch+'00100',rf=$,var,< rf3]
rf4 = [rfch=ch+'00010',rf=$,var,< rf4]
rf5 = [rfch=ch+'00001',rf=$,var,< rf5]
rfall = [rf1=$,[rf2=$,[rf3=$,[rf4=$,rf5=$##]##]##]##]
start = [vcc=1,gnd=0,ch='10000',ldr=0,lightAlarm=0,darkAlarm=0,lightThreshold=100,darkThreshold=0,wifi=1]
--- Rules
Wificon -> mqtt=1
Mqttcon -> light #60
!alarm -> log='alarm off'
-A0/20/'.' -> light
A0/20/'.' -> light
darkAlarm & ldr<=darkThreshold -> [alarm=1,log='dark alarm ' + darkThreshold +'%']
lightAlarm & ldr>=lightThreshold -> [alarm=1,log='light alarm '+ lightThreshold +'%']