2018 Nov 28, 06:35 PM
Hi Ben,
It has been a while.
I wanted to let you know about some progress I have made with the tank level plugin.
I have re-written the code for the ultrasonic distance sensor using the pigpio library.
http://abyz.me.uk/rpi/pigpio/
The library is included with the latest full versions of Raspbian OS. If you are using the light version you can install it with:
and enable it to start at boot up with:
The new version of the ultrasonic code is much more accurate than the original and has fewer lines of code. The pigpio
library uses a C program running in the background and does timing in micro seconds.
Here is some output from the new code without averaging multiple measurements:
155.2
155.3
155.2
155.2
155.2
155.2
155.2
If you get a chance, try it out and let me know what you think:
I will be working on the plugin over the next few days. The rainy season has started here and I have time for coding.
Dan
It has been a while.
I wanted to let you know about some progress I have made with the tank level plugin.
I have re-written the code for the ultrasonic distance sensor using the pigpio library.
http://abyz.me.uk/rpi/pigpio/
The library is included with the latest full versions of Raspbian OS. If you are using the light version you can install it with:
Code:
sudo apt-get update
sudo apt-get install pigpio python-pigpio python3-pigpio
and enable it to start at boot up with:
Code:
sudo systemctl enable pigpiod
sudo reboot
The new version of the ultrasonic code is much more accurate than the original and has fewer lines of code. The pigpio
library uses a C program running in the background and does timing in micro seconds.
Here is some output from the new code without averaging multiple measurements:
155.2
155.3
155.2
155.2
155.2
155.2
155.2
If you get a chance, try it out and let me know what you think:
Code:
#!/usr/bin/python
from __future__ import print_function, division
import pigpio
from time import sleep
pi = pigpio.pi()
# Define GPIO pins to use on Pi (BMC numbering)
pin_trigger = 23
pin_echo = 24
start = None
distance = None
# Speed of sound in cm/ms at temperature (changed from cm/s)
temperature = 24
sound_speed = 33.1 + (0.0006*temperature)
# Set pin modes and output level
pi.set_mode(pin_trigger, pigpio.OUTPUT)
pi.write(pin_trigger, 0)
pi.set_mode(pin_echo, pigpio.INPUT)
def calc(gpio, level, tick):
"""
Callback function to handle distance calculation.
"""
global start, distance
if level == 1: # Start of echo pulse
start = tick
if start and level == 0: # If start has a value and echo pulse ended
echo = pigpio.tickDiff(start, tick) # Length of echo pulse in microseconds
distance = round(((echo / 1000) * sound_speed)/2, 1)
print(distance)
start = None
def measure():
"""
Send a pulse and time the echo.
"""
pi.gpio_trigger(pin_trigger, 10, 1) # Send a 10 microsecond pulse on the trigger pin
cb = pi.callback(pin_echo, pigpio.EITHER_EDGE, calc) # catch the echo and pass timing to calc callback function
while True:
measure()
sleep(1)
I will be working on the plugin over the next few days. The rainy season has started here and I have time for coding.
Dan