Trigger and return values from python script


I have a Raspberry pi running particle-agent and have been looking around for the best way to trigger an ultrasonic sensor. Is there a way that i cloud do that on the .ino file on the agent. I have gotten it to work with a python script, and thought of using the method “Process” where you run the python script and get the values each time the agent gets triggered, but that way did not work. Is there a better way of doing it?

This is my code for python

import RPi.GPIO as GPIO
import time

TRIG = 17
ECHO = 27

# print "Distance Measurement In Progress"


GPIO.output(TRIG, False)

# while True:

# print "Waiting For Sensor To Settle"
def main():

    GPIO.output(TRIG, True)
    GPIO.output(TRIG, False)

    while GPIO.input(ECHO)==0:
        pulse_start = time.time()

    while GPIO.input(ECHO)==1:
        pulse_end = time.time()

    pulse_duration = pulse_end - pulse_start

    distance = pulse_duration * 17150

    distance = round(distance, 1)

    print "dis=",distance,"'C"

    # return distance
    return distance

if __name__ == '__main__':

This is my code on the cloud

void loop() {
  Process proc = Process::run("python /home/pi/Desktop/");
  float final = proc.out().parseFloat();
  String final = String(cpuTemp);
  Particle.publish("triggered", finaltemp);

Thanks in advance.


You could try pulseIn().

When you are using Particel Agent, then you may want to do as much as you can in the actual Wiring sketch. This way you may well avoid the python skript completely.


Hello! Thanks for reply!
and yes, using the particle agent only is my main goal, but after trying everything i know turned into running the python script, which i did not want to do at all.

I did try doing this, but it did not work. my value is always 0. (checked the pins, and it functions using the python scripts)

unsigned long duration;

void setup()

    pinMode(D3, OUTPUT);   // LED on D7 pin 22
                           // ultrasonic range finder Robotshop RB-lte-54
                           // GND pin goes to ground
    pinMode(D2, INPUT);    // echo pin 27
    pinMode(D1, OUTPUT);   // Trig pin 17
                           // VCC pin goes to VIN on the pi 5v


void loop(){
        digitalWrite(D1, HIGH);         // activate trigger
        digitalWrite(D1, LOW);          // de-activate trigger

        duration = pulseIn(D2, HIGH);   // how long until a reply?
                                        // a blocking call so may wait a few seconds                
        if (duration > 2000    ){        // raw data from 200 to 16000                                         
            digitalWrite(D3, HIGH);     // D7 Blue LED on if far
        } else {
            digitalWrite(D3, LOW);      // D7 Blue LED off

To be honest, not sure what to do anymore. open for anything to anything at this point.


Closing this thread since OP has moved on to seperate thread focusing on the fully contained approach without the use of Python