Wednesday 24 May 2017

My Project Details

In my first project is PIR sensor connected to the raspberry pi.


import RPi.GPIO as GPIO
import time
import mosquitto ,os , urlparse
import mosquitto
client = mosquitto.Mosquitto()

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

PIR_PIN=21
GPIO.setup(PIR_PIN,GPIO.IN)
GPIO.setup(10,GPIO.OUT)


def on_connect(mosq, obj, rc):
    print("rc: " + str(rc))

def on_message(mosq, obj, msg):
    print(msg.topic + "pir " + str(msg.qos) + " motion" + str(msg.payload))

def on_publish(mosq, obj, mid):
    print("mid: " + str(mid))

def on_subscribe(mosq, obj, mid, granted_qos):
    print("Subscribed: " + str(mid) + " " + str(granted_qos))

def on_log(mosq, obj, level, string):
    print(string)

mqttc = mosquitto.Mosquitto()
# Assign event callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe

# Uncomment to enable debug messages
#mqttc.on_log = on_log

# Parse CLOUDMQTT_URL (or fallback to localhost)
cloudurl = raw_input("enter url of broker:")
url_str = os.environ.get('CLOUDMQTT_URL',cloudurl)
url = urlparse.urlparse(url_str)

# Connect
mqttc.username_pw_set('yhkjbhzj','5H2qvOhoLiVU')
mqttc.connect(cloudurl, 13452)

topicname=raw_input("enter topic name:")

# Start subscribe, with QoS level 2
mqttc.subscribe(topicname, 2)

# Publish a message
message=raw_input("message:")
mqttc.publish(topicname,message)
rc = 0
while rc == 0:
    rc = mqttc.loop()
    print("rc: " + str(rc))
    while(1):
        if GPIO.input(PIR_PIN):
            print "motion detected"
            camera.start_preview()
            camera.capture('/home/pi/image%s.jpg' % counter)
            counter = counter + 1
            camera.stop_preview()
            time.sleep (5)
        else:
            # Communication error message with the sensor
            print ("Failed to read data from PIR sensor !!!")

This is following cloudmqtt link

https://www.cloudmqtt.com/

you get the pir sensor capture the photos to mqtt dashboard.

1 comment:

  1. Hi, it's a very interesting blog. Thank you for sharing it will be helpful for engineering students to develop their own academic projects. I have found interesting projects in this Raspberry Pi Projects for Engineering Students

    ReplyDelete