In this post we are going to learn how to construct an IoT based energy monitoring system using ESP8266 NodeMCU and Arduino IDE. We will understand several technical aspects of the proposed project such as, how we are going to send data to a cloud service and access data on your browser in real time with graphical representation.
We will see:
- Block diagram of IoT based energy monitoring system.
- Advantages and disadvantages of the proposed project.
- Full circuit diagram.
- Program code for the energy monitoring system.
- Prototype images.
- Output on Thingspeak cloud.
Block diagram of IoT based energy monitoring system:
Explanation of IoT based energy monitoring system block diagram:
The above image illustrates the block diagram of IoT based energy monitoring system and the circuit we are going to develop will be based on the above blocks.
- The brain of the project is an ESP8266 NodeMCU; it has built-in internet access capability and supports several peripheral communication protocols.
- A load is connected to a voltage and current sensor module and the load has an external power supply which is separate from 5V supply that is applied for functioning of the measuring circuit.
- An I2C LCD adapter module is used for driving a 16 x 2 display which shows real-time voltage, current and power measurements.
- The NodeMCU is connected to internet and sends the measured data to a server called Thingspeak where the data is stored for future analysis.
- The real time data can be accessed on a PC / laptop / smartphone using any browser.
Advantages of IoT based energy monitoring system:
- Real time energy monitoring from anywhere in the world. Since the project is connected to internet and as long as you have access to internet, you can read the data in real time anywhere.
- Real-time energy consumption data feedback to the power plant to efficiently generate power. In real life power plants need to adjust the production of power as per consumer’s demand in real-time. If electric power companies produce less power than the demand, blackouts will occur and if they produce more power than the demand, voltage on the grid will rise to dangerous levels and protection systems will kick-in and blackout will occur (also here). With real time data the power companies can manage the power generation efficiently with zero to minimal blackouts.
- No need for manual labor for collecting monthly power consumption data on individual houses or industries and power generation companies can bill the customers directly.
- Individual consumers can keep track of their energy consumption and may able to cut down power consumption to save on the bills.
Disadvantages of IoT based energy monitoring system:
- Privacy and data concerns. New technologies not only helpful for the society but also brings new opportunities for tech frauds, scammers and hackers. The data they harvest illegally can be used against you for their financial gain.
- Maintaining the software and hardware will cost the companies.
- Initial replacement cost of replacing old energy meters with new one will be incurred by the consumers and also power companies.
The proposed circuit diagram consists of the components presented in the block diagram. Now let’s look into each of the components in detail.
The heart of the project is an ESP8266 NodeMCU which drives the LCD display, communicates with INA219 (current and voltage sensor) and also connects to internet via Wi-Fi.
We need to power the NodeMCU via micro USB port and we are also pulling out 5V DC from NodeMCU to power INA219 and LCD module.
We are using NodeMCU’s I2C peripheral which is D1 (SCL) and D2 (SDA) pins for controlling the LCD display and also for extracting data from INA219 module.
There are two on-board buttons on NodeMCU called flash and reset which we will be using when we are programming the board.
NOTE: The 5V output for INA219 and LCD are available at VU pin and GND for Lolin NodeMCU, please refer the pin diagram of your version of NodeMCU to know 5V output pin.
INA219 voltage and current sensor module:
INA219 is a module that can measure voltage and current. Let’s see the technical specification of this module:
- Input supply (Vcc) 3.0V to 5.5V.
- Vin+ and Vin- are measuring terminals.
- Voltage measurement up to 26V.
- Current measurement up to 3.2A in both directions.
- Shunt resistor: 0.1 ohm.
- Supported communication protocol: I2C.
The module is connected to NodeMCU via D1 and D2 pins. 5V to this module is supplied from NodeMCU’s 5V output pin.
Vin+ and Vin- pins measure current through a load as well as voltage across the load. Please follow the circuit diagram carefully and don’t reverse the load polarity or external supply’s polarity.
There are two choices to connect the load to INA219 module, if you are using a breadboard you may connect the load to Vin+ and Vin- terminals situated alongside SDA and SCL pins and if your load consumes more than 500mA use the screw terminals.
I2C adapter and LCD display:
The proposed project utilizes I2C adapter for 16×2 LCD display, this will reduce the number of wires that connects from NodeMCU to LCD to four: Vcc, GND, SCL and SDA.
The module is connected to same SDA and SCL bus as INA219 module and 5V is used for powering the I2C adapter module. You can adjust the contrast of the display using the provided trim pot on the module.
NOTE: Please enter your Wi-Fi credentials here:
Please enter Thingspeak credentials here:
Installing libraries to Arduino IDE:
The project was developed using Arduino IDE and we need to download and install some library files and board packages so that the code can compile successfully.
1) Installing ESP8266 board package:
You need to install ESP8266 board package so that ES8266 based boards get compatible with Arduino IDE. Please skip this step if have already installed ESP8266 board package.
- Go to Files > Preferences and past this URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
- Now, go to Tools > Board > Boards Manager and enter “ESP8266” and click install. Wait for few minutes to get the packages downloaded and install.
2) Installing library files INA219:
Go to Sketch > Include Library > Manage libraries and enter INA219. Select Adafruit INA219 and select version 1.1.0 or latest version and press install.
3) Installing I2C library file.
Go to Sketch > Include library > Add .Zip file and brows the downloaded Zip file.
I2C Library File: Click here
How to uploading code to NodeMCU:
Press and hold the flash button and press rest button momentarily once and leave the flash button. This will enable the NodeMCU to accept a new program.
Press upload button on the IDE and you will see the below:
Setting up your Thingspeak channel:
- Signup for a Thingspeak account if you don’t have one and log in to do the following changes to your channel settings.
NOTE: Please take note of your channel ID, which need to be entered in the code.
API Write Key:
You need to copy your “Write API key” and paste it in the code. Write API key is responsible for writing data you channel.
- In the prototype we have used a light bulb as load, you may use any DC load up to 26V and 3A maximum.
- The circuit needs an active Wi-Fi connection for its normal operation and once it established connection to the Wi-Fi network that you have entered in the code, it will show the measurements as illustrated below:
- The “Power” is the instantaneous power consumption of the load in milliwatt (mW), current (I) is in mA and voltage (V) is in volt.
Output on Thingspeak:
How to setup analog gauge meter on Thingspeak:
This is a completely optional step, if you want to show the analog meters on your “Private view” screen.
- Click on “Private view” tab and click “Add widgets”
- The shown below screen will pop-up:
- Select the analog gauge widget and click next and fill the following form shown below and press create.
If you have any questions regarding this project, feel free to ask us in the comment section and you will get a guaranteed reply for us.