endail / hx711-rpi-py

Python bindings for Raspberry Pi HX711 C++ Library
MIT License
10 stars 4 forks source link

import problems with unknown module 'HX711' and invalid 'hx711-rpi-py' #12

Open herbbetz opened 3 months ago

herbbetz commented 3 months ago

Dear Mr. Robertson,

I was unlucky today when following your instructions. All your examples and calibration.py use ' from HX711 import * '. HX711 is nowhere to be found by Python 3.9.2 on my RPi4B with bullseye lite 32bit.

Following your instruction 'pip3 install --upgrade hx711-rpi-py' I found in 'pip3 list': hx711-rpi-py 1.65.0 . However 'import hx711-rpi-py ' or 'from hx711-rpi-py import * ' is invalid syntax because of the dash.

Module importlib could not help this either. Please enlighten me, what to import to get your python scripts running. Yours, herber7be7z@gmail.com

P.S.: I did successfully the 'make and install' of http://abyz.me.uk/lg/lg.zip and https://github.com/endail/hx711 , as 'apt-get install -y liblgpio-dev' was not available.

endail commented 2 months ago

Hi,

After you installed the HX711 library did you run ldconfig?

herbbetz commented 2 months ago

I had no idea, I had to run this. Obviously updates the linker cache 'etc/ld.so.cache'. Now 'import HX711' is working in the python REPL, despite I cannot find the module in 'pip list'. Somewhat confusing, but thanks ... Your HX711 python examples are now working! How is this explained?

After the setup of liblgpio (abyz.me.uk) there was 'import lgpio' possible (this is used in their DHT.py), also despite not being in 'pip list', but it now no longer works in the python REPL (even after ldconfig). So I cannot read the DHT22 sensor using liblgpio any more? This would have been very useful ...

Similarly there was 'import hx711' possible after installing https://github.com/endail/hx711 from source, but not in 'pip list' and now no longer working either. Then after the instructions on https://github.com/endail/hx711-rpi-py, there is this hx711-rpi-py in 'pip list', which python 3.9.2 and VSCode mark as invalid dash syntax. What is it for?

For me this is a lot of python confusion. I was relieved that at least the CPP compilings worked as expected. Therefore I was considering linking a CPP-lib (CPP-code for DHT22 and HX711) and importing that into python using 'import ctypes'. I need these sensors to work with Picamera on a single core RPiZero2W cpu. But I did not find the time yet to try this nerdy solution, which might have its own disadvantages ...

Another question: How could I keep your SimpleHX711.py example from hanging and driving the cpu load to 100%, when there is no hx711 sensor connected? A try...except loop maybe?

Greetings from Germany and many thanks for any help... My main concern would now be, how to read the DHT22 sensor using liblgpio?

endail commented 2 months ago

The python library that you install via pip is generated using a C++ file which "links" the C++ HX711 library to python by #includeing it as if it were a regular C++ project. The process uses pybind11, which is really handy.

The C++ HX711 library installs separately - and is located separately - to the hx711-rpi-py library. Think of hx711-rpi-py as a python specific interface to the C++ code.

As for the hanging issue, does that still occur if you use time-based sampling?

lgpio has a python interface, although I have not used it.

herbbetz commented 2 months ago

Dear Mr. Robertson, thanks for the interesting insight. My original project as pensioner and hobbyist wouldbe programmer consisted in adapting a german universities birdhouse software from their RPi4 to my RPiZero2W (https://www.wiediversistmeingarten.org/de/). A raspberry camera is triggered by a balance (on hx711) to upload a feeding birds image and DHT22 data to their platform. This proved shockingly difficult and I still try to find the easiest way. I will not find time to try the time based sampling. At the moment I changed to https://abyz.me.uk/rpi/pigpio , which provides easy scripts for hx711 and dht22, albeit the necessary pigpiod seems not quite stable. I now avoid pip completely, as most python packages are preinstalled by apt-get. Python gives me lots of problems with multiprocessing, multithreading and so on. This is due to my trials to run the script for each sensor in its own space, while on the other hand they need to communicate. Alas the university software and my modifications keep freezing the RPi02W about once a day => kernel:[14023.187191] Internal error: Oops - undefined instruction: 0 [#1] ARM.

herbbetz commented 1 month ago

Hello Mr. Robertson, this is, what I finally ended up with and it seems to work so far: https://github.com/herbbetz/betzBirdiary . Thanks for your inspiration. Outdoors the scale based on a strain gauge and hx711 seems quite sensitive, and I isolated its code as far as possible in my project. So it could easily be tried also using your software ...