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.