Take picture when motion with Pi 3 and Agent 2.0 CayenneMQTTClient() and notify by email - how to trigger a script on Pi 3


#1

About This Project

Using Raspberry Pi 3 and Python3 with Cayenne Agent 2.0

Want to monitor my sailbout, with temp, humidity, water level inside and picture if motion detected.
If motion, I also want email or sms notification.
Later I will have notification if any of the other sensors go above treshold levels.

What’s Connected

During this inital testing only motion detector and camera is connected. I have WiFi in the boat.

Triggers & Alerts

On motion, picture is taken and email sent

Motion detecton on GPIO 18

During testing a ButtonS was made, and a Quit button to stop the python program.

Scheduling

No

Dashboard Screenshots



I used Custom Widgets -> Button
and
Custom Widgets -> 2 state

This is working with triggers and will send emails and sms

(Paste screenshots of dashboard, triggers & alerts, Scheduling)

Photos of the Project

(Take some pictures of your project functioning in the wild!)

Video

(Upload a YouTube video showcasing your project in action!)
Python3 code:

#!/usr/bin/env python

from picamera import PiCamera
from time import sleep
import RPi.GPIO as GPIO
import datetime

camera = PiCamera()
GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN) #PIR
camera.rotation =180 # flip the pic

import cayenne.client
import time
x = 0
# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.

#MQTT_USERNAME  = ""
#MQTT_PASSWORD  = ""
#MQTT_CLIENT_ID = ""


# The callback for when a message is received from Cayenne.
def on_message(message):
    global x
    print("message received: " + str(message))
    # If there is an error processing the message return an error string, otherwise return nothing.
    if (message.channel == 4) and (message.value == "1"):
        print("button is pressed")
        x = 1
    if (message.channel == 5) and (message.value == "1"):
        print("quit button is pressed")
        quit()

client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883)

i=0
timestamp = 0

motion = False
motioncounter = 0
while True:
    client.loop()
    if motion:
        motioncounter += 1
    if (not motion) or (motioncounter > 30):
        if motioncounter > 30:
            motioncounter = 0
        if GPIO.input(18):
            print("Motion Detected...")
            dt = datetime.datetime.now()
            fn = '{:/home/pi/mqtt/examples/pic/%Y%m%dT%H%M%S.jpg}'.format(dt)
            client.virtualWrite(2,1,"digital_sensor","d")
            camera.start_preview()
            #time.sleep(5) ####If you really do need a delay here make it less than 1 second
            camera.capture(fn)
            #camera.capture('/home/pi/Desktop/image.jpg')
            camera.stop_preview()
            motion = True
            motioncounter = 0
        else:
            if (time.time() > timestamp + 10):
                print("making two state zero")
                client.virtualWrite(2,0,"digital_sensor","d")
                timestamp = time.time()
                motion = False
    time.sleep(0.1)

Maps, camera, gps stream. When will the complete cayenne agent be ready?
Photo and Video stream from cayenne dashboard
Raspberry Pi Agent 2.0
Cayenne with camera
Stream Pi camera / video on Cayenne
Photo and Video stream from cayenne dashboard
#2

nice @svein.utne, cant wait to see a video of it in wild.