Wi-Fi based Smart Energy Meter using Arduino

Interfacing of ESP8266 with Smart Energy Meter using Arduino

In this project, we are going to discuss that how to design a Wi-Fi based Smart Energy Meter using Arduino. Major focus of this article is towards the interfacing setup of Wi-Fi module (ESP8266) with Smart Energy Meter.

I have already written a comprehensive article on how to design a Smart Energy Meter here.

You can also study an article on how to design a 3 phase Smart Energy Meter using Arduino here.

The above articles will give you step by step details towards the designing of Smart Energy Meter. Here in this article the focus is on how to connect some Wi-Fi module with the above designed Smart Energy Meters.

So, let’s start with some important basics of Wi-Fi module.

What is ESP8266 module?

It is an open source Wi-Fi module which enables engineers and innovators to use this technology easily. Due to its open source hardware and software it is getting famous among engineers. Mostly IoT projects use this Wi-Fi module to connect themselves with the Internet.

Hardware of ESP8266

As ESP8266 required 3.3 volt to operate that’s why you will need separate voltage regulator for Wi-Fi module. The connection of the voltage regular will be as shown in the image above. This is the complete connections of ESP8266 module.

ESP8266 Connection Diagram
Figure 1: ESP8266 Connection Diagram

There are two push buttons present here. These push buttons are useful in long term application. If you don’t want to connect these buttons then it is your choice.

RST Push button is for hardware Reset of ESP8266. On the other hand, FLASH Push button is to update the software of the Wi-Fi module.

How to start with ESP8266 Module?

The quickest and easiest way to get started with an ESP8266 is to access it via the AT command interface. Wi-Fi module ESP8266 has a built-in UART (Serial) connection. This means that it can both send and receive data using the UART protocol.

What if we had an application that run on the ESP8266 which took “instructions” received over the serial link, executed them and then returned a response? This would then allow us to use the ESP8266 without ever having to know the programming languages that are native to the device.

This is exactly what a program that has so far been found to be pre-installed on the ESP8266. The program is called the “AT command processor” named after the format of the commands sent through the serial link. These commands are all prefixed with “AT”.

These AT Commands will control all the data that should be sent or received through the Wi-Fi module.

How to use ThingSpeak with Smart Energy Meter?

We have used an already available IOT platform called “ThingSpeak” to store the readings from micro-controller in the form of graphs.

The complete procedure is divided into two sections:

  • Signing up for ThingSpeak and connecting esp8266 Wi-Fi module with Wi-Fi
  • Sending meter readings to ThingSpeak

Creating account on ThingSpeak and connecting esp8266 Wi-Fi module with Wi-Fi

Step 1

The first step is to create an account on ThingSpeak. Visit ThingSpeak and create your account.

Sign in Form of ThingSpeak
Figure 2: Sign in to ThingSpeak

After signing up, create a channel as shown in figure below: Arduino will send the data.

Go to my channels of ThingSpeak
Figure 3: Go to My Channels

After clicking on Green New Channel button, they will ask about the requirements. Fill the fields as you want like shown below:

Field form of ThingSpeak
Figure 4: Field form of ThingSpeak

After that open API Keys tab and save your write keys to use later in the Arduino Code. Arduino will send measured readings to the ThingSpeak using this API write key.

API Key Tab of ThingSpeak
Figure 5: API Key Tab of ThingSpeak

Step 2

Now the second step is to connect ESP8266 with Wi-Fi router. ESP8266, in its default configuration, boots up into the serial modem mode. In this mode you can communicate with it using a set of AT commands. These AT commands are then used to transfer data to the ThingSpeak channel. Below are the Arduino commands used to connect esp8266 with Wi-Fi.

In second step we will also include a Softwarserial library in Arduino code. This library is to use Arduino pins as RX and TX other than the default RX, TX pins (pin 0 and 1).

The following line will serve the above purpose:

#include <SoftwareSerial.h> SoftwareSerial esp8266(Rx pin number, Tx pin number);

Step 3

Next step is to declare your Wi-Fi router related information like ssid, password and ThingSpeak’s ip address

#define SSID “your Wi-Fi ssid” #define PASS “your Wi-Fi password” #define IP “”

Step 4

In the next step create a serial port for communicating with esp8266 using below command

esp8266.begin (9600);

Here 9600 is the baud rate choose for esp8266 module.

Step 5

First, check if the module is working properly using below command

esp8266.println ("AT");

If Serial monitor shows “OK” as the response to this command, it means the esp8266 module is working properly.

Step 6

In next step, use AT commands to connect esp8266 module with Wi-Fi.

esp8266.println ("AT+CWMODE=1");
String cmd="AT+CWJAP=\"";

The first line of code “AT+CWMODE=1” sets the esp8266 module in client mode. ESP8266 module has 3 modes named as client mode, host mode and dual mode. We are using our Wi-Fi module as client mode.

Second line declares a new String variable. Line 2, 3, 4, 5 and 6 when combined are used to connect ESP8266 module with Wi-Fi router. The whole command is AT+CWJAP=SSID,PASS;

As we have already declared SSID and PASS variables at the start of code, so these values will be replaced here. To observe if ESP8266 module connects to your Wi-Fi router properly, open the Serial monitor and select baud rate 9600. Type this command in the Command box “AT+CWJAP=SSID,PASS”.

If ESP8266 responds with “OK” it means you have written the command correctly and Wi-Fi module has been connected to your Wi-Fi router.

Sending Smart Energy Meter readings to ThingSpeak

Now as the ESP8266 module have already connected to Wi-Fi router. Use below commands to transfer readings to ThingSpeak channel which you had created.

Command to Start the Connection

String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP; cmd += "\",80";

AT+CIPSTART command helps to to start the connection as a client. The whole command generally is “AT+CIPSTART=type,addr,port”. Our lines of code 1, 2 and 3 are such as “AT+CIPSTART=TCP,IP,80”.

After establishing a connection as client, ESP8266 module is ready to send data to ThingSpeak. To do so, use below lines of code.

Command to Send the Data

String cmd = "GET /update?key=Your channel’s API key";
cmd += "&field1=";
cmd += The value you want in first Field/Graph;
cmd += "&field2=";
cmd += The value you want in second Field/Graph;
cmd += "\r\n";

Note: To get the Channel’s API key by logging into the ThingSpeak account. Go to API key tab and copy the Write API key value.

To send values to ThingSpeak channel the whole line of code is “GET /update?key=Your channel’s API key“&field1= The value you want in first graph“&field2= The value you want in second graph”

Commands to Define Length of Data

We have broken down this whole line to various sections in order to be able to put variable values in field 1 and field 2. It is also necessary to send the length of command you are about to send before sending the actual command. Following line is helping to do the purpose:

esp8266.println (cmd.length());

After defining the length of command, we send the actual command to ThingSpeak channel using

esp8266.print (cmd);

You can view the live readings which are updating on ThingSpeak from the homepage of your account.

ThingSpeak Setup with Wi-Fi and Arduino
Figure 6: ThingSpeak Setup with Wi-Fi and Arduino

I think this is enough discussion of ThingSpeak and Wi-Fi module ESP8266.

If you want to add your opinion then feel free to comment below and don’t forget to like our facebook page.

2 thoughts on “Wi-Fi based Smart Energy Meter using Arduino”

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.