Hallo,
habe mit folgendem sketch das Problem, das unsinnige Batteriewerte 150 bis 153% angezeigt werden.
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
#include <SPI.h>
#include <MySensors.h>
#include <BH1750.h>
#include <Wire.h>
#define CHILD_ID_LIGHT 0
unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
BH1750 lightSensor;
// V_LIGHT_LEVEL should only be used for uncalibrated light level 0-100%.
// If your controller supports the new V_LEVEL variable, use this instead for
// transmitting LUX light level.
MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
// MyMessage msg(CHILD_ID_LIGHT, V_LEVEL);
uint16_t lastlux;
//=========================
// BATTERY VOLTAGE DIVIDER SETUP
// 1M, 470K divider across battery and using internal ADC ref of 1.1V
// Sense point is bypassed with 0.1 uF cap to reduce noise at that point
// ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
// 3.44/1023 = Volts per bit = 0.003363075
#define VBAT_PER_BITS 0.003363075
#define VMIN 1.9 // Vmin (radio Min Volt)=1.9V (564v)
#define VMAX 3.0 // Vmax = (2xAA bat)=3.0V (892v)
int batteryPcnt = 0; // Calc value for battery %
int batLoop = 0; // Loop to help calc average
int batArray[3]; // Array to store value for average calc.
int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
//=========================
void setup()
{
lightSensor.begin();
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Light Lux Sensor", "1.0");
// Register all sensors to gateway (they will be created as child devices)
present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
}
void loop()
{
uint16_t lux = lightSensor.readLightLevel();// Get Lux value
Serial.println(lux);
if (lux != lastlux) {
send(msg.set(lux));
lastlux = lux;
}
batM();
sleep(SLEEP_TIME); //sleep a bit
}
void batM() //The battery calculations
{
delay(500);
// Battery monitoring reading
int sensorValue = analogRead(BATTERY_SENSE_PIN);
delay(500);
// Calculate the battery in %
float Vbat = sensorValue * VBAT_PER_BITS;
int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");
// Add it to array so we get an average of 3 (3x20min)
batArray[batLoop] = batteryPcnt;
if (batLoop > 2) {
batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]);
batteryPcnt = batteryPcnt / 3;
if (batteryPcnt > 100) {
batteryPcnt=100;
}
Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %");
sendBatteryLevel(batteryPcnt);
batLoop = 0;
}
else
{
batLoop++;
}
sleep(SLEEP_TIME);
}
Laut dem Sketch dürfte doch garnichts über 100% angezeigt werden oder?
Sp siehts in der Konsole aus:
Battery percent: -81 %
Battery Average (Send): -105 %
794046 TSF:MSG:SEND,101-101-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:151
794054 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
794060 TSF:TDI:TSL
Hier das device in fhem:
defmod MYSENSOR_101 MYSENSORS_DEVICE 101
attr MYSENSOR_101 IODev MySensorUnoGateway
attr MYSENSOR_101 mapReading_brightness 0 brightness
attr MYSENSOR_101 mapReading_id1 1 id
attr MYSENSOR_101 mapReading_level 0 level
attr MYSENSOR_101 mode node
attr MYSENSOR_101 room MY_Sensors
attr MYSENSOR_101 stateFormat Helligkeit:brightness lux
edit:
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
#include <SPI.h>
#include <MySensors.h>
#include <BH1750.h>
#include <Wire.h>
#define CHILD_ID_LIGHT 0
unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
BH1750 lightSensor;
// V_LIGHT_LEVEL should only be used for uncalibrated light level 0-100%.
// If your controller supports the new V_LEVEL variable, use this instead for
// transmitting LUX light level.
MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
// MyMessage msg(CHILD_ID_LIGHT, V_LEVEL);
uint16_t lastlux;
//=========================
// BATTERY VOLTAGE DIVIDER SETUP
// 1M, 470K divider across battery and using internal ADC ref of 1.1V
// Sense point is bypassed with 0.1 uF cap to reduce noise at that point
// ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
// 3.44/1023 = Volts per bit = 0.003363075
#define VBAT_PER_BITS 0.003363075
#define VMIN 1.9 // Vmin (radio Min Volt)=1.9V (564v)
#define VMAX 3.5 // Vmax = (2xAA bat)=3.0V (892v)
int batteryPcnt = 0; // Calc value for battery %
int batLoop = 0; // Loop to help calc average
int batArray[3]; // Array to store value for average calc.
int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
//=========================
void setup()
{
analogReference(INTERNAL); // For battery sensing
delay(500); // Allow time for radio if power used as reset
lightSensor.begin();
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Light Lux Sensor", "1.0");
// Register all sensors to gateway (they will be created as child devices)
present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
}
void loop()
{
uint16_t lux = lightSensor.readLightLevel();// Get Lux value
Serial.println(lux);
if (lux != lastlux) {
send(msg.set(lux));
lastlux = lux;
}
batM();
sleep(SLEEP_TIME); //sleep a bit
}
void batM() //The battery calculations
{
delay(500);
// Battery monitoring reading
int sensorValue = analogRead(BATTERY_SENSE_PIN);
delay(500);
// Calculate the battery in %
float Vbat = sensorValue * VBAT_PER_BITS;
int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");
// Add it to array so we get an average of 3 (3x20min)
batArray[batLoop] = batteryPcnt;
if (batLoop > 2) {
batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]);
batteryPcnt = batteryPcnt / 3;
if (batteryPcnt > 100) {
batteryPcnt=100;
}
Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %");
sendBatteryLevel(batteryPcnt);
batLoop = 0;
}
else
{
batLoop++;
}
}
So sieht es besser aus.
Gruß Holger