Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All sensors output 0.0 #79

Closed
jwhendy opened this issue Sep 24, 2017 · 16 comments
Closed

All sensors output 0.0 #79

jwhendy opened this issue Sep 24, 2017 · 16 comments
Labels

Comments

@jwhendy
Copy link

jwhendy commented Sep 24, 2017

Greetings,

I'm just getting started with the sense hat and am likely doing something wrong. I was working through this guide and controlling the LEDs was going great. I moved on to try the accelerometer example and just get zeros.

$ cat acc-example.py
from sense_hat import SenseHat

sense = SenseHat()

while True:
    acceleration = sense.get_accelerometer_raw()
    x = acceleration['x']
    y = acceleration['y']
    z = acceleration['z']

    x=round(x, 0)
    y=round(y, 0)
    z=round(z, 0)

    print("x={0}, y={1}, z={2}".format(x, y, z))

When running the above, I just get this:

$ python acc-example.py 
x=0.0, y=0.0, z=0.0
x=0.0, y=0.0, z=0.0
x=0.0, y=0.0, z=0.0
x=0.0, y=0.0, z=0.0
...

Examples from the prompt showing other sensor behavior:


$ python
Python 3.6.2 (default, Jul 22 2017, 21:19:22) 
[GCC 7.1.1 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sense_hat import SenseHat
>>> s = SenseHat()
>>> s.set_imu_config(True, True, True) ## just to be sure

>>> s.get_orientation_radians()
{'roll': 0.0, 'pitch': -0.0, 'yaw': -0.0}

>>> s.get_compass()            
-0.0

>>> s.get_gyroscope()
{'roll': 0.0, 'pitch': -0.0, 'yaw': -0.0}

>>> s.get_accelerometer_raw()
{'x': 0, 'y': 0, 'z': 0}

I'm using arch ARM.

$ cat /boot/config.txt 
# See /boot/overlays/README for all available options

gpu_mem=128
initramfs initramfs-linux.img followkernel

start_file=start_x.elf
fixup_file=fixup_x.dat

dtparam=i2s=on
dtparam=spi=on
dtparam=i2c_arm=on
dtoverlay=rpi-sense

$ cat /etc/modules-load.d/raspberrypi.conf 
snd-bcm2835
i2c-dev
bcm2835-v4l2

Does anything look awry above, or would you have tips on finding out what's wrong?

@jwhendy
Copy link
Author

jwhendy commented Sep 24, 2017

Solved. It was a permissions issue.

Default:

$ ls -l /dev/i2c-1 
crw------- 1 root root 89, 1 Aug 16 12:20 /dev/i2c-1

After implementing a udev rule:

$ cat /etc/udev/rules.d/99-i2c.rules 
KERNEL=="i2c-[0-7]",MODE="0666"

$ ls -l /dev/i2c-1 
crw-rw-rw- 1 root root 89, 1 Aug 16 12:20 /dev/i2c-1

@jwhendy jwhendy closed this as completed Sep 24, 2017
@jwhendy
Copy link
Author

jwhendy commented Sep 24, 2017

Aside/comment: in trying to troubleshoot this, there are a lot of examples online where sudo is suggested in an error message. Mine didn't feature this:

from sense_hat import SenseHat
import time
 
sense = SenseHat()
 
while True:
 
   t = sense.get_temperature()
   p = sense.get_pressure()
   h = sense.get_humidity()

   msg = "Temperature = %s, Pressure=%s, Humidity=%s" % (t,p,h)
   print(msg)
   time.sleep(0.5)
  File "scripts/sensors.py", line 8, in <module>
    t = sense.get_temperature()
  File "/usr/lib/python3.6/site-packages/sense_hat/sense_hat.py", line 606, in get_temperature
    return self.get_temperature_from_humidity()
  File "/usr/lib/python3.6/site-packages/sense_hat/sense_hat.py", line 582, in get_temperature_from_humidity
    self._init_humidity()  # Ensure humidity sensor is initialised
  File "/usr/lib/python3.6/site-packages/sense_hat/sense_hat.py", line 549, in _init_humidity
    raise OSError('Humidity Init Failed')
OSError: Humidity Init Failed

This led me to think it was an issue with my devices or modules vs. permissions. Finally it dawned on me to just try sudo anyway, and to my amazement it worked!

Could the error message be improved to help users understand if it's device existence vs. permissions?

@bennuttall
Copy link
Contributor

sudo shouldn't be needed if the Pi is configured correctly. I see you're using Python 3.6 which is not the standard in Raspbian. Are you not using Raspbian? How are you using Python 3.6?

The apt package sense-hat will make everything work. As long as it's installed, you can even use a virtualenv and just install the python libraries required - see my comments here: #58 (comment)

@theckman
Copy link

@bennuttall running into this issue on an RPi that I needed to run Ubuntu Server on. I'm trying to use the Python module to validate my own Sense driver I'm writing in another language, and unfortunately I'm getting all zeroes back for only the Python code.

Where can I find out more about what's in the sense-hat package to see if there's something there that will help fix this up for me?

@beamerblvd
Copy link

@theckman, I'm encountering the same issue. I'm running Ubuntu Server, just like you, and all my sensor values are 0. This is what I did:

  • Installed G++
  • Cloned https://github.com/RPi-Distro/RTIMULib
  • Created a virtualenv in Python for using Sense Hat
  • Ran python setup.py build and python setup.py install in RTIMULib/Linux/python to compile and install the RTMI Python bindings
  • Ran pip install sense-hat
  • Opened a Python shell
  • Imported and instantiated SenseHat
  • I can successfully use show_message to display messages on the 8x8 screen on the Sense Hat, but all sensor methods (get_temperature, get_humidity, get_pressure, etc.) return 0.

Some guidance on how to get this to work / what to try next would be appreciated. Thanks!

@theckman
Copy link

@beamerblvd Thank you for clarifying your steps; I probably did pretty much the same as you.

@jwhendy
Copy link
Author

jwhendy commented Oct 27, 2020

RTIMULib (and forks) seem to have no issues for whatever reason, and that trend continues with the recently discovered RTIMULib2, though one does. I just posted an inquiry there to see if we can find out why none of the sensors are working. If you look at my post there, you can see an attempt to manually walk through the sense hat initialization of pressure/humidity sensors here.

I can't get them to initialize. I have the same experience as @beamerblvd : accel reads, lights can be changed, but none of the other sensors are outputting. Depending on how I do it, I get 0 or the OSError.

@fjctp
Copy link

fjctp commented Nov 1, 2020

I am encountering the same issue. I am running Ubuntu Server 20.04.1. It is a fresh install.

Issues

  1. I got this error when I ran get_temperature(), get_humidity(), or get_pressure().
OSError: XXXX Init Failed
  1. I got zeros reading when I ran get_accelerometer_raw(), get_gyroscope(), and get_compass()
{'roll': 0.0, 'pitch': 0.0, 'yaw': 0.0}

Debug steps

  1. Check if i2c is working
ubuntu@ubuntu:~$ ls -al /dev/i2c-1
crw-rw---- 1 root i2c 89, 1 Apr  1  2020 /dev/i2c-1
ubuntu@ubuntu:~$ cat /boot/firmware/syscfg.txt
# This file is intended to be modified by the pibootctl utility. User
# configuration changes should be placed in "usercfg.txt". Please refer to the
# README file for a description of the various configuration files on the boot
# partition.

enable_uart=1
dtparam=audio=on
dtparam=i2c_arm=on
dtparam=spi=on
cmdline=cmdline.txt
ubuntu@ubuntu:~$ i2cdetect -F 1
Functionalities implemented by /dev/i2c-1:
I2C                              yes
SMBus Quick Command              yes
SMBus Send Byte                  yes
SMBus Receive Byte               yes
SMBus Write Byte                 yes
SMBus Read Byte                  yes
SMBus Write Word                 yes
SMBus Read Word                  yes
SMBus Process Call               yes
SMBus Block Write                yes
SMBus Block Read                 no
SMBus Block Process Call         no
SMBus PEC                        yes
I2C Block Write                  yes
I2C Block Read                   yes
ubuntu@ubuntu:~$ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- UU -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- UU -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- UU -- -- 5f
60: -- -- -- -- -- -- -- -- -- -- 6a -- -- -- -- --
70: -- -- -- -- -- -- -- --
  1. add user to i2c group
sudo addgroup ubuntu input
sudo addgroup ubuntu i2c
  1. Tried to access sensor data with sudo python3 test_script.py. Still had the same issues

  2. Build RTIMULib from source

git clone https://github.com/RPi-Distro/RTIMULib
cd RTIMULib/Linux/python
python setup.py build
python setup.py install

@jwhendy
Copy link
Author

jwhendy commented Nov 1, 2020

@fjctp I think we're at the mercy of someone with RTIMULib knowledge assisting. Unfortunately, of the many forks, I only found one with an issues tab. I posted there. If you look at where the sensors are initialized in the code here, and manually reproduce these steps with RTIMU directly, you'll see they aren't being initialized. So, it's not exactly a sense-hat thing, per se, it's that this key library is not functioning.

@bsimmo
Copy link

bsimmo commented Nov 1, 2020 via email

@fjctp
Copy link

fjctp commented Nov 1, 2020

@bsimmo Thank you very much! The blacklist solved the problem.

@fjctp
Copy link

fjctp commented Nov 1, 2020

@fjctp I think we're at the mercy of someone with RTIMULib knowledge assisting. Unfortunately, of the many forks, I only found one with an issues tab. I posted there. If you look at where the sensors are initialized in the code here, and manually reproduce these steps with RTIMU directly, you'll see they aren't being initialized. So, it's not exactly a sense-hat thing, per se, it's that this key library is not functioning.

Correct. I complied some examples in RTIMULib and found that it couldn't find the sensors in i2c. I think it is related to the driver. Look at the link posted by @bsimmo. That fixed the problem.

@jwhendy
Copy link
Author

jwhendy commented Jan 11, 2021

@bsimmo and @fjctp I tried the blacklisting on arch and it did not work for me. It does work as root, however. This is a little bit annoying as I have to install python libs to the system folders, and I generally use --user for built packages.

This was pretty cool to see after years of not being able to do so:

# python sense_hat_demo.py 
Temperature: 20.3 °C - 68.5 °F
Humidity: 40.1 %rH
CPU Temperature: 39.2 °C
p: 2.54382626296944, r: 295.6990145496125, y: 179.13083611631552
North: 179.13083611631552
p: 2.589574229699951, r: 295.71873330574635, y: 179.11773581348746
p: 2.5504402339635397, r: 295.95182034136224, y: 179.12165634207102

Now, that said... other than this, what else can I check with respect to why this would only work as root?

$ cat /etc/udev/rules.d/99-i2c.rules 
KERNEL=="i2c-[0-7]",MODE="0666"

The EnvTrackerNode readme states "Application requires sudo" as a possible problem, but doesn't state how to fix it. I'm posting an issue there, but would appreciate any other suggestions here. I'm actually not sure if it was the blacklisting or root use that actually fixed this for me in the end.

@J-Pai
Copy link

J-Pai commented Jan 11, 2021

Just wanted to leave my $0.02 here. The reason for the blacklist is b/c the RPi build of Ubuntu Server 20.04.1 comes with the Industrial I/O modules preloaded.
Without the blacklist, you get a initialization errors on some of the sensors. I think this has something to do with the fact that the Industrial I/O modules grab on to the i2c sensors on the Sense HAT and refuse to let them go or allow them to be read correctly.

To resolve this issue the blacklist disables those drives and allows the Sense HAT drivers (rpisense_fb) to actually pick up the sensors.

I figured this out via lsmod. Since @jwhendy is using Arch (not Ubuntu), this may not actually impact his use case since I'm not sure if arch has those modules pre-installed.

Example lsmod output:

ubuntu@ubuntupi:~$ lsmod | grep st_
st_magn_spi            16384  0
st_pressure_spi        16384  0
st_sensors_spi         16384  2 st_pressure_spi,st_magn_spi
st_magn_i2c            16384  0
st_pressure_i2c        16384  0
st_pressure            20480  2 st_pressure_i2c,st_pressure_spi
st_magn                20480  2 st_magn_i2c,st_magn_spi
st_sensors_i2c         16384  2 st_pressure_i2c,st_magn_i2c
st_sensors             28672  6 st_pressure,st_pressure_i2c,st_magn_i2c,st_pressure_spi,st_magn,st_magn_spi
industrialio_triggered_buffer    16384  2 st_pressure,st_magn
industrialio           94208  9 st_pressure,industrialio_triggered_buffer,st_sensors,st_pressure_i2c,kfifo_buf,st_magn_i2c,st_pressure_spi,st_magn,st_magn_spi

If you notice the module names noted in the output above, they are associated with the problematic sensors that fail to be initialized. With the blacklist, these modules will not be loaded and the sensehat_fb (and associated modules) will be able to work as expected.

Not sure why the ARM/RPi version of Ubuntu has this module pre-installed, but at least we have the option of disabling them.

@jwhendy
Copy link
Author

jwhendy commented Jan 11, 2021

I'll try un-blacklisting those on arch to see if I note any effect. With the insight on root and i2c permissions, I haven't A/B tested to check the black list. I can confirm from memory that insutrialio* were loaded, but I don't think I checked for st_* before blacklisting and rebooting. On arch, there were also hts221* modules and kfifo_buf that were loading industrialio and had to be blacklisted.

@jwhendy
Copy link
Author

jwhendy commented Jan 11, 2021

K. Confirming blacklist matters on arch as well.

$ lsmod |grep st_
st_pressure_spi        16384  0
st_magn_spi            16384  0
st_sensors_spi         16384  2 st_pressure_spi,st_magn_spi
regmap_spi             16384  2 st_sensors_spi,hts221_spi
st_pressure_i2c        16384  0
st_magn_i2c            16384  0
st_pressure            16384  2 st_pressure_spi,st_pressure_i2c
st_magn                16384  2 st_magn_i2c,st_magn_spi
st_sensors_i2c         16384  2 st_magn_i2c,st_pressure_i2c
st_sensors             24576  6 st_magn_i2c,st_pressure_spi,st_pressure,st_magn_spi,st_pressure_i2c,st_magn
industrialio_triggered_buffer    16384  3 hts221,st_pressure,st_magn
industrialio           73728  10 hts221,st_magn_i2c,st_sensors,st_pressure_spi,st_pressure,st_magn_spi,st_pressure_i2c,industrialio_triggered_buffer,st_magn,kfifo_buf

Woohoo, I think this can actually be closed! tl;dr for the future:

  • blacklist modules as described at EnvTrackerNode
  • in addition to checking /etc/udev/rules.d/* for /dev/i2c-1 access, also consider the i2c group method referenced here. A symptom of this will be the ability to run the EnvTrackerNode/scripts/sense_hat_demo.py only as root/with sudo
  • tbd: whether or not building RTIMULib and python-sense-hat are required vs. installing packages, which I'll leave to someone else to test

@jwhendy jwhendy closed this as completed Jan 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants