ESP8266 Projects Blog
Home of CBDB / MPDMv4 /SmartMon Development boards (ESP-12/ESP-07).
Guidelines and ESP8266 programming examples using LUA, Eclipse and Arduino IDE, ESP Basic and many more!
As you might read already in my previous ESP8266 Battery Monitor System post I was using Thingspeak.com, because of it's flexibility, as a logger for live data uploading.
In the latest week I was exploring deeper Thigspeak capabilities.
What I was interested for was to check the possibility to build/configure and use a standalone Thingspeak Server that can run independently of Internet infrastructure on a small footprint, low power consumption platform.
The interesting part regarding the Thingspeak Server deployment is the chosen platform, the new RASPBERRY PI 2 Board!
I will present you below the step-by-step process to install your own
RPI Thingspeak Server that can be tailored on your own needs and
ofcourse, not affected by Internet connection availability and uptime if
installed in the same physical location/LAN as your ESP8266 BMS or any sensor grid/array you might want to use with!
INSTALLATION PROCESS:
1. Install a Raspberry Pi Operating System image on SD card
4. MySQL Database configuration pi@RPIMON1~$mysql --user=root mysql -p useyourpasswd here pi@RPIMON1~$mysql> CREATE USER 'thing'@'localhost' IDENTIFIED BY 'speak’; pi@RPIMON1~$mysql> GRANT ALL PRIVILEGES ON *.* TO 'thing'@'localhost' WITH GRANT OPTION; pi@RPIMON1~$mysql> commit; pi@RPIMON1~$mysql> exit;
================== 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.
DO NOT OVER CHARGE / BURN / OPEN Li-Ion Bats
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: http://dirtypcbs.com/view.php?share=5876&accesskey=3d0fd70d53d6dc20c0bf0255f67cde65
------------------------------------------------------------------------------------------------------------------
function sendDataTh() print("Sending data to thingspeak.com") conn=net.createConnection(net.TCP, 0) conn:on("receive", function(conn, payload) print(payload) end) conn:connect(80,'184.106.153.149') conn:send("GET /update?key=YOURKEYHERE&field1="..adcV.."&field2="..adcI.."& field3="..pwr.."&field4="..cpct.."HTTP/1.1\r\n") 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:send("\r\n") conn:on("sent",function(conn) print("Closing connection") conn:close() end) conn:on("disconnection", function(conn) print("Got disconnection...") end) end
5. MAIN program
If 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.setmode(wifi.STATION) wifi.sta.config ( "YOUR_WIFI_SSID" , "PASSWORD" ) print(wifi.sta.getip())
-- 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.lua' file: 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:
One of the main problem in battery powered projects is to choose/use the proper battery size/model/type. As market is flooded now with a lot of low quality batteries claiming thousands of mAh ( Ultrafire fakes stories is just an example) the only way to proper check them is to run a set of tests.
A simple basic capacity test / loading / discharging time values can give you valuable informations about the parameters and health of the battery. And of course also you can see how are looking the numbers against the datasheet claims :).
Few general considerations that I want to bring to your attention:
The capacity of a battery tells you how much charge it can store. This means that they can theoretically provide a current of 2A
for one hour, 1A for two hours, or 200mA for ten hours. In reality a 2,000mAh battery would
not provide 20mA for 100 hours, nor 2A for one hour.
Discharge load - this is a tricky one and most of the time not clear explained by battery manufacturers in their datasheets. Mostly of them are using a discharge load of 0.1C, that means one-tenth of current capacity when rating their batteries. Therefore, a battery which can provide 200mA for 10 hours would be rated at 10x200 = 2,000mAh !!
A faster discharge rate would result in less energy being extracted from the battery, and
therefore the measured capacity would be lower. If you are using batteries in energy-hungry
devices that need more than 0.1C, the real capacity will be lower than that published.
A "good-to-have" add-in for a battery powered project is a system that will be able to monitor over the entire battery lifetime at least few parameters like, voltage, current, power consumption and stored energy between charges.
What we will need:
CBDB Board ( or any other ESP8266 Board you may like but who has voltage/current measurement capabilities)
20 Ω / 5W resistor (or 2 x 10 Ω /5 W in series - even better )
Connection wires - various colors
Schematics:
As the CBDBv2 Evo DevBoard has already Voltage and Current measurements capabilities onboard, the only thing that we will need to do is to connect the battery module that we want to measure to the board:
GREEN - V+
BLUE - GND
RED - SH_IN+
WHITE - SH_IN-
By default the CBDBv2 Evo Board comes prepared for Li-Ion batteries voltage range, with the onboard default voltage divider values and Rshunt you haveVmaxIN=4.3V and ImaxIN = 1A. If you need a different Voltage/Current range just change/ask for different default values.
CBDB v2 Evolution DevBoard with INA21x Current monitor
The Current shunt monitor used for this project is a INA21x family one. The INA210, INA211, INA212, INA213, INA214, and INA215 are voltage-output, current-shunt monitors that can sense drops across shunts at common-mode voltages from –0.3 V to 26 V, independent of the supply voltage. Five fixed gains are available: 50 V/V, 75 V/V, 100 V/V, 200 V/V, 500 V/V, or 1000 V/V. The low offset of the zero-drift architecture enables current sensing with maximum drops across the shunt as low as 10-mV full-scale.
These devices operate from a single 2.7-V to 26-V power supply, drawing a maximum of 100 μA of supply current. All versions are specified over the extended operating temperature range (–40°C to 125°C), and offered in an SC70 package. The INA210, INA213, and INA214 are also offered in a thin UQFN package.
INA21x Features: • Wide Common-Mode Range: –0.3 V to 26 V • Offset Voltage: ±35 μV (Max, INA210) (Enables Shunt Drops of 10-mV Full-Scale) • Accuracy: – ±1% Gain Error (Max over Temperature) – 0.5-μV/°C Offset Drift (Max) – 10-ppm/°C Gain Drift (Max) • Choice of Gains: – INA210: 200 V/V – INA211: 500 V/V – INA212: 1000 V/V – INA213: 50 V/V – INA214: 100 V/V • Quiescent Current: 100 μA (max)
for Current - add your favourite Current Monitor but don't forget about the VmaxIN for ESP8266 ADC of about 1V!
BLMS Software For programming CBDBv2 Board and uploading the drivers and the software we will continue to use the LuaUploader as before.
How to Measure Battery Capacity When measuring the real battery capacity what we are interested in is the amount of energy stored in a battery since it is this energy we need to power our devices. Stored energy is measured in Watt-hours - the same unit used to measure our domestic electricity consumption (where 1,000Wh = 1kWh = 1 unit of electricity).
In order to measure the stored energy in a battery a power resistor is used as the load,
and a fully charged battery is fully discharged through it. By
measuring the voltage across this resistor at regular intervals during
the discharge process it is simple to calcuate the total energy
dissipated and therefore the total energy which had been stored in the
battery.
Using Ohm's Law (I=U/R) we can then calculate the current flowing through the load
since we know the voltage across it. Instantaneous power is
given by multiplying the measured voltage by the calculated current (P=U*I).
By taking readings for a certain amount of time until the battery is completely discharged, and adding up the energy dissipated in each time interval, we can calculate the total energy
taken from the battery and dissipated in the resistor and therefore the
total energy that was stored in the fully charged battery.
Software Implementation 1. Define used GPIO pins and variables:
gpio.write(ADC_SRC,1) -- Voltage Measurement - Voltage Divider Source selected gpio.write(ADC_SRC,0) -- Current Measurement - Current Shunt Monitor output selected
voltdiv= 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
3. Number format function for proper LCD printing nr_format = function (fnr,unit) if (fnr > 1000) then fnri=fnr/1000 uniti=string.sub(unit, 2) nrf=string.format(" %.3f%s ",fnri, uniti) else if (fnr < 100) then if (fnr < 10) then nrf = string.format(" %.1f%s ",fnr, unit) else nrf = string.format(" %.1f%s ",fnr,unit) end else nrf = string.format("%.1f%s ",fnr,unit) end end return nrf end
4. LCD Print - > Voltage / Current / Energy / Power
For testing, just save the code on ESP as 'blms.lua', restart ESP and run: dofile("blms.lua") -- Start the Battery Live Monitoring System
Running BLMS program
If you want the BLMS software to start automatically when your CBDB module
starts or reboots, then you neet to create and add some lines in your
'init.lua' file: dofile("blms.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.
That's all for today, next time we will continue with Web Interface and online logging system for the BLMS!
WARNING!! You will play with LIVE MAINS!! Deadly zone!!
If you don't
have any experience and are not qualified for working with MAINS power I
will not encourage you to play arround!. The
author take no responsibility for any injury or
death resulting, directly or indirectly, from your inability to
appreciate the hazards of household mains voltages.
The circuit diagrams are as accurately as possible, but are offered with no
guarantees whatsoever.
There is no guarantee that this design meets any Rules which may be in force in your country so please check before your local rules/regulations.
For any new requests please feel free to use as usual: tech at esp8266-projects.com. If
you want to order MAINS POwer Dimmer/Switch bare PCBs only,
you can also do it directly at Dirty PCBs, our preferred PCB House:
MAINS Power Dimmer / Switch Module - MPDMv3 - Connections
CBDB Evolution DevBoard
Because I don't like to have flying MAINS wires on my workdesk I have created a temporary separate MPDMv3 Module Box, containing all the MAINS part of the story :).
Isolated MAINS unit - keep your fingers away from the Deadly zone !
Remember, Safety First! And also more relaxing knowing that is no Russian Roulette wire game on progress on my table. Shit happens all the time, but at least let's try to reduce the chances to hit badly the fan too often :)
Live Testing on progress
In the Box above is enough space for all the parts going inside, including ESP8266 module, power supply, Dimmer Module, Choke, etc, but at this stage I find it easier to connect them together like that.
MPDMv3 Web Server Software
For programming CBDBv2 Board and uploading the driver and the software we will continue to use the LuaUploader as before. 1. Define used GPIO pin:
outpin=7 -- Select Triac Command pin - GPIO13 gpio.mode(outpin,gpio.OUTPUT) gpio.write(outpin,gpio.LOW) -- Triac OFF inpin=6 -- Zero crossing detector input - GPIO12 gpio.mode(inpin,gpio.INT,gpio.PULLUP) -- attach interrupt to ZCD
2. Zero Cross Detector function and Triac command
For a more detailed explanation how Zero cross detection circuit works please take a look at the previous MPDMv3 article.
function zero_cross() dt = 76*dim --print("Zero cross detected!") stat = "ON" tmr.delay(dt) -- Firing delay time calculated above gpio.write(outpin,gpio.HIGH) -- Triac ON - Zero cross detected tmr.delay(100) -- Triac ON - Propagation time gpio.write(outpin,gpio.LOW) -- Triac OFF - let's be sure it's OFF before next cycle :) tmr.wdclr() return stat end
3. WEB Server
srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive", function(conn,payload) --debugging output only -- print(payload) if (string.find(payload, "GET / HTTP/1.1") ~= nil) then --print("GET received") sendPage(conn) else swstat={string.find(payload,"cmd=")} --If POST value exist, set power switch status if swstat[2]~=nil then --print("Command received: " .. payload) PwrSW(swstat,payload) sendPage(conn) end end end) conn:on("sent", function(conn) conn:close() conn = nil -- clear and allow the garbage collector to free the memory --print("Connection closed")
CSS file created for this example is just for fancy decorations. If you don't like it you can very easy change the style attributes or add local ones.
5.Check received Dimmer value and update status
function PwrSW(swstat,payload) gpio.mode(outpin,gpio.OUTPUT) newstat=string.sub(payload,swstat[2]+1,#payload) status = tonumber(newstat) print("Dimmer Value:" .. status) end
4. Main code
status = 60 -- around 50% - choose your desired start value. newstat = 60 outpin=7 -- Select Triac Command pin - GPIO13 gpio.mode(outpin,gpio.OUTPUT) gpio.write(outpin,gpio.LOW) -- Triac OFF inpin=6 -- Zero crossing detector input - GPIO12 gpio.mode(inpin,gpio.INT,gpio.PULLUP) -- attach interrupt to ZCD
gpio.trig(inpin,"up",zero_cross) -- ZCD interrupt attached - trigger on falling edge print(wifi.sta.getip()) -- print our new MPDMv3 WebServer IP
For testing, just save the code on ESP as 'dimserver.lua', restart ESP and run: dofile("dimserver.lua") -- Start the Dimmer Listening WebServer =wifi.sta.getip() -- find the IP Address where your Web Server will be
Open your favorite Web browser and type your new MPDMv3 Web Server IP address. If all ok, should look something like below :
MPDMv3 Web Interface
If you want the MPDMv3 software to start automatically when your CBDB module
starts or reboots, then you neet to create and add some lines in your
'init.lua' file: dofile("dimserver.lua") -- Start automatically the Dimmer Listening WebServer
Save the code on ESP as 'init.lua', restart ESP. It should reboot and restart automatically the program.
A short video presentation, testing the Web Interface: