DOOR SENSOR

18. Door sensor

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).

Figure 1 - how the reed switch works

Figure 1 - Door sensor circuit

1. Circuit

Connect the Door sensor as shown in Figure 1:

  • a PIN to GND,
  • other PIN to D3.

Note: PINs can also be inverted.

2. Sketch

Write a sketch K to define:

  • the input connection: Door :: D3
  • the events to send: Closed, Open
  • the external event: State
  • the commands: start, msg
  • the rule for send the door status when asked through the external event State
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:

  • x == 0 <-> x == false <-> ! x
  • x! = 0 <-> x == true <-> x

So you can also define:

Closed = !Door
Open = Door

3. Try and learn

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.

4. Summarizing

In this lesson you learned or repeated:

  • how to use a Door sensor
  • how to send and receive events over the Internet.