How to trigger a script on pi 3?

The agent can be installed by navigating to the root directory of the agent code and running:

sudo python3 setup.py install

I wanted to do this, but

I do not know where that directory is or the name of it.

I got agent 2.0 installed

2018-05-05 21:46:04 - myDevices - DEBUG - /usr/local/lib/python3.4/dist-packages/myDevices-2.0.0-py3.4.egg/myDevices/system/systemconfig.py getConfig:66> SystemConfig: {‘OneWire’: 1, ‘DeviceTree’: 1, ‘SPI’: 1, ‘Serial’: 1, ‘I2C’: 1}
2018-05-05 21:46:04 - myDevices - INFO - Send changed data: [{‘sys:cpu;load’: 19.3}, {‘sys:cpu;temp’: 45.622}, {‘sys:ram;usage’: 425218048}, {‘sys:storage:/;usage’: 6645415936}, {‘sys:gpio:24;function’: ‘IN’}]
2018-05-05 21:46:05 - myDevices - DEBUG - /usr/local/lib/python3.4/dist-packages/myDevices-2.0.0-py3.4.egg/myDevices/cloud/cayennemqtt.py publish_packet(topic=data/json)(packet=[{“value”: 19.3, “unit”: “p”, “type”: “cpuload”, “channel”: “sys:cpu;load”}, {“value”: 45.622, “unit”: “c”, “type”: “temp”, “channel”: “sys:cpu;temp”}, {“value”: 425218048, “unit”: “b”, “type”: “memory”, “channel”: “sys:ram;usage”}, {“value”: 6645415936, “unit”: “b”, “type”: “memory”, “channel”: “sys:storage:/;usage”}, {“value”: “IN”, “channel”: “sys:gpio:24;function”}])(qos=0)(retain=False):231> Publish to v1/a7fc5bb0-e16c-11e6-a446-0d180dc59d42/things/1a3a67d0-5093-11e8-b9fb-15665fa66930/data/json
^Z
[1]+ Stopped python3 -m myDevices -d
pi@raspberrypi:~ $

But I have lost the other type of connection, where I had the motion detector.

Now it looks like only 2.0 connection is working, but the motion detector is not finish in “bring your own”. So maybe I just got to wait some week until this is readdy?

you are trying to add motion sensor on raspberry pi agent 2.0 and a button on BYOT on same raspberry pi?

Yes, that is correct.

-------- Opprinnelig melding --------

so that when there is motion sensed it clicks a photo?

Yes, that is the plan, but now the motion sensor is not working with 2.0

-------- Opprinnelig melding --------

i have another approach for this. in your BYOT code add a code for motion sensor.

Thank you for your help but I am more than 60 years old, and do not know how to do that,or what to write.
I have the motion sensor on GPIO18, so I ftied this code for the callback function# The callback for when a message is received from Cayenne.
def on_message(message):
print("message received: " + str(message))
if (message.channel == 4) and (message.value == “1”):
print(“button is pressed”)
camera.start_preview()
sleep(5)
camera.capture(‘/home/pi/Desktop/image.jpg’)
camera.stop_preview()
if (message.channel == 18) and (message.value == “1”):
print(“motion”)

But there was no reaction.
I tested with a voltmeter, and the motion sensor changed the voltage from 0 til 3,2v on GPIO18, so it is working.

Maybe a high voltage is not the same as “1”?

try this code first and see if you get pir sensor reading:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN) #PIR
 
while True:
    if GPIO.input(pirPin):
            print("Motion Detected...")
            time.sleep(5) #to avoid multiple detection
    time.sleep(0.1)

Yes, it is working. Had to change pirPin till 18. I guess this should have been declared, but yes it is working, so I could make the motion detaction and taking the picture without using Cayenne part, but I want to be notified when that happed. Most likly someone is in my boat then.

#!/usr/bin/env python
#import cayenne.client
import time
from picamera import PiCamera
from time import sleep
camera = PiCamera()
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN) #PIR

while True:
if GPIO.input(18):
print(“Motion Detected…”)
# time.sleep(5) #to avoid multiple detection

        camera.start_preview()
        sleep(5)
        camera.capture('/home/pi/Desktop/image.jpg')
        camera.stop_preview()

time.sleep(0.1)

this is where the awesome cayenne comes into picture. add a two state widget. when a motion is detected the state becomes 1 and then you set a sms notification to let you know that someone is at your boat.

Yes, that will be my next challenge.
I looked at the send example and see this part of the code:
while True:
client.loop()

if (time.time() > timestamp + 10):
    client.celsiusWrite(1, i)
    client.luxWrite(2, i*10)
    client.hectoPascalWrite(3, i+800)
    timestamp = time.time()
    i = i+1

I guess client.xxxxx(channel, value)

so I need to find a xxxxx that fits motion.

have a look at this Data types for Cayenne MQTT API

Thank you so much.
I have never been in a forum where someone has given me so much help so fast for free.
Thank again.

2 Likes

You’re welcome, sir.
more than happy to help you complete the awesome project with cayenne.

1 Like

I feel like a child needing a helping hand for every step on the way.
Now I wanted to communicate the motion back to Cayenne so it could trigger an sms message to me.
I have the sms trigger attached to motion in Channel 2.

Here is part of the code:

# If there is an error processing the message return an error string, otherwise$

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

while True:
    client.loop()
    if GPIO.input(18):
            print("Motion Detected...")
            client.virtualWrite(2,1,"Motion","d")
            time.sleep(5) #to avoid multiple detection
            camera.start_preview()
            time.sleep(5)
            camera.capture('/home/pi/Desktop/image.jpg')
            camera.stop_preview()
            time.sleep(12)  #wait so Cayenne web will show motion
    else:
            client.virtualWrite(2,0,"Motion","d")
    time.sleep(0.1)

There is no reaction in the dashboard when there is motion,
so the sms trigger is not activated.

change this:

and

to
client.virtualWrite(2,1,"digital_sensor","d")
and
client.virtualWrite(2,0,"digital_sensor","d")

Try this:

motion = false
motioncounter = 0
while True:
    client.loop()
    if motion:
        motioncounter += 1
    if (not motion) or (motioncounter > 50):
        if motioncounter > 50:
            motioncounter = 0
        if GPIO.input(18):
            print("Motion Detected...")
            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('/home/pi/Desktop/image.jpg')
            camera.stop_preview()
            motion = true
            motioncounter = 0
        else:
            client.virtualWrite(2,0,"digital_sensor","d")
            motion = false
    time.sleep(0.1)

The issue I see is that you are sleeping too long. client.loop() needs to be called at a minimum every 2 seconds or you will get disconnects

1 Like