<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[while (!sensor.begin()) error]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I'm sorry, this must be a really stupid question to most of you but I failed to get a solution elsewhere:(</p>
<p dir="auto">I try to run this sketch</p>
<pre><code>/**
 * 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 &lt;henrik.ekblad@mysensors.org&gt;
 * 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 &lt;MySensors.h&gt;  

#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 &lt;SparkFunHTU21D.h&gt;
static HTU21D sensor;

#ifdef REPORT_BATTERY_LEVEL
#include &lt;Vcc.h&gt;
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    -&gt; 0%
const float VccMax        = 2.0*1.6;  // Maximum expected Vcc level, in Volts: 2xAA fresh Alkaline -&gt; 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 &amp; 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&lt;uint8_t&gt;(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 &lt; oldBatteryPcnt )
  {
      sendBatteryLevel(batteryPcnt);
      oldBatteryPcnt = batteryPcnt;
  }
#endif

  // Sleep until next update to save energy
  sleep(UPDATE_INTERVAL); 
}
</code></pre>
<p dir="auto">but the compiler always complains about this line:</p>
<pre><code>  while (!sensor.begin())
  {
    Serial.println(F("Sensor not detected!"));
    delay(5000);
  }
</code></pre>
<p dir="auto">it says:</p>
<pre><code>D:\XX\Mysensors\TempSen_HTU21D\TempSen_HTU21D.ino: In function 'void setup()':
TempSen_HTU21D:89:24: error: could not convert 'sensor.HTU21D::begin((* &amp; Wire))' from 'void' to 'bool'
   while (!sensor.begin())
                        ^
TempSen_HTU21D:89:24: error: in argument to unary !

</code></pre>
<p dir="auto">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...</p>
<p dir="auto">I'd be super glad if anybody can point me in the right direction...</p>
]]></description><link>https://forum.mysensors.org/topic/11673/while-sensor-begin-error</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Apr 2026 23:53:50 GMT</lastBuildDate><atom:link href="https://forum.mysensors.org/topic/11673.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 29 Apr 2021 16:31:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to while (!sensor.begin()) error on Fri, 30 Apr 2021 09:22:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/maddhin" aria-label="Profile: maddhin">@<bdi>maddhin</bdi></a> 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?</p>
]]></description><link>https://forum.mysensors.org/post/110102</link><guid isPermaLink="true">https://forum.mysensors.org/post/110102</guid><dc:creator><![CDATA[skywatch]]></dc:creator><pubDate>Fri, 30 Apr 2021 09:22:10 GMT</pubDate></item><item><title><![CDATA[Reply to while (!sensor.begin()) error on Fri, 30 Apr 2021 07:04:45 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bearwithbeard" aria-label="Profile: BearWithBeard">@<bdi>BearWithBeard</bdi></a> said in <a href="/post/110096">while (!sensor.begin()) error</a>:</p>
<blockquote>
<p dir="auto">In other words: SparkFun doesn't test if the sensor has been initialized properly, so you can't either.</p>
</blockquote>
<p dir="auto">awesome answer, <a class="plugin-mentions-user plugin-mentions-a" href="/user/bearwithbeard" aria-label="Profile: BearWithBeard">@<bdi>BearWithBeard</bdi></a>!! 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!</p>
]]></description><link>https://forum.mysensors.org/post/110106</link><guid isPermaLink="true">https://forum.mysensors.org/post/110106</guid><dc:creator><![CDATA[maddhin]]></dc:creator><pubDate>Fri, 30 Apr 2021 07:04:45 GMT</pubDate></item><item><title><![CDATA[Reply to while (!sensor.begin()) error on Fri, 30 Apr 2021 06:09:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/maddhin" aria-label="Profile: maddhin">@<bdi>maddhin</bdi></a> The while / if won't work here, since SparkFun's implementation of the <code>begin()</code> function doesn't return a value, as both the compile error and <a class="plugin-mentions-user plugin-mentions-a" href="/user/electrik" aria-label="Profile: electrik">@<bdi>electrik</bdi></a> suggest.</p>
<p dir="auto">Take a look at the <a href="https://github.com/sparkfun/SparkFun_HTU21D_Breakout_Arduino_Library/blob/master/src/SparkFunHTU21D.h#L53" rel="nofollow ugc">SparkFunHTU21D.h</a> header, where the function is defined:</p>
<pre><code>void begin(TwoWire &amp;wirePort = Wire); //If user doesn't specificy then Wire will be used
</code></pre>
<p dir="auto">Its return type is <code>void</code>. It returns <em>nothing</em>. While and if conditions need something to compare, so the function would need to be a <code>bool begin(...)</code> 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).</p>
<p dir="auto">In other words: SparkFun doesn't test if the sensor has been initialized properly, so you can't either.</p>
]]></description><link>https://forum.mysensors.org/post/110096</link><guid isPermaLink="true">https://forum.mysensors.org/post/110096</guid><dc:creator><![CDATA[BearWithBeard]]></dc:creator><pubDate>Fri, 30 Apr 2021 06:09:53 GMT</pubDate></item><item><title><![CDATA[Reply to while (!sensor.begin()) error on Thu, 29 Apr 2021 21:19:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/yveaux" aria-label="Profile: Yveaux">@<bdi>Yveaux</bdi></a> said in <a href="/post/110094">while (!sensor.begin()) error</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/maddhin" aria-label="Profile: maddhin">@<bdi>maddhin</bdi></a> you need to pass a Wire instance in the call to begin(). Search the net for an example.</p>
</blockquote>
<p dir="auto">I did some rearching but I think the problem is that I don't really understand what that means :)</p>
<p dir="auto">I tried adjusting the code close to the official Sparkfun example code with include wire.h etc. but that didn't work either.</p>
<pre><code>#include &lt;Wire.h&gt;
#include "SparkFunHTU21D.h"

//Create an instance of the object
HTU21D myHumidity;

void setup()
{
  Serial.begin(9600);
  Serial.println("HTU21D Example!");

  myHumidity.begin();
}
</code></pre>
<p dir="auto">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 :)</p>
]]></description><link>https://forum.mysensors.org/post/110095</link><guid isPermaLink="true">https://forum.mysensors.org/post/110095</guid><dc:creator><![CDATA[maddhin]]></dc:creator><pubDate>Thu, 29 Apr 2021 21:19:29 GMT</pubDate></item><item><title><![CDATA[Reply to while (!sensor.begin()) error on Sat, 01 May 2021 06:18:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/maddhin" aria-label="Profile: maddhin">@<bdi>maddhin</bdi></a> you need to pass a Wire instance in the call to begin(). Search the net for an example.</p>
<p dir="auto"><em>Edit</em>: never mind; answered too quickly ;-)    It doesn't return a bool indeed so you cannot use it in a while loop.</p>
]]></description><link>https://forum.mysensors.org/post/110094</link><guid isPermaLink="true">https://forum.mysensors.org/post/110094</guid><dc:creator><![CDATA[Yveaux]]></dc:creator><pubDate>Sat, 01 May 2021 06:18:33 GMT</pubDate></item><item><title><![CDATA[Reply to while (!sensor.begin()) error on Thu, 29 Apr 2021 18:45:03 GMT]]></title><description><![CDATA[<p dir="auto">It looks like sensor.begin() doesn't return a value</p>
]]></description><link>https://forum.mysensors.org/post/110090</link><guid isPermaLink="true">https://forum.mysensors.org/post/110090</guid><dc:creator><![CDATA[electrik]]></dc:creator><pubDate>Thu, 29 Apr 2021 18:45:03 GMT</pubDate></item></channel></rss>