I have an ultrasonic sensor that writes the distance in a txt file, I want to get that data and plot it on a line graph in real time but I can't find a way to do it.
What I have done till now is that it would read the file but it never shows the data.
sensor.py
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
GPIO.output(TRIG, True)
GPIO.output(TRIG,False)
def get_distance():
GPIO.output(TRIG, True)
time.sleep(0.0001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO) == False:
start = time.time()
while GPIO.input(ECHO) == True:
end = time.time()
sig_time = end - start
distance = sig_time/0.000058
print('Distance: {} cm'.format(round(distance)))
return distance
while True:
distance = get_distance()
data = round(distance)
output = open("front sensor distance.txt", "w")
output.write(str(data))
time.sleep(2)
output.close()
Whenever I run the code the distance gets deleted instantly and does not wait for time.sleep(2)
