Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Lux sensor (BH1750) sending battery level (V)

Lux sensor (BH1750) sending battery level (V)

Scheduled Pinned Locked Moved Development
12 Posts 6 Posters 1.8k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FrankVK
    wrote on last edited by
    #3

    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));
    
    
    zboblamontZ 1 Reply Last reply
    0
    • F FrankVK

      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));
      
      
      zboblamontZ Offline
      zboblamontZ Offline
      zboblamont
      wrote on last edited by zboblamont
      #4

      @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 cycle
      

      That 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.

      1 Reply Last reply
      0
      • electrikE Offline
        electrikE Offline
        electrik
        wrote on last edited by
        #5

        Is there a typo in your message or in your code?
        First you used ReadVcc(), then readVcc()

        1 Reply Last reply
        0
        • F FrankVK

          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

          mfalkviddM Offline
          mfalkviddM Offline
          mfalkvidd
          Mod
          wrote on last edited by
          #6

          @frankvk could you post the entire sketch, including where readVcc comes from? Seeing the code will make it much easier to help you.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bgunnarb
            wrote on last edited by
            #7

            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.

            I have never been so busy since I retired!

            K 1 Reply Last reply
            1
            • B bgunnarb

              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.

              K Offline
              K Offline
              kimot
              wrote on last edited by
              #8

              @bgunnarb
              But readVcc returns long

              long 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;
              }```
              mfalkviddM 1 Reply Last reply
              0
              • K kimot

                @bgunnarb
                But readVcc returns long

                long 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;
                }```
                mfalkviddM Offline
                mfalkviddM Offline
                mfalkvidd
                Mod
                wrote on last edited by mfalkvidd
                #9

                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.968

                See "Integer and floating point division" at https://www.learncpp.com/cpp-tutorial/32-arithmetic-operators/

                zboblamontZ F B 3 Replies Last reply
                1
                • mfalkviddM mfalkvidd

                  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.968

                  See "Integer and floating point division" at https://www.learncpp.com/cpp-tutorial/32-arithmetic-operators/

                  zboblamontZ Offline
                  zboblamontZ Offline
                  zboblamont
                  wrote on last edited by
                  #10

                  @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...

                  1 Reply Last reply
                  0
                  • mfalkviddM mfalkvidd

                    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.968

                    See "Integer and floating point division" at https://www.learncpp.com/cpp-tutorial/32-arithmetic-operators/

                    F Offline
                    F Offline
                    FrankVK
                    wrote on last edited by FrankVK
                    #11

                    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!

                    1 Reply Last reply
                    1
                    • mfalkviddM mfalkvidd

                      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.968

                      See "Integer and floating point division" at https://www.learncpp.com/cpp-tutorial/32-arithmetic-operators/

                      B Offline
                      B Offline
                      bgunnarb
                      wrote on last edited by
                      #12

                      @mfalkvidd
                      Yep, this is a great way of introducing errors that will be nearly impossible to find!:grinning:

                      I have never been so busy since I retired!

                      1 Reply Last reply
                      0

                      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
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      14

                      Online

                      12.0k

                      Users

                      11.2k

                      Topics

                      113.4k

                      Posts


                      Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • MySensors
                      • OpenHardware.io
                      • Categories
                      • Recent
                      • Tags
                      • Popular