while (!sensor.begin()) error



  • Hi,

    I'm sorry, this must be a really stupid question to most of you but I failed to get a solution elsewhere:(

    I try to run this sketch

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0: Yveaux
     * 
     * DESCRIPTION
     * This sketch provides an example of how to implement a humidity/temperature
     * sensor using a HTU21D sensor.
     *  
     * 
     */
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable REPORT_BATTERY_LEVEL to measure battery level and send changes to gateway
    //#define REPORT_BATTERY_LEVEL
    
    // Enable and select radio type attached 
    #define MY_RADIO_RF24
    //#define MY_RADIO_RFM69
    //#define MY_RS485
    
    #include <MySensors.h>  
    
    #define CHILD_ID_HUM  0
    #define CHILD_ID_TEMP 1
    
    static bool metric = true;
    
    // Sleep time between sensor updates (in milliseconds)
    //static const uint64_t UPDATE_INTERVAL = 60000;
    //for debugging:
    static const uint64_t UPDATE_INTERVAL = 5000;
    
    #include <SparkFunHTU21D.h>
    static HTU21D sensor;
    
    #ifdef REPORT_BATTERY_LEVEL
    #include <Vcc.h>
    static uint8_t oldBatteryPcnt = 200;  // Initialize to 200 to assure first time value will be sent.
    const float VccMin        = 1.8;      // Minimum expected Vcc level, in Volts: Brownout at 1.8V    -> 0%
    const float VccMax        = 2.0*1.6;  // Maximum expected Vcc level, in Volts: 2xAA fresh Alkaline -> 100%
    const float VccCorrection = 1.0;      // Measured Vcc by multimeter divided by reported Vcc
    static Vcc vcc(VccCorrection); 
    #endif
    
    void presentation()  
    { 
      // Send the sketch info to the gateway
      sendSketchInfo("TemperatureAndHumidity", "1.0");
    
      // Present sensors as children to gateway
      present(CHILD_ID_HUM, S_HUM,   "Humidity");
      present(CHILD_ID_TEMP, S_TEMP, "Temperature");
    
      //metric = getControllerConfig().isMetric; //Erase later
    }
    
    void setup()
    {
      while (!sensor.begin())
      {
        Serial.println(F("Sensor not detected!"));
        delay(5000);
      }
    }
    
    
    void loop()      
    {  
      // Read temperature & humidity from sensor.
      const float temperature = float( sensor.readTemperature() ) / 100.0;
      const float humidity    = float( sensor.readHumidity() ) / 100.0;
    
    #ifdef MY_DEBUG
      Serial.print(F("Temp "));
      Serial.print(temperature);
      Serial.print(metric ? 'C' : 'F');
      Serial.print(F("\tHum "));
      Serial.println(humidity);
    #endif
    
      static MyMessage msgHum( CHILD_ID_HUM,  V_HUM );
      static MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    
      send(msgTemp.set(temperature, 2));
      send(msgHum.set(humidity, 2));
    
    #ifdef REPORT_BATTERY_LEVEL
      const uint8_t batteryPcnt = static_cast<uint8_t>(0.5 + vcc.Read_Perc(VccMin, VccMax));
    
    #ifdef MY_DEBUG
      Serial.print(F("Vbat "));
      Serial.print(vcc.Read_Volts());
      Serial.print(F("\tPerc "));
      Serial.println(batteryPcnt);
    #endif
    
      // Battery readout should only go down. So report only when new value is smaller than previous one.
      if ( batteryPcnt < oldBatteryPcnt )
      {
          sendBatteryLevel(batteryPcnt);
          oldBatteryPcnt = batteryPcnt;
      }
    #endif
    
      // Sleep until next update to save energy
      sleep(UPDATE_INTERVAL); 
    }
    

    but the compiler always complains about this line:

      while (!sensor.begin())
      {
        Serial.println(F("Sensor not detected!"));
        delay(5000);
      }
    

    it says:

    D:\XX\Mysensors\TempSen_HTU21D\TempSen_HTU21D.ino: In function 'void setup()':
    TempSen_HTU21D:89:24: error: could not convert 'sensor.HTU21D::begin((* & Wire))' from 'void' to 'bool'
       while (!sensor.begin())
                            ^
    TempSen_HTU21D:89:24: error: in argument to unary !
    
    

    Does anybody know where the problem is? I also tried IF and all kinds of other stuff but it seems the problem is actually not this line but some other issue...

    I'd be super glad if anybody can point me in the right direction...



  • It looks like sensor.begin() doesn't return a value


  • Mod

    @maddhin you need to pass a Wire instance in the call to begin(). Search the net for an example.

    Edit: never mind; answered too quickly 😉 It doesn't return a bool indeed so you cannot use it in a while loop.



  • @Yveaux said in while (!sensor.begin()) error:

    @maddhin you need to pass a Wire instance in the call to begin(). Search the net for an example.

    I did some rearching but I think the problem is that I don't really understand what that means 🙂

    I tried adjusting the code close to the official Sparkfun example code with include wire.h etc. but that didn't work either.

    #include <Wire.h>
    #include "SparkFunHTU21D.h"
    
    //Create an instance of the object
    HTU21D myHumidity;
    
    void setup()
    {
      Serial.begin(9600);
      Serial.println("HTU21D Example!");
    
      myHumidity.begin();
    }
    

    I'll search some more tomorrow but, I guess, I leave out the while/if as this seems to be too complicated for a "simple" error handler 🙂



  • @maddhin The while / if won't work here, since SparkFun's implementation of the begin() function doesn't return a value, as both the compile error and @electrik suggest.

    Take a look at the SparkFunHTU21D.h header, where the function is defined:

    void begin(TwoWire &wirePort = Wire); //If user doesn't specificy then Wire will be used
    

    Its return type is void. It returns nothing. While and if conditions need something to compare, so the function would need to be a bool begin(...) or some other simple numeric type. Only then can a while or if clause determine if a condition is true (non-zero) or false (zero).

    In other words: SparkFun doesn't test if the sensor has been initialized properly, so you can't either.



  • @BearWithBeard said in while (!sensor.begin()) error:

    In other words: SparkFun doesn't test if the sensor has been initialized properly, so you can't either.

    awesome answer, @BearWithBeard!! This explains everything and this makes a whole lot of sense. I guess I need to further up my programming skills/understanding 😉 Thank you so much!



  • @maddhin FWIW I always use the adafruit library for htu21D and it has a similar test in setup that does not give me any errors - might be worth a try?


Log in to reply
 

Suggested Topics

  • 3
  • 3
  • 2
  • 10
  • 2
  • 24

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts