
%% bare_jrnl.tex
%% V1.4
%% 2012/12/27
%% by Michael Shell
%% see http://www.michaelshell.org/
%% for current contact information.
%%
%% This is a skeleton file demonstrating the use of IEEEtran.cls
%% (requires IEEEtran.cls version 1.8 or later) with an IEEE journal paper.
%%
%% Support sites:
%% http://www.michaelshell.org/tex/ieeetran/
%% http://www.ctan.org/tex-archive/macros/latex/contrib/IEEEtran/
%% and
%% http://www.ieee.org/


\documentclass[journal]{IEEEtran}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern} % load a font with all the characters

\usepackage[style=numeric-comp,backend=biber,url=false]{biblatex}
\addbibresource{bibliography.bib}

\usepackage{cleveref}


% *** GRAPHICS RELATED PACKAGES ***
\usepackage[pdftex]{graphicx}
\graphicspath{{figures/}}
\DeclareGraphicsExtensions{.pdf,.jpg,.png}


% *** MATH PACKAGES ***
%
\usepackage[cmex10]{amsmath}
\interdisplaylinepenalty=2500

% correct bad hyphenation here
\hyphenation{}

% Glossaries
\usepackage[acronym,nopostdot,style=super,nonumberlist]{glossaries}
\makeglossaries
\loadglsentries[main]{glossaries}


\usepackage{gensymb}

\begin{document}
\boldmath

\title{Multi-purpose system for measuring electrical power supplied by electric sockets}

\author{Peter~Babič\\% <-this % stops a space
        Department of Theoretical and Industrial Electrical Engineering (DTIEE)\\%
        babicpet@gmail.com%
}

% The paper headers
\markboth{Paper covering Masters thesis, May~2016}%
{}


% make the title area
\maketitle


\begin{abstract}
The paper shows the process of designing, building and programming of an inter-connected electronic system. It starts with explaining the fundamentals of the physics underlining the electronic power measurement process. The main part includes diagrams describing the inner working of the hardware, and later software running on it. The manufactured device is capable of measuring the electric power provided by the electric socket to the appliance and send the measured values over Wi-Fi to the cloud, to be visualised on a custom web server employing a charting library to plot the measured quantities over time.
\end{abstract}

% Note that keywords are not normally used for peerreview papers.
\begin{IEEEkeywords}
electrical, power, socket, system
\end{IEEEkeywords}



\IEEEpeerreviewmaketitle



\section{Introduction}

\IEEEPARstart{T}{he} idea is to invent way of measuring the electrical power and some more related information, preferably in a non-invasive way. The non-invasive way means, that the appliance that is being measured does not require any modifications, for instance in a form of some probe or a man-in-the-middle plug, suggesting an embedded system. When the data are obtained, they are presented to the user, preferably plotted as a quantity over time, not just and actual measurement. Since the solution is going to be multi-purpose, it has to incorporate at least one additional function, than just the measurement. In this case it is going to be the remote power-on/power-off of the appliance. The name of the thesis also suggests, that the final solution has to be compatible with the electrical sockets used in the local region, in this case the European ones. Since the solution is going to be an \textit{embedded system} measuring a \textit{physical quantity}, these two topics are described in following chapters.


\section{Electric power fundamentals} \label{s:el_power}
In general physics terms, power is defined as the rate at which energy is transferred (or transformed). Electric energy in particular, begins as electric potential energy – what we commonly refer to as voltage. When electrons flow through that potential energy, it turns into electric energy. In most useful circuits, that electric energy transforms into some other form of energy. Electric power is measured by combining both how much electric energy is transferred, and how fast that transfer happens.

The electric power P is equal to the energy consumption E divided by the consumption time t \cite{meade2002foundations}
$$P = \frac Et$$
where P is the electric power in watt [W], E is the energy consumption in joule [J] and 
t is the time in seconds [s].

Electrical Power, in a circuit is the amount of energy that is absorbed or produced within the circuit. A source of energy such as a voltage will produce or deliver power while the connected load absorbs it. Light bulbs and heaters for example, absorb electrical power and convert it into heat or light. The higher their value or rating in watts the more power they will consume.

\subsection{Ohm's law}
Ohm's Law deals with the relationship between the voltage and the current in an ideal conductor. This relationship states that: the potential difference (voltage) across an ideal conductor is proportional to the current through it \cite{henry2008ohm}. The constant of proportionality is called the \textit{resistance}.
$$I = \frac U R $$
where I is the current expressed in amperes [A], U is the voltage, bearing the volt units [V] and R is the electrical resistance in ohms [\ohm].

The Ohms's law can be further expanded \cite{beaty1998electric}, to get these three quantities in relationship with \textbf{power}, such as
$$P = I \cdot U = I^2 \cdot R = \frac{U^2}R$$


\subsection{Direct current (DC) circuits}
Generally, Ohm's law is used on \gls{dc} circuits, containing a current of fixed magnitude (amplitude) and a definite direction associated with it. \acrlong{dc} is produced by power supplies, batteries, dynamos and solar cells to name a few.

We also know that \gls{dc} power supplies do not change their value with regards to time\cite{herman2012direct}, they are a constant value flowing in a continuous steady state direction. In other words, \gls{dc} maintains the same value for all times and a constant uni-directional DC supply never changes or becomes negative unless its connections are physically reversed.


\subsection{Waveforms and alternating current (AC) circuits} \label{ss:power_ac}
An alternating function or \gls{ac} waveform on the other hand is defined as one that varies in both magnitude and direction in more or less even manner with respect to time making it a “bi-directional” waveform \cite{whitaker2006ac}. An AC function can represent either a power source or a signal source with the shape of an AC waveform generally following that of a mathematical sinusoid as defined by
$$A(t) = A_{max} \cdot sin(2 \pi f t)$$

\begin{figure}[ht!]
\centering
\includegraphics[width=1\linewidth,angle=0]{waveforms}
\caption{The common types of waveforms visualised as a function of amplitude}\label{f:waveforms}
\end{figure}

The term AC or to give it its full description of Alternating Current, generally refers to a time-varying waveform with the most common of all being called a \textbf{Sinusoid} better known as a \textbf{Sinusoidal waveform}. Sinusoidal waveforms are more generally called by their short description as \textbf{Sine Waves}. Sine waves are by far one of the most important types of AC waveform used in electrical engineering.

This means then that the \gls{ac} waveform is a “time-dependent signal” with the most common type of time-dependant signal being that of the Periodic Waveform. The periodic or \gls{ac} waveform is the resulting product of a rotating electrical generator. Generally, the shape of any periodic waveform can be generated using a fundamental frequency and superimposing it with harmonic signals of varying frequencies and amplitudes but that is out of the waveform fundamentals theory.

Alternating voltages and currents can not be stored in batteries or cells like \gls{dc} can, it is much easier and cheaper to generate these quantities using alternators or waveform generators when they are needed. The type and shape of an AC waveform depends upon the generator or device producing them, but all \gls{ac} waveforms consist of a zero voltage line that divides the waveform into two symmetrical halves. The main characteristics of an \gls{ac} waveform \cite{nicolaides1996electrical} are defined as:

\begin{itemize}
\item \textbf{Period (T)} is the length of time in seconds that the waveform takes to repeat itself from start to finish. This can also be called the Periodic Time of the waveform for sine waves, or the Pulse Width for square waves
\item \textbf{Frequency} is the number of times the waveform repeats itself within a one second time period. Frequency is the reciprocal of the time period, defined as $f = \frac 1 T$, with the unit of frequency being the Hertz [Hz]
\item \textbf{Amplitude}  is the magnitude or intensity of the signal waveform 
\end{itemize}


\subsection{Power in AC circuits} \label{ss:ac_power}
When a reactance (either inductive or capacitive) is present in an \gls{ac} circuit, the Ohm's law formula does not apply and different approach must be taken to express and calculate power \cite{rawlins2000basic}.

\textbf{Real power} (or true power) is the power that is used to do the work on the load:
$$P = U_{RMS} \cdot I_{RMS} \cdot cos\,\varphi$$
where P is the real power in watts, $U_{RMS}$ is the \gls{rms} voltage, defined as $U_{peak}/\sqrt{2}$ in volts, $I_{RMS}$ is the RMS current, defined as $I_{peak}/\sqrt{2}$ in amperes and $\varphi$ is the impedance phase angle - phase difference between voltage and current.

\textbf{Reactive power} on the other hand, is the power that is wasted and not used to do work on the load. Curiously, it is defined as 
$$Q = U_{RMS} \cdot I_{RMS} \cdot sin\,\varphi$$
with $Q$ being the reactive power in volt-ampere-reactive [var].

\textbf{Apparent power} is the power that is supplied to the circuit. Definition:
$$S = U_{RMS} \cdot I_{RMS}$$
where the unit of apparent power $S$ is volt-ampere [VA]. It can be seen that it is not phase-angle dependent.

The relation all these three quantities are in is defined as 
$$ P^2 + Q^2 = S^2 $$
however, again, nothing in the real world is perfect, and this relation only applies for a perfectly \textbf{sinusoidal waveforms}!


\subsection{Phasor and phase shift}
A phasor\cite{2009electrical} is a constant complex number representing the complex amplitude (magnitude and phase) of a sinusoidal function of time. It is usually expressed in exponential form. Phasors are used in engineering to simplify computations involving sinusoids, where they can often reduce a differential equation problem to an algebraic one. The origin of the word phasor comes from phase + vector.

Phasor is a vector that represents a sinusoidally varying quantity, as a current or voltage, by means of a line rotating about a point in a plane, the magnitude of the quantity being proportional to the length of the line and the phase of the quantity being equal to the angle between the line and a reference line.

\begin{figure}[ht!]
\centering
\includegraphics[width=1\linewidth,angle=0]{phase_diff}
\caption{The phase difference between voltage (blue) and current (red), the origin of phase difference of angle $\varphi$}\label{f:ph_diff}
\end{figure}

Considering the figure \ref{f:ph_diff}, the voltage waveform above starts at zero along the horizontal reference axis, but at that same instant of time the current waveform is still negative in value and does not cross this reference axis until 30\degree  later. Then there exists a Phase difference between the two waveforms as the current cross the horizontal reference axis reaching its maximum peak and zero values after the voltage waveform.

As the two waveforms are no longer \textit{in-phase}, they must therefore be \textit{out-of-phase} by an amount determined by phi, $\varphi$. The waveform of the current can also be said to be \textit{lagging} behind the voltage waveform by the phase angle $\varphi$. This angle represents the phase shift (also called phase difference) between two sinusoids \cite{maxfield2011electrical}.


\subsection{Power factor and power factor correction}
The power factor is just a specific name for a phase shift between the sinusoids of a current and voltage. So the figure \ref{f:ph_diff} in fact shows the power factor. However, it is not expressed in a plane angle, but rather as a dimensionless number between -1 and 1.

The power factor is defined as $\frac{P}{S}$, as a ratio of the real power over the apparent power\cite{dixit2010electrical}. If $\varphi$ is the phase angle between the current and voltage, then the power factor is equal to the cosine of the angle, $cos\,\varphi$.

If the power factor is 1, it means that all the supplied power is completely consumed by purely resistive load. A positive power factor that is lower than 1 indicates that some power is not consumed by the load and is returned back. The lower the factor, the more power is returned. When power factor is equal to 0, the energy flow is entirely reactive, and stored energy in the load returns to the source on each cycle. A negative power factor means that the device, considered to be power load is in fact a power source (produces more power than consumes).

How can this information be useful? Every load with a power factor other than 1 returns some power back to the transmission line. Since the transmission lines does have some resistance, this returned power translates to some wasted power in a form of heat. Energetic companies want to minimise the power wasted in the transmission lines to increase their profit, so numerous laws are coming into effect to correct \cite{singh2008electric} (increase) the power factor.



%\subsection{Electric power measurement}
%Measuring the electric power makes most sense on the customer appliances. The first reason is, that they generally consume power that is purchased on contract. The energetic company measures all the power used up by the end customer, but customer has no easy way to see how much and how \textit{effectively} is power used by the appliances. The second important reason is that the appliances has a standardised connector (plug) that is guaranteed to fit in all the area using it, which is not a case for example on battery powered devices (batteries has different sizes, connectors and general properties.
%
%When it comes to measuring the electrical power, the first and the most important thing to discuss is safety. Only after all the safety precautions had been made clear, the theory can be clarified and subsequently, the practice can be applied. 
%
%If not handled with care, operating or manipulating with voltage can cause permanent damage to appliance or health, or can cause fire or even death. Thus, respect, increased care and knowledge is necessary in all further practical steps involved.


\subsection{Power measuring integrated circuits} \label{ss:pmic}

Although it is possible to construct a circuit out of discrete components that would measure \cite{webster2003electrical} the required physical quantities, and such a solution would probably be the cheapest solution out there, it would be highly impractical due to multiple reasons.

The most importantly, the obtained accuracy of the measurements would be dependent on the implementation and used components. It is safe to assume, that without multiple design iterations, the accuracy may be too low to be used in practice.

Another point is that, there is no definitive guide, ready to follow, about how to design such circuit. The reason of this is the vast amount of components available on the market and a lot of design considerations to take into account, depending on the requirements.

A special purpose \glspl{ic} are being developed for the exact purpose of measuring the real, apparent and reactive power, the power factor, and in most cases, gathering some other relevant information.

\begin{figure}[ht!]
\centering
\includegraphics[width=1\linewidth,angle=0]{measurement_IC_diag}
\caption{The simplified block diagram for a power measurement \gls{ic}}\label{f:meas_IC_diag}
\end{figure}

From the block diagram \ref{f:meas_IC_diag}, it can be seen that the power measuring \gls{ic} is just a specialised microcontroller. It takes the data from the sensing circuitry, which in case of voltage can be measured \textit{directly}, provided that the galvanic isolation is included, for the sake of safety. The current however, must be measured \textit{indirectly}. There are three common ways \cite{srinivasan2015composite} of doing so:

\begin{enumerate}
\item \textbf{shunt resistor} - a resistor with a very small but precise value, that causes a voltage drop with a current passing through it due to the Ohm's law, regardless of frequency. The actual voltage drop is so small, that it can be assumed insignificant, but measurable. However, the voltage drop is still present and may cause some issues, if not taken into account. The advantage is really low price. External galvanic isolation must be provided.
\item \textbf{current transformer} - a current passing wire inside a current sensing coil. Since it is a magnetic induction based transformer, the galvanic isolation is naturally present. The disadvantage is, that the transformer has a cut-off frequency, below which it's effect diminishes rapidly. External magnetic fields can cause problems too. Suitable for measuring current of a fixed (or non-decreasing) frequency.
\item \textbf{Hall-effect sensor} - a sensor measuring absolute electromagnetic field in a conductor. In contrast to the current transformer, this sensor is able to measure low frequency currents, down to \gls{dc}, which is a feat that the shunt resistor possesses too. Can be placed anywhere near the current path and doesn't require physical connection, thus providing galvanic isolation too. The price increases with operating currents range and precision. Prone to be disturbed by external magnetic fields, too.
\end{enumerate}

Using dedicated power measuring IC has another advantage apart from being more accurate. In fact, the part \gls{datasheet} can be consulted and if all application notes and advices are abided, the specified accuracy can be guaranteed.



\section{Embedded system}
An embedded \gls{system} is some combination of \gls{computer} \gls{hw} and \gls{sw}, either fixed in capability or programmable, that is specifically designed for a particular function \cite{ganssle2008embedded}. Industrial machines, automobiles, medical equipment, cameras, household appliances, airplanes, vending machines and toys (as well as the more obvious cellular phone and \gls{pda}) are among the myriad possible hosts of an embedded \gls{system}. Embedded \glspl{system} that are programmable are provided with programming \glspl{interface}, and embedded \glspl{system} programming is a specialized occupation.

\subsection{Processing units}
The term embedded \gls{system} is quite broad, so there is no surprise that the spectrum of used processing units is also wide. Since the general purpose microprocessors require external components, namely memories and \glspl{peripheral}, they tend to consume extra power and a board space. Since the design limitations of an embedded \glspl{system} are most of the time low physical size, low power consumption and/or long uptime and ruggedness (more components mean more parts could fail), microprocessors are seldom used. However, most of the commonly used architectures and word lengths are covered. Due to aforementioned reasons, microcontrollers are favored over microprocessors. 

\subsection{ESP8266 Wi-Fi module} \label{s:esp8266}
The ESP8266 Wi-Fi module is a self contained \gls{soc} with integrated \gls{tcpip} protocol stack that can give any microcontroller access to your Wi-Fi network. The ESP8266 is capable of either hosting an application or offloading all Wi-Fi networking functions from another application processor. The ESP8266 module is an extremely cost effective solution, with a huge code-base and community, making it a preferable option for many modern projects, mainly the ones that follow the \gls{iot} trend.

\begin{figure}[ht!]
\centering
\includegraphics[width=.4\linewidth,angle=0]{esp-12e}
\caption{The certified ESP-12E module exposing all \glspl{gpio}}\label{f:esp-12e}
\end{figure}

This module has a powerful enough on-board processing and storage capability that allows it to be integrated with the sensors and other application specific devices through its \glspl{gpio} with minimal development up-front and minimal loading during runtime. Its high degree of on-chip integration allows for minimal external circuitry, including the front-end module, is designed to occupy minimal \gls{pcb} area. 




\section{Hardware components breakdown} \label{ss:hw}
The device under test will be referred to as \textbf{appliance} and the produced device will be referred to as \textbf{client node} (displayed as a simplified schematic in figure \ref{f:schem_block}) is already integrated as an ESP-8266 module, described in more detail in the chapter \ref{s:esp8266}. The module contains the \gls{tcpip} stack, micro-controller (application processor) running the user program, \gls{wlan} and light indication, all in one piece, so this greatly simplifies the design process and allows for more focus on the actual measurement circuitry. The ESP-12E has been chosen as an actual module, because of the available certification\cite{online:2ADUIESP-12}, which allows it to be introduced to the market later. It was already shown in the figure \ref{f:esp-12e}. The \gls{pwm} is present there too, so sound indication requires just a sound emitting device.

\begin{figure}[ht!]
\centering
\includegraphics[width=.8\linewidth,angle=0]{client_node_diag}
\caption{The proposed block diagram of a \textit{client node}}\label{f:client_node}
\end{figure}

Talking about the measurement circuitry \ref{f:schem_block}, the viable candidate is MAX78615 \cite{online:MAX78615} with the companion \gls{ic} MAX78700 \cite{online:MAX78700}. The couple should be used, because it provides multiple ways of same voltage level communication with the processor, galvanic isolation via the pulse transformer for improved circuitry protection, great precision, accuracy and utility. The shunt resistor is utilised as a way of obtaining measurements, described in the sub-chapter \ref{ss:pmic}.

For the protection against fire a standard glass fuse or a resettable \gls{ptc} fuse\cite{wright2004electric} should be used. Because of the variable nature of most used devices, it is hard to calculate the current consumption of the circuit. It can be measured after the first iteration is manufactured. Thus, the easily replaceable standard glass fuse has been chosen because of its versatility. The circuit protection against high voltage should be solved with an isolated DC-to-DC converter\cite{carr1996linear} or with a linear transformer coupled with a linear voltage regulator\cite{2008linear}. Since the former one is either expensive or hard to design, and this work does not want to focus on more complexities, the latter option has been chosen.

Choosing the voltage level for the digital electronics (the output voltage of the linear regulator) is straightforward. Since the ESP-12E works on nominal 3.3V, this is the level that has been chosen. Having \glspl{ic} using the same voltage level removes the need to level-shift the communication between them, thus increasing the simplicity of the design.

\begin{figure}[ht!]
\centering
\includegraphics[width=1\linewidth,angle=0]{schematic_block}
\caption{Greatly simplified schematic of a client node sketching the inner working}\label{f:schem_block}
\end{figure}

Talking about the measurement circuitry, the candidate is MAX78615 \cite{online:MAX78615}, working on nominal 3.3V level, along with the companion \gls{ic} MAX78700 \cite{online:MAX78700}. The couple has been chosen, because it provides multiple ways of communication with the processor (buses/serial interfaces), galvanic isolation via a pulse transformer for improved circuitry protection, great precision, accuracy and utility. The resistor network, including the shunt resistor is utilised as a way of obtaining measurements. The shunt resistor is also briefly described in the sub-chapter \ref{ss:pmic}.

The remaining part of the client node's block diagram \ref{f:client_node} not yet mentioned is switching. Either a mechanical relay or a semiconductor device, such as a thyristor or a \gls{ssr} isolated by an opto-coupler\cite{trzynadlowski2015introduction} will do. Mechanical relays tend to be larger and produce sound noise, have slow response time, but have inbuilt separate isolation and are capable of switching higher currents without additional thermal issues than their semiconductor counterparts\cite{blume2008electric}. The disadvantages of the mechanical relay are not relevant here, thus it has been chosen.

\begin{figure}[]
\centering
\includegraphics[width=.8\linewidth,angle=0]{pcb_top}
\caption{The top layer of the designed \gls{pcb} (client node), exposing mainy \gls{tht} components}\label{f:pcb_top}
\end{figure}
\begin{figure}[]
\centering
\includegraphics[width=.8\linewidth,angle=0]{pcb_bottom}
\caption{The bottom layer of the designed \gls{pcb} (client node), exposing mainly \gls{smt} components}\label{f:pcb_bottom}
\end{figure}


\section{Conclusion}

The manufactured client node has been inserted into the enclosure\cite{online:enclosure}, portrayed in the figure \ref{f:enclosure}, containing an European mains socket (female) on one side and an European mains plug (male) on the other side, forming a man-in-the-middle adaptor, that can be non-invasively put between wall socket and an appliance. The result can be observed in figure \ref{f:project_inside}.

The client node is capable of measuring \textit{RMS voltage} and \textit{RMS current}. By multiplying them together, the \textit{apparent power} can be obtained, as discussed back in the sub-chapter \ref{ss:power_ac}. The intentions are to fix the design, enabling full range of physical quantities, discussed throughout the chapter \ref{s:el_power}, to be measured. The plan is to use multiple client nodes to measure and track power consumption of the inactive, but plugged-in \gls{smps} chargers, in a form of a collaborative global experiment.

\begin{figure}[]
\centering
\includegraphics[width=.8\linewidth,angle=0]{enclosure}
\caption{The 3D visualisation of the enclosure, displaying both the plug and the socket}\label{f:enclosure}
\end{figure}
\begin{figure}[]
\centering
\includegraphics[width=.8\linewidth,angle=0]{project_inside}
\caption{The view into the client node's enclosure, before the final assembly, exposing top side of the board containing linear transformer T1 (green), mains connectors J1 and J2 (blue), a fuse holder for F1 (yellow-ish), a relay K1 (white) and an ESP-12E module}\label{f:project_inside}
\end{figure}



\newpage
\section*{Acknowledgment}
I would like to express my sincere thanks to my supervisor Ing. Tibor Vince,
PhD., for his constant and constructive guidance throughout all the struggles that
have occurred during this work. And to all other who gave a helping hand, I say
thank you very much, too.


\printbibliography


\end{document}


