<div style="color: #878787; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">
   <div id="105747915554748821" align="left" class="wcustomhtml" style="width: 850px; overflow-y: hidden;">
      <h1 align="center" style="color: black;">Battery voltage monitor with nodemcu Esp8266-12E WiFi module</h1>
      </div>
   </div>
<div class="paragraph" style="overflow-wrap: break-word; font-size: 13px; margin: 0px; padding: 0.5em 0px; font-family: Arial, Helvetica, sans-serif; text-align: justify; line-height: 24px !important; color: #2a2a2a !important;"><font size="3">Measuring voltage of your solar panel, ups and other daily use batteries consumes a lot of time. Grabbing multi-meter, opening the battery boxes and touching both the multi-meter leads to terminals of battery takes some effort. How about developing an efficient IOT system through which you can see the status of your battery on your desktop and mobile web browsers. Lets start and make this diy project. Since we are going to develop and IOT system which can monitor battery status and update us on our browser. We need some circuit/device/controller which can effectively measure voltage. A WiFi device which can connect to our local home WiFi, takes voltage reading from controller and updates the end user about the current battery level. For this purpose i decided to use nodemcu WiFi module in the project. It can not only work as controller but also it can connect it self to a WiFi network as server or client. Battery monitoring circuit is a traditional voltage divider&nbsp;circuit. I am going to measure 12 volt batteries. Circuit can be modified to measure 24 volt batteries and even more 48 volts parallel&nbsp;battery cluster.</font></div>
<div style="color: #878787; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">
   <div id="480362449164041961" align="left" class="wcustomhtml" style="width: 850px; overflow-y: hidden;">
      <h3 style="font-size: 1.3em; margin: 0px 0px 10px; color: black;">Voltage divider working and calculations</h3>
      </div>
   </div>
<div class="paragraph" style="overflow-wrap: break-word; font-size: 13px; margin: 0px; padding: 0.5em 0px; font-family: Arial, Helvetica, sans-serif; text-align: justify; line-height: 24px !important; color: #2a2a2a !important;"><font size="3">Nodemcu is a tiny device, it works on 3.3 volts. Since it is working on 3.3 volts its pins can source and sunk 3.3 volts only. Voltage greater than 5 volt may blow the pin or fry the nodemcu. In our case we want to measure 12 volt battery and nodemcu adc(analog to digital channel)can only accept 3.3 volts. We need to play smartly&nbsp;here. What we will do is divide the voltage between two resistors and measure only voltage across one resistor and remaining resistor voltage will be calculated mathematically. Typical voltage divider circuit and formula is given below.</font></div>
<div style="color: #878787; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">
   <div class="wsite-image wsite-image-border-none " style="position: relative; padding-top: 10px; padding-bottom: 10px; margin-left: 0px; margin-right: 0px; text-align: center;"><a href="https://www.microcontroller-project.com/uploads/2/2/1/5/22159166/battery_orig.jpg" class="w-fancybox" style="text-decoration-line: none; color: #008000;" target="_blank"><img src="https://www.microcontroller-project.com/uploads/2/2/1/5/22159166/battery_orig.jpg" alt="Voltage divider with battery circuit" style="border-width: 0px !important; border-style: initial; border-color: initial; border-image: initial; width: auto; max-width: 100%;" /></a>
      <div style="font-size: 11.7px;">Voltage divider with battery circuit</div>
      </div>
   </div>
<div class="paragraph" style="overflow-wrap: break-word; font-size: 13px; margin: 0px; padding: 0.5em 0px; font-family: Arial, Helvetica, sans-serif; text-align: justify; line-height: 24px !important; color: #2a2a2a !important;"><font size="3">Now lets calculate the values for Rtop and Rbottom. Here we need some important considerations to be taken seriously.&nbsp;</font>
   <ul style="padding-left: 3em !important; margin: 5px 0px !important; list-style-position: outside !important; list-style-image: initial !important;">
      <li style="padding-left: 5px !important; margin: 3px 0px 0px !important; list-style: disc outside !important;"><font size="3">Low ohm resistors can sunk much current and wires could be heated instantly. Consequently wires can melt down in seconds. So always use sufficient amount of resistors for bigger ampere hour batteries. I selected one resistor Rbottom to be 10k ohm.</font>&nbsp;<br /></li>
      <li style="padding-left: 5px !important; margin: 3px 0px 0px !important; list-style: disc outside !important;"><font size="3">During charging battery voltage can increase to 18 volts. For example150 watt solar panel outputs 17&nbsp;volts at 6&nbsp;amperes&nbsp;during full sun, output voltage can even reach above 18 volts</font>.&nbsp;<font size="3">Solar charge controller also output voltage approximately equal to 15 volts to charge the batteries.</font></li>
      </ul>
   </div>
<div style="color: #878787; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">
   <div id="373278147752635110" align="left" class="wcustomhtml" style="width: 850px; overflow-y: hidden;">
      <h3 style="font-size: 1.3em; margin: 0px 0px 10px; color: black;">Formula Calculations</h3>
      </div>
   </div>
<div class="paragraph" style="overflow-wrap: break-word; font-size: 13px; margin: 0px; padding: 0.5em 0px; font-family: Arial, Helvetica, sans-serif; text-align: justify; line-height: 24px !important; color: #2a2a2a !important;"><font size="3">I am going to measure voltage across Rbottom and i decided its value randomly to be 10k ohm. We know Vout can be up to maximum 3.3 volts since nodemcu works and accepts maximum 3.3 volts at its I/O pins. Vin is 18 volts when the battery is charging. Now we can find Rtop.&nbsp;</font></div>
<div style="color: #878787; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">
   <div class="wsite-image wsite-image-border-none " style="position: relative; padding-top: 10px; padding-bottom: 10px; margin-left: 0px; margin-right: 0px; text-align: center;"><a href="https://www.microcontroller-project.com/uploads/2/2/1/5/22159166/voltage-devider-resistance-value-calculation_orig.png" class="w-fancybox" style="text-decoration-line: none; color: #008000;" target="_blank"><img src="https://www.microcontroller-project.com/uploads/2/2/1/5/22159166/voltage-devider-resistance-value-calculation_orig.png" alt="Voltage divider resistance value calculation" style="border-width: 0px !important; border-style: initial; border-color: initial; border-image: initial; width: auto; max-width: 100%;" /></a>
      <div style="font-size: 11.7px;">Voltage divider resistance value calculation</div>
      </div>
   </div>
<div class="paragraph" style="overflow-wrap: break-word; font-size: 13px; margin: 0px; padding: 0.5em 0px; font-family: Arial, Helvetica, sans-serif; text-align: justify; line-height: 24px !important; color: #2a2a2a !important;"><font size="3">Now if 18 volts are at battery side it will be divided across resistors, 3.3 volts drops at 10k resistor and remaining 14.7 volts drops at 44.54 k resistor. 44.54 k ohm resistor is not available in market i am going to use the one above this rating and easily can be find from any electronics shop 47 k ohm resistor.&nbsp; If battery is not charging and say supplying 12 volts what will be the voltage drop across resistors? Lets calculate it</font></div>
<div style="color: #878787; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">
   <div class="wsite-image wsite-image-border-none " style="position: relative; padding-top: 10px; padding-bottom: 10px; margin-left: 0px; margin-right: 0px; text-align: center;"><a href="https://www.microcontroller-project.com/uploads/2/2/1/5/22159166/voltage-drop-across-resistance-of-voltage-divider-circuit_orig.png" class="w-fancybox" style="text-decoration-line: none; color: #008000;" target="_blank"><img src="https://www.microcontroller-project.com/uploads/2/2/1/5/22159166/voltage-drop-across-resistance-of-voltage-divider-circuit_orig.png" alt="Voltage drop across voltage divider resistors" style="border-width: 0px !important; border-style: initial; border-color: initial; border-image: initial; width: auto; max-width: 100%;" /></a>
      <div style="font-size: 11.7px;">Voltage drop across resistance of voltage divider circuit</div>
      </div>
   </div>
<div class="paragraph" style="overflow-wrap: break-word; font-size: 13px; margin: 0px; padding: 0.5em 0px; font-family: Arial, Helvetica, sans-serif; text-align: justify; line-height: 24px !important; color: #2a2a2a !important;"><font size="3">Its obvious from the above discussion that the voltage across Rbottom will not exceed 3.3 volts now. I hope it makes sense to readers about the calculations. The question now is how the 3.33 volts is converted to 12 volt by nodemcu or how from 3.33 volts we can predict that at the battery side the voltage is 12 volts. Well a little more mathematics is involved here. Since the resistor values are fixed we can calculate the voltage ratio across the resistors with respect to the source and use it in code for actual voltage at source. How ratio is calculated is below.</font></div>
<div style="color: #878787; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">
   <div id="524359916487231677" align="left" class="wcustomhtml" style="width: 850px; overflow-y: hidden;"><center><ins class="adsbygoogle" data-ad-client="ca-pub-8954746221155592" data-ad-slot="9040383063" data-adsbygoogle-status="done" style="display: inline-block; width: 300px; height: 250px;"><ins id="aswift_3_expand" style="display: inline-table; border: none; height: 250px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 300px;"><ins id="aswift_3_anchor" style="display: block; border: none; height: 250px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 300px;"><iframe width="300" height="250" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_3" name="aswift_3" style="left: 0px; position: absolute; top: 0px; border-width: 0px; border-style: initial; width: 300px; height: 250px;"></iframe></ins></ins></ins></center></div>
   </div>
<div style="color: #878787; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">
   <div class="wsite-image wsite-image-border-medium " style="position: relative; padding-left: 4px !important; padding-right: 4px !important; padding-top: 5px; padding-bottom: 10px; margin-left: 0px; margin-right: 10px; text-align: center;"><a href="https://www.microcontroller-project.com/uploads/2/2/1/5/22159166/voltage-divider-ratio_orig.png" class="w-fancybox" style="text-decoration-line: none; color: #008000;" target="_blank"><img src="https://www.microcontroller-project.com/uploads/2/2/1/5/22159166/voltage-divider-ratio_orig.png" alt="Voltage divider ration calculation" style="border: 1px solid rgba(0, 0, 0, 0.13); padding: 3px !important; margin-left: -4px !important; margin-right: -4px !important; width: auto; max-width: 100%;" /></a>
      <div style="font-size: 11.7px;">Voltage divider ratio calculation</div>
      </div>
   </div>
<div class="paragraph" style="overflow-wrap: break-word; font-size: 13px; margin: 0px; padding: 0.5em 0px; font-family: Arial, Helvetica, sans-serif; text-align: justify; line-height: 24px !important; color: #2a2a2a !important;"><font size="3">Two cases are given above when source is at 18 volt and when source is at 12 volt in both the cases the ratio comes out to be constant value. This ratio is utilized in code for predicting the actual source/battery voltage. Ratio is multiplied with voltage at Rbottom for actual voltage value.<br />Project circuit diagram is given below. I am using ADC0 channel of nodemcu to measure the battery voltage. Both the battery and nodemcu power must be grounded together in order to complete the circuit. Its a most common mistake will measuring the voltage that both grounds are not grounded together. If the nodemcu ground is not taped with the battery ground the adc0 pin will become a floating pin and it starts reading floating values.</font><br /></div>
<div style="color: #878787; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">
   <div class="wsite-image wsite-image-border-none " style="position: relative; padding-top: 10px; padding-bottom: 10px; margin-left: 0px; margin-right: 0px; text-align: center;"><a href="https://www.microcontroller-project.com/uploads/2/2/1/5/22159166/battery-monitoring-with-nodemcu_orig.png" class="w-fancybox" style="text-decoration-line: none; color: #008000;" target="_blank"><img src="https://www.microcontroller-project.com/uploads/2/2/1/5/22159166/battery-monitoring-with-nodemcu_orig.png" alt="Battery voltage monitoring with nodemcu" style="border-width: 0px !important; border-style: initial; border-color: initial; border-image: initial; width: auto; max-width: 100%;" /></a>
      <div style="font-size: 11.7px;">Battery voltage monitoring with nodemcu</div>
      </div>
   </div>
<div class="paragraph" style="overflow-wrap: break-word; font-size: 13px; margin: 0px; padding: 0.5em 0px; font-family: Arial, Helvetica, sans-serif; text-align: justify; line-height: 24px !important; color: #2a2a2a !important;"><font size="3">After you completed the circuit its time to move on to code. Code is written in arduino IDE. I used ESP8266WiFi.h library in the code so first please make sure you installed this library in your arduino library folder. If its not present download it from github and install it first. Then enter the SSID and password of the WiFi network to which you want your nodemcu to be connected to. Most probably it will be your home WiFi. So just enter the ssid and password. Now upload the code in nodemcu. Before uploading make sure you selected the right board from arduino boards. If nodemcu is not appearing in your boards drop down menu than import its link from boards manager and install its necessary files. You can find many tutorials on internet about how to install nodemcu support for arduino IDE.&nbsp;&nbsp;</font></div>
<div style="color: #878787; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">
   <div class="wsite-multicol" style="position: relative; direction: ltr;">
      <div class="wsite-multicol-table-wrap" style="margin: 0px -15px;">
         <table class="wsite-multicol-table" style="position: relative; table-layout: fixed; width: 880px; margin: 0px !important; border-width: 0px !important; border-style: initial !important; border-color: initial !important; padding: 0px !important;">
            <tbody class="wsite-multicol-tbody">
               <tr class="wsite-multicol-tr">
                  <td class="wsite-multicol-col" style="vertical-align: top; padding: 0px 15px; width: 410px; margin: 0px !important; border-width: 0px !important; border-style: initial !important; border-color: initial !important;">
                     <div class="paragraph" style="overflow-wrap: break-word; font-size: 1em; line-height: 24px !important; margin: 0px; padding: 0.5em 0px; color: #2a2a2a !important; text-align: justify;"><font size="3">After uploading code you have to do a little one time complex work. Open arduino serial monitor. As soon as you open it you will see nodemcu server starting and your WiFi assigning an IP to your nodemcu.&nbsp;<font color="#508d24">This IP is very important. You have to enter this IP in your mobile or desktop browser to see the battery status.</font>&nbsp;If you do not see any message on serial monitor. Check for serial monitor communication speed it must be 9600 bps. Change it to 9600. If still you do not see any thing than might be code is not uploaded correctly or board is disconnected. Check for the possible errors. Once the IP is allotted by your router to nodemcu it will remain same all the time. I hope so :D&nbsp;</font></div>
                     </td>
                  <td class="wsite-multicol-col" style="vertical-align: top; padding: 0px 15px; width: 410px; margin: 0px !important; border-width: 0px !important; border-style: initial !important; border-color: initial !important;">
                     <div class="wsite-image wsite-image-border-none " style="position: relative; padding-top: 10px; padding-bottom: 10px; margin-left: 0px; margin-right: 0px; text-align: center;"><a style="color: #008000;"><img src="https://www.microcontroller-project.com/uploads/2/2/1/5/22159166/serial-monitor_1_orig.jpg" alt="Picture" style="border-width: 0px !important; border-style: initial; border-color: initial; border-image: initial; width: auto; max-width: 100%;" /></a>
                        <div style="font-size: 11.7px;">&nbsp;</div>
                        </div>
                     </td>
                  </tr>
               </tbody>
            </table>
         </div>
      </div>
   </div>
<div style="color: #878787; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">
   <div id="590819516143209058" align="left" class="wcustomhtml" style="width: 850px; overflow-y: hidden;">
      <h2 align="center" style="overflow-wrap: break-word; font-size: 24px; margin: 0px; padding: 0.3em 0px; line-height: 1.2; font-weight: normal; font-family: Glegoo, &quot;Myriad Pro&quot;, Arial, Helvetica, sans-serif; color: #8fc563; -webkit-text-stroke-width: 0.3px;">Project Code</h2>
      <font color="Black" align="center">
         <h3 style="font-size: 1.3em; margin: 0px 0px 10px;">Download the project code from the links given at the bottom of the Post.</h3>
         </font><ins class="adsbygoogle" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-8954746221155592" data-ad-slot="3951931537" data-adsbygoogle-status="done" style="display: block; text-align: center; height: 200px;"><ins id="aswift_4_expand" style="display: inline-table; border: none; height: 200px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 850px;"><ins id="aswift_4_anchor" style="display: block; border: none; height: 200px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 850px;"><iframe width="850" height="200" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_4" name="aswift_4" style="left: 0px; position: absolute; top: 0px; border-width: 0px; border-style: initial; width: 850px; height: 200px;"></iframe></ins></ins></ins></div>
   </div>
<div style="color: #878787; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">
   <div id="740458275156048346" align="left" class="wcustomhtml" style="width: 850px; overflow-y: hidden;">
      <div class="code-toolbar" style="position: relative;">
         <pre class=" language-arduino" style="margin-top: 0.5em; margin-bottom: 0.5em; padding: 1em; color: #f8f8f2; background: rgb(39, 40, 34); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace; word-spacing: normal; word-break: normal; overflow-wrap: normal; line-height: 1.5; overflow: auto; max-height: 500px; tab-size: 4; hyphens: none; border-radius: 0.3em; white-space: pre-wrap !important;">
            <code class=" language-arduino" style="background: none; text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace; word-spacing: normal; word-break: normal; overflow-wrap: normal; line-height: 1.5; overflow-y: auto; max-height: 500px; tab-size: 4; hyphens: none; overflow-x: auto !important;"><span class="token comment" style="color: slategray;">/*****************************************************
 * Date: 18 june 2018
 * Written by: Usman Ali Butt
 * Property off: <a class="vglnk" href="http://microcontroller-project.com/" rel="nofollow" style="text-decoration-line: none;" target="_blank">microcontroller-project.com</a>
 * ***************************************************/</span><span class="token macro property" style="color: #f92672;">#<span class="token directive keyword" style="color: #66d9ef;">include</span><span class="token string" style="color: #a6e22e;">&lt; ESP8266WiFi.h&gt;</span></span>
const char<span class="token operator">*</span> ssid <span class="token operator">=</span><span class="token string" style="color: #a6e22e;">"Your SSID"</span><span class="token punctuation">;</span>
const char<span class="token operator">*</span> password <span class="token operator">=</span><span class="token string" style="color: #a6e22e;">"Your Wifi Password"</span><span class="token punctuation">;</span><span class="token keyword keyword-int" style="color: #66d9ef;">int</span> BAT<span class="token operator">=</span> A0<span class="token punctuation">;</span><span class="token comment" style="color: slategray;">//Analog channel A0 as used to measure battery voltage</span>
float RatioFactor<span class="token operator">=</span><span class="token number" style="color: #ae81ff;">5.714</span><span class="token punctuation">;</span><span class="token comment" style="color: slategray;">//Resistors Ration Factor</span><span class="token builtin" style="color: #a6e22e;">WiFiServer</span><span class="token function" style="color: #e6db74;">server</span><span class="token punctuation">(</span><span class="token number" style="color: #ae81ff;">80</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword keyword-void" style="color: #66d9ef;">void</span><span class="token keyword keyword-setup" style="color: #66d9ef;">setup</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">begin</span><span class="token punctuation">(</span><span class="token number" style="color: #ae81ff;">9600</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function" style="color: #e6db74;">delay</span><span class="token punctuation">(</span><span class="token number" style="color: #ae81ff;">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">print</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"Connecting to "</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span>ssid<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token builtin" style="color: #a6e22e;">WiFi</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">begin</span><span class="token punctuation">(</span>ssid<span class="token punctuation">,</span> password<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword keyword-while" style="color: #66d9ef;">while</span><span class="token punctuation">(</span><span class="token builtin" style="color: #a6e22e;">WiFi</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">status</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">!=</span> WL_CONNECTED<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token function" style="color: #e6db74;">delay</span><span class="token punctuation">(</span><span class="token number" style="color: #ae81ff;">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">print</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"."</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"WiFi connected"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  server<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" style="color: slategray;">// Start the server</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"Server started"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" style="color: slategray;">// Print the IP address on serial monitor</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">print</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"Use this URL to connect: "</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">print</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"http://"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" style="color: slategray;">//URL IP to be typed in mobile/desktop browser</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">print</span><span class="token punctuation">(</span><span class="token builtin" style="color: #a6e22e;">WiFi</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">localIP</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"/"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword keyword-void" style="color: #66d9ef;">void</span><span class="token keyword keyword-loop" style="color: #66d9ef;">loop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword keyword-int" style="color: #66d9ef;">int</span> value <span class="token operator">=</span><span class="token constant" style="color: #f92672;">LOW</span><span class="token punctuation">;</span>
  float Tvoltage<span class="token operator">=</span><span class="token number" style="color: #ae81ff;">0.0</span><span class="token punctuation">;</span>
  float Vvalue<span class="token operator">=</span><span class="token number" style="color: #ae81ff;">0.0</span><span class="token punctuation">,</span>Rvalue<span class="token operator">=</span><span class="token number" style="color: #ae81ff;">0.0</span><span class="token punctuation">;</span><span class="token comment" style="color: slategray;">// Check if a client has connected</span><span class="token builtin" style="color: #a6e22e;">WiFiClient</span> client <span class="token operator">=</span> server<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">available</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword keyword-if" style="color: #66d9ef;">if</span><span class="token punctuation">(</span><span class="token operator">!</span>client<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword keyword-return" style="color: #66d9ef;">return</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment" style="color: slategray;">// Wait until the client sends some data</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"new client"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword keyword-while" style="color: #66d9ef;">while</span><span class="token punctuation">(</span><span class="token operator">!</span>client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">available</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token function" style="color: #e6db74;">delay</span><span class="token punctuation">(</span><span class="token number" style="color: #ae81ff;">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment" style="color: slategray;">// Read the first line of the request</span><span class="token keyword keyword-String" style="color: #66d9ef;">String</span> request <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">readStringUntil</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">'\r'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span>request<span class="token punctuation">)</span><span class="token punctuation">;</span>
  client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" style="color: slategray;">// Match the request</span><span class="token keyword keyword-if" style="color: #66d9ef;">if</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">indexOf</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"/bat=ON"</span><span class="token punctuation">)</span><span class="token operator">!=</span><span class="token operator">-</span><span class="token number" style="color: #ae81ff;">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment" style="color: slategray;">/////////////////////////////////////Battery Voltage//////////////////////////////////  </span><span class="token keyword keyword-for" style="color: #66d9ef;">for</span><span class="token punctuation">(</span>unsigned <span class="token keyword keyword-int" style="color: #66d9ef;">int</span> i<span class="token operator">=</span><span class="token number" style="color: #ae81ff;">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span><span class="token number" style="color: #ae81ff;">10</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  Vvalue<span class="token operator">=</span>Vvalue<span class="token operator">+</span><span class="token function" style="color: #e6db74;">analogRead</span><span class="token punctuation">(</span>BAT<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" style="color: slategray;">//Read analog Voltage</span><span class="token function" style="color: #e6db74;">delay</span><span class="token punctuation">(</span><span class="token number" style="color: #ae81ff;">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" style="color: slategray;">//ADC stable</span><span class="token punctuation">}</span>
  Vvalue<span class="token operator">=</span><span class="token punctuation">(</span>float<span class="token punctuation">)</span>Vvalue<span class="token operator">/</span><span class="token number" style="color: #ae81ff;">10.0</span><span class="token punctuation">;</span><span class="token comment" style="color: slategray;">//Find average of 10 values</span>
  Rvalue<span class="token operator">=</span><span class="token punctuation">(</span>float<span class="token punctuation">)</span><span class="token punctuation">(</span>Vvalue<span class="token operator">/</span><span class="token number" style="color: #ae81ff;">1024.0</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number" style="color: #ae81ff;">5</span><span class="token punctuation">;</span><span class="token comment" style="color: slategray;">//Convert Voltage in 5v factor</span>
  Tvoltage<span class="token operator">=</span>Rvalue<span class="token operator">*</span>RatioFactor<span class="token punctuation">;</span><span class="token comment" style="color: slategray;">//Find original voltage by multiplying with factor</span><span class="token comment" style="color: slategray;">/////////////////////////////////////Battery Voltage//////////////////////////////////</span>
    value <span class="token operator">=</span><span class="token constant" style="color: #f92672;">HIGH</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment" style="color: slategray;">// Return the response</span>
  client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"HTTP/1.1 200 OK"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"Content-Type: text/html"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" style="color: slategray;">//  do not forget this one</span>
  client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"&lt;!DOCTYPE HTML&gt;"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"&lt;html&gt;"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"Battery Voltage ="</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">print</span><span class="token punctuation">(</span>Tvoltage<span class="token punctuation">)</span><span class="token punctuation">;</span>
  client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"&lt;br&gt;"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword keyword-if" style="color: #66d9ef;">if</span><span class="token punctuation">(</span>value <span class="token operator">==</span><span class="token constant" style="color: #f92672;">HIGH</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"Updated"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword keyword-else" style="color: #66d9ef;">else</span><span class="token punctuation">{</span>
    client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">print</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"Not Updated"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span>
  client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"--------"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword keyword-if" style="color: #66d9ef;">if</span><span class="token punctuation">(</span>Tvoltage<span class="token operator">&lt;=</span><span class="token number" style="color: #ae81ff;">5</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"Battery dead OR disconnected"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword keyword-else" style="color: #66d9ef;">else</span><span class="token keyword keyword-if" style="color: #66d9ef;">if</span><span class="token punctuation">(</span>Tvoltage<span class="token operator">&gt;</span><span class="token number" style="color: #ae81ff;">5</span><span class="token operator">&amp;&amp;</span> Tvoltage<span class="token operator">&lt;=</span><span class="token number" style="color: #ae81ff;">10</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"Need Imediate recharge"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword keyword-else" style="color: #66d9ef;">else</span><span class="token keyword keyword-if" style="color: #66d9ef;">if</span><span class="token punctuation">(</span>Tvoltage<span class="token operator">&gt;</span><span class="token number" style="color: #ae81ff;">10</span><span class="token operator">&amp;&amp;</span> Tvoltage<span class="token operator">&lt;=</span><span class="token number" style="color: #ae81ff;">12</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"Recharge"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword keyword-else" style="color: #66d9ef;">else</span><span class="token punctuation">{</span>
      client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"Battery Full"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span>
  client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"&lt;br&gt;&lt;br&gt;"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"&lt;a href=\"/bat=ON\"\"&gt;&lt;button&gt;Status&lt;/button&gt;&lt;/a&gt;&lt;br /&gt;"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
  client<span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"&lt;/html&gt;"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function" style="color: #e6db74;">delay</span><span class="token punctuation">(</span><span class="token number" style="color: #ae81ff;">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">"Client disonnected"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token builtin" style="color: #a6e22e;">Serial</span><span class="token punctuation">.</span><span class="token function" style="color: #e6db74;">println</span><span class="token punctuation">(</span><span class="token string" style="color: #a6e22e;">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span></code>
            </pre>
         <div class="toolbar" style="position: absolute; top: 0.3em; right: 0.2em; transition: opacity 0.3s ease-in-out 0s; opacity: 0;">
            <div class="toolbar-item" style="display: inline-block;"><span style="color: #bbbbbb; font-size: 0.8em; padding: 0px 0.5em; background: rgba(224, 224, 224, 0.2); box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">Arduino</span></div>
            </div>
         </div>
      </div>
   </div>
<div style="color: #878787; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">
   <div class="wsite-multicol" style="position: relative; direction: ltr;">
      <div class="wsite-multicol-table-wrap" style="margin: 0px -15px;">
         <table class="wsite-multicol-table" style="position: relative; table-layout: fixed; width: 880px; margin: 0px !important; border-width: 0px !important; border-style: initial !important; border-color: initial !important; padding: 0px !important;">
            <tbody class="wsite-multicol-tbody">
               <tr class="wsite-multicol-tr">
                  <td class="wsite-multicol-col" style="vertical-align: top; padding: 0px 15px; width: 410px; margin: 0px !important; border-width: 0px !important; border-style: initial !important; border-color: initial !important;">
                     <div class="paragraph" style="overflow-wrap: break-word; font-size: 1em; line-height: 24px !important; margin: 0px; padding: 0.5em 0px; color: #2a2a2a !important; text-align: justify;"><font size="3">As soon as you hit the assigned IP in your browser you will see the page displaying battery status and a button. Press this button to get the updated temperature when ever you want.<br /><strong>Note</strong>: Nodemcu and your client mobile or desktop on which you want to view&nbsp;the voltage must be connected to same WiFi. If your server-nodemcu and client-mobile or computer, laptop etc are connected to different networks you can not view any thing after hitting the IP.</font></div>
                     </td>
                  <td class="wsite-multicol-col" style="vertical-align: top; padding: 0px 15px; width: 410px; margin: 0px !important; border-width: 0px !important; border-style: initial !important; border-color: initial !important;">
                     <div class="wsite-image wsite-image-border-none " style="position: relative; padding-top: 10px; padding-bottom: 10px; margin-left: 0px; margin-right: 0px; text-align: center;"><a href="https://www.microcontroller-project.com/uploads/2/2/1/5/22159166/battery-voltage-monitor-over-wifi_orig.png" class="w-fancybox" style="text-decoration-line: none; color: #008000;" target="_blank"><img src="https://www.microcontroller-project.com/uploads/2/2/1/5/22159166/battery-voltage-monitor-over-wifi_orig.png" alt="battery voltage monitor over WiFi" style="border-width: 0px !important; border-style: initial; border-color: initial; border-image: initial; width: auto; max-width: 100%;" /></a>
                        <div style="font-size: 11.7px;">battery voltage monitor over WiFi</div>
                        </div>
                     </td>
                  </tr>
               </tbody>
            </table>
         </div>
      </div>
   </div>
<div class="paragraph" style="overflow-wrap: break-word; font-size: 13px; margin: 0px; padding: 0.5em 0px; font-family: Arial, Helvetica, sans-serif; text-align: justify; line-height: 24px !important; color: #2a2a2a !important;"><font size="3">This is a base project. Further more developments can be made to project. Like data can be send to remote web site to be displayed real time on we pages. An alert can be generated when voltage is low. Leds or lcd can be connected to nodemcu for on spot status view.</font><br /></div>
<div class="paragraph" style="overflow-wrap: break-word; font-size: 13px; margin: 0px; padding: 0.5em 0px; font-family: Arial, Helvetica, sans-serif; line-height: 24px !important; color: #2a2a2a !important;"><strong>In order to learn about other ways to measure battery voltage take the below tutorial. Tutorial is about measuring batteries when they are connected in series and parallel combination.&nbsp;</strong></div>