NorthernMan54 / homebridge-mcuiot

Homebridge platform plugin that support's a network of nodemcu's running nodemcu-dht-yl69-mdns to display Temperature and Humidity.
30 stars 12 forks source link

voltage reading #9

Closed dado-si closed 5 years ago

dado-si commented 5 years ago

Hi! Can you add to homebridge-mcuiot voltage reading from ESP8266? I have 2600mAh battery connected to ESP8266 on my terrace and would like to know when the battery level is critical and I have to charge it.

NorthernMan54 commented 5 years ago

Hi,

There are two pieces to this, first is the circuit to read the battery voltage, and second is the software to read the adc pin and have it activate battery low in HomeKit. For reading the adc pin, and activate battery low I can do this, pretty straight forward.

Do you have any thoughts on the circuit?

Sent from my iPad

On Jul 27, 2018, at 5:12 AM, dado-si notifications@github.com wrote:

Hi! Can you add to homebridge-mcuiot voltage reading from ESP8266? I have 2600mAh battery connected to ESP8266 on my terrace and would like to know when the battery level is critical and I have to charge it.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

dado-si commented 5 years ago

As I found on http://www.electronicwings.com/nodemcu/nodemcu-adc-with-esplorer-ide It seems that there is no need for a divider as ESP can read it's internal voltage via adc.readvdd33 command.

NorthernMan54 commented 5 years ago

If you look at the code/schematic, with the moisture sensor I’m reading the ADC pin already. With the current code, you could do a POC, by adding the YL option to the NodeMCU config, and it will read the pin, and show it in the messages.

If you run homebridge in debug mode, ie DEBUG=* homebridge It will log the messages between the NodeMCU and the plugin.

And connect ADC to 3v3 pin on the NodeMCU

I’m worried that the voltage regulator on the NodeMCU won’t show the actual voltage.

Adding a config option called BAT and have it say battery low is pretty straight forward, just need to figure out when to trigger it.

On Jul 27, 2018, at 7:49 AM, dado-si notifications@github.com wrote:

As I found on http://www.electronicwings.com/nodemcu/nodemcu-adc-with-esplorer-ide It seems that there is no need for a divider as ESP can read it's internal voltage via adc.readvdd33 command.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

dado-si commented 5 years ago

I wouldn't use any external voltage divider as it will use some of the power and discharge battery faster, so is there a way to change the configuration of ESP8266 to use the ADC for reading the system voltage? I have changed in main.lua lines 16, 17 and 18 to adc.readvdd33 and now I'm getting a result of - "Moisture": 65535. By the documentation, this result means that ESP is reading external voltage on A0 pin.

NorthernMan54 commented 5 years ago

I think you may have missed this

https://nodemcu.readthedocs.io/en/master/en/modules/adc/#adcforce_init_mode https://nodemcu.readthedocs.io/en/master/en/modules/adc/#adcforce_init_mode

I started to code this, and have created a new branch of the code base with support for reading vdd and set the battery level. I called the branch “Battery”, but I have not tested it yet.

On Jul 31, 2018, at 9:20 AM, dado-si <notifications@github.com mailto:notifications@github.com> wrote:

I wouldn't use any external voltage divider as it will use some of the power and discharge battery faster, so is there a way to change the configuration of ESP8266 to use the ADC for reading the system voltage? I have changed in main.lua lines 16, 17 and 18 to adc.readvdd33 and now I'm getting a result of - "Moisture": 65535. By the documentation, this result means that ESP is reading external voltage on A0 pin.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/NorthernMan54/homebridge-mcuiot/issues/9#issuecomment-409218254, or mute the thread https://github.com/notifications/unsubscribe-auth/AS5CmIDwlmaFWLTcXjwNP6U7j2iS4FGOks5uMFmqgaJpZM4VjK9g.

NorthernMan54 commented 5 years ago

I have completed testing of the NodeMCU side, and it is working.

This is from one of my sensors with a DHT 22 attached.

