This example of digital input uses a door sensor to detect and report via the Internet two events: open door (Open) and closed door (Closed). It also sends the door status if requested by external event State.
With this sensor you can connect to the Internet doors, windows, drawers or anything that opens and closes. The sensor consists of a reed switch and a magnet. The reed switch contacts are of magnetic material and the external magnet acts directly on them (Figure 1).
Connect the Door sensor as shown in Figure 1:
Note: PINs can also be inverted.
Write a sketch K to define:
Door :: D3
Closed = Door==0
Open = Door==1
State = 0
msg = [date,time,sn,Door]
start = [led=Door *]
State -> msg
or simply load the sketch in the workspace with the command
] load e18_door_sensor
Note
The following equivalences are valid:
So you can also define:
Closed = !Door
Open = Door
Fix the magnet on the door and Smick with the reed switch on its frame.
Open the door
< Open
the Open event happens and is sent over the Internet; the status LED turns on.
Close the door
< Closed
the Closed event happens and is sent over the Internet; the status LED turns off.
Via internet send to Smick the State external event
> State
Smick reply with a message containing the door status over the date, time and its serial number
< date=03-09-2017,time=19:29:45,sn=25396549,Door=0
Door=0 indicates closed door and Door=1 indicates open door.
In this lesson you learned or repeated: