Created
November 29, 2017 03:12
-
-
Save bmidgley/fa479880028577c0db7e725e61c4ab98 to your computer and use it in GitHub Desktop.
Monitor a raspberry pi motion sensor via mqtt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import RPi.GPIO as GPIO | |
import time | |
import os | |
import sys | |
import threading | |
import paho.mqtt.publish as publish | |
GPIO.setmode(GPIO.BCM) | |
motion_pin = 15 | |
led_pin = 16 | |
GPIO.setup(motion_pin, GPIO.IN) | |
GPIO.setup(led_pin, GPIO.OUT) | |
lastMotion = 0 | |
while True: | |
motion = GPIO.input(motion_pin) | |
if motion != lastMotion: | |
publish.single('env1/motion', "{\"motion\": %d}" % lastMotion, hostname='mqtt.sample.com', port=1883, client_id='pi0') | |
publish.single('env1/motion', "{\"motion\": %d}" % motion, hostname='mqtt.sample.com', port=1883, client_id='pi0') | |
GPIO.output(led_pin, motion) | |
lastMotion = motion | |
time.sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment