Custom Input for hc...
 
Notifications
Clear all

Custom Input for hc sr04 ultrasonic distance sensor  


kennedys1337
(@kennedys1337)
Active Member
Joined: 1 month ago
Posts: 12
Topic starter  

Dear Community and Kyle,

first of all thank you very much for sharing this awesome project.

I was wondering if somebody could write a custom Input for the sensor mentioned in the title.

It is a distance sensor, which I want to use for measuring the water level. I know the volume of my reservoir and I want to calculate the water volume in percent depending on the measured distance.

I am not a programmer at all, but I can change the code to suit my needs ;)

You guys would do me a big favor.

Cheers

Fabian


Quote
Topic Tags
kennedys1337
(@kennedys1337)
Active Member
Joined: 1 month ago
Posts: 12
Topic starter  

Nobody?

Or how do you guys measure the water level?

Fabian


ReplyQuote
Kyle Gabriel
(@kylegabriel)
Member Admin
Joined: 6 years ago
Posts: 429
 

I just added an Input module for the HC-SR04 ultrasonic distance sensor. It will be included in the next release, but if you would like to test whether it works before the release is made, please upgrade to master and the Input will be available to try. Please let me know if it works or not, and provide any errors found in the daemon log if it doesn't. Use the Adafruit guide to hook it up. A link to the adafruit guide is provided once the Input has been added in Mycodo, just expand the options and it will be in the Literature section.

Mycodo Developer


ReplyQuote
kennedys1337
(@kennedys1337)
Active Member
Joined: 1 month ago
Posts: 12
Topic starter  

Ok, after installing sudo pip3 install adafruit-circuitpython-hcsr04, the error did not come up anymore, gonna test it now.

 

@kylegabriel Hey Kyle,

thank you very much for your effort. I get the following error when installing the dependencies:

Collecting pyusb>=1.0.0
[2020-12-23 15:23:44]   Using cached pyusb-1.1.0.tar.gz (78 kB)
[2020-12-23 15:23:45]   Installing build dependencies: started
[2020-12-23 15:24:55]   Installing build dependencies: still running...
[2020-12-23 15:24:57]   Installing build dependencies: finished with status 'done'
[2020-12-23 15:24:58]   Getting requirements to build wheel: started
[2020-12-23 15:25:03]   Getting requirements to build wheel: finished with status 'done'
[2020-12-23 15:25:03] ERROR: Exception:
[2020-12-23 15:25:03] Traceback (most recent call last):
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_vendor/packaging/requirements.py", line 104, in __init__
[2020-12-23 15:25:03]     req = REQUIREMENT.parseString(requirement_string)
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_vendor/pyparsing.py", line 1955, in parseString
[2020-12-23 15:25:03]     raise exc
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_vendor/pyparsing.py", line 3250, in parseImpl
[2020-12-23 15:25:03]     raise ParseException(instring, loc, self.errmsg, self)
[2020-12-23 15:25:03] pip._vendor.pyparsing.ParseException: Expected W:(abcd...), found '_'  (at char 0), (line:1, col:1)
[2020-12-23 15:25:03] 
[2020-12-23 15:25:03] During handling of the above exception, another exception occurred:
[2020-12-23 15:25:03] 
[2020-12-23 15:25:03] Traceback (most recent call last):
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3101, in __init__
[2020-12-23 15:25:03]     super(Requirement, self).__init__(requirement_string)
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_vendor/packaging/requirements.py", line 108, in __init__
[2020-12-23 15:25:03]     requirement_string[e.loc : e.loc + 8], e.msg
[2020-12-23 15:25:03] pip._vendor.packaging.requirements.InvalidRequirement: Parse error at "'_'": Expected W:(abcd...)
[2020-12-23 15:25:03] 
[2020-12-23 15:25:03] During handling of the above exception, another exception occurred:
[2020-12-23 15:25:03] 
[2020-12-23 15:25:03] Traceback (most recent call last):
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/cli/base_command.py", line 224, in _main
[2020-12-23 15:25:03]     status = self.run(options, args)
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/cli/req_command.py", line 180, in wrapper
[2020-12-23 15:25:03]     return func(self, options, args)
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/commands/install.py", line 321, in run
[2020-12-23 15:25:03]     reqs, check_supported_wheels=not options.target_dir
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/resolver.py", line 122, in resolve
[2020-12-23 15:25:03]     requirements, max_rounds=try_to_avoid_resolution_too_deep,
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_vendor/resolvelib/resolvers.py", line 445, in resolve
[2020-12-23 15:25:03]     state = resolution.resolve(requirements, max_rounds=max_rounds)
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_vendor/resolvelib/resolvers.py", line 339, in resolve
[2020-12-23 15:25:03]     failure_causes = self._attempt_to_pin_criterion(name, criterion)
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_vendor/resolvelib/resolvers.py", line 207, in _attempt_to_pin_criterion
[2020-12-23 15:25:03]     criteria = self._get_criteria_to_update(candidate)
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_vendor/resolvelib/resolvers.py", line 198, in _get_criteria_to_update
[2020-12-23 15:25:03]     for r in self._p.get_dependencies(candidate):
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/provider.py", line 172, in get_dependencies
[2020-12-23 15:25:03]     for r in candidate.iter_dependencies(with_requires)
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/provider.py", line 171, in <listcomp>
[2020-12-23 15:25:03]     r
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 257, in iter_dependencies
[2020-12-23 15:25:03]     requires = self.dist.requires() if with_requires else ()
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 239, in dist
[2020-12-23 15:25:03]     self._prepare()
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 226, in _prepare
[2020-12-23 15:25:03]     dist = self._prepare_distribution()
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 319, in _prepare_distribution
[2020-12-23 15:25:03]     self._ireq, parallel_builds=True,
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/operations/prepare.py", line 480, in prepare_linked_requirement
[2020-12-23 15:25:03]     return self._prepare_linked_requirement(req, parallel_builds)
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/operations/prepare.py", line 524, in _prepare_linked_requirement
[2020-12-23 15:25:03]     req, self.req_tracker, self.finder, self.build_isolation,
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/operations/prepare.py", line 88, in _get_prepared_distribution
[2020-12-23 15:25:03]     abstract_dist.prepare_distribution_metadata(finder, build_isolation)
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/distributions/sdist.py", line 39, in prepare_distribution_metadata
[2020-12-23 15:25:03]     self._setup_isolation(finder)
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/distributions/sdist.py", line 99, in _setup_isolation
[2020-12-23 15:25:03]     conflicting, missing = self.req.build_env.check_requirements(reqs)
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_internal/build_env.py", line 155, in check_requirements
[2020-12-23 15:25:03]     if ws.find(Requirement.parse(req)) is None:
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3148, in parse
[2020-12-23 15:25:03]     req, = parse_requirements(s)
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3094, in parse_requirements
[2020-12-23 15:25:03]     yield Requirement(line)
[2020-12-23 15:25:03]   File "/home/pi/Mycodo/env/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3103, in __init__
[2020-12-23 15:25:03]     raise RequirementParseError(str(e))
[2020-12-23 15:25:03] pip._vendor.pkg_resources.RequirementParseError: Parse error at "'_'": Expected W:(abcd...)

