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

new auto-night-mode binary tuning #332

Closed
khseal opened this issue May 7, 2018 · 88 comments
Closed

new auto-night-mode binary tuning #332

khseal opened this issue May 7, 2018 · 88 comments
Labels

Comments

@khseal
Copy link

khseal commented May 7, 2018

I do not work to switch night vision. =(

@jmtatsch
Copy link
Collaborator

jmtatsch commented May 8, 2018

Doesn't seem to work for me either.
@jthwho do you have an idea what could be wrong?

@jthwho
Copy link
Contributor

jthwho commented May 8, 2018

I can confirm it works on my cameras (which I admit is hardly helpful :-P). I'll need a bit more detail to help.

@khseal can you confirm the autonight application is running after you've started it? Also, it'd be helpful if you could do the following:

  • Stop the autonight application (via the web interface or helper script)
  • Run the autonight application on the command line in verbose mode (autonight -v)
  • Expose the camera to light for 60 seconds.
  • Cover the camera's light sensor for 60 seconds (to simulate it getting dark)
  • Re expose the camera to light for 60 seconds.
  • Post the data that is printed to the console by autonight to this issue.

@khseal
Copy link
Author

khseal commented May 8, 2018

[root@DAFANG:~]# /system/sdcard/bin/autonight -v
Current value: 523.80
Current value: 188.00
Current value: 188.00
Current value: 188.00
Current value: 533.88
Window (5) Avg: 324.34
Current value: 419.52
Window (5) Avg: 303.48
Current value: 188.00
Window (5) Avg: 303.48
Current value: 187.88
Window (5) Avg: 303.46
Current value: 188.00
Window (5) Avg: 303.46
Current value: 188.00
Window (5) Avg: 234.28
Current value: 188.00
Window (5) Avg: 187.98
Current value: 188.00
Window (5) Avg: 187.98
Current value: 533.64
Window (5) Avg: 257.13
Current value: 526.68
Window (5) Avg: 324.86
Current value: 534.48
Window (5) Avg: 394.16
Current value: 548.16
Window (5) Avg: 466.19
Current value: 548.40
Window (5) Avg: 538.27

Binary work. 548 is light on and 188 light off. But nothing happens, the camera mode does not switch.
It seems somewhere does not work what that trigger.

@jthwho
Copy link
Contributor

jthwho commented May 8, 2018

It looks like your values are significantly different than on my camera. You can modify the on/off setpoints via the /system/sdcard/config/autonight.conf file. Add something like the following to that file:
-O 200 -F 300

The -O parameter will set the trigger on point. The night mode will be turned on when the windowed average drops below that value. The -F parameter will set the trigger off point. The night mode will be turned off when the windowed average goes above that value. If you're curious, doing a autonight -h will show you all the options that can be fed into the application to configure it for your needs.

Currently the default thresholds are 40 and 45. I chose these values arbitrarily based on my cameras, so they're likely wrong once we account for sensor/ADC variance. I'll make note of your numbers. Hopefully others can report their numbers as well. And then we'll have (perhaps) saner defaults.

It'd probably be a good idea to have these numbers adjustable via the web interface, but I haven't dug into that side of things yet.

@nphil
Copy link

nphil commented May 9, 2018

my numbers are quite different:

Current value: 3056.28
Window (5) Avg: 3042.36
Current value: 3055.32
Window (5) Avg: 3042.55
Current value: 3055.92
Window (5) Avg: 3042.53
Current value: 216.68
Window (5) Avg: 2474.78
Current value: 214.16
Window (5) Avg: 1919.67
Current value: 207.56
Window (5) Avg: 1349.93
Current value: 204.08
Window (5) Avg: 779.68
Current value: 204.32
Window (5) Avg: 209.36
Current value: 206.24
Window (5) Avg: 207.27
Current value: 204.92
Window (5) Avg: 205.42
Current value: 201.56
Window (5) Avg: 204.22
Current value: 202.28
Window (5) Avg: 203.86
Current value: 3055.44
Window (5) Avg: 774.09
Current value: 3054.96
Window (5) Avg: 1343.83
Current value: 3055.20
Window (5) Avg: 1913.89
Current value: 3055.68
Window (5) Avg: 2484.71
Current value: 3054.48
Window (5) Avg: 3055.15

low of ~200 to a high of 3000

edit: I actually tested at my install location, and setting -O 100 -F 200 works fine with my setup after some trial and error. Maybe some kind of initial calibration tool would be helpful?

@khseal
Copy link
Author

khseal commented May 9, 2018

I have the same parameters as you. Yesterday I tested in the evening and when the light was turned on, the parameter was low.

@jthwho
Copy link
Contributor

jthwho commented May 9, 2018

@nphil That's a good idea, I'll add some calibration function to the autonight application. I'm also going to go back and make sure my assumptions on how the sensor is connected to the ADC are sound.

@khseal Are you saying that you're still unable to get the autonight application to switch into night mode? If so, try running the application on the command line with your -O and -F values and see if it ever prints out it's setting night mode on or off.

@khseal
Copy link
Author

khseal commented May 9, 2018

After writing the parameters to the config, it all worked for me.

@jmtatsch jmtatsch changed the title New binary autonight work? new auto-night-mode binary tuning May 10, 2018
@tijo1987
Copy link

tijo1987 commented May 14, 2018

Does anybody have an idea. When I have the command /system/sdcard/bin/autonight -v with the lights on or off it gives a result of about 700.

Window (5) Avg: 698.16
Current value: 701.04
Window (5) Avg: 698.62
Current value: 698.16
Window (5) Avg: 698.45
Current value: 701.40
Window (5) Avg: 699.26
Current value: 698.40
Window (5) Avg: 699.43
Current value: 699.00
Window (5) Avg: 699.60
Current value: 698.40
Window (5) Avg: 699.07
Current value: 697.92
Window (5) Avg: 699.02
Current value: 696.36
Window (5) Avg: 698.02
Current value: 701.88
Window (5) Avg: 698.71
Current value: 697.56
Window (5) Avg: 698.42
Current value: 698.52
Window (5) Avg: 698.45

@jthwho
Copy link
Contributor

jthwho commented May 14, 2018

That's curious. Is there something occluding the light sensor on your camera? Another thought is to try one of the other ADC devices via the -D flag (ex: -D /dev/jz_adc_aux_1).

@tijo1987
Copy link

Sorry for my ignorance. What am I doing wrong

[root@DAFANG:dev]# /dev/jz_adc_aux_1 -D
-sh: /dev/jz_adc_aux_1: Permission denied

@jthwho
Copy link
Contributor

jthwho commented May 15, 2018

Oh, sorry. That would be the command line option for autonight so something like: autonight -D /dev/jz_adc_aux_1

@ivanfor
Copy link

ivanfor commented May 15, 2018

Hi.
In my case /dev/jz_adc_aux_0 seems to have an erratic behaviour and, as expected, doesn't work:
[root@DAFANG:config]# /system/sdcard/bin/autonight -v -D /dev/jz_adc_aux_0
Current value: 4193870.00
Current value: 83.00
Current value: 167835.56
Current value: 4193870.00
Current value: 4193870.00
Window (5) Avg: 2549905.71

However, /dev/jz_adc_aux_1 (and 2) behave better and can be calibrated. In my case the night threshold is below 650.
Starting from darkness and switching lights on:
[root@DAFANG:config]# /system/sdcard/bin/autonight -v -D /dev/jz_adc_aux_1 -O 650 -F 700
Current value: 626.36
Current value: 664.80
Current value: 664.80
Current value: 620.64
Current value: 618.72
Window (5) Avg: 639.06
Night Mode Enabled
Current value: 916.92
Window (5) Avg: 697.18
Current value: 1170.68
Window (5) Avg: 798.35
Night Mode Disabled
Current value: 1312.56
Window (5) Avg: 927.90

@jthwho
Copy link
Contributor

jthwho commented May 15, 2018

@ivanfor thanks for the details. It's odd that the device is different. I wonder if the /dev/jz_adc_aux* devices aren't constrained to actual physical devices (and yours ends up being detected in a different order than mine). I'll do some digging to figure that out. Worst case, hopefully I can add some code to autonight that'll pick the right device. I suppose it's also possible there are hardware revisions that might have changed which ADC the light sensor is connected to, but that seems less likely.

@tijo1987
Copy link

Thanks guys, I think my sensor is broken no matter which one I take, the value don't change

@jmtatsch
Copy link
Collaborator

I have one camera that works well during the day:

Current value: 3055.32
Current value: 3054.84
Current value: 3055.32
Current value: 3055.20
Current value: 3054.48
Window (5) Avg: 3055.03
Current value: 3054.96
Window (5) Avg: 3054.96
Current value: 3054.84
Window (5) Avg: 3054.96
Current value: 3055.08
Window (5) Avg: 3054.91
Current value: 3054.48
Window (5) Avg: 3054.77

But mostly reads garbage at night:

Current value: 671056.16
Current value: 44.00
Current value: 4193870.00
Current value: 4026116.96
Current value: 44.00
Window (5) Avg: 1778226.22
Current value: 44.00
Window (5) Avg: 1644023.79
Current value: 4193870.00
Window (5) Avg: 2482788.99
Current value: 40.88
Window (5) Avg: 1644023.17
Current value: 35.00
Window (5) Avg: 838806.78
Current value: 4193870.00
Window (5) Avg: 1677571.98
Current value: 1006555.40
Window (5) Avg: 1878874.26
Current value: 2684087.00
Window (5) Avg: 1576917.66

Any ideas why that might be?

@jthwho
Copy link
Contributor

jthwho commented May 18, 2018

I've got some ideas, but it'll be hard to know for sure without digging around in the kernel driver. Do you see any jz_adc_aux messages showing up in your kernel logs?

There are some suspect things in the driver:
https://github.com/EliasKotlyar/Xiaomi-Dafang-Software/blob/master/kernel/drivers/mfd/jz_adc_aux.c

int jz_adc_aux_sample_volt(enum aux_ch channels,struct jz_adc_aux *jz_adc_aux)
{
	unsigned long tmp;
	unsigned int sadc_volt = 0;

	if (!jz_adc_aux) {
		printk("jz_adc_aux is null ! return\n");
		return -EINVAL;
	}

	INIT_COMPLETION(jz_adc_aux->read_completion);

	jz_adc_aux->cell->enable(jz_adc_aux->pdev);
	enable_irq(jz_adc_aux->irq);

restart:
	tmp = wait_for_completion_interruptible_timeout(&jz_adc_aux->read_completion, HZ);
	if (tmp > 0) {
		sadc_volt = readl(jz_adc_aux->base) & 0xfff;
	} else if(tmp == -ERESTARTSYS){
		goto restart;
	}
	else {
		sadc_volt = tmp ? tmp : -ETIMEDOUT;
	}

	if (sadc_volt < 0) {
		printk("jz_adc_aux read value error!!\n");
		return -EIO;
	}

	disable_irq(jz_adc_aux->irq);
	jz_adc_aux->cell->disable(jz_adc_aux->pdev);

	sadc_volt = sadc_volt * VREF_ADC / AUXCONST;

	return sadc_volt;
}

This code is responsible for fetching the ADC value and then doing a bit of math on it and returning it in millivolts (?). It's curious the code takes steps to not return right away on error (in the first if block) but then has enough if block that returns on error right after that, but before the ADC and IRQ have been disabled. It seems like it might be a cut/copy error, maybe? I wonder if this is causing a subsequent read to return garbage?

I also noticed the jz_adc_aux_sample_volt() function uses readl() to read the value from the ADC register, but the jz_adc_aux_read_value() function above it uses readw(). Both immediately mask the bottom 12 bits, so maybe it doesn't matter. But it's curious that it does it both ways. It's also curious that jz_adc_aux_read_value() doesn't appear to be called anywhere else, so may be vestigial.

Finally, I also noticed that the VREF_ADC is modifiable via ioctl(). Perhaps this value is modified in different versions of the cameras, so that might explain why we've seen different values. Unfortunately, the driver doesn't have a mechanism for me to read back the current VREF_ADC value. So it looks like I'll have to get the kernel building to get to the bottom of that one too :-/.

Hopefully I can get some time this this weekend and take a look. I'm not sure how easy it'll be to build and boot a kernel to debug the driver. As a quick fix, it might make sense to have the autonight application test the sanity of the value it just read back and ignore (or try again) if it read the wrong value.

EDIT: spelling

@amagnolo
Copy link

I have the same situation as @tijo1987 , autonight always reads around 860 regardless of actual light condition. The other ADC devices don't return meaningful numbers either. The Dafang camera is brand new.

@jthwho
Copy link
Contributor

jthwho commented May 29, 2018

@amagnolo I'm not sure why this might be. Could it be the kernel version your camera shipped with is different than mine? What does uname -a report back on your camera?

@amagnolo
Copy link

[root@DAFANG:~]# uname -a
Linux DAFANG 3.10.14 #21 PREEMPT Thu Oct 19 11:38:16 CST 2017 mips GNU/Linux

@khseal
Copy link
Author

khseal commented May 29, 2018

hm,
[root@DAFANG:~]# uname -a
-sh: uname: not found

@tijo1987
Copy link

Same as @amagnolo

[root@DAFANG:~]# uname -a
Linux DAFANG 3.10.14 #21 PREEMPT Thu Oct 19 11:38:16 CST 2017 mips GNU/Linux

@amagnolo
Copy link

amagnolo commented Jun 4, 2018

@jthwho was the uname output useful?
is there anything else I can do to help you diagnose the problem?

@EliasKotlyar
Copy link
Owner

I suppose there is a hardware change, which changed the way how the ADC is readed. Maybe some Pin needs to be HIGH or LOW to allow a correct measurement. There is a way we can try to find out the issue:

  1. Boot into the original Firmware
  2. Connect to camera using the serial headers.
  3. Run the autonight-script from microsd to check if its working correctly.
  4. If so, try to check all the pins and their values.
  5. Compare them with the pins on the CFW

I suppose we may find a solution like this

@jthwho
Copy link
Contributor

jthwho commented Jun 5, 2018

@amagnolo I haven't had a chance to look at this yet. @EliasKotlyar suggestion of running the original firmware would certainly be helpful, if you're able to get to the serial header.

@amagnolo
Copy link

amagnolo commented Jun 6, 2018

Unfortunately I don't think I can connect to serial headers, I'm not even sure what they are.

@nik0
Copy link
Collaborator

nik0 commented Jul 28, 2018

@b0ssman,
see this: #557
For now I closed the PR, the algorithm night/day need to be improved. If you have time ...
And here https://github.com/Dafang-Hacks/Main there is the script to compile imageMagick

@ivanfor
Copy link

ivanfor commented Jul 28, 2018

@nik0,
I've compiled imagemagick in order to try to help improving the algorithm but when I execute "convert" the rtspserver crashes and, sometimes, the camera (xiaofang) suddenly reboots.
There's nothing helpful in the rtspserver logs. Any hint?

@nik0
Copy link
Collaborator

nik0 commented Jul 28, 2018

you compiled it or you use the one I provided?
May be memory issue, you may be try:

@ivanfor
Copy link

ivanfor commented Jul 30, 2018

Hi.
#178 did it. I've reproduced night on - night off - night on - etc behaviour.
I'll try to work a bit in the algorithm ....
Thanks!

@ark-git
Copy link

ark-git commented Aug 2, 2018

@nik0 I can run this command in the browser and night vision comes on, however it does not do automatically, even though Autonight is enabled

https://user:pass@[ip-address]/cgi-bin/action.cgi?cmd=ir_led_on
https://user:pass@[ip-address]/cgi-bin/action.cgi?cmd=ir_cut_off
https://user:pass@[ip-address]/cgi-bin/action.cgi?cmd=toggle-rtsp-nightvision-on

@aartrost
Copy link

aartrost commented Aug 2, 2018

@ark-git If your camera is newer, you likely don't have a light sensor. See #332 (comment) and #332 (comment) for more information

@ark-git
Copy link

ark-git commented Aug 2, 2018

I have a Wyze Cam V2 which was detecting night mode when it was with the default Wyze Cam V2 software.

@FuzzyMistborn
Copy link
Contributor

@ark-git the v2 does not have a light sensor. the stock firmware is using software to detect whether to turn on/off nightvision, something this custom software can't do yet.

@ark-git
Copy link

ark-git commented Aug 2, 2018

Is there a command line (from ssh) for the above commands so that I can create a script to run these commands?

@snjoetw
Copy link

snjoetw commented Aug 12, 2018

Anyway to tell if the camera has light sensor or not? I got a Xiaofang 1S and auto night doesn’t seem to work.

I’ve also ran “autonight -v” and the value is always 816 regardless if my lights are on or off. So it seems like it doesn’t have a light sensor but I’d like to get some confirmation

@ZipperZ
Copy link
Contributor

ZipperZ commented Sep 17, 2018

Hi.

I have tried to do something similar on Xiaofang as yawor did on his Dafang and I want to share.
First of all I have tried to trace the tracks on the pcb to figure out what was planned to do, this is what I have found out:
image
It does seem that engineers left "space" for several different approaches. It would be interesting if someone could measure their old Xiaofang (is the circuit similar) what the values of components are. Or maybe someone has Xiaofang 1S with light sensor (does it exist)?

So I have decided to go the easiest possible way just by creating a voltage divider:
image
What I have seen in photos of Xiaofang LDR was used in later versions (not phototransistor or photodiode)

List of used components:
Q2 - ADVANCED PHOTONIX NSL 19M51. LDR, 20 Mohm, 50 mW, 100 V (Farnell - 3168335) (Important)
C18, C107 - MULTICOMP MC0402N101K160CT SMD Multilayer Ceramic Capacitor, 0402 [1005 Metric], 100 pF, 16 V, ± 10%, C0G / NP0, MC Series (Farnell - 2627388) (Not necessary, would work without)
R395 - PANASONIC ELECTRONIC COMPONENTS ERJ2RKF1003X SMD Chip Resistor, 100 kohm, ERJ2R Series, 50 V, Thick Film, 0402 [1005 Metric], 100 mW (Farnell - 2302839) (Important)
R503 - PANASONIC ELECTRONIC COMPONENTS ERJ2GE0R00X SMD Chip Resistor, 0 ohm, ERJ2G Series, 50 V, Thick Film, 0402 [1005 Metric], 100 mW (Farnell - 2059190) (Solder blob would work fine)
L5 - MURATA BLM15AG121SN1D Ferrite Bead, 0402 [1005 Metric], 120 ohm, 500 mA, BLM15AG_SN Series, 0.25 ohm, ± 25% (Farnell - 1515760) (Solder blob would work fine)

Using this configuration and "-O 200 -F 300" it does work, but if there is a surface somewhere near it toggles, so something like "-O 300 -F 400" is also good.
The original software in this case PWM's (dimms) the IR LED's. (I did not check but can we adjust the brightness of the IR LEDs using moded firmware?)

During the day (cloudy) it is something like "2500".

"Populated" PCB:
image

End result:
image

@piotrdal
Copy link

Hello I have Dafang with light sensor but there is problem with readings from autonight - some are good, some are much to high... sometimes the readings from jz_adc_aux_1 are good, sometimes from jz_adc_aux_0
Something like this:

[root@DAFANG:~]# autonight -v -D /dev/jz_adc_aux_1
Current value: 840285.60
Current value: 840283.96
Current value: 840270.52
Current value: 1893.40
Current value: 1893.52
Window (5) Avg: 504925.40
Current value: 1880.32
Window (5) Avg: 337244.34
Current value: 1879.60
Window (5) Avg: 169563.47
Current value: 1894.60
Window (5) Avg: 1888.29
Current value: 840283.60
Window (5) Avg: 169566.33

Maybe there is way to eliminate this higher values?

@aartrost
Copy link

Hi @piotrdal,

I re-compiled the autonight binary to automatically discard values > 9999. This should work for you as well. For the download link see #332 (comment). Just place the autonight file in the bin directory

@piotrdal
Copy link

I'll try, it would be great, I plan to use this sensor to turn on light :-)

@tjharman
Copy link

I use home assistant to turn night mode on and off, based on sunrise/sunset. It works perfectly and has removed a real hassle I had with these cameras.
Here's my config from automations.yaml in case anyone else would like it:

- id: camera_night_mode_off
  alias: (Time) Cameras Night Mode Off
  trigger:
  - event: sunrise
    platform: sun
  action:
  - data:
      topic: "Home/camera1/ir_cut/set"
      payload: "OFF"
    service: mqtt.publish
  - service: mqtt.publish
    data:
      topic: "Home/camera1/ir/set"
      payload: "OFF"
  - service: mqtt.publish
    data:
      topic: "Home/camera1/night_mode/set"
      payload: "OFF"

- id: camera_night_mode_on
  alias: (Time) Cameras Night Mode On
  trigger:
  - event: sunset
    platform: sun
  action:
  - data:
      topic: "Home/camera1/ir_cut/set"
      payload: "ON"
    service: mqtt.publish
  - service: mqtt.publish
    data:
      topic: "Home/camera1/ir/set"
      payload: "ON"
  - service: mqtt.publish
    data:
      topic: "Home/camera1/night_mode/set"
      payload: "ON"

NOTE: This assumes the following:

  1. You have setup MQTT in Home Assisant!
  2. You have the dafang MQTT Control Server running and working
    [No, I won't talk you through this, sorry]

@corvy
Copy link

corvy commented Sep 18, 2018

As a workaround I just leave the camera in night mode 24/7 :) Would the software be developed to do analysis of the video to automatically turn on night mode on the newer models?

@TungstenE2
Copy link

@aartrost what exactly is your recompiled autonight file doing? will autonight be working with dafang without light sensor?

@jmtatsch
Copy link
Collaborator

jmtatsch commented Sep 21, 2018

there is a new autonight version which doesn't need the physical light sensor here for testing #699

@aartrost
Copy link

@aartrost what exactly is your recompiled autonight file doing? will autonight be working with dafang without light sensor?

The only thing added to my version of autonight is the following code:

                if( value > 9999 ) {
                    if(verbose) printf("discarded value: %.2lf\n", value);
                    sleep(delayBetweenReads);
                    continue;
                }

It discards any incoming values that are over 9999 and keeps waiting until a value below this threshold comes in. This will only apply to people who have installed their own light sensors and will have no effect on camera's without a sensor.

@sanjeewasam
Copy link

I use home assistant to turn night mode on and off, based on sunrise/sunset. It works perfectly and has removed a real hassle I had with these cameras.
Here's my config from automations.yaml in case anyone else would like it:

- id: camera_night_mode_off
  alias: (Time) Cameras Night Mode Off
  trigger:
  - event: sunrise
    platform: sun
  action:
  - data:
      topic: "Home/camera1/ir_cut/set"
      payload: "OFF"
    service: mqtt.publish
  - service: mqtt.publish
    data:
      topic: "Home/camera1/ir/set"
      payload: "OFF"
  - service: mqtt.publish
    data:
      topic: "Home/camera1/night_mode/set"
      payload: "OFF"

- id: camera_night_mode_on
  alias: (Time) Cameras Night Mode On
  trigger:
  - event: sunset
    platform: sun
  action:
  - data:
      topic: "Home/camera1/ir_cut/set"
      payload: "ON"
    service: mqtt.publish
  - service: mqtt.publish
    data:
      topic: "Home/camera1/ir/set"
      payload: "ON"
  - service: mqtt.publish
    data:
      topic: "Home/camera1/night_mode/set"
      payload: "ON"

NOTE: This assumes the following:

1. You have setup MQTT in Home Assisant!

2. You have the dafang MQTT Control Server running and working
   [No, I won't talk you through this, sorry]

Thanks Mate. This work for me perfectly. I will probably change the trigger so that only when by hue lights are off in these locations sunset is true then camera will go to night mode as I do not want to have the night mode on when lights are on

@stale
Copy link

stale bot commented May 19, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

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