<?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[Battery Tank level node and Openhab]]></title><description><![CDATA[<p dir="auto">Hi All,<br />
I am a bit confused again (its not hard..) I am trying to combine a previous sketch from <a class="plugin-mentions-user plugin-mentions-a" href="/user/boots33" aria-label="Profile: Boots33">@<bdi>Boots33</bdi></a> with battery reporting to openhab.  The tank sketch works great (Thanks Boots!) but I can not figure out how to send the battery level.  Here is my adapted sketch-:</p>
<pre><code>/*
  Sketch to read level of water in a round tank and  then send data back to controller
  uses MySensors V2 .

  Libraries used
  MySensors          https://www.mysensors.org/
  NewPing            https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home
  SPI                 installed with arduino IDE

*/

// Enable debug prints to serial monitor
#define MY_DEBUG

// Enable and select radio type attached
#define MY_RADIO_NRF24
// Enabled repeater feature for this node
//#define MY_REPEATER_FEATURE

//#define MY_RF24_CHANNEL 84    // channel of nrf network

#include &lt;MySensors.h&gt;
#include &lt;SPI.h&gt;
#include &lt;NewPing.h&gt;

#define MY_NODE_ID 3    // comment out this line if you use dynamic id's
#define CHILD_ID_WATER 1
#define CHILD_ID_PERCENT 2


// newping settings
#define TRIGGER_PIN  6                  // Arduino pin 6 connected to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     7                  // Arduino pin 7 connected to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 240                // Maximum distance we want to ping for (in centimeters). you should set this to be 
// a bit more than fullHeight + sensHeight.

/*------change these to suit your tank setup-----*/
const int tankRad = 170;                // Radius of tank in cm
const int fullHeight = 198;             // Height of water when tank full in cm
const int sensHeight = 30;              // height of sensor above full water mark in cm
const int outletHeight = 7;             // height of water when tank reading will show empty in cm
/*----------------------------------------------*/

int BATTERY_SENSE_PIN = A0;             // select the input pin for the battery sense point
int oldBatteryPcnt = 0;
unsigned long lastSent;
unsigned long heartbeatDelay = 15;     // how often the heartbeat will be sent, in minutes
//unsigned long lastHeartbeat = millis(); // holder for last time heartbeat was sent
uint32_t SLEEP_TIME = 10000;           // 900000=15 mins sleep time between reads (seconds * 1000 milliseconds)
unsigned long pingHeight;               // holds total height from ultrasonic sender to current water height
unsigned int waterAvail;                // holds current water available in litres

byte oldWaterPercent;
byte waterPercent = 0 ;                 // used to hold the tank water level percentage

// NewPing setup of pins and maximum distance.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

MyMessage msgVolume(CHILD_ID_WATER, V_LEVEL);              // water available in litres
MyMessage msgPercent(CHILD_ID_PERCENT, V_LEVEL);           // water percentsge available

void setup() {

  pinMode(5, OUTPUT);
  analogReference(INTERNAL);
}

void presentation()  {
  // Send the sketch version information to the gateway and Controller
  sendSketchInfo("Tank Level", "1.4");

  // Register all sensors to gateway (they will be created as child devices)
  present(CHILD_ID_WATER, S_DUST, "Water Available");
  present(CHILD_ID_PERCENT, S_DUST, "Water Percentage Available");
}


void loop()
{
// get the battery Voltage
int sensorValue = analogRead(BATTERY_SENSE_PIN);
#ifdef MY_DEBUG
Serial.println(sensorValue);
#endif

// 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
// ((2.2e6+330e3)/330e3)*1.1 = Vmax = 8.4 Volts
// 8.4/1023 = Volts per bit = 0.001075275

int batteryPcnt = sensorValue / 10;

#ifdef MY_DEBUG
//float batteryV  = sensorValue * 0.001075275;
float batteryV = sensorValue * 9.17 / 1023;

Serial.print("Battery Voltage: ");
Serial.print(batteryV);
Serial.println(" V");

Serial.print("Battery percent: ");
Serial.print(batteryPcnt);
Serial.println(" %");
#endif

if (oldBatteryPcnt != batteryPcnt) {
  // Power up radio after sleep
  sendBatteryLevel(batteryPcnt);
  oldBatteryPcnt = batteryPcnt;
}
{
  Serial.println("Waking out of sleep");
  digitalWrite(5, HIGH);
  Serial.println("Ultrasonic module ON");
  delay (1000);

  data_calc();      // perform calculations to get water remaining etc.


  if (oldWaterPercent != waterPercent) {        //check to see if new water data is available
    send(msgVolume.set(waterAvail));
    send(msgPercent.set(waterPercent));
    oldWaterPercent = waterPercent;
    lastSent = millis();
  }

  heartbeatCheck();                                    // call heartbeat function
  digitalWrite(5, LOW);
  Serial.println("Ultrasonic module OFF");

  Serial.println("Entering Sleep");
  sleep(SLEEP_TIME);

}
}
/*-------------------------start of functions-------------------*/

void heartbeatCheck() {
  unsigned long millisNow = millis();           // get the current time
  if ((millisNow - lastSent) &gt; (heartbeatDelay * 60000)) {
    send(msgVolume.set(waterAvail));
    send(msgPercent.set(waterPercent));
    lastSent = millisNow;
  }

  /*if ((millisNow - lastHeartbeat) &gt; (heartbeatDelay*60000)) {
    sendHeartbeat();
    wait(5);
    // sendBatteryLevel(100);
    lastHeartbeat = millis();
    #ifdef MY_DEBUG
      Serial.println("Heartbeat Sent" );
    #endif
    } */
}

void data_calc() {
  pingHeight = sonar.ping_median(5);             //- Do multiple (5) pings and return median
  pingHeight = sonar.convert_cm(pingHeight);     // then convert to cm
#ifdef MY_DEBUG
  Serial.print("Ping Height raw in cm: ");
  Serial.println(pingHeight);
#endif
  pingHeight  = constrain(pingHeight, sensHeight, (fullHeight - outletHeight) + sensHeight); // keep pingHeight within the expected values
  waterPercent = map(pingHeight, sensHeight, (fullHeight - outletHeight) + sensHeight, 100, 0);   // calculate the percentage of water available
  waterAvail = PI * pow(tankRad, 2) * (((fullHeight - outletHeight) + sensHeight) - pingHeight) / 1000; // calculate water available in litres

  // Write some debug info
#ifdef MY_DEBUG
  Serial.print("Ping Height constrained in cm: ");
  Serial.println(pingHeight);
  Serial.print("Percentage Available: ");
  Serial.println(waterPercent);
  Serial.print("Litres Available: ");
  Serial.println(waterAvail);
#endif
}
</code></pre>
<p dir="auto">I am trying to figure out how the sketch sends the battery info to openhab and how I view it.  Any tips or pointers would be most appreciated.  Do I have to add another line in the void presentation for the voltage?  And should Openhab discover the voltage data as another Thing?</p>
<p dir="auto">Cheers<br />
Crumpy</p>
]]></description><link>https://forum.mysensors.org/topic/10417/battery-tank-level-node-and-openhab</link><generator>RSS for Node</generator><lastBuildDate>Tue, 18 Aug 2026 05:49:04 GMT</lastBuildDate><atom:link href="https://forum.mysensors.org/topic/10417.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 26 May 2019 09:41:40 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Battery Tank level node and Openhab on Tue, 28 May 2019 06:51:23 GMT]]></title><description><![CDATA[<p dir="auto">I do not know openhab, but in Domoticz this info ( battery and radio level ) I can see only in all devices list tab. Not like special sensor.</p>
]]></description><link>https://forum.mysensors.org/post/99436</link><guid isPermaLink="true">https://forum.mysensors.org/post/99436</guid><dc:creator><![CDATA[kimot]]></dc:creator><pubDate>Tue, 28 May 2019 06:51:23 GMT</pubDate></item><item><title><![CDATA[Reply to Battery Tank level node and Openhab on Mon, 27 May 2019 23:37:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bgunnarb" aria-label="Profile: bgunnarb">@<bdi>bgunnarb</bdi></a> thanks for the reply. That’s what I thought, that the battery monitor is included in the library.  I just don’t know how that part gets the info through to openHAB.  I am using the Ethernet gateway by the way.  I have managed to get the info through using the multimeter function but I think there is a better way for battery level!</p>
]]></description><link>https://forum.mysensors.org/post/99430</link><guid isPermaLink="true">https://forum.mysensors.org/post/99430</guid><dc:creator><![CDATA[Crumpy10]]></dc:creator><pubDate>Mon, 27 May 2019 23:37:29 GMT</pubDate></item><item><title><![CDATA[Reply to Battery Tank level node and Openhab on Mon, 27 May 2019 12:50:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/crumpy10" aria-label="Profile: Crumpy10">@<bdi>Crumpy10</bdi></a> The sendBatteryLevel is a method included in the library.<br />
How do you connect MySensors to OpenHAB? Do you use the serial GW or MQTT? What binding do you use on the OH-side?<br />
I am using MQTT myself so I am not that much at home with serial or with the MySensors binding for OH.</p>
]]></description><link>https://forum.mysensors.org/post/99412</link><guid isPermaLink="true">https://forum.mysensors.org/post/99412</guid><dc:creator><![CDATA[bgunnarb]]></dc:creator><pubDate>Mon, 27 May 2019 12:50:52 GMT</pubDate></item><item><title><![CDATA[Reply to Battery Tank level node and Openhab on Mon, 27 May 2019 09:07:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dbemowsk" aria-label="Profile: dbemowsk">@<bdi>dbemowsk</bdi></a> Hi, Thanks for the reply.  I am very new to Mysensors and not a programmer.  I think the sendbatteryLevel is part of the included libraries but not fully sure.  I am seeing both voltage and percent in the serial monitor but just cant work out how to send it through the gateway to the controller (openhab).  I just cant work out what extra I need in the sketch to do this?</p>
]]></description><link>https://forum.mysensors.org/post/99406</link><guid isPermaLink="true">https://forum.mysensors.org/post/99406</guid><dc:creator><![CDATA[Crumpy10]]></dc:creator><pubDate>Mon, 27 May 2019 09:07:33 GMT</pubDate></item><item><title><![CDATA[Reply to Battery Tank level node and Openhab on Sun, 26 May 2019 12:37:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/crumpy10" aria-label="Profile: crumpy10">@<bdi>crumpy10</bdi></a> Just looking over your sketch, I see on line 108 you have a call to a method named sendBatteryLevel().  I do not see that method defined anywhere in your sketch.  I don't know if it is part of one of your included libraries, but if it is not, you need to define that.</p>
]]></description><link>https://forum.mysensors.org/post/99384</link><guid isPermaLink="true">https://forum.mysensors.org/post/99384</guid><dc:creator><![CDATA[dbemowsk]]></dc:creator><pubDate>Sun, 26 May 2019 12:37:22 GMT</pubDate></item></channel></rss>