Making an IoT-based intrusion detector
Now that Raspberry Pi is set up and we are ready to make it IoT enabled let's see how we are going to connect the system to the internet and make things work. Firstly, we need to connect Raspberry Pi to the devices, which we want to control using the IoT technology. So go ahead and use the following diagram to make the connection:
Once you have set up all the components, let's go ahead and upload the following code:
import time import paho.mqtt.client as mqtt import RPi.gpio as gpio pir = 23 gpio.setmode(gpio.BCM) gpio.setup(pir, gpio.IN) client = mqtt.Client() broker="broker.hivemq.com" port = 1883 pub_topic = "IntruderDetector_Home" def SendData(): client.publish(pub_topic,"WARNING : SOMEONE DETECTED AT YOUR PLACE") def on_connect(client, userdata, flag,rc): print("connection returned" + str(rc)) SendData() while True: client.connect(broker,port) client.on_connect = on_connect if gpio.output(pir) == gpio.HIGH : SendData() client.loop_forever...