[2020-12-23 15:25:06] End install of adafruit_hcsr04

[2020-12-23 15:25:07] 
[2020-12-23 15:25:07] #### Setting permissions

[2020-12-23 15:25:11] #### Dependency install finished
This post was modified 1 month ago by kennedys1337

ReplyQuote
kennedys1337
(@kennedys1337)
Active Member
Joined: 1 month ago
Posts: 12
Topic starter  

Hi @kylegabriel ,

when I activate the sensor the cpu load is at 100% always. I am using a pizero.

cpu; mem; time; command;

94.8 0.3 22:11.91 libgpiod_pulsei

The deamon log shows this error:

2021-01-06 20:58:50,083 - ERROR - mycodo.controllers.controller_input_573be3a4 - Error while attempting to read input: 'NoneType' object has no attribute 'next'
Traceback (most recent call last):
  File "/var/mycodo-root/mycodo/controllers/controller_input.py", line 391, in update_measure
    measurements = self.measure_input.next()
AttributeError: 'NoneType' object has no attribute 'next'

I am able to read the distance manually with circuitpython on the pi by using this code:

import time
import board
import adafruit_hcsr04

sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.D18, echo_pin=board.D24)

while True:
try:
print((sonar.distance,))
except RuntimeError:
print("Retrying!")
time.sleep(0.1)

Output:

pi@raspberrypi:~ $ python3 ditance.py
Retrying!
Retrying!
(8.568000000000001,)
(8.517000000000001,)
Retrying!
Retrying!
Retrying!
Retrying!
Retrying!
(8.534,)
Retrying!
Retrying!
Retrying!
Retrying!
(3.0940000000000003,)
Retrying!
Retrying!
(8.5,)
Retrying!
(7.6160000000000005,)
Retrying!
Retrying!
Retrying!
Retrying!
(8.534,)
(8.925,)
Retrying!
Retrying!
Retrying!
(7.344,)
(8.517000000000001,)
Retrying!
Retrying!
Retrying!
Retrying!
Retrying!
Retrying!
Retrying!
Retrying!
(8.534,)
Retrying!
Retrying!
Retrying!
Retrying!
Retrying!
(8.534,)
Retrying!
Retrying!
(8.534,)
Retrying!
Retrying!
(8.568000000000001,)
(8.483,)
Retrying!
(8.551,)
Retrying!
(8.551,)
Retrying!
(8.126000000000001,)
Retrying!
(8.517000000000001,)
Retrying!
Retrying!
(8.568000000000001,)
Retrying!

The cpu load is much lower then, although it is using the same command libgpiod_pulsei.

