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

Various issues with lightsensor.service using TSL2561 #515

Closed
techdev5521 opened this issue Nov 23, 2021 · 37 comments
Closed

Various issues with lightsensor.service using TSL2561 #515

techdev5521 opened this issue Nov 23, 2021 · 37 comments
Labels
bug Something isn't working no-issue-activity Stale re-opened

Comments

@techdev5521
Copy link
Contributor

What phone do you have?

Google Pixel 5

What OS version? / What crankshaft version?

Alpha 7.4

Steps to reproduce the bug, if you're planning to report a bug. Please indicate whether the bug is always repoducible.

  • Results vary according to unknown factors.
  • Connect TSL2561 to Pi
TSL2561 Pin Pi Pin
VCC --> Pin 1 (3.3v)
GND --> Pin 9 (GND)
SCL --> Pin 5 (SCL)
SDA --> Pin 3 (SDA)
  • Enable light sensor in settings: Settings --> CS HW --> Lightsensor = tsl2561
  • (Optional) Enable Lux output on screen: Settings --> TSL2561 --> Check 'Show Lux value....'
  • Restart

Please provide any further information that you might find helpful if available.

Issues include :

  • Occasional inability to detect TSL2561 device. (This could be my own connection.)
  • Occasional issue with rounding Lux value resulting in non-integer value used for screen brightness:
