For this excercise you need a (free) account at http://www.realtime.co/; if you create an account and start a "Realtime Messaging Free" subscription, you can put its "Application Key" in the variable below. This key will then be used in the communications we'll set up further on.
You will also have to have your Raspberry Pi connected to the internet for the communications to work.
In [ ]:
APPKEY = "******"
We will again use the "RPi.GPIO" library in our code
And for the GPIO pins we will, as usual, use the BCM numbering (cfr numbers on the case)
In [ ]:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
ortc is a Python library that is linked to the realtime.co cloud communication system (http://www.realtime.co/)
You can compare it to e-mail, Twitter or Facebook Messenger, but intended to be used by computers or devices.
In [ ]:
import ortc
oc = ortc.OrtcClient()
oc.cluster_url = "http://ortc-developers.realtime.co/server/2.1"
# Connecting to the account
oc.connect(APPKEY)
With person_pins we create a dictionary that links each of the GPIO pins to the person who the door bell of that pin belongs to.
In [ ]:
person_pins = {
17: "Maleficent",
27: "BigBadWolf",
}
Let's see if that works:
In [ ]:
print(person_pins[17])
Now let's create a function with a parameter called "pin" (no confusion possible there...); we intend to use this function to send a message, specific to the relevant pin.
If the value of pin is 17, we'll send a message "Maleficent" on the "doorbell" channel.
In [ ]:
def send_message(pin):
oc.send("deurbel", person_pins[pin])
Last piece of the setup: for each of the possible pin values, we 'll:
In [ ]:
for pin in person_pins:
GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)
GPIO.add_event_detect(pin, GPIO.FALLING, send_message, 200)
Time to test is out: press the button and see if your message appears in the cloud!
Follow the following procedure to check it:
And clean up again:
In [ ]:
for pin in person_pins:
GPIO.remove_event_detect(pin)
In [ ]:
GPIO.cleanup()