How to trigger a script on pi 3?

I want to take a picture when motion sensor is triggered. I was waithing for Cayenne to make the camera widget, but it is taking too long. I have a script that will do it, but I need to send a command to start the script. What to do?

this should solve your problem Executing custom code on your Raspberry Pi from Cayenne

Thank you for the quick response. This looks to be what I need. Is it possible to keep the Cayenne dasboard connection, and just add the MQTT connection for this script running? Or is it only possible to use one type of connection running at the same time?

are you saying that using pi agent and MQTT on one pi at same time? i gave it a try and it works. you can try it out.

Thank you for you help, now I have pago-mqtt version 1.3.1 running on the pi, but I am not sure where to fine the MQTT_username and password and client ID I need to put in the python files.

on your dashboard go to: add new > devices > Bring Your Own Thing > you will find MQTT credential.

Thank you. It was the screen on Vnc to pi that was too small to see it all,but whenone knows where to scroll Imanageto find it.
Now it is running, but I wonder about something. I use to test scripts in python to see if it is working, and test.py never run, put test2.py runs every 10 seconds.
This code never run:
def on_message(message):
print("message received: " + str(message))
exec(open(‘test.py’).read())
I had to put in:
client.luxWrite(4, ‘1’) as a way to get value “1” inchannel 4.
This is the whole file:

#!/usr/bin/env python
import cayenne.client
import time

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "MQTT_USERNAME"
MQTT_PASSWORD  = "MQTT_PASSWORD"
MQTT_CLIENT_ID = "MQTT_CLIENT_ID"


# The callback for when a message is received from Cayenne.
def on_message(message):
    print("message received: " + str(message))
    exec(open('test.py').read())
    # If there is an error processing the message return an error string, otherwise return nothing.
    
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 (time.time() > timestamp + 10):
        client.celsiusWrite(1, i)
        client.luxWrite(2, i*10)
        client.hectoPascalWrite(3, i+800)
        client.luxWrite(4, '1')
        exec(open('test2.py).read())
        timestamp = time.time()
        i = i+1

What am I doing wrong?

try the below code. next, on your cayenne dashboard add a button: add new > device > custom widget > button > fill all the details and select channel as 4 and press add widget.

#!/usr/bin/env python
import cayenne.client
import time

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "MQTT_USERNAME"
MQTT_PASSWORD  = "MQTT_PASSWORD"
MQTT_CLIENT_ID = "MQTT_CLIENT_ID"


# 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")    
    # If there is an error processing the message return an error string, otherwise return nothing.
    
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)

while True:
    client.loop()

on clicking the button on dashboard, check for the print statement “button is pressed” in the connsole.if all ok then you will get the print.

I did not get time to test it before today, and to my surprice it worked at first run.
I used ‘python3 mycode.py’
If I try to use ‘python mycode.py’ I got some error messages.
Also I needed to reboot the pi, when I made some changes to the file to make it work.
Now I will try to adjust so the motion sensor triggers the camera.
Reboot was not needed, just a refres of the web page.
printing text works fine, but when I try to take a picture it fails.
I have made a very simple line of command “raspistill -o picture.jpg”
To turn the button off works fine, but when I turn it on, the devise goes offline and no file “picture.jpg” is made.
When I try to run python test.py where I have the “raspistill -o picture.jpg” command, it fails too, but when I run the “raspistill -o picture.jpg” from the command promt it works fine?
So this is more a python problem than a Cayenne problem, but any help to fix it will be appresiated.
I tried to run the command from a sh file insted, but when I put in the first line
#!/bin/bash
raspistill -o pic.jpg
command was not found?
I fond the error.
To run the sh file from the command prompt I needed to write ./ in front of the filename. Sorry I am more than 60 years old and programs like a 6 year old child.
Now I can run the sh file and make it take pictures, and I can start py files from the py file running mqtt, but when I try to start sh files from the py file it does not work so good?
So I need to find out how to start sh files from py files, or how to run shell commands directly from py.
I use the following command to start the sh file:
exec(open(“./test.sh”).read())
Maybe I just need to import something in the py file to make it run shell commands?

I have been trying
from subprocess import call
and import os
but with negative result.

try adding the below in the cayenne code:

from picamera import PiCamera
from time import sleep

camera = PiCamera()

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

Fantastic!!!
It is working.
Thank you so much.

would love to see what you end up doing. once complete share your project in the community.

Now I need to replace the button with the motion sensor I got on GPIO18.
Any sugesstions?

I plan to have it in the sailboat to monitor temp, humidity,water level andpicture of any going into the boat.

use cayenne trigger function. let the button be there. now add a trigger if motion detected on GPIO18 then turn button ON hence taking the picture.

cool project. share with the community once completed.

I will share, but now I have added two triggers. One to turn on button when motion, and one to turn off button when no motion.
The triggers have run 0 times?
Do I need to activate the triggers in some way?
When I push “my triggers” they come up with green colour and run 0 times.
When I look at the dasboard, the motion detector is working fine.
Looks like the Raspberry Pi is out of resourses…
After reboot of the Pi the result is the same.

I feel I am so close now. There is only the triggering that is needed. I have been planing this for over a year, and if this works, it can be expanded with a second USB camera that can be controled by motors in two axes and a lot of other sensors.
Maybe I should upgrade the kernal from 4.4?

you are using pi agent 2.0?

No, but I get worned that I shold upgrade to it. But I am not sure how to do it.
I guess it must be done on the pi?