# Relevant lines from journalctl -u lightsensor.service -b
Nov 23 18:02:35 CRANKSHAFT-NG systemd[1]: Started Crankshaft Lightsensor Service.
Nov 23 18:02:36 CRANKSHAFT-NG service_lightsensor.py[4466]: TSL2561
Nov 23 18:02:36 CRANKSHAFT-NG service_lightsensor.py[4466]: Lux = 4.3 | Level 1 -> trigger night
Nov 23 18:02:36 CRANKSHAFT-NG service_lightsensor.py[4466]: /usr/local/bin/crankshaft: line 691: [: b30: integer expression expected
Nov 23 18:02:36 CRANKSHAFT-NG service_lightsensor.py[4466]: /usr/local/bin/crankshaft: line 697: [: b30: integer expression expected
Nov 23 18:02:36 CRANKSHAFT-NG root[4507]: CSMT-LOGGER: --------------------------------------------------------------------
Nov 23 18:02:36 CRANKSHAFT-NG root[4508]: CSMT-LOGGER: Brightness Level Set To: b30
Nov 23 18:02:36 CRANKSHAFT-NG root[4509]: CSMT-LOGGER: --------------------------------------------------------------------
  • Occasional issue with communicating with TSL2561 at all.
# Relevant lines from journalctl -u lightsensor.service -b
Nov 23 17:35:24 CRANKSHAFT-NG service_lightsensor.py[1542]: TSL2561
Nov 23 17:35:24 CRANKSHAFT-NG service_lightsensor.py[1542]: Traceback (most recent call last):
Nov 23 17:35:24 CRANKSHAFT-NG service_lightsensor.py[1542]:   File "/opt/crankshaft/service_lightsensor.py", line 101, in <module>
Nov 23 17:35:24 CRANKSHAFT-NG service_lightsensor.py[1542]:     Luxrounded = get_LUX(LIGHTSENSOR)
Nov 23 17:35:24 CRANKSHAFT-NG service_lightsensor.py[1542]:   File "/opt/crankshaft/service_lightsensor.py", line 34, in get_LUX
Nov 23 17:35:24 CRANKSHAFT-NG service_lightsensor.py[1542]:     Lux = get_LUX_TSL2561(TSL_I2C_BUS, TSL_ADDR)
Nov 23 17:35:24 CRANKSHAFT-NG service_lightsensor.py[1542]:   File "/opt/crankshaft/service_lightsensor.py", line 46, in get_LUX_TSL2561
Nov 23 17:35:24 CRANKSHAFT-NG service_lightsensor.py[1542]:     i2cBus.write_byte_data(TSL_ADDR, 0x80, 0x03)
Nov 23 17:35:24 CRANKSHAFT-NG service_lightsensor.py[1542]: OSError: [Errno 121] Remote I/O error

Debug Info

debug.zip

@techdev5521
Copy link
Contributor Author

This bug is both to report the issue as well as to document the work I am doing to address it.

@Novocaine85
Copy link

Novocaine85 commented Nov 23, 2021

I would like to add that the automatic brightness is not working at all with the sensor. On previous release (I don't remember which one, but it was a very old one like 2019) was working good.

P.s.: As the author of this issue mentioned, randomly on startup the light sensor is not detected (the lux is not even display on the main screen.) The only way to make it work again is to re-pair up crankshaft with my mobile with WiFi (using client mode).

@matt2005
Copy link
Contributor

@techdev5521 I suspect the brightness issue is due to Line 691 and Line 697 as they are erroring as per your issue report
It should be reporting as an Integer but appear to be b30 so it may need a type conversion.

I suspect it got broken when i added the TSL2591 support TSL2591 support

@techdev5521
Copy link
Contributor Author

I agree @matt2005 -- my plan is to debug the service script by stopping the service and running the script as a standalone file to see where exactly issues are. I am confident I can resolve the issues with the TSL2561 but I do not have a TSL2591 and may need your help ensuring I don't broke that if you have one. I'll see what i can do later today.

@jefftherobot
Copy link

jefftherobot commented Dec 30, 2021

My TSL2591 isn't adjusting brightness. I have the same log errors as @techdev5521

@techdev5521
Copy link
Contributor Author

I found the issue in the Python script that handles light sensor values but I'm currently traveling without my hardware or stable Internet. I should be back to both next week and can do some testing / post updates.

@matt2005
Copy link
Contributor

matt2005 commented Feb 8, 2022

@techdev5521 have you been able to fix the script?

@techdev5521
Copy link
Contributor Author

@matt2005 Yes -- I've resolved this issue. I'll try to make a PR this weekend.

@matt2005
Copy link
Contributor

matt2005 commented Apr 9, 2022

@techdev5521 any update on the pr for the fix?

@techdev5521
Copy link
Contributor Author

techdev5521 commented Apr 11, 2022 via email

@matt2005
Copy link
Contributor

Any joy with the pr?

@github-actions
Copy link

This issue is stale because it has been open 120 days with no activity. Remove stale label or comment or this will be closed in 60 days.

@techdev5521
Copy link
Contributor Author

Hey -- sorry for dropping off the planet. I'll make time to work on this this week and submit work. The fix is fairly simple.

@CoordinatedEffort
Copy link

@techdev5521 just wanted to see if you've had a chance to implement a fix.

@MikeDotNetty
Copy link

@techdev5521 just wanted to see if there was any movement on this? Don't mean to be a hassle and wish you the best, but I would love this simple fix to be implemented if you have the time.

@techdev5521
Copy link
Contributor Author

techdev5521 commented Dec 21, 2022 via email

@MikeDotNetty
Copy link

@techdev5521 Thanks, best of luck and happy holidays!

@MikeDotNetty
Copy link

@techdev5521 just wondering if you had a chance over the last few days to look into this?

@techdev5521
Copy link
Contributor Author

Hey all. Sitting down now with my dev kit to submit a fix for this.

@myfrom
Copy link
Contributor

myfrom commented Feb 10, 2023

Hi, I have been struggling with this issue as well but I managed to make a workaround and I'm close to finding the source of the problem, I think.

In short, the crankshaft brightness set xx command is being called with additional letter "b" in front of the value , which is what causes the error:
/usr/local/bin/crankshaft: line 697: [: b30: integer expression expected
My workaround replaces $3 callback with a variable that omits first character from $3, however this isn't optimal as it breaks many things including setting brightness from command line.

I am using sensor TSL25911 (which afaik is almost identical to TSL2591 but produced by Waveshare and has hardware more suitable for my application).

My findings lead my to believe the error most likely originates in service_lightsensor.py:10.

get_var function returns all values incorrectly:

>>> import subprocess
>>> CMD = 'echo $(source ./crankshaft_env.sh; echo $%s)' % 'DISP_BRIGHTNESS_1'
>>> p = subprocess.Popen(CMD, stdout=subprocess.PIPE, shell=True, executable='/bin/bash')
>>> print(p.stdout.readlines()[0].strip())
b'30'

Running int() on the output corrects it:

>>> import subprocess
>>> CMD = 'echo $(source ./crankshaft_env.sh; echo $%s)' % 'DISP_BRIGHTNESS_1'
>>> p = subprocess.Popen(CMD, stdout=subprocess.PIPE, shell=True, executable='/bin/bash')
>>> print(int(p.stdout.readlines()[0].strip()))
30
>>>

This is why there are no errors for example on line 107

Changing lines 109, 113, 117, 121, 125 and 129 to int instead of str should fix the issue IMO. If I'll have time, I'll test my theory by the end of this week.

Edit: Changed the example to use the same value, however values such as 'LUX_LEVEL_1' are also returned incorrectly (eg b'5')

@MikeDotNetty
Copy link

@myfrom just wondering if you've had any luck with this?

@myfrom
Copy link
Contributor

myfrom commented Feb 17, 2023

I got stuck because my car went to a mechanic and with it the lightsensor setup 🫤 I'm expecting to get it back today or Monday and will work on that as soon as that happens. If you have a running setup and would be willing to test it before that time I can send more info

@MikeDotNetty
Copy link

@myfrom sorry to hear that...trips to the mechanic are never fun. I do have crankshaft setup in my car on my raspberry pi 3b+, and I have the TSL25911 sensor... I'd be more than happy to test for you. Just let me know. Thanks!

@myfrom
Copy link
Contributor

myfrom commented Feb 17, 2023

Awesome! I'm really grateful for the help

You would need to replace /opt/crankshaft/service_lightsensor.py on the Raspberry to this modified file from my fork (you can download with Save as).

You can do it via ssh, once you connect you need to run
crankshaft system unlock
it'll turn system files writeable and you can either manually edit the file with
sudo nano /opt/crankshaft/service_lightsensor.py
or try with something like SCP (disconnect from ssh first and then:)
scp <path-on-your-device>/service_lightsensor.py pi@<hostname>:/opt/crankshaft/
you can then verify with nano or cat via ssh that the changes are made and reboot the Pi. System should re-lock by itself when booting.

Here's diff between og and my file and here's original file in case you need to roll back.

If it doesn't solve the issue, please try to ssh and run journalctl -u lightsensor.service -b and send the results

@MikeDotNetty
Copy link

Thanks! I'll do that this weekend and report my results. Much appreciated!

@myfrom
Copy link
Contributor

myfrom commented Feb 17, 2023

Update: I got my car back (yay) and tested the changes right away. In my testing, they do work and lightsensor.service changes screen brightness as expected. PR opened.

(Btw, @MikeDotNetty if you still want to carry on your own tests, I highly recommend editing the file with nano, trying to move the file is more elaborate than I thought, running into permission and chmod issues)

@MikeDotNetty
Copy link

That's great news! I'll certainly try it this weekend. And yes I go the nano way and report back. Thanks again!

@MikeDotNetty
Copy link

@myfrom While I was not able to get your modified code to work. I think it might be due to the fact that my TSL25911 is really a "TSL25911FN". I have attached the error. This error is NOT related to your updated code, but rather it's failing on obtaining the LUX value to work with. REGARDLESS, you set me on the right path, and I was able to update this service_lightsensor.py file specific to my sensor, such that it IS WORKING now. So thanks for that!

Here's the error in case you are interested:

Feb 14 05:12:52 CRANKSHAFT-NG systemd[1]: Starting Crankshaft Lightsensor Service...
Feb 14 05:12:52 CRANKSHAFT-NG systemd[1]: Started Crankshaft Lightsensor Service.
Feb 14 05:12:53 CRANKSHAFT-NG service_lightsensor.py[1447]: TSL2561
Feb 14 05:12:53 CRANKSHAFT-NG service_lightsensor.py[1447]: Traceback (most recent call last):
Feb 14 05:12:53 CRANKSHAFT-NG service_lightsensor.py[1447]: File "/opt/crankshaft/service_lightsensor.py", line 101, in
Feb 14 05:12:53 CRANKSHAFT-NG service_lightsensor.py[1447]: Luxrounded = get_LUX(LIGHTSENSOR)
Feb 14 05:12:53 CRANKSHAFT-NG service_lightsensor.py[1447]: File "/opt/crankshaft/service_lightsensor.py", line 39, in get_LUX
Feb 14 05:12:53 CRANKSHAFT-NG service_lightsensor.py[1447]: return round(Lux, 1)
Feb 14 05:12:53 CRANKSHAFT-NG service_lightsensor.py[1447]: TypeError: type NoneType doesn't define round method
Feb 14 05:12:53 CRANKSHAFT-NG systemd[1]: lightsensor.service: Main process exited, code=exited, status=1/FAILURE
Feb 14 05:12:53 CRANKSHAFT-NG systemd[1]: lightsensor.service: Failed with result 'exit-code'.
Feb 14 05:12:58 CRANKSHAFT-NG systemd[1]: lightsensor.service: Service RestartSec=5s expired, scheduling restart.
Feb 14 05:12:58 CRANKSHAFT-NG systemd[1]: lightsensor.service: Scheduled restart job, restart counter is at 1.
Feb 14 05:12:58 CRANKSHAFT-NG systemd[1]: Stopped Crankshaft Lightsensor Service.
Feb 14 05:12:58 CRANKSHAFT-NG systemd[1]: Starting Crankshaft Lightsensor Service...
Feb 14 05:12:58 CRANKSHAFT-NG systemd[1]: Started Crankshaft Lightsensor Service.
Feb 14 05:12:58 CRANKSHAFT-NG service_lightsensor.py[1523]: TSL2561
Feb 14 05:12:58 CRANKSHAFT-NG service_lightsensor.py[1523]: Traceback (most recent call last):
Feb 14 05:12:58 CRANKSHAFT-NG service_lightsensor.py[1523]: File "/opt/crankshaft/service_lightsensor.py", line 101, in
Feb 14 05:12:58 CRANKSHAFT-NG service_lightsensor.py[1523]: Luxrounded = get_LUX(LIGHTSENSOR)
Feb 14 05:12:58 CRANKSHAFT-NG service_lightsensor.py[1523]: File "/opt/crankshaft/service_lightsensor.py", line 39, in get_LUX
Feb 14 05:12:58 CRANKSHAFT-NG service_lightsensor.py[1523]: return round(Lux, 1)
Feb 14 05:12:58 CRANKSHAFT-NG service_lightsensor.py[1523]: TypeError: type NoneType doesn't define round method
Feb 14 05:12:58 CRANKSHAFT-NG systemd[1]: lightsensor.service: Main process exited, code=exited, status=1/FAILURE
Feb 14 05:12:58 CRANKSHAFT-NG systemd[1]: lightsensor.service: Failed with result 'exit-code'.
Feb 18 12:46:37 CRANKSHAFT-NG systemd[1]: lightsensor.service: Service RestartSec=5s expired, scheduling restart.
Feb 18 12:46:37 CRANKSHAFT-NG systemd[1]: lightsensor.service: Scheduled restart job, restart counter is at 2.
Feb 18 12:46:37 CRANKSHAFT-NG systemd[1]: Stopped Crankshaft Lightsensor Service.
Feb 18 12:46:37 CRANKSHAFT-NG systemd[1]: Starting Crankshaft Lightsensor Service...
Feb 18 12:46:37 CRANKSHAFT-NG systemd[1]: Started Crankshaft Lightsensor Service.
Feb 18 12:46:37 CRANKSHAFT-NG service_lightsensor.py[1659]: TSL2561
Feb 18 12:46:37 CRANKSHAFT-NG service_lightsensor.py[1659]: Traceback (most recent call last):
Feb 18 12:46:37 CRANKSHAFT-NG service_lightsensor.py[1659]: File "/opt/crankshaft/service_lightsensor.py", line 101, in
Feb 18 12:46:37 CRANKSHAFT-NG service_lightsensor.py[1659]: Luxrounded = get_LUX(LIGHTSENSOR)
Feb 18 12:46:37 CRANKSHAFT-NG service_lightsensor.py[1659]: File "/opt/crankshaft/service_lightsensor.py", line 39, in get_LUX
Feb 18 12:46:37 CRANKSHAFT-NG service_lightsensor.py[1659]: return round(Lux, 1)
Feb 18 12:46:37 CRANKSHAFT-NG service_lightsensor.py[1659]: TypeError: type NoneType doesn't define round method
Feb 18 12:46:37 CRANKSHAFT-NG systemd[1]: lightsensor.service: Main process exited, code=exited, status=1/FAILURE
Feb 18 12:46:37 CRANKSHAFT-NG systemd[1]: lightsensor.service: Failed with result 'exit-code'.
Feb 18 12:46:42 CRANKSHAFT-NG systemd[1]: lightsensor.service: Service RestartSec=5s expired, scheduling restart.
Feb 18 12:46:42 CRANKSHAFT-NG systemd[1]: lightsensor.service: Scheduled restart job, restart counter is at 3.
Feb 18 12:46:42 CRANKSHAFT-NG systemd[1]: Stopped Crankshaft Lightsensor Service.
Feb 18 12:46:42 CRANKSHAFT-NG systemd[1]: lightsensor.service: Start request repeated too quickly.
Feb 18 12:46:42 CRANKSHAFT-NG systemd[1]: lightsensor.service: Failed with result 'exit-code'.
Feb 18 12:46:42 CRANKSHAFT-NG systemd[1]: Failed to start Crankshaft Lightsensor Service

@MikeDotNetty
Copy link

@myfrom actually, now that I think about it ..without your fix, my fix most likely would not have worked, so thanks for that!

@myfrom
Copy link
Contributor

myfrom commented Feb 18, 2023

Glad I could help and thanks for trying it out 😉

@github-actions
Copy link

This issue is stale because it has been open 120 days with no activity. Remove stale label or comment or this will be closed in 60 days.

@github-actions
Copy link

This issue was closed because it has been stalled for 60 days with no activity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 19, 2023
@CoordinatedEffort
Copy link

@MikeDotNetty @myfrom Would either of you mind giving instructions on how to get this working? I understand the changes that need to be made but I'm a Linux newb and I don't know how to actually do it.

@CoordinatedEffort
Copy link

CoordinatedEffort commented Aug 5, 2023

@MikeDotNetty @myfrom I figured out how to SSH and use nano to change the file. However, I noticed that the home screen doesn't display the lux value anymore in the upper left corner and the brightness still doesn't seem to be adjusting automatically.

Edit: after rebooting a few times, now it shows initial lux value on startup but then it never updates after that. The GUI's brightness does actually change depending on this initial lux value. I have a TSL2591.

Edit 2: after a few more reboots, it stopped showing any lux value again and isn't adjusting the GUI's brightness.

@myfrom
Copy link
Contributor

myfrom commented Sep 24, 2023

@MikeDotNetty @myfrom I figured out how to SSH and use nano to change the file. However, I noticed that the home screen doesn't display the lux value anymore in the upper left corner and the brightness still doesn't seem to be adjusting automatically.

Edit: after rebooting a few times, now it shows initial lux value on startup but then it never updates after that. The GUI's brightness does actually change depending on this initial lux value. I have a TSL2591.

Edit 2: after a few more reboots, it stopped showing any lux value again and isn't adjusting the GUI's brightness.

I'd check the wiring and sensor first. It sounds like that might be the issue with what you're describing

@techdev5521
Copy link
Contributor Author

@CoordinatedEffort I have these issues mapped out with details, files, causes and whatnot but I've not the time to transfer my paper notes to a proper comment. Would you be interested in a voice/video chat sometime to go over that info?

@CoordinatedEffort
Copy link

@techdev5521 I'm definitely not opposed to having a chat and I'm very grateful for the offer of your time, but my technical expertise is basically non-existent. I'd really only be able to follow detailed instructions (as you might be able to tell from my earlier comments) so I'm not sure that I'd have much to offer in a discussion. Is it that you have a possible solution that you'd like me to try?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working no-issue-activity Stale re-opened
Projects
None yet
Development

No branches or pull requests

7 participants