If you need more information,please let me know.

Thank you,

Fabian

This post was modified 3 weeks ago 2 times by kennedys1337

ReplyQuote
Kyle Gabriel
(@kylegabriel)
Member Admin
Joined: 6 years ago
Posts: 429
 
Posted by: @kennedys1337

AttributeError: 'NoneType' object has no attribute 'next'

The error occurs after the input has already crashed. You will need to find the error produced after the "Activated" line in the daemon log.

Mycodo Developer


ReplyQuote
Kyle Gabriel
(@kylegabriel)
Member Admin
Joined: 6 years ago
Posts: 429
 

There's nothing different about the code in the Input than your test script. It is concerning that there are so many exceptions in your test script, though. Looking at the pin list, if D18 for trigger and D24 for echo are working for your test script, you should be using 18 and 24 in the pin options, respectively.

PS. I just pushed a minor updates to try 3 times before failing (you will need to upgrade to master if you want to use the new code). This in no way "fixes" anything, though, so you don't need to update. Making sure you're using the correct pins is more important and determining the correct lines in the daemon log are what you should focus on.

Mycodo Developer


ReplyQuote
kennedys1337
(@kennedys1337)
Active Member
Joined: 1 month ago
Posts: 12
Topic starter  

@kylegabriel

here is the log:

2021-01-06 21:45:42,285 - ERROR - mycodo.controllers.controller_input_573be3a4 - initialize_variables() Exception: Timed out waiting for PulseIn message. Make sure libgpiod is installed.
Traceback (most recent call last):
File "/var/mycodo-root/mycodo/controllers/base_controller.py", line 72, in run
self.initialize_variables()
File "/var/mycodo-root/mycodo/controllers/controller_input.py", line 338, in initialize_variables
self.measure_input = input_loaded.InputModule(self.input_dev)
File "/home/pi/Mycodo/mycodo/inputs/hcsr04_circuitpython.py", line 91, in __init__
self.initialize_input()
File "/home/pi/Mycodo/mycodo/inputs/hcsr04_circuitpython.py", line 130, in initialize_input
echo_pin=bcm_to_board[self.pin_echo - 1])
File "/var/mycodo-root/env/lib/python3.7/site-packages/adafruit_hcsr04.py", line 105, in __init__
self._echo = PulseIn(echo_pin)
File "/var/mycodo-root/env/lib/python3.7/site-packages/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py", line 73, in __init__
message = self._wait_receive_msg(timeout=0.25)
File "/var/mycodo-root/env/lib/python3.7/site-packages/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py", line 92, in _wait_receive_msg
"Timed out waiting for PulseIn message. Make sure libgpiod is installed."
RuntimeError: Timed out waiting for PulseIn message. Make sure libgpiod is installed

This post was modified 3 weeks ago by kennedys1337

ReplyQuote
kennedys1337
(@kennedys1337)
Active Member
Joined: 1 month ago
Posts: 12
Topic starter  
Posted by: @kylegabriel

There's nothing different about the code in the Input than your test script. It is concerning that there are so many exceptions in your test script, though. Looking at the pin list, if D18 for trigger and D24 for echo are working for your test script, you should be using 18 and 24 in the pin options, respectively.

I am using the same pins in mycodo GPIO18 and GPIO24 .

 


ReplyQuote
kennedys1337
(@kennedys1337)
Active Member
Joined: 1 month ago
Posts: 12
Topic starter  

Am I missing something stupid^^?


ReplyQuote
Kyle Gabriel
(@kylegabriel)
Member Admin
Joined: 6 years ago
Posts: 429
 

I don't see any difference from the example code and the Mycodo input module. Your example script initializes successfully every single time you run it? I do see Adafruit recommends a level shifter, as they state the sensor requires 5-volt logic (and the Pi produces 3.3), at https://circuitpython.readthedocs.io/projects/hcsr04/en/latest/api.html

Mycodo Developer


ReplyQuote
kennedys1337
(@kennedys1337)
Active Member
Joined: 1 month ago
Posts: 12
Topic starter  
Posted by: @kylegabriel

I don't see any difference from the example code and the Mycodo input module. Your example script initializes successfully every single time you run it?

Yes, it does. 


ReplyQuote
kennedys1337
(@kennedys1337)
Active Member
Joined: 1 month ago
Posts: 12
Topic starter  
Posted by: @kylegabriel

do see Adafruit recommends a level shifter, as they state the sensor requires 5-volt logic (and the Pi produces 3.3), at https://circuitpython.readthedocs.io/projects/hcsr04/en/latest/api.html

I hooked it up like in the attached file. 


ReplyQuote
kennedys1337
(@kennedys1337)
Active Member
Joined: 1 month ago
Posts: 12
Topic starter  

I may found the problem. Gonna use different resistors, like it is recommended. 

  • Pi D6 to 10k resistor - other side to GND
  • Sensor echo to 10k resistor - other side to D6

ReplyQuote