IoT based Battery Monitoring System using NodeMCU and ThingSpeak

IoT based Battery Monitoring System using NodeMCU and ThingSpeak

Battery is the most important component for any device as it powers the whole system. And it is important to monitor the voltage level of the battery as improper charging and discharging of a lithium battery may lead to a big safety issue. There is a separate system in electric vehicles, called Battery Management System (BMS) which monitors all the properties of the battery pack like voltage, current, temperature, etc. and ensures the safety and handling of Lithium batteries.

Previously Battery Monitoring System only monitors the condition of the battery and alarms the user via battery indicator inside the vehicle. Due to the advancement in technology, now Internet of Things (IoT) can be used to notify the manufacturer and users remotely regarding the battery status. They can check the battery status of the car’s battery on their smartphones from anywhere in the world and this is considered as one of the maintenance support provided by the manufacturer.

So, in this project, we will also build an IoT based Battery Monitoring System where you can not only monitor the charging and discharging status of the battery but can also get an alert email when the battery is full or empty. Here we will use NodeMCU 12E to send the battery status data to ThingSpeak cloud and will use IFTTT to send the Email alerts on battery status.

 

Components Required:

  1. NodeMCU (ESP-12E)
  2. Lithium Polymer Battery
  3. LiPo Charger (TP4056)
  4. Resistors
  5. Any DC load like DC motors
  6. Jumper Wires

 

Calculating Battery Voltage

NodeMCU analog pin can withstand only 3.3v so we have to make an arrangement to convert high input voltage within the range of NodeMCU analog pin and then write code to compute the actual voltage being measured.

Now, the arrangement that will convert the voltage is Voltage divider circuit. The voltage divider decreases the voltage being measured within the range of the NodeMCU analog input which is 3.3v. We have to find the values of the required resistors for the circuit. The maximum input voltage will depend on the values of the resistors. So, I choose R1=44k and R2=10k (These are the printed value but actual value may be different so first measure the actual value using multimeter then use these values for further calculations). In my case, actual values are R1=47k and R2=9.5k

Voltage Divider Circuit

So by using the Voltage divider formula, the input voltage can be calculated as:

Vout (A0) = (VIN*R1) / (R1+R2)
Max Vin = Vout*(R1+R2) / R1

Consider Vout is 3.3v

After putting the resistor and Vout values Max Vin will be calculated 19v approx. But in practice, it is better to provide a voltage which is less than the max value so you can use 15v battery voltage safely.

 

Circuit Diagram

Circuit diagram for Battery Monitoring System is shown below. Output of the voltage divider circuit is given to the A0 pin of NodeMCU. A TP4056 charging module is used for charging the LiPo battery and a DC motor is used for discharging purpose which is connected to the output terminal of TP4056.

 IoT Battery Monitoring System Circuit Diagram using NodeMCU

 

IoT based Battery Monitoring Project Hardware Setup

 

Configuring ThingSpeak to plot Charging and Discharging Voltage

ThingSpeak provides a very good tool for IoT based projects. By using the ThingSpeak site, we can monitor our data and control our system over the Internet, using the Channels and webpages provided by ThingSpeak. ThingSpeak ‘Collects’ the data from the sensors, ‘Analyze and Visualize’ the data and ‘Acts’ by triggering a reaction.

Here we are briefly explaining to use ThingSpeak for this IoT Battery Monitoring Project. You can check various ThingSpeak based Iot projects here to learn more about it.

We will use ThingSpeak to monitor battery charging and discharging voltage. We will also use IFTTT platform to connect with ThingSpeak to send email/message alerts for a full or empty battery.

 

Step 1:- First of all, the user needs to Create an Account on ThingSpeak.com, then Sign In and click on Get Started.

 

Step 2:-  Now go to the ‘Channels’ menu and click on New Channel option on the same page for further process. You will see a form for creating the channel, fill in the Name and Description as per your choice. Then fill ‘Charging Voltage’, and ‘Discharging Voltage’ in Field 1 and Field 2 labels, tick the checkboxes for the Fields. Click on Save channel at bottom of the page. Now your new channel has been created.

ThingSpeak for IoT based Battery Monitoring System

 

Step 3:- Click on API Keys menu and copy the Write API key. This key will be used in the code to send the data on ThingSpeak.

ThingSpeak Configuration for IoT based Battery Monitoring System

 

Step 4:- Now, we will use ThingHTTP app of the server to trigger the IFTTT applet to send mail whenever the battery is full or empty. You can specify actions in ThingHTTP, which you want to trigger using other ThingSpeak apps such as React.

To make New ThingHTTP, we will need a URL for triggering which we can be obtained from IFTTT.

 

Configuring IFTTT for triggering Mail/SMS for Battery Monitoring

Here we are linking ThingSpeak with IFTTT via webhooks to automatically send the email when Battery is Full or Empty. To learn more about how IFTTT works, here are many tutorials and live IoT projects using IFTTT.

Step 1:- Login to IFTTT and search for Webhooks and click on it.

Configuring IFTTT for Triggering Mail

 

Step 2:- Click on Documentation.

Webhooks Setup for IoT based Battery Monitoring System

 

Step 3:- Type “bat_full” in the event box and copy the URL. This URL will be used in ThingHTTP.

Configuration to Trigger an Event

 

Now let’s make Applet to link ThingHTTP to Gmail to send an email. After that, we will jump to complete our ThingHTTP.

Step 4:- Click on New Applete in My Applete option.

IFTTT Configuration for IoT based Battery Monitoring System

 

 

Step 5:- Click on “+this” and search for Webhooks and click on it. Choose trigger as “Receive a web request”.

Now let’s make Applet to link ThingHTTP to Google sheet and to send email/SMS. After that, we will jump to complete our ThingHTTP.

 

Step 6:- Click on “+this” and search for Webhooks and click on it. Choose trigger as “Receive a web request”.

Trigger Setup for Battery Monitoring System

 

Step 7:- Type the Event Name which is the same as you write in the event box in webhooks URL. Click on Create Trigger.

 

Trigger Configuration for IoT based Battery Monitoring System

Step 8:- Click on “+that” and search for Gmail and click on it. Now, click on Send an email.

Email Configuration for IoT based Battery Monitoring System

 

Type the email addresses on which you wish to receive an email when there is an update regarding the battery.

IoT based Battery Monitoring System using NodeMCU

 

Type the body content you wish to send in the email and click on create action. Review it and finish.

IoT based Battery Monitoring System using NodeMCU

 

Similarly, make an applet for getting mail regarding battery empty alert.

We have made our applets to perform the tasks. Now, come back to ThingSpeak->Apps->ThingHTTP.

 

ThingHTTP for connecting ThingSpeak with IFTTT for Email Alerts

Step 1:- Click on New ThingHTTP. Give any name and Paste the URL that you copied from the webhooks documentation. Fill Remaining information as shown below.

ThingHTTP for connecting ThingSpeak with IFTTT for Email Alerts

In the same manner, we have to make ThingHTTP for “bat_empty”. Follow the same steps.

In URL, write bat_empty in place of bat_full. Body remains empty and all other information is same as in the previous ThingHTTP. Save it.

 

Now, we have to make React to trigger the URL.

React works with ThingHTTP app to perform actions when channel data meets a certain condition.

To make React, click on Apps -> React. Click on New React.

 

Step 2:- Give a name to your React. Condition type as Numeric and Test Frequency as on Data Insertion.

Choose the Condition on which you want to trigger the URL. Select your channel from the If Channel drop-down menu. Choose field 1 i.e. charging_volatge and make a condition of greater than and equal to 4.1. As shown

ThingHTTP for connecting ThingSpeak with IFTTT for Email Alerts

 

Choose ThingHTTP from Action drop-down menu and select the bat_full.

Select “Run action each time condition is met” and click on Save React.

Similarly, make react for bat_empty.

We have done with all the Web-based work. Now, we will see the code for our NodeMCU.

 

Code Explanation:

Complete code with a working video for this IOT based Battery Monitoring System is given at the end of this tutorial, here we are explaining the code in detail.

  1. First include all the required libraries. Also, initialize SSID and password of your Wi-Fi network and write API key that we had copied from ThingSpeak platform.
#include <ESP8266WiFi.h>
String apiKey = "***********";     
const char* ssid =  "********";     
const char* pass =  "********"; 
const char* server = "api.thingspeak.com";
  1. Now declare and initialize resistors values and some other variables and constants.
float volt =0.0;
float temp=0.0;
float r1=47000.0;    
float r2=9700.0;      
int flag = 1;
  1. In void setup() function, initialize serial communication and check for the Wi-Fi connection .
void setup()
{
   Serial.begin(115200);     
   WiFi.begin(ssid, pass);
      while (WiFi.status() != WL_CONNECTED) 
     {
            delay(100);
            Serial.print("*");}
  1. In void loop() function, we will take readings from an analog pin and will convert this value to actual voltage value as shown below.
int analog value = analogRead(A0);
    temp = (analogvalue * 3.3) / 1024.0;
    volt = temp / (r2/(r1+r2));  

 

Now, we will use a flag variable to track the status of the battery whether the battery is full or empty then accordingly send the voltage data to the respective graph on the ThingSpeak. Initially, we have taken the flag as 1 which means the battery is charging.

if (volt >= 4.1){
    flag=0;
    }
    if (volt <= 3.2){
      flag = 1;
      }

 

For sending voltage data to check for the flag and upload the data in the respective graph.

if(flag == 1) {
    if (client.connect(server,80))        { 
       String sendData = apiKey+"&field1="+String(volt)+"\r\n\r\n"; 
       client.print("POST /update HTTP/1.1\n");
       client.print("Host: api.thingspeak.com\n");
..
       }
Similarly for field 2.
Else if (flag == 0){
    if (client.connect(server,80))        { 
       String sendData = apiKey+"&field2="+String(volt)+"\r\n\r\n"; 
       client.print("POST /update HTTP/1.1\n");
       client.print("Host: api.thingspeak.com\n");
..

Reading will be updated every 10 seconds. For this, delay or timer function can be used. Here I am using the delay function.

Now, the coding part is done. Full code is given at the end of the tutorial. Upload this code to the NodeMCU and make sure to enter the credentials correctly.

You can check the reading on the serial monitor and ThingSpeak as shown below.

Readings of IoT based Battery Monitoring System

Graphical Readings of IoT based Battery Monitoring System using NodeMCU

The charging and discharging curves on ThingSpeak are shown in the above figure. The left figure shows the curve during charging and the right one shows the curve during the discharging of Lithium battery.

Now, whenever the battery is full or empty, the user will get an email notifying that battery is FULL or EMPTY.

Battery Status alert for IoT based Battery Monitoring System

 

So this is how a Battery voltage status can be monitored from anywhere in the world using ThingSpeak cloud and NodeMCU Wi-Fi module.

Also, check the video below.

Code

#include <ESP8266WiFi.h>

String apiKey = "************";     
const char* ssid =  "awesome";     // Enter your WiFi Network's SSID
const char* pass =  "awesome12"; // Enter your WiFi Network's Password
const char* server = "api.thingspeak.com";

WiFiClient client;
 
float volt =0.0;
float temp=0.0;
float r1=47000.0;    //r1 value 47k 
float r2=9700.0;      //r2 value 9.7 k
int flag = 1;
void setup()
{
   Serial.begin(115200);     
   Serial.println("Connecting to ");
   Serial.println(ssid);
   WiFi.begin(ssid, pass);
 
      while (WiFi.status() != WL_CONNECTED) 
     {
            delay(100);
            Serial.print("*");
     }
      Serial.println("");
      Serial.println("WiFi connected");
}

void loop()
{
    int analogvalue = analogRead(A0);
    temp = (analogvalue * 3.3) / 1024.0;       
volt = temp / (r2/(r1+r2));
if (volt < 0.1) 
   {
     volt=0.0;
   } 
   if (volt >= 4.1){
    flag=0;
    }
    if (volt <= 3.2){
      flag = 1;
      }
       Serial.print("Voltage: ");
       Serial.println(volt);
   if(volt>=3.2 && volt<=4.2 && flag == 1) {
    if (client.connect(server,80))         { 
       
       String sendData = apiKey+"&field1="+String(volt)+"\r\n\r\n"; 
       client.print("POST /update HTTP/1.1\n");
       client.print("Host: api.thingspeak.com\n");
       client.print("Connection: close\n");
       client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
       client.print("Content-Type: application/x-www-form-urlencoded\n");
       client.print("Content-Length: ");
       client.print(sendData.length());
       client.print("\n\n");
       client.print(sendData);

       Serial.println("%. Connecting to Thingspeak.");
       }
      
      client.stop();
       Serial.println("Sending....");
   }
 else if(volt>=3.2 && volt < 4.2 && flag == 0){
    if (client.connect(server,80))   //   "184.106.153.149" or api.thingspeak.com
      { 
     
       String sendData = apiKey+"&field2="+String(volt)+"\r\n\r\n"; 
       client.print("POST /update HTTP/1.1\n");
       client.print("Host: api.thingspeak.com\n");
       client.print("Connection: close\n");
       client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
       client.print("Content-Type: application/x-www-form-urlencoded\n");
       client.print("Content-Length: ");
       client.print(sendData.length());
       client.print("\n\n");
       client.print(sendData);
     
       Serial.println("Connecting to Thingspeak.");
       }
      
      client.stop();
      Serial.println("Sending....");
 }
 delay(10000);
}
 

Video

631 Comments

I am also writing to let you know of the terrific encounter my cousin's girl went through using your webblog. She mastered several pieces, which included how it is like to possess an ideal giving mindset to have men and women clearly gain knowledge of a variety of tortuous things. You really exceeded readers' expectations. I appreciate you for churning out these practical, safe, revealing and in addition unique tips on that topic to Ethel.

Thank you so much for providing individuals with remarkably nice chance to discover important secrets from this blog. It's usually so nice and also full of amusement for me personally and my office colleagues to visit your site nearly three times in 7 days to see the latest guidance you have. And lastly, we're actually happy concerning the staggering solutions served by you. Some 4 tips in this posting are in truth the most effective I have ever had.

I must voice my gratitude for your kindness giving support to men who actually need help on in this topic. Your real dedication to getting the solution all-around turned out to be wonderfully practical and have always empowered regular people much like me to reach their ambitions. This useful guidelines indicates a whole lot to me and extremely more to my office colleagues. Many thanks; from all of us.

I must show some appreciation to the writer just for rescuing me from this type of situation. After searching through the the net and getting solutions which are not helpful, I figured my entire life was over. Existing minus the solutions to the difficulties you've solved by way of your site is a serious case, and the kind that might have adversely damaged my career if I hadn't discovered your blog post. Your actual mastery and kindness in dealing with all areas was vital. I'm not sure what I would've done if I hadn't come upon such a step like this. I can at this moment look ahead to my future. Thank you so much for the impressive and amazing guide. I will not think twice to suggest your web site to anybody who needs and wants guide about this situation.

I wanted to put you this bit of observation just to thank you the moment again considering the fantastic ideas you have shared at this time. It has been particularly generous of people like you to convey unreservedly what exactly a number of us might have made available for an e book in order to make some cash for their own end, notably now that you could possibly have done it if you desired. The ideas as well acted as the easy way to realize that other people have the identical interest similar to my personal own to know very much more in terms of this matter. I know there are numerous more enjoyable occasions up front for many who browse through your blog.

I have to express some appreciation to the writer just for rescuing me from this type of predicament. Because of surfing around through the the web and finding ways which are not productive, I figured my life was over. Existing devoid of the strategies to the difficulties you have solved by way of your good guide is a serious case, and ones that would have in a wrong way damaged my entire career if I had not encountered your web page. Your own knowledge and kindness in dealing with all the pieces was important. I'm not sure what I would have done if I had not come upon such a subject like this. It's possible to at this time relish my future. Thanks a lot very much for your skilled and result oriented guide. I won't think twice to propose your web site to anyone who should get support on this area.

I am just writing to make you understand what a helpful experience my wife's daughter went through reading your web site. She discovered a lot of pieces, with the inclusion of how it is like to possess an excellent giving style to make the rest very easily gain knowledge of certain grueling subject matter. You actually did more than readers' expected results. I appreciate you for supplying those priceless, trustworthy, revealing as well as unique tips on this topic to Lizeth.

I as well as my pals were actually studying the great procedures located on your website and so unexpectedly came up with a terrible suspicion I had not thanked the website owner for those strategies. Most of the guys were definitely totally excited to study them and already have truly been making the most of these things. Appreciation for actually being so kind and also for making a decision on some smart areas millions of individuals are really wanting to know about. My personal sincere apologies for not expressing gratitude to you sooner.

Needed to post you that very little word to finally say thanks again for all the marvelous suggestions you have featured on this website. It has been simply generous of you giving publicly all that a number of people could possibly have supplied as an ebook to generate some money for themselves, chiefly since you could possibly have done it if you wanted. The concepts in addition acted to become good way to be sure that some people have the same fervor much like my personal own to figure out very much more on the topic of this condition. I'm sure there are many more enjoyable occasions in the future for folks who examine your blog.

Undeniably imagine that which you stated. Your favourite reason appeared to be on the internet the easiest thing to bear in mind of. I say to you, I certainly get annoyed while folks think about concerns that they plainly do not realize about. You managed to hit the nail upon the highest and also defined out the whole thing without having side effect , folks could take a signal. Will likely be back to get more. Thank you|

I must point out my gratitude for your kindness giving support to people that need help on this one theme. Your very own dedication to passing the message all over appears to be surprisingly valuable and have always allowed many people like me to arrive at their objectives. The helpful publication indicates much to me and even further to my office workers. Many thanks; from each one of us.

I must express some thanks to you just for rescuing me from this type of predicament. As a result of looking through the search engines and seeing notions that were not powerful, I assumed my entire life was well over. Being alive minus the solutions to the issues you've solved by way of your good short article is a serious case, as well as the ones which could have badly affected my entire career if I hadn't come across your site. Your own capability and kindness in playing with all the pieces was crucial. I am not sure what I would have done if I had not come across such a stuff like this. I am able to at this moment look forward to my future. Thanks very much for the reliable and results-oriented help. I will not hesitate to recommend your web site to anybody who should have support about this subject matter.

I truly wanted to develop a brief message to be able to express gratitude to you for those precious tactics you are posting at this website. My time-consuming internet search has at the end been paid with good facts and techniques to share with my co-workers. I would tell you that we readers are very lucky to dwell in a decent site with many awesome professionals with great opinions. I feel truly lucky to have come across the web page and look forward to some more fun times reading here. Thank you once again for all the details.

I in addition to my friends were actually checking the great recommendations from your web site while unexpectedly I had a horrible feeling I had not expressed respect to the web blog owner for them. These young boys were so joyful to read all of them and have now clearly been loving those things. Appreciation for really being well considerate and for getting some superior information millions of individuals are really eager to be informed on. My personal honest regret for not expressing appreciation to earlier.

I am writing to let you know of the amazing experience my friend's daughter obtained reading your blog. She figured out a wide variety of things, with the inclusion of how it is like to have a marvelous giving nature to let the mediocre ones with no trouble gain knowledge of various very confusing things. You actually surpassed our desires. Thank you for providing these precious, dependable, explanatory and as well as unique thoughts on your topic to Emily.

I definitely wanted to write down a quick comment in order to appreciate you for those stunning information you are showing on this website. My prolonged internet look up has at the end been paid with good quality ideas to talk about with my family. I 'd point out that most of us website visitors actually are very endowed to dwell in a very good site with so many awesome individuals with useful techniques. I feel rather fortunate to have discovered your entire website and look forward to many more fun minutes reading here. Thank you once again for a lot of things.

I wanted to jot down a simple message to be able to appreciate you for some of the fantastic recommendations you are posting on this site. My particularly long internet search has now been rewarded with good quality content to share with my close friends. I would state that that many of us readers actually are unequivocally lucky to be in a superb place with very many lovely people with interesting tips and hints. I feel very much grateful to have discovered your web page and look forward to really more fabulous moments reading here. Thank you once more for everything.

I not to mention my friends happened to be taking note of the excellent hints located on your web page while then I got a terrible feeling I had not thanked the blog owner for those strategies. The ladies were thrilled to read them and now have sincerely been taking advantage of them. Appreciation for getting indeed considerate and also for having some awesome guides most people are really needing to discover. My very own honest regret for not expressing appreciation to you sooner.

I would like to show my affection for your generosity in support of persons that really need help with this concept. Your real dedication to passing the message up and down appears to be incredibly informative and have regularly empowered ladies like me to achieve their ambitions. Your entire important key points can mean a whole lot a person like me and far more to my office colleagues. Many thanks; from all of us.

I actually wanted to post a simple remark so as to thank you for some of the superb guides you are posting at this website. My considerable internet lookup has now been rewarded with good quality tips to exchange with my classmates and friends. I 'd admit that most of us readers actually are very much endowed to be in a fine community with so many outstanding people with helpful opinions. I feel really happy to have come across the website page and look forward to really more enjoyable minutes reading here. Thank you once again for all the details.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.