, "Battery": 2952 Status: 0 Temp: 25 Humi: 64.8 Moisture: 65535 Baro: -999 Dew: -999

It is showing that my MacBook is powering it at 2.952 volts...

Next is the plugin side

dado-si commented 5 years ago

Oh, I wasn't paying enough attention when reading :) I have managed to change adc mode to internal with commands in the article and now I'm getting reading of 3466mV in the past two hours. As this is reading after internal stabilizer probably when the battery will be close to ending with capacity, Voltage will be lower. I will send you the results.

dado-si commented 5 years ago

So I have changed ESP reading with "adc.force_init_mode(adc.INIT_VDD33)" and then main.lua file. Here is main.lua attached. main.lua.txt

NorthernMan54 commented 5 years ago

In the Battery branch, I have it working now. I did add more debugging to the plugin code, so you can see the actual value from the device.

This is from the plugin logging

MCUIOT MCUIOT Response { "Hostname": "NODE-871ED8", "Model": "DHT-BAT", "Version": "1.5", "Firmware": "2.2.0", "Data": { "Temperature": 25.1, "Humidity": 59.7, "Moisture": 65535, "Status": 0, "Barometer": -999, "Dew": -999, "Battery": 2955 }

And this is it working

img_2252

dado-si commented 5 years ago

I have successfully updated ESP with config, main and setup lua files that you build. The only thing I had to change was in config.lua line: module.DHT22 = 1

And now I get this response via web browser: { "Hostname": "NODE-1A3242", "Model": "DHT-BAT", "Version": "1.5", "Firmware": "2.2.0", "Data": {"Temperature": 27.1, "Humidity": 43.5, "Moisture": 65535, "Status": 0, "Barometer": -999, "Dew": -999, "Battery": 3464 }}

On Raspberry Pi I have updated index.js file in /usr/lib/node_modules/homebridge-mcuiot but now homebridge is crashing. Here is the output I see in terminal:

/usr/lib/node_modules/homebridge-mcuiot/index.js:427 .getCharacteristic(Characteristic.BatteryLevel).updateValue(response.Data.Battery/3300*100); ^

TypeError: Cannot read property 'getCharacteristic' of undefined at mcuiot. (/usr/lib/node_modules/homebridge-mcuiot/index.js:427:13) at Request._callback (/usr/lib/node_modules/homebridge-mcuiot/index.js:781:7) at Request.self.callback (/usr/lib/node_modules/homebridge-mcuiot/node_modules/request/request.js:185:22) at emitTwo (events.js:126:13) at Request.emit (events.js:214:7) at Request. (/usr/lib/node_modules/homebridge-mcuiot/node_modules/request/request.js:1157:10) at emitOne (events.js:116:13) at Request.emit (events.js:211:7) at IncomingMessage. (/usr/lib/node_modules/homebridge-mcuiot/node_modules/request/request.js:1079:12) at Object.onceWrapper (events.js:313:30)

If I use my old index.js file homebridge is running but I don't see any battery info. Can you tell me what did go wrong with homebridge and how to solve this?

NorthernMan54 commented 5 years ago

I added a new characteristic for battery level and to enable it you need to have the accessory setup again.

To do this, unplug the nodemcu device from the network. And in the Home app, press the mcuiot-reset button. This will cause the plugin to remove any mcuiot devices that can’t be found. The plugin will discover the device, and set it up again.

And add the battery characteristic.

Sent from my iPad

On Aug 6, 2018, at 4:07 PM, dado-si notifications@github.com wrote:

I have successfully updated ESP with config, main and setup lua files that you build. The only thing I had to change was in config.lua line: module.DHT22 = 1

And now I get this response via web browser: { "Hostname": "NODE-1A3242", "Model": "DHT-BAT", "Version": "1.5", "Firmware": "2.2.0", "Data": {"Temperature": 27.1, "Humidity": 43.5, "Moisture": 65535, "Status": 0, "Barometer": -999, "Dew": -999, "Battery": 3464 }}

On Raspberry Pi I have updated index.js file in /usr/lib/node_modules/homebridge-mcuiot but now homebridge is crashing. Here is the output I see in terminal:

/usr/lib/node_modules/homebridge-mcuiot/index.js:427 .getCharacteristic(Characteristic.BatteryLevel).updateValue(response.Data.Battery/3300*100); ^

TypeError: Cannot read property 'getCharacteristic' of undefined at mcuiot. (/usr/lib/node_modules/homebridge-mcuiot/index.js:427:13) at Request._callback (/usr/lib/node_modules/homebridge-mcuiot/index.js:781:7) at Request.self.callback (/usr/lib/node_modules/homebridge-mcuiot/node_modules/request/request.js:185:22) at emitTwo (events.js:126:13) at Request.emit (events.js:214:7) at Request. (/usr/lib/node_modules/homebridge-mcuiot/node_modules/request/request.js:1157:10) at emitOne (events.js:116:13) at Request.emit (events.js:211:7) at IncomingMessage. (/usr/lib/node_modules/homebridge-mcuiot/node_modules/request/request.js:1079:12) at Object.onceWrapper (events.js:313:30)

If I use my old index.js file homebridge is running but I don't see any battery info. Can you tell me what did go wrong with homebridge and how to solve this?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

dado-si commented 5 years ago

Thanks a lot for your help and explanations! Now everything runs ok and I can see battery level in Home app.

NorthernMan54 commented 5 years ago

Now the big question, does the voltage drop as the usb charger discharges? And can we create a notification/event when it passes a particular value ( Ie do you have enough time to take action ? )

On Aug 7, 2018, at 4:56 PM, dado-si notifications@github.com wrote:

Thanks a lot for your help and explanations! Now everything runs ok and I can see battery level in Home app.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/NorthernMan54/homebridge-mcuiot/issues/9#issuecomment-411199809, or mute the thread https://github.com/notifications/unsubscribe-auth/AS5CmK51AUH6jdymGDhwzbkn5J_FZE7Eks5uOf8WgaJpZM4VjK9g.

dado-si commented 5 years ago

Yes, that why I have started to log battery info on two ESP units. During the day results were within 0,02V range. As soon as batteries are discharged I will let you know the outcome of this test.

dado-si commented 5 years ago

I'm using Adafruit Huzzah ESP8266 and its voltage regulator does a very good job as voltage was running from 3469mV up to 3480mV. In the attachment you wiil find my readings...

screenshot

I'm not sure if the drop from 3475-3477mV to 3472-3474mV was because the battery was close to discharge (at 17:40 ESP was self-shut off) or because outside temperature was above 30ºC at that time of the day. I will test this more in the next few days. As I see the first results, it seems that a more precise result would be received if I use a voltage divider and ADC input, as you suggested in the beginning. This is particularly true if I tell you that on the other Huzzah ESP8266, that I use in my home, voltage didn't drop for 2mV few readings before it was shut off. Actually, it stayed between 3463-3466mV the whole time the battery had enough power. But let's see what would come out in next few days as I continue to test this solution.

dado-si commented 5 years ago

I finally finished a few tests with two of my USB batteries. Unfortunately measuring internal voltage didn't give me results I was expecting. Voltage was very uninformed the whole time so I made a voltage divider, connected it to A pin on ESP and started to measure voltage directly from the battery (before any voltage stabilizer). The results were now helpful. Voltage reading during charging battery was up to 860 and lowest reading before ESP went off was 500. When I recalculate this in real voltage it means that the charging level is 4,21V, the lowest working value is 2,45V. The critical level warning should be at 3,5V (reading of 716) as this means that I have some 6 hours to connect charger before battery level would be to low.

screenshot

This would mean that the use of a voltage divider is the only workable solution to measure battery level. As ESP has only one analog probe this would mean that it can't be used in conjunction with Water Leak Sensor. Am I right?

NorthernMan54 commented 5 years ago

Correct, the ESP8266 only has 1 analog input but it also has a number of digital inputs, is your water leak sensor analog or digital?

As an option have you looked I2C based AD convertors like the ads1115? I used it to provide more analog inputs for my first version of a garage door monitor

https://github.com/NorthernMan54/homebridge-mcuiot/blob/master/nodemcu/ESP%208266%20-%20MCUIOT%20-%20BME-GD_schem.jpg https://github.com/NorthernMan54/homebridge-mcuiot/blob/master/nodemcu/ESP%208266%20-%20MCUIOT%20-%20BME-GD_schem.jpg

On Aug 25, 2018, at 10:16 AM, dado-si notifications@github.com wrote:

I finally finished a few tests with two of my USB batteries. Unfortunately measuring internal voltage didn't give me results I was expecting. Voltage was very uninformed the whole time so I made a voltage divider, connected it to A pin on ESP and started to measure voltage directly from the battery (before any voltage stabilizer). The results were now helpful. Voltage reading during charging battery was up to 860 and lowest reading before ESP went off was 500. When I recalculate this in real voltage it means that the charging level is 4,21V, the lowest working value is 2,45V. The critical level warning should be at 3,5V (reading of 716) as this means that I have some 6 hours to connect charger before battery level would be to low. https://user-images.githubusercontent.com/41781903/44618908-30552700-a87f-11e8-8776-a85051437dc3.png This would mean that the use of a voltage divider is the only workable solution to measure battery level. As ESP has only one analog probe this would mean that it can't be used in conjunction with Water Leak Sensor. Am I right?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/NorthernMan54/homebridge-mcuiot/issues/9#issuecomment-415972228, or mute the thread https://github.com/notifications/unsubscribe-auth/AS5CmJgntF0jMYjs4TtyQc8uT3Zci745ks5uUVwsgaJpZM4VjK9g.

dado-si commented 5 years ago

For this ESP8266 I don't plan to use a water leak sensor so it will not be a problem. It only reads outside teperature and humidity. Thanks for an idea with ads1115. Can I ask you to rewrite DHT-BAT part of the code to read battery status from A pin instead of internal converter and to show it in accessory details?

NorthernMan54 commented 5 years ago

Sure, give me a day or 2

On Aug 25, 2018, at 1:30 PM, dado-si notifications@github.com wrote:

For this ESP8266 I don't plan to use a water leak sensor so it will not be a problem. It only reads outside teperature and humidity. Thanks for an idea with ads1115. Can I ask you to rewrite DHT-BAT part of the code to read battery status from A pin instead of internal converter and to show it in accessory details?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

NorthernMan54 commented 5 years ago

Done, check out the Battery branch, I don’t have the ability to test this version so please let me know about any errors or problems. If you say it is okay, I will publish as the main version.

On Aug 25, 2018, at 1:30 PM, dado-si notifications@github.com wrote:

For this ESP8266 I don't plan to use a water leak sensor so it will not be a problem. It only reads outside teperature and humidity. Thanks for an idea with ads1115. Can I ask you to rewrite DHT-BAT part of the code to read battery status from A pin instead of internal converter and to show it in accessory details?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/NorthernMan54/homebridge-mcuiot/issues/9#issuecomment-415984652, or mute the thread https://github.com/notifications/unsubscribe-auth/AS5CmG5HV9vudANA1ivd5_8xY054WkFKks5uUYmpgaJpZM4VjK9g.

dado-si commented 5 years ago

All good! It is three days in operation and there is still no errors. If something pops up I will let you know. Thank you very much for setting battery level as an option!

NorthernMan54 commented 5 years ago

No problem, good luck. I will now publish this as the main version

Sent from my iPad

On Sep 3, 2018, at 5:45 AM, dado-si notifications@github.com wrote:

All good! It is three days in operation and there is still no errors. If something pops up I will let you know. Thank you very much for setting battery level as an option!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

NorthernMan54 commented 5 years ago

This has been published as pull request #17

chimcen commented 5 years ago

@dado-si your project sound interesting - can you share more information about (pictures, wireing plans)? How long will your ESP run with one battery-charging cycle?