===================== W A R N I N G ! ! =====================
Li-Ion batteries are very dangerous if not handled properly.
Use them only with proper protection circuits and chargers.
     Using any of the informations available on esp8266-projects.com
 is on your own risk !!

    For any new CBDBv2 Evo orders/requests please feel free to use as usual:
     tech at esp8266-projects.com.
     If you want for your own experiments to order CBD v2 EVO bare PCBs only, you can also do it directly at Dirty PCBs, our preferred PCB House:

As promised, today we will continue with our Live Battery Monitor System.

Subjects of the day:

  •  WebServer interface for direct access
  •  Live Data upload over the Internet for wordwide access on Thingspeak.com.

What we will need:

For schematics, theory of operation and technical related things please see previous article: Battery Live Monitor System

Live monitoring data, with 10 and 20ohm resistor Load:

BLMS Software
  For programming CBDBv2 Board and uploading the drivers and the software we will continue to use the LuaUploader as before. 1. Define used GPIO pins and variables:
ADC_SRC = 5                    -- GPIO14 - select Voltage Divider / Current Inputsda=2                                   -- GPIO4  - SDA
scl=1                                    -- GPIO5  - SCL  gpio.mode(ADC_SRC,gpio.OUTPUT, gpio.PULLUP) 
 gpio.write(ADC_SRC,1)      -- Voltage Measurement - Voltage Divider Source selected
gpio.write(ADC_SRC,0)      -- Current Measurement - Current Shunt Monitor output selectedvoltdiv= 0.00412                -- Voltage reading calibration
dival = 0.00096                  -- ADC volt/div value - CALIBRATE !!  
resdiv = 4.31447                -- Voltage Divider Ratio - CALIBRATE!! 
divalI = 0.9425                  -- Current volt/div ratio - CALIBRATE!!
cpct = 0                              -- Calculated Delivered Energy
adcI = 0                             -- ADC readings -  Curent
 adcV = 0                            -- ADC readings - Voltage
pwr = 0                              -- Calculate Power
t=0                                     -- time 
2. READ ADC Process function  and calculations 
function readUI() 
      print("\nVoltage     : " ..string.format("%g",adcV).." V")
      print("Current     : " ..string.format("%g",adcI).." mA")   --instantaneous Power consumption calculation 
    pwr = adcI*adcV
   print("Power       : " ..string.format("%g",pwr).." mW")
    -- total delivered Energy calculation
     if (adcV > 1) then 
      t = t +1 
       print("Energy      : " ..string.format("%g",cpct).." W")
  else  tmr.stop(0)
        dtime = t/360         -- calculate discharge time
         print("Discharge Time: %.2fh  ",dtime)
3. BMS WebServer
        conn:send("HTTP/1.1 200 OK\n\n")
        conn:send("<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"5\">")
        conn:send("<html><title>Battery Live Monitor System - ESP8266</title><body>")
        conn:send("<h1>Battery Live Monitor System - ESP8266</h1><BR>")
        conn:send("Voltage :<B><font color=red size=4>" 
                           ..string.format("%g",adcV).." V</font></b><br>")
        conn:send("Current :<B><font color=blue size=4>"
                          ..string.format("%g",adcI).." mA</font></b><br>")
        conn:send("Power   :<B><font color=red size=4>"
                         ..string.format("%g",pwr).." mW</font></b><br>")
        conn:send("Energy  :<B><font color=green size=4>"
                        ..string.format("%g",cpct).." Wh</font></b><br>")
        conn:send("<BR><BR><BR>Node.HEAP     : <b>" .. node.heap() .. "</b><BR>")
        conn:send("IP ADDR    :<b>".. wifi.sta.getip() .. "</b><BR>")
        conn:send("TMR.NOW    :<b>" .. tmr.now() .. "</b><BR<BR><BR>")
        conn:on("sent",function(conn) conn:close() end) 
        conn = nil     
4. Thingspeak.com data upload  
function sendDataTh()
    print("Sending data to thingspeak.com")
    conn=net.createConnection(net.TCP, 0)
        conn:on("receive", function(conn, payload) print(payload) end)
        conn:send("GET /update?key=YOURKEYHERE&field1="..adcV.."&field2="..adcI.."&
        conn:send("Host: api.thingspeak.com\r\n")
        conn:send("Accept: */*\r\n")
        conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
        conn:on("sent",function(conn) print("Closing connection")
    conn:on("disconnection", function(conn) print("Got disconnection...")

5. MAIN programIf you have a new ESP module or is the first time when using it on a new WIFI network, don’t forget to do the WIFI setup:

        -- One time ESP Setup --
         wifi.sta.config ( "YOUR_WIFI_SSID" , "PASSWORD" )  

— Read ADC data every 10s, print values on local Web interface and send them to thingspeak.com

   tmr.alarm(0, 10000, 1, function() readUI() sendDataTh() end)
   For testing, just save the code on ESP as ‘blmsweb.lua‘, restart ESP and run:   
           dofile("blmsweb.lua")      -- Start the Battery Live Monitoring System
             print(wifi.sta.getip())       --print your BMS WebServer IP address
                                                        BLMS – First RUN

  If you want the BLMS software to start automatically when your CBDBv2 Evo Board starts or reboots, then you neet to create and add some lines in your ‘init.luafile:

dofile("blmsweb.lua")      -- Start the Battery Live Monitoring System

Save the code on ESP as ‘init.lua‘, restart ESP. It should reboot and restart automatically the program.

Open your favorite Web browser and type your new BLMS Web Server IP address. If all ok, it should look like below :

               BLMS – Web Server interface – 20ohm Load

Thingspeak data upload

I will not insist on the Thingspeak.com account setup, it’s a pretty simple process:

  1. Sign Up for a ThingSpeak account
  2. Create a new Channel by going to your Channels page and clicking Create New Channel
  3. Update your Channel via URL:
  4. View your Channel feed:

Take a look at this Tutorial for more details for common devices and applications

Thingspeak.com live data uploaded – 10ohm/10W Load for stress test calibration
You can also see on graph below the moment when battery protection kicks in, around 2.4V, 2.37745 V in this case, to be more exactly:

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

battery monitor system

Thingspeak – SmartMon Ext Board P4

This is Part 4 of the SmartMon Extension Board series. SmartMon v2.7ex Board  is a extension board fully compatible with Arduino, ESP8266, ARM, PIC & other available MCu’s out there.  As long as your MCU/Dev Read more…

battery monitor system

Smart Mon Ext Board – Software example – P3

This is Part 3 of the SmartMon Extension Board series. SmartMon v2.7ex Board  is a extension board fully compatible with Arduino, ESP8266, ARM, PIC & other available MCu’s out there.  As long as your MCU/Dev Read more…

battery monitor system

Smart Mon Ext Board – RTC clock driver example – P2

Part 2 of the SmartMon v2.7ex series.UPDATE !! SmartMon v2.7Ex is available also on Tindie Store ! This time we are exploring the Real Time Clock  implementation and we will see how easy or complicated Read more…