Trigger and return values from python script


#1

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
GPIO.setmode(GPIO.BCM)

TRIG = 17
ECHO = 27

# print "Distance Measurement In Progress"

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG, False)

# while True:

# print "Waiting For Sensor To Settle"
def main():
    time.sleep(1)

    GPIO.output(TRIG, True)
    time.sleep(0.00001)
    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
    time.sleep(1)
    return distance
    GPIO.cleanup()

if __name__ == '__main__':
    main()

This is my code on the cloud

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

Thanks in advance.


#2

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.


#3

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
        delayMicroseconds(10);
        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
        } 
        delay(3000);  
}

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


closed #4

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