Lux sensor (BH1750) sending battery level (V)
-
Hi Zboblamont,
Thanks for your response, but the error occurs in the line, obviously because Vcc is of the type 'float'
send(msgVcc.set(Vcc));@frankvk I'm no expert on this, but do know all my nodes send battery as a float for the last 2 years, excerpt as follows:
// Call for battery reading, send in every 12th RTC call? if(halfday==12){ digitalWrite(BatteryOn, HIGH); voltread = analogRead(BatteryIn); float voltage = (7.272 * voltread) / 1024; sleep(5); digitalWrite(BatteryOn, LOW); send(msg3.set(voltage,2)); sleep(50); voltread=0; sleep(50); halfday=0;//Reset for 12 hourly cycle } halfday++;//Increment hourly cycleThat is based on the internal value defined without any complications, but note that the message is sent to 2 decimal places, whether that truncation is essential for the MySensors environment I do not know, it was all I required.
-
Hi,
I really enjoy using MySensors!
Got a small general question though: I'm using a BH1750 light sensor, powered by a 3V battery and I'd like to keep an eye on the battery status.
I have added a line to the existing BH1750 scetch:
#define CHILD_ID_VCC = 1 MyMessage msgVcc(CHILD_ID_VCC, V_VOLTAGE); .. void Presentation() { sendSketchInfo("Light Lux Sensor", "1.1"); present(CHILD_ID_LIGHT, S_LIGHT_LEVEL); present(CHILD_ID_VCC, S_MULTIMETER); } Loop() { uint16_t lux = lightSensor.readLightLevel(); float Vcc = ReadVcc(); send(msgLux.set(Lux)); send(msgVcc.set(Vcc)); }Domoticz recieves the data now, but the Voltage is reading 2968V because readVcc delivers mV
If I change the code to:float Vcc = readVcc()/1000;I get an error message "call of overloaded 'set(float&)'is ambiguous"
How can I send decimal values to the Domoticz controller from MySensors?Regards, Frank
-
The line
float Vcc = readVcc()/1000;should be:
float Vcc = readVcc()/1000.;to start with. Note the decimal point! If read Vcc() returns a float, you cannot divide by an integer.
@bgunnarb
But readVcc returns longlong readVcc() { long result; // Read 1.1V reference against AVcc ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); delay(2); // Wait for Vref to settle ADCSRA |= _BV(ADSC); // Convert while (bit_is_set(ADCSRA,ADSC)); result = ADCL; result |= ADCH<<8; result = 1126400L / result; // Back-calculate AVcc in mV return result; }``` -
@bgunnarb
But readVcc returns longlong readVcc() { long result; // Read 1.1V reference against AVcc ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); delay(2); // Wait for Vref to settle ADCSRA |= _BV(ADSC); // Convert while (bit_is_set(ADCSRA,ADSC)); result = ADCL; result |= ADCH<<8; result = 1126400L / result; // Back-calculate AVcc in mV return result; }```A float can be divided by an integer. The result will be a float.
A long can be divided by an integer or a float. The result will differ though.
2968/1000 will give the integer 2
2968/1000.0 will give the float 2.968See "Integer and floating point division" at https://www.learncpp.com/cpp-tutorial/32-arithmetic-operators/
-
A float can be divided by an integer. The result will be a float.
A long can be divided by an integer or a float. The result will differ though.
2968/1000 will give the integer 2
2968/1000.0 will give the float 2.968See "Integer and floating point division" at https://www.learncpp.com/cpp-tutorial/32-arithmetic-operators/
@mfalkvidd @bgunnarb Something new learned from that, hadn't realised a procedure call could be mathematically manipulated like that, nor the subtle differences in the outputs...
-
A float can be divided by an integer. The result will be a float.
A long can be divided by an integer or a float. The result will differ though.
2968/1000 will give the integer 2
2968/1000.0 will give the float 2.968See "Integer and floating point division" at https://www.learncpp.com/cpp-tutorial/32-arithmetic-operators/
Actually, I needed to change 2 things: first one was dividing by 1000.0 rather than 1000 and the other one was calling send(msgVcc.set(Vcc, 2)); rather than send(msgVcc.set(Vcc)); Now the log reads 2.95V.
Thanks Mikael (@mfalkvidd) & everyone! Happy coding!
-
A float can be divided by an integer. The result will be a float.
A long can be divided by an integer or a float. The result will differ though.
2968/1000 will give the integer 2
2968/1000.0 will give the float 2.968See "Integer and floating point division" at https://www.learncpp.com/cpp-tutorial/32-arithmetic-operators/
@mfalkvidd
Yep, this is a great way of introducing errors that will be nearly impossible to find!:grinning:
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login