energy monitoring with power line communication

39
ENERGY MONITORING WITH POWER LINE COMMUNICATION By Matthew Feddersen Evan Richards Saheed Rosenje Final Report for ECE 445, Senior Design, Fall 2014 TA: Kevin Chen 10 December 2014 Project No. 33

Upload: others

Post on 04-Apr-2022

0 views

Category:

Documents


0 download

TRANSCRIPT

ENERGY MONITORING WITH POWER LINE COMMUNICATION

By

Matthew Feddersen

Evan Richards

Saheed Rosenje

Final Report for ECE 445, Senior Design, Fall 2014

TA: Kevin Chen

10 December 2014

Project No. 33

ii

Abstract This report details the design and implementation of an energy monitoring system for household

electrical devices. The system under review performs measurements of power and energy consumption

for numerous electrical devices placed throughout a building and sends usage data back to a central

location to be uploaded online for consumer-friendly analysis. Although an entire system

implementation was unsuccessful, significant insight was gained throughout the design and prototyping

process.

iii

Contents

1 – Introduction....................................................................................................................................... 1

1.1 – Block Diagrams............................................................................................................................ 1

1.2 – Block Descriptions ....................................................................................................................... 2

1.2.1 – Measurement and Transmitter Module ................................................................................ 2

1.2.2 – Receiver Module .................................................................................................................. 3

2 – Design................................................................................................................................................ 4

2.1 – Measurement and Transmitter Module....................................................................................... 4

2.1.1 – Power Circuit ........................................................................................................................ 4

2.1.2 – Measurement Circuit ............................................................................................................ 5

2.1.3 – Microcontroller .................................................................................................................... 6

2.1.4 – PLC Chip ............................................................................................................................... 7

2.2 – Receiver Module ......................................................................................................................... 9

2.2.1 – Power Supply ....................................................................................................................... 9

2.2.2 – PLC Chip ............................................................................................................................. 10

2.2.3 – WiFi Microcontroller .......................................................................................................... 11

3 – Design Verification ........................................................................................................................... 12

3.1 – Power Supply Circuits ................................................................................................................ 12

3.2 – Measurement Circuit ................................................................................................................ 12

3.3 – Microcontroller ......................................................................................................................... 12

3.3.1 – UART Transmission ............................................................................................................. 12

3.3.2 – ESP430 Initialization ........................................................................................................... 13

3.3.3 – Reading from Measurement Circuit .................................................................................... 13

3.4 – PLC Chip .................................................................................................................................... 13

3.4.1 – Tx Active and Passive Filter & Rx Passive Filter .................................................................... 13

3.4.2 – Transmitting and Receiving Data ........................................................................................ 17

3.5 – WiFi Module ............................................................................................................................. 17

4 – Costs ................................................................................................................................................ 18

4.1 – Parts ......................................................................................................................................... 18

4.2 – Labor ........................................................................................................................................ 18

iv

4.3 – Total ......................................................................................................................................... 18

5 – Conclusion ....................................................................................................................................... 19

5.1 – Accomplishments ...................................................................................................................... 19

5.2 – Failed Requirements ................................................................................................................. 19

5.3 – Ethical considerations ............................................................................................................... 19

5.4 – Future work .............................................................................................................................. 20

References ............................................................................................................................................ 21

Appendix A – Requirements and Verifications Tables............................................................................. 22

Appendix B – Flowcharts ....................................................................................................................... 25

Appendix C – Source Code ..................................................................................................................... 27

C.1 – MSP430 Code............................................................................................................................ 27

C.2 – Spark Core Code........................................................................................................................ 35

1

1 – Introduction The goal of our project was to develop a modular system which will allow consumers to

measure the energy usage of multiple appliances and other electronic devices in their home, office, or

other space. There are several different energy monitoring devices currently on the market. Some of

these devices, such as the well-known Kill-A-Watt, have LCDs on the device itself to display the

measured energy usage. Devices such as these are limited, as the consumer must go to each individual

device to see its measurements. Other products solve this issue by using wireless protocols such as WiFi

or ZigBee to send the data to another location for display. Rather than either of these options, we have

chosen to send the measurement data via power line communication (PLC) over the existing power lines

in a building. We chose this method of communication as it is an emerging technology in the field of

energy metering for smart grids, and we wished to obtain experience using it. We also believe that it

may be more efficient than wireless communication if the PLC modem is switched from transmitting

mode to a low power receiving mode when not sending data.

1.1 – Block Diagrams Two distinct devices are needed for operation of the system: the Measurement and Transmitter

Module, and the Receiver Module. The modular nature of our system allows the user to add any

number of the Measurement and Transmitter Modules while only needing one receiver module. For

demonstration purposes, we intended to build two Measurement and Transmitter Modules and one

Receiver Module.

In the block diagrams for each device, solid lines represent 120 Vac 60 Hz power, dashed lines

represent DC power, and dot-dashed lines represent the flow of measurement data. A top level view of

the system can be seen in Figure 1, while Figures 2 and 3 show the Measurement and Transmitter

Module and the Receiver Module, respectively.

Figure 1 - Top level system layout

2

Figure 2 - Measurement and transmitter module

Figure 3 - Receiver module

1.2 – Block Descriptions

1.2.1 – Measurement and Transmitter Module

The Measurement and Transmitter Module acts as the device in between the wall outlet and

the home appliance being monitored. Power from the 120 Vac line is converted to dc by the ac/dc

Power Circuit and then sent to power the PLC chip, which in turn powers the microcontroller. The

Measurement Circuit receives power from the 120 Vac line and measures the voltage and current. It

then passes the power along to the home appliance and sends the measured values to the

Microcontroller. The Microcontroller converts the analog measurements to digital values using a built in

ADC. It sends this data to the PLC chip, which then injects a message into the 120 Vac power line in the

form of a modulated signal.

1.2.1.1 – Power Circuit

This provides dc power to each component throughout the transmitter module. It consists of a

transformer and rectifier with a linear voltage regulator to maintain a constant dc voltage. For the

transmitter module, this circuit should output 12 Vdc up to 500 mA.

3

1.2.1.2 – Measurement Circuit

This circuit performs the necessary measurements of the line voltage and the current drawn by

the consumer device. To measure current, the voltage drop across a shunt resistor is measured. To

measure voltage, a voltage divider is used to scale down the input to a safer level, around 200 mA. Both

the current and voltage measurement circuits will send ac voltages to the microcontroller ADC.

1.2.1.3 – Microcontroller

We are using an MSP430FE4272 as our microcontroller. Its main purpose is to keep track of the

voltage and current measurements while calculating energy consumption. It sends this data to the PLC

chip for further transmission. We chose this particular microcontroller for its low power consumption

and low cost. In our project, the microcontroller serves as an ADC, converting the measurement circuit’s

analog output to digital, then using those values to calculate relevant energy data. This data is

forwarded to the power line communication chip for transmission.

1.2.1.4 – PLC Chip

For Power Line Communication, we are using the ST7540 chip. The ST7540 is a frequency-shift

keying transceiver modem designed for power line communication network applications. When in

transmission mode, the data signal is first modulated at a preselected carrier frequency of 132.5kHz

minus half the baud rate for bit 1 and 132.5kHz plus half the baud rate for bit 0. Next the mains

frequency, 60Hz, is filtered from the modulated signal in order to make the wire available for the

transmission signal. The signal is now ready to be sent along the main power line.

1.2.2 – Receiver Module

The Receiver Module is the central device responsible for receiving and aggregating the

transmitter messages from the power line. Like the Transmitter Module, the Receiver has an ac/dc

Power Circuit to power each of its components. It also has a PLC chip to receive and digitize the

communication from the power line, and a microcontroller to decode this communication into a useful

format. The microcontroller sends this information to the cloud through a WiFi module.

1.2.2.1 – Power Circuit

This circuit is similar to the one in the Measurement and Transmitter Module. It converts the ac

voltage from the wall outlet to the DC voltage necessary to power each component in the Receiver

Module. This circuit has outputs for 10 mA at 12 Vdc and 300 mA at 5 Vdc.

1.2.2.2 – PLC Chip

The ST7540 also performs as a receiver of the modulated signal. In reception mode, the

modulated signal enters the PLC chip and is filtered by three bandpass filters to receive the signal with a

low sound to noise ratio, adapt the waveform amplitude and then centered around the selected channel

frequency. After the signal is filtered, it is then demodulated and sent to the microcontroller.

1.2.2.3 – WiFi Microcontroller

This is a Spark Core WiFi board with a built in TI CC3000 WiFi chip. It takes in all the data coming

from the PLC chip, keeps track of various energy consumption values, and sends them to the cloud.

4

2 – Design

2.1 – Measurement and Transmitter Module

2.1.1 – Power Circuit

This circuit provides the power needed by the MSP430 microcontroller and the ST7540 PLC chip.

The PLC chip operates at 12 Vdc, while the microcontroller operates at 3.3 Vdc. The power supply circuit

has been designed such that it only needs to power the PLC chip because the ST7540 has a built-in

linear regulator output which can supply 3.3 Vdc at up to 50 mA, more than enough to power the

MSP430. From the application notes and datasheets for each device in references [1]-[2], the expected

maximum current draw from the power supply will be 500 mA. The power supply circuit has thus been

designed according to these specifications. An overall schematic of the power supply can be seen in

Figure 4.

Figure 4 - Measurement and transmitter module power supply circuit

It can be seen in Figure 4 that we chose a relatively simple ac/dc power supply design which

incorporates a rectifier followed by a linear regulator. There are several alternative designs which could

have been used, mainly switching power supplies such as a buck converter or flyback converter. These

designs would have been more efficient, but also more complex. Due to the time constraints on our

project and the fact that this was not the main focus of our project, we chose to use the more simpler

linear regulator design.

Our original plan was to have a male NEMA 5-15 connector, commonly known as a standard

three conductor electrical plug, as the point where power would enter the circuit from a wall outlet.

After an unsuccessful search for PCB mount versions of the NEMA 5-15 connector, we decided to modify

our design to use 3-pole terminal blocks instead. A standard three-conductor electrical cord will plug

into the wall outlet and the other end will have three stripped conductors which will be inserted into the

terminal block. The output power to the consumer device will have a similar terminal block with

electrical cord attached, supplying the female plug needed by the device.

After entering the board through the terminal block, the line voltage splits into two paths. One

path takes it to the measurement circuit and then to the consumer device, while the other continues to

the power supply circuit and passes through a 250 V, 500 mA fuse for user safety and internal circuit

protection. The fuse used is a Cooper Bussmann type MDL fuse as recommended by the transformer

manufacturer in [3]. The transformer itself has a winding ratio of 115:12, with two secondary windings

connected in parallel to obtain an output of 12 V with a rating of 1 A. Assuming a minimum line voltage

5

of 115 Vrms, 5V below standard line voltage, this transformer will step the line voltage down to 12

Vrms, which has a peak of 17 V. At this point a full-wave bridge rectifier is used in order to rectify the

voltage. The rectifier MB12S was picked to have a maximum blocking voltage higher than 15 Vrms and a

maximum output current higher than 500 mA [4].

In order to adjust the voltage to the final desired level of 12 Vdc, a capacitor and a linear

regulator are added after the rectifier. The linear regulator, although less efficient than a switching buck

converter, is an effective way to achieve a constant output voltage without having to implement the

switching power converter with feedback. As long as the input to the regulator remains above the

desired output by the regulator’s designated dropout voltage, the output voltage will remain essentially

constant. For the linear regulator chosen, the dropout voltage is 2.5 V, meaning that the input must be

at least 14.5 Vdc in order to obtain an output voltage of 12 Vdc [5]. From this requirement it is possible

to calculate the necessary value of the rectifier output capacitor.

After passing through the rectifier, the peak voltage will be reduced by the 1V forward voltage

drop across the rectifier. Still assuming our previous line voltage of 115Vrms, the rectified voltage will

have a peak of 16V. Thus, in order for the linear regulator to function properly, the maximum ripple

voltage across the output capacitor of the rectifier is 1.5V. Using the general capacitor relationship

𝐼 = 𝐶𝑑𝑉

𝑑𝑡, it can be found that the necessary capacitance is

𝐶 =𝐼

𝑓∆𝑉 (1)

where I is the maximum output current of 500 mA, f is the 120 Hz frequency of the rectified waveform,

and ∆V is the ripple voltage of 1.5 V. From Equation (1) it can be calculated that a capacitance of at least

2834 μF is necessary. As a result, the power supply was designed with a 3300 μF capacitor at the output

of the rectifier and the input to the voltage regulator.

The linear voltage regulator was chosen according to its output specifications of 12 Vdc at 500

mA. After having problems with dropping voltage under load with fixed 12 V linear regulators, we chose

to switch to an adjustable linear regulator which has feedback, the LM317. The feedback resistor was

chosen according to 𝑉𝑜𝑢𝑡 = 1.25(1 + 𝑅2 𝑅1⁄ ), where Vout is the desired 12 V, R1 is chosen as 240 Ω,

taken from the datasheet [5]. R2 was thus calculated to be 2.1 kΩ. This fixed the voltage dropping

problems and allowed us to achieve a steady output.

2.1.2 – Measurement Circuit

This circuit performs the necessary measurements of the line voltage and the current drawn by

the consumer device. The circuit must be designed to output voltage levels which are within the

tolerance of the analog microcontroller inputs. We are using a version of the MSP430 microcontroller to

convert the measurements to digital values, as well as perform calculations and interpret them before

sending the data to the PLC circuit. As a result, the design of the measurement circuit in Figure 5 is

based upon the design found in [2]. The voltage measuring circuit is essentially the same, whereas a few

changes were made to the current measuring circuit. Instead of measuring the current with a current

transformer, we have chosen to use a shunt resistor. The first impression of this choice is that it is

inherently inefficient. With the choice of a very small, precise resistor, though, the losses can be

minimized. We have chosen to use a precision resistor with a resistance of 0.005 Ω. This will result in a

6

maximum voltage of 75mV under full load and the power dissipated will be only 1.13W. This is an

acceptable power loss and the voltage will be within the measurable range of the MSP430.

Figure 5 - Measurement circuit

2.1.3 – Microcontroller

Acting as an ADC between the Measurement Circuit and the Power Line Communication module

is an MSP430FE4272 microcontroller. The MSP430FE42x2 family of microprocessors is specifically

designed for energy-metering applications, due in part to a built-in embedded signal processor that

calculates and updates relevant power values in real time [2]. In addition, it draws very little power

while active, making it an ideal choice for our project.

Other options for our microcontroller were the NXP EM773 and the Freescale MC9S08GW64,

both targeted towards energy metering. The NXP EM773 is a 32-bit ARM Cortex-M0 based

microcontroller, but it comes only in QFN packages, making it very difficult to solder. The Freescale

MC9S08GW64, while similar to the MSP430 that we chose, did not have an intuitive or apparent

programming interface, so we chose the more standard and readily available MSP430 series.

The microcontroller on the transmitter module takes the voltages from the measurement

circuit, maps them to a digital value with an onboard ADC, and then transmits relevant results and

calculations to the PLC chip through UART. The ADC uses 16-bit Sigma Delta modulation to convert

analog values to digital. Some of the relevant settings include channel gain, threshold values, sampling

frequency, delay, and reference voltage. A full commented setup sequence is shown in Appendix B.

The input range of each ADC channel is determined by the reference voltage and the gain

setting, such that

𝑉𝑟𝑎𝑛𝑔𝑒 =±𝑉𝑟𝑒𝑓

2 × 𝐺𝐴𝐼𝑁 (2)

The default reference voltage is an internal 1.2V source in the microcontroller, and the gain

setting can be either 1, 2, 4, 8, 16, or 32. For our setup, we used a gain of 8 for the current measurement

channel and a gain of 2 for voltage measurement channel, since our current measurement voltage

output would be in the range of 0-70 mVac and our voltage measurement would be in the range of 0-

200 mVac. Calculations for each are

𝑉𝐼 =±1.2

2 × 8= ±75 mV (3)

𝑉𝑉 =±1.2

2 × 2= ±300 mV

(4)

7

This input range determines the upper and lower values of the digital output from each ADC

channel. By default, the ADC maps -Vrange to 0x0000 and +Vrange to 0xFFFF.

The embedded signal processing module on the microcontroller automatically calculates

relevant power data as the ADC updates its measurements. The most important value to us is the active

energy calculation, which is stored as a 32-bit float in two 16-bit registers, ACTENERGY_HI and

ACTENERGY_LO. We take those values and transmit them in 4 byte-size packets via UART to the PLC

chip.

Figure 6 below shows the relevant connections going to and from the microcontroller. These

include the RX/TX lines coming from the MSP to the ST7540 PLC chip, the input current and voltage

values coming from the measurement circuit to the MSP, and the JTAG programming/debug lines on the

MSP430. See Figure 17 in Appendix B for a flowchart of the microcontroller program.

Figure 6 - Microcontroller connections

2.1.4 – PLC Chip

To transmit and receive data from our module, we have selected the ST7540 transceiver FSK

chip [6]. This will add a modulated carrier signal to the 60 Hz power line with our encoded messages.

The modulated signal will arrive at the receiver to be demodulated. Our data will be sent using a

frequency modulation scheme named binary frequency shift keying. When transmitting data, BFSK uses

two frequencies to transmit binary information. With this scheme, the “1” is called the mark frequency

and the “0” is called the space frequency. We have selected the ST7540 default communication

frequency of 132.5kHz which is within the FCC allow frequency C-band [1]. We are also selecting a Baud

Rate of 4800bps and a standard deviation of 1. This deviation widens the range between our mark and

space frequencies of 1300046Hz and 134928Hz. The data transmission and reception mode will be set to

asynchronous since we are using a UART microcontroller to handle the transmission and reception

timing.

During transmission and reception, external signals at interfering frequencies or noise from

other devices turning on and off, can cause distortion in our signal. In order to eliminate some of these

8

distortions, we must strengthen our Rx and Tx signal. In transmission, the ST7540 filters our output

signal spectrum and reduces the harmonic distortion. In addition, there is a variable gain amplifier with a

gain range of 0db to 30db. Figure 7 depicts our transmission signal flow.

Figure 7 - PLC internal filter and line injection diagram for Tx [1]

In addition to the internal filters, the ST7540 has an external filter for the transmission signal.

The filter introduces attenuation approximately an octave above the transmission channel frequency.

Figure 8 below depicts the flow of the transmission signal.

Figure 8 - PLC external filter and line injection circuitry [1]

The external filter consists of a Tx active and passive filter and Rx passive filter. The Tx active

filter is a low pass filter with a 9dB gain consisting of a RC low pass stage and a Sallen-Key 2-pole cell.

Frequencies above the corner frequency are attenuated. Calculated below in Equation (5) is the corner

frequency of the active filter.

9

(5)

Depicted in Figure 9 is a simulation of the RC low pass stage showing the attenuation of a 4V

peak-to-peak sine-wave signal.

Figure 9 - Simulation of RC low pass stage

The ST7540 was selected because of its low power consumption at 5mA and low cost in

comparison to other marketed products. The ability to switch between 8 different carrier frequencies

and 4 different baud rates provides for a flexible choice of signal characteristics. Through our research of

low frequency power line communication, the ST7540 was recommended as the leading product for our

specifications.

2.2 – Receiver Module

2.2.1 – Power Supply

The power supply for the receiver module is very similar to the power supply for the transmitter

and can be seen in Figure 10. In this case, the circuit must provide power to the ST7540 PLC chip as well

as the Spark Core WiFi microcontroller. Unlike the previous design, in which the ST7540 was able to

provide power to the MSP430 through an internal 3.3 V linear regulator, the power supply circuit must

supply the Spark Core directly, as it will draw more than the 50 mA provided by the ST chip.

In receiving mode, the ST7540 will draw less than 10mA at 12Vdc, while the Spark Core will draw

anywhere from 30 mA to 300 mA at 5 Vdc [1]. Thus, in order to meet both these requirements, the

power supply has an additional 5 V linear voltage regulator in parallel with the 12 V linear regulator.

Everything else is the same as the transmitter power supply in order to maintain a more modular design

and cut down on testing.

10

Figure 10 - Receiver power supply circuit

2.2.2 – PLC Chip

This is the same as the chip on the Transmitter Module, except it is set to specifically receive

messages instead of sending them. This will interface directly with the microcontroller unit, sending any

messages there for processing. This utilizes the same filter and line injection circuits as on the

Transmitter.

Similarly, in reception, the ST7540 receives signals and filters the signals to improve the sound

to noise ratio before it reaches the demodulator. In addition, the ST7540 provides carrier/preamble

detection which identifies signals with a harmonic component close to the signal frequency and detects

whether a carrier is modulated at the Baud Rate. Figure 11 below depicts the flow of the received signal.

Figure 11 - PLC internal filter and line injection diagram for Rx [1]

11

2.2.3 – WiFi Microcontroller

For our WiFi module, we chose the Spark Core, which is a development board for the TI CC3000

WiFi chip built with a STM32F103 ARM Cortex M3 microcontroller. This fits within the power

requirements of our receiver module and vastly simplifies the problem of wireless data transfer. The

Spark Core is specifically built to simplify the interface between electronics and the internet, and there

hasn’t been a compelling enough reason for us to design and build our own interface when this one

conveniently exists.

The Spark Core takes in data from the PLC chip via UART. Since the data is 4 bytes long, the

Spark Core must stitch them together to create a single float value for the energy transmitted. The

module takes 5V input from the power circuit, and draws approximately 300mA of current. See Figure

18 in Appendix B for a flowchart of the Spark Core program.

12

3 – Design Verification

3.1 – Power Supply Circuits Verification of the power supply circuits was performed by connecting the output of each circuit

to a simple resistive load which corresponded to the maximum current the circuit needed to supply in

the overall design.

For the measurement and transmitter power supply, a 24 Ω power resistor was used as a load in

order to draw the required 500 mA at 12 Vdc. The circuit was then powered on and the voltage across

the resistor was measured using a multimeter. The measured voltage was 11.85 V, which was within our

design requirement of 12 ± 0.2 V under a load of 500 mA.

For the receiver power supply, a 1200 Ω power resistor was used as a load for the 12 V output,

while a 16 Ω power resistor was used as a load for the 5 V output. This resulted in the required output

currents of 10 mA and 300 mA, respectively. The measured voltages were 12.16 V and 5.05 V, which

were within our requirements of 12 ± 0.2 V and 5 ± 0.1 V, respectively.

3.2 – Measurement Circuit Verification of the measurement circuit was performed through two separate measurements.

First, the voltage measurement circuit was tested by plugging the circuit into the 120 V mains line with

no consumer device attached. The output of the voltage measurement circuit was then measured using

a multimeter. For a line voltage of 120 Vrms, we would expect to measure a voltage of 183 mVrms from

the circuit. Unfortunately, this test failed. The voltage measured by the multimeter was nearly ten times

higher than this expected value, at 1.3 Vrms. This test was performed several different times, but was

never successful. The resistances were measured to verify the correct voltage division was occurring,

and were confirmed to be the correct values. It is thus unknown why something as simple as the voltage

divider would not function correctly. Due to time constraints, we were unable to rebuild the voltage

measurement circuit with different components to test it in isolation from the rest of our circuit.

The current measurement circuit was tested by connecting a consumer device and measuring

the voltage output of the current measurement circuit. This was tested using two different consumer

devices, one that drew 8 A and another that drew 1.5 A. Using the first 8 A device, a voltage of 39.4

mVrms was measured at the output of the circuit, which was only 0.6 mV from the expected value of 40

mV. For the second device, the measured value was 7.6 mVrms, which was only 0.1 mV from the

expected value of 7.5 mVrms. Thus, our current measurement circuit performed within our accuracy

requirements of ± 1 mV.

3.3 – Microcontroller

3.3.1 – UART Transmission

To show that the MSP430 could send UART data at a set baud rate, we used the Spark Core

attached to a computer COM port to intercept and display the transmitted data. The Spark Core has a

USB-Serial interface with the computer, and through Putty we were able to successfully show the

transmitted data from the MSP430.

13

3.3.2 – ESP430 Initialization

The initialization of the ESP430 module was non-trivial, as it required thorough searching

through Texas Instruments’ datasheet archives for the appropriate documentation as well as multiple

trials and errors. Various problems occurred initially due to the nature of the ESP registers, which are

called mailbox registers. To store and retrieve values, one has to make a request to the registers and

then await a response before acting further. We had problems with the initialization crashing because

we did not fully understand this system at first. In the end, however, we were able to set and read the

mailbox registers correctly, and were able to verify with a debugging interface that the ESP was

successfully initialized with no crashes.

3.3.3 – Reading from Measurement Circuit

The measurement circuit outputs two analog signals, one for current measurement and one for

voltage measurement. The MSP430 should be able to use the ADC to turn those analog values into

digital ones, and then use that information to calculate the resulting energy. During tests, we found

unusual amounts of noise on the ADC pins that skewed our ability to accurately read input signals.

With absolutely nothing attached to the pins, the ADC channel read 0V. However, probing with a

multimeter, hooking the pins up to a waveform generator, and even attaching open wires to the pins

caused the ADC to register voltage in the 0-15 mV range while on. Inputting a waveform anywhere from

30-70 mV 60Hz AC across the pins caused the ADC to saturate and read close to 0xFFFF, which is the

maximum input value. Connecting a multimeter at the same time across the pins read an excess of 100

mV, which did not change by a definite amount based on the waveform amplitude. 100 mV was outside

the range of the ADC given our gain setting (we set the gain to 8, which gave us a range of 75 mV). A

gain of 1 would give a range of 500 mV, but since the apparent voltage did not change in response to the

input waveform, it wouldn’t have allowed us to extrapolate a measurement.

We theorized that the wires that we were using to hook up our test equipment somehow

resonated with inherent noise from the ADC. The ADC utilizes an 8 MHz source clock for the Sigma-Delta

modulation, which samples directly from the pins on the microcontroller. This sampling may have

introduced small amounts of noise on the pins, but we find that unlikely given the relatively large

amplitude of the noise (0-15 mV). More likely, we unknowingly shorted a logical component in the ADC

that ended up amplifying any signals we input to the channel.

3.4 – PLC Chip

3.4.1 – Tx Active and Passive Filter & Rx Passive Filter

To verify the functionality of the Tx active and passive filters we first built the RC low pass filter

of the active filter and injected a 4V peak-to-peak sine-wave signal. We then tested the signal at

frequencies ranging from 10 kHz to 1 MHz. Figure 12 shows measured outputs of the filter at our carrier

frequency of 132.5 kHz and 1 MHz.

14

Figure 12 - Filter outputs at 132.5 kHz and 1 MHz

The measure data verifies the attenuation of our signal at approximately 200 kHz to 1 MHz and ensures

the signal integrity of our transmitted signal.

While verifying the active filter, we had challenges producing the proper output. We injected

the ST7540 with a 4V peak-to-peak square-wave signal at or carrier frequency of 132.5 kHz. We

expected a frequency shift-keyed AC signal at the approximate mark and space frequencies with peak

voltage regulated by

(6)

(7)

where VCC is the 12 V input voltage of the power amplifier and VPAOUT is the AC output voltage signal of

the Tx active filter. The VPAOUT signal is measured below in Figure 13.

15

Figure 13 - VPAOUT signal

The measurement shows an improper signal output. After this outcome, the circuit was re-

inspected to ensure all connections were correct and all inputs were within the allowable range for the

ST7540. After confirming all connections of the PLC chip were connected properly, we re-tested the PLC

with the same result. We established through further testing of the external filter that our power

amplifier may have malfunctioned. In the ST7540 datasheet a recommended test circuit of the power

amplifier is displayed as Figure 14 below.

Figure 14 - PA test circuit

During testing of the power amplifier circuit we also chose to regulate the VCC input voltage to under

100V/ms. Also, the datasheet called for VCC= 9V input. Measured below in Figure 15 are the power

amplifier plus input, minus input and output.

16

Figure 15 - PA testing results

The power amplifier minus input should have identical measurement to the power amplifier plus input.

As seen the minus input is distorted and has zero voltage signal output. In addition, the power amplifier

output was also distorted and show zero voltage. We expected an output voltage as simulated in Figure

16.

Figure 16 - PA simulated output

After using the test circuit of the power amplifier we decided to test the amplifier as a feedback

amplifier and inverter to determine if the power amplifier functioned properly. We continued to

measure zero output signal during these test. We further determined the power amplifier would not

respond to basic amplifier inputs and we determined the power amplifier is extremely sensitive.

Replacing the internal power amplifier with a matching external power amplifier due to not being able

to match the amplifiers specifications.

17

3.4.2 – Transmitting and Receiving Data

Due to our challenges with the power amplifier of the Tx Filter, we were unable to test

transmitting and receiving data. If given more time, we would have switch our PLC chip manufacturer.

For example Texas Instruments offers the AFE030/AFE031 but we would lose the ability for our carrier

frequency to be high than 96 kHz which would put us out of the range of FCC regulated frequency for

similar power monitoring devices. Further research would be need to find a compatible PLC chip.

3.5 – WiFi Module The requirement for the Wifi module was very straightforward. It had to take input data

received from UART and forward it to a cloud server. The Spark Core is a convenient solution to this,

since it interfaces directly with the company’s cloud server (api.spark.io) through specially programmed

variables. We were able to set a variable called “Energy”, configure it for cloud updates, and change it

based on incoming UART data without any issues.

Accessing the variable’s value in the cloud was possible through the URL

https://api.spark.io/v1/devices/50ff69065067545628300587/Energy?access_token=46c1613cbbb8ded3

8d1b2c2cebe14ed78ff8d7d3, which displayed the value as part of a JSON token. Unfortunately, the

above URL only works when the Spark Core is on and actively communicating with the cloud server.

18

4 – Costs

4.1 – Parts Table 1 - Parts cost

Part Manufacturer Retail Cost ($) Quantity Actual Cost ($)

ST7540 PLC ST Microelectronics 9.57 3 28.71

MSP430FE4272 Texas Instruments 6.80 2 13.60

Spark Core WiFi Spark 39.00 1 39.00

F24-500-C2 XFMR Triad Magnetics 9.09 3 27.27

3300 μF cap Nichicon 1.97 3 5.91

LM317 Linear Reg Fairchild Semiconductor 0.71 3 2.13

OSTTC032162 Term Block

On Shore Technology 0.51 5 2.55

BK/HBH-I-R Fuse Holder

Cooper Bussmann 3.21 3 9.63

BK/MDL-1/2-R Fuse Cooper Bussmann 0.92 3 2.76

MB12S-TP Rectifier Micro Commercial Co. 0.54 3 1.62

12FR005E Current Sensing Resistor

Ohmite 2.59 2 5.18

Electrical cords unknown 8.99 3 26.97

Various passive components (RLC)

unknown 15.00

PCBs 33.00 3 99.00

TOTAL 279.33

4.2 – Labor Table 2 - Labor costs

Name Hourly Rate ($) Hours Total ($) = 2.5*Rate*Hours

Matthew Feddersen 30.00 150 11,250.00

Evan Richards 30.00 150 11,250.00

Saheed Rosenje 30.00 150 11,250.00

TOTAL 33,750.00

4.3 – Total Table 3 - Total costs

Item Total ($)

Parts Cost 279.33

Labor Cost 33,750.00

Total Cost 34,029.33

19

5 – Conclusion

5.1 – Accomplishments We were able to design and implement a steady and stable power supply circuit to convert 120

Vac mains power to 12 V and 5 V dc power. In our measurement circuit, our current measuring shunt

resistor behaved as expected. With the microcontroller, we successfully initialized and operated the ADC

and UART modules, as well as the finicky ESP430 module. While it output noisy values, it worked

nonetheless.

Overall, we gained an immense amount of knowledge and experience in research,

communication, experimentation and planning. We learned many hard lessons, but came out with more

experience and ability in the end.

5.2 – Failed Requirements Unfortunately, not all of our portions worked as intended, and as a result the overall project

could not come together. Our issues boiled down to three sections, with details in the Verifications

section of this report.

First, the voltage measurement circuit did not output the correct voltage range. Given our

voltage divider circuit, we expected 181.5 mV maximum across the measuring points when connected to

the 120V mains, but strangely, we measured around 1.3V during our tests.

Second, the ADC inputs to the MSP430 chip were unusually noisy when connected to any and all

test equipment.

Third, the power amplifier of the ST7540 PLC chip did not work as expected. The internal power

amplifier was quite difficult to get working, despite the amount of work that we put into research and

experimentation on it. The precise specifications made it difficult to find a suitable standalone

replacement, and proved to be the downfall of the PLC section of our project. The power amplifier was

important to increase the gain of our signal in order for us to both transmit and receive the signal on the

main power line.

5.3 – Ethical considerations Our project had some inherent safety risks that became ethical concerns in accordance with the

IEEE Code of Ethics [7]. Specifically, the 1st and 9th points:

1. to accept responsibility in making decisions consistent with the safety, health, and welfare of the

public, and to disclose promptly factors that might endanger the public or the environment;

9. to avoid injuring others, their property, reputation, or employment by false or malicious action;

As such, we made sure to address any safety risks involved in working directly with mains power,

ensuring our own safety while working on the project as well as the safety of others while using it.

Our device’s purpose is to allow consumers to be informed of their home appliance power

consumption. This benefits the consumer, as they can use this to be more aware of the energy use and

efficiency of their products. This applies to the 5th point:

5. to improve the understanding of technology; its appropriate application, and potential

consequences;

20

In addition, as a group, we strove to improve our understanding of the formal engineering

design process while offering constructive feedback to each other throughout. While this may be a

capstone design project, there was still plenty to learn and improve upon for each of us. This is

consistent with points 6, 7, 8, and 10 from the IEEE Code of Ethics:

6. to maintain and improve our technical competence and to undertake technological tasks for

others only if qualified by training or experience, or after full disclosure of pertinent limitations;

7. to seek, accept, and offer honest criticism of technical work, to acknowledge and correct errors,

and to credit properly the contributions of others;

8. to treat fairly all persons and to not engage in acts of discrimination based on race, religion,

gender, disability, age, national origin, sexual orientation, gender identity, or gender expression;

10. to assist colleagues and co-workers in their professional development and to support them in

following this code of ethics.

5.4 – Future work There are a number of improvements that we could make to our project in the future. First, a

selection of a different power line communication chip or gaining a further understanding of the

sensitivity of the ST7540 would be beneficial. Finding a PLC chip that works in the FCC regulated

frequency range and has very low power consumption would be the primary objective. We would also

need to find a manufacturer with a lot more documentation on their product and better troubleshooting

assistance since STMicroelectronics had very little response to our inquiries.

Second, we could improve our power supply to be switching instead of based on a linear

regulator. This would both improve our efficiency as well as lower the weight and physical footprint on

our PCB.

Finally, the interface between our measurement circuit and our microcontroller would need to

be worked on, both to improve our measurement accuracy as well as to reduce the noise on our

microcontroller leads.

21

References

[1] ST7540 FSK powerline transceiver design guide for AMR, application note, STMicroelectronics,

January 2010. Available at:

http://www.st.com/web/en/resource/technical/document/application_note/CD00143379.pdf

[2] MSP430FE42x2 Mixed Signal Microcontroller, datasheet, Texas Instruments, Inc., July 2008.

Available at: http://www.ti.com/lit/ds/symlink/msp430fe4272.pdf

[3] F24-500-C2 Class 2/3 Transformer, datasheet, Triad Magnetics, December 2013. Available at:

http://www.mouser.com/ds/2/410/24-500-C2-263457.pdf

[4] MB12S Thru MB110S, datasheet, Micro Commercial Components Corp., July 2014. Available at:

http://www.mccsemi.com/up_pdf/MB12S-MB110S(MBS-1).pdf

[5] KA317/LM317 3-Terminal Positive Adjustable Regulator, datasheet, Fairchild Semiconductor Corp.,

September 2014. Available at: https://www.fairchildsemi.com/datasheets/LM/LM317.pdf

[6] ST7540 FSK powerline transceiver, datasheet, STMicroelectronics, September 2006. Available at:

http://www.st.com/st-web-

ui/static/active/en/resource/technical/document/datasheet/CD00096923.pdf

[7] IEEE Code of Ethics. Available at: http://www.ieee.org/about/corporate/governance/p7-8.html

22

Appendix A – Requirements and Verifications Tables

Table 4 - Measurement and transmitter module requirements and verifications

AC/DC Power Circuit Requirements Verification

Vout = 12 ± 0.2 Vdc

Iout,max = 500 mA

Attach output of the voltage regulator to a 24 Ohm resistor. Plug board into 120V mains outlet. Measure the voltage at the output of the voltage regulator with a multimeter.

Measurement Circuit Requirements Verification

Voltage circuit outputs 0.1815 ± 0.001 Vrms for 120 Vrms line

Current circuit outputs 0.010 ± 0.001 Vrms for 2 A consumer device

Plug board into 120V mains outlet. Measure AC voltage across V+ and V- points. Measure AC voltage of mains.

Plug board into 120V mains outlet, and plug a device rated at 2A into the board. Measure AC voltage across I+ and I- points.

MSP430 Requirements Verification

Able to transmit UART data with accurate timing; any discrepancies are correctable by the receiver

Able to successfully initialize internal ESP430 signal processor module.

Able to read voltage and current data from V+, V-, I+, and I- pins using the internal ESP430 module.

Remove fuse from board. Power the MSP430 with a JTAG connector to a debug interface. Connect TX pin on MSP430 to RX pin on a commercial serial transceiver module that can display received bytes (buspirate, logic analyzer, etc). Run test program on MSP430 that sends a 9600 baud UART message, at a rate of 5 messages per second. The test program will send the digits 1-127 to the receiver, and the receiver display should output those digits in order without error. If there is an error, it should be easily recognizable and correctable.

Remove fuse from board. Power the MSP430 with a JTAG connector to a debug interface. Through the debug interface, run a program that initializes the ESP430, and verify that the program does not stall or crash. After initialization, pause the program and verify that register CTRL0 has been set to a non-zero value.

Remove fuse from board. Power the MSP430 with a JTAG connector to a debug interface. Through the debug interface, run a program that initializes the ESP430 and runs a measurement loop with the built in ADC. Plug the board into the 120V

23

mains outlet, and then plug a load greater than 40 mA into the board. Through the debugging interface, verify that the registers ACTENERGY_LO or ACTENERGY_HI have non-zero values. Also check values for IRMS_LO, IRMS_HI, and VRMS.

PLC Circuit Requirements Verification

PLC successfully receives data from MSP430 with no more than 5% error

PLC successfully transmits data across power line

Insure signals greater than 200 kHz are attenuated.

Feed digital signal into ST7540 chip and verify output transmitted peak signal is between VCC and VCC- 3V and verify its minimum is between 0V and 1.5V. The peak to peak height should be verified to be approximately VPA_OUT.

Monitor input signal of receiver module and verify output transmitted signal matches the input signal.

Input digital signals at frequency ranging from 0Hz, or as low as possible, to 1MHz and verify proper attenuation.

24

Table 5 - Receiver module requirements and verifications

AC/DC Power Circuit Requirements Verification

Vout = 12 ± 0.2 Vdc and 5 ± 0.1 Vdc

Iout,max = 10 mA at 12 V and 300 mA at 5 V

Attach output of the 12 V voltage regulator to a 1.2 kΩ resistor. Attach output of the 5 V voltage regulator to a 16 Ω resistor. Plug board into 120V mains outlet. Measure the voltage at the output of each voltage regulator with a multimeter.

PLC Circuit Requirements Verification

PLC successfully receives data across powerline

Able to filter and demodulate signal from power line

Output correct data from demodulated signal, with 95% accuracy

Test received signal against original transmitted signal. Verify accuracy of compared signals.

Test demodulating digital signal against original transmitted digital signal. Verify accuracy of compared signals.

Test demodulated signal with oscilloscope to verify accuracy of received digital data with the transmitted digital data. Verify accuracy of compared signals.

Spark Core WiFi Requirements Verification

Able to upload data to the cloud in an accessible format

Run a test program that uploads a non-zero value called “Energy” to the address https://api.spark.io/v1/devices/ 50ff69065067545628300587/ Energy?access_token=46c16 13cbbb8ded 38d1b2c2cebe14ed78ff8d7d3. Visit the site to verify that the displayed value is non-zero while code is running.

25

Appendix B – Flowcharts

Figure 17 - MSP430 flowchart

26

Figure 18 - Spark WiFi flowchart

27

Appendix C – Source Code

C.1 – MSP430 Code

#include <msp430.h>

#include "main.h"

/*

* main.c

*/

float energy_val = 0;

float total_energy = 0;

unsigned int firmware_version;

int count = 0;

static unsigned int SD_results[SIZE_SD16_ARRAY];

int results_empty = 0;

void init_clocks(void)

{

FLL_CTL0 |= DCOPLUS + XCAP14PF; // DCO+, Configure load caps

SCFI0 |= FN_4; // x2 DCO frequency, 8MHz

nominal

SCFQCTL = 121; // (121+1) x 32768 x 2 =

7.99 MHz

CCTL0 = OUT; // TXD output

TACTL = TASSEL_2 + MC_2; // SMCLK, continuous mode

}

void init_UART(void)

{

P2SEL = BIT4+BIT5; // P1.0/1 TA0 for TXD/RXD

function

P2DIR = BIT4; // TXD output on P1

U0TCTL |= 0b00110001;

U0BR0 = 0x41; // 8MHz / 9600 =

833.333, which rounds to 0x0341, even better since our "8MHz" is actually 7.99

U0BR1 = 0x03; // the UxBRx registers

act as delay counters between each UART bit, we want 9600 baud

U0MCTL = 0x44;

28

ME1 |= 0x80;

U0CTL &= ~SWRST;

IE1 = 0x80; // enable UART

interrupts

count ++;

}

void init_SD16(void)

{

ESPCTL &= ~ESPEN;

//set SD16 control register

// SD16SSEL_1: use SMCLK as source clock

// SD16DIV_3: divide 8 MHz source clock by 8 to get ADC clock

// SD16REFON: use internal 1.2V reference voltage

SD16CTL = SD16SSEL_1 | SD16DIV_3 | SD16REFON;

//this purportedly adds delay to the ADC clock, necessary for high MCLK frequencies

//0x48 maps to 40ns

SD16CONF0 = 0x48;

SD16INCTL0 = SD16INCH_0 | I1_Gain;

SD16INCTL1 = SD16INCH_0 | V_Gain;

SD16CCTL0 = SD16OSR_256 | SD16IE;

SD16CCTL1 = SD16OSR_256;// | SD16IE;

volatile unsigned int i;

for (i = 0; i < 0x3600; i++); // Delay for 1.2V ref startup

}

void init_ESP(void)

{

ESPCTL |= ESPEN;

MBCTL = 0;

volatile unsigned int timeout;

//set idle

if ((RET0 & 0x8000) != 0)

{

MBOUT1= modeIDLE; // ESP_IDLE;

29

MBOUT0= mSET_MODE;

timeout= 0xffff;

while (((RET0 & 0x8000) != 0) && (timeout-- > 0));

}

//check firmware version

MBOUT0= mSWVERSION;

timeout= 0xffff;

do

{

while (((MBCTL & IN0IFG) == 0) && (timeout > 0)) timeout--;

if (timeout == 0) { return; }

} while (MBIN0 != mSWRDY);

firmware_version= MBIN1; // Save firmware version.

//set parameters

set_parameter(mSET_CTRL0, 0b00101000);

set_parameter(mSET_INTRPTLEVL_LO, 0xFFDD3FD8 & 0xFFFF);

set_parameter(mSET_INTRPTLEVL_HI, (0xFFDD3FD8 >> 16) & 0xFFFF);

set_parameter(mSET_NOMFREQ, 60);

set_parameter(mSET_PHASECORR1, 0);

set_parameter(mSET_STARTCURR_INT, STARTCURR_INT);

set_parameter(mSET_STARTCURR_FRAC, STARTCURR_FRAC);

}

void set_parameter(unsigned int param, unsigned int data)

{

volatile unsigned int timeout= 0xffff;

MBOUT1 = data;

MBOUT0 = param;

do

{

while (((MBCTL & IN0IFG) == 0) && ((--timeout) > 0)) ;

if (timeout == 0)

{

_EINT();

return;

}

} while ((MBIN0 != mPARAMSET) || (MBIN1 != param));

30

}

void start_measurement(void)

{

// // Set event message request flags:

//

// set_parameter(mSET_EVENT, ILREACHEDME + ENRDYME);

//

// total_energy = 0;

//

// MBCTL= IN0IE;

//

// // Start measurement (set Embedded Signal Processing into "Measurement" mode)

// MBOUT1= modeMEASURE; //ESP_MEASURE;

// MBOUT0= mSET_MODE;

// //OP_Mode = measure;

//

// _EINT();

//Using SD16 only, no ESP because we're missing the V+/- correct circuitry

SD16CCTL0 |= SD16SC;

}

void send_test_UART(void)

{

unsigned char data = (unsigned char)count;

U0TXBUF = data;

}

unsigned int calc_peak_val(void)

{

float result = 0;

unsigned int val = 0;

volatile int i=0;

unsigned int max = 0;

unsigned int min = 0x8000;

for(i = 0; i<SIZE_SD16_ARRAY; i++)

{

if((val = SD_results[i]) < 0x8000)

{

31

if (val < min) min = val;

val = 0x8000 + (0x8000 - val);

}

if (val > max) max = val;

result = (result * i + val) / (i+1);

}

result = (0x8000 - result > 0 ? 0x8000 - result : result - 0x8000);

return result;

}

int main (void)

{

WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

init_clocks();

init_UART();

init_SD16();

//init_ESP();

//start_measurement();

_BIS_SR(GIE);

for (;;)

{

volatile unsigned int i;

send_test_UART();

i = 80000; // Delay

do (i--);

while (i != 0);

}

return 0;

}

// SD16 measurement ready interrupt service routine

#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)

#pragma vector=SD16_VECTOR

__interrupt void SD_INT (void)

32

#elif defined(__GNUC__)

void __attribute__ ((interrupt(SD16_VECTOR))) SD_INT (void)

#else

#error Compiler not supported!

#endif

{

static unsigned int index = 0;

static unsigned int skip = 0;

static unsigned int peak = 0;

if (SD16IV == 4 || SD16IV == 6)

{

SD_results[index] = SD16MEM0;

if (SD_results[index] > skip)

skip = SD_results[index];

if (++index == SIZE_SD16_ARRAY)

{

peak = calc_peak_val();

//float rms_val = peak / 1.4142135;

U0TXBUF = (unsigned char)((int)(peak));

index = 0;

__delay_cycles(1000);

}

}

}

// ESP energy measurement interrupt service routine

#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)

#pragma vector=ESP430_VECTOR

__interrupt void ESP_INT (void)

#elif defined(__GNUC__)

void __attribute__ ((interrupt(ESP430_VECTOR))) ESP_INT (void)

#else

#error Compiler not supported!

#endif

{

//Energy calculation is 32-bit energy value from registers, divided by pre-calculated Cz value

(result is in Watt-seconds)

energy_val = IRMS_HI * kI; //changing from ACTENERGY1_HI to

total_energy += energy_val;

long temp = (long)(energy_val*1000);

33

//shifting by 3, converting to 8 bit value

//max should theoretically be 1800, which takes 11 bits

//shift 3 to reduce the resolution to multiples of 8, but fits within the byte size packet

U0TXBUF = (unsigned char)(temp); //set TX buffer to be the data, this should trigger the TX

send

}

#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)

#pragma vector=USART0TX_VECTOR

__interrupt void UART_TX (void)

#elif defined(__GNUC__)

void __attribute__ ((interrupt(USART0TX_VECTOR))) UART_TX (void)

#else

#error Compiler not supported!

#endif

{

count = 100;

}

----------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------

/*

* main.h

*

* Created on: Nov 30, 2014

* Author: Matthew

*/

#ifndef MAIN_H_

#define MAIN_H_

#define SIZE_SD16_ARRAY 100 //we'll see how this does for now

#define I1_Gain SD16GAIN_4

#define V_Gain SD16GAIN_2

34

#define kV 0.00610666

#define kI 0.00091554

#define kE 0.00559090

#define Cz 178862.078

#define CALVOLTAGE 120

#define CALCURRENT 10

#define CalPhi 60

#define CalCosPhi 0.5

#define CalFreq 60

#define STARTCURR (0.004 * CALCURRENT)

#define STARTCURR_INT (unsigned int)((STARTCURR * 1000 / kI))

#define STARTCURR_FRAC (unsigned int)((STARTCURR * 0x010000 * 1000 / kI) - (0x010000

* STARTCURR_INT))

#define VAL_CTRL0 0x0200

#define VAL_EVENT 0x0204

#define VAL_PHASECORR1 0

#define VAL_V1OFFSET 0

#define VAL_I1OFFSET 0

#define VAL_GAINCORR1 0x4000 //default gain = 2^14

#define VAL_POFFSET1_LO 0x0216

#define VAL_POFFSET1_HI 0x0218

#define VAL_INTRPTLEVL_LO 0x0220

#define VAL_INTRPTLEVL_HI 0x0222

#define VAL_CALCYCLCNT 0x0224

#define VAL_STARTCURR_FRAC 0x0226

#define VAL_STARTCURR_INT 0x0228

#define VAL_NOMFREQ 60 //nominal frequency = 60 Hz

#define VAL_VDROPCYCLS 0x022C

#define VAL_VDROPLEVEL 0x0232

#define VAL_VPEAKLEVEL 0x0234

#define VAL_IPEAKLEVEL 0x0236

#define VAL_DCREMPER 0x0238

void init_clocks(void);

void init_UART(void);

void init_SD16(void);

void init_ESP(void);

void start_measurement(void);

void set_parameter(unsigned int param, unsigned int data);

35

void send_test_UART(void);

unsigned int calc_peak_val(void);

#endif /* MAIN_H_ */

C.2 – Spark Core Code

int energy = 0;

unsigned long lastReadTime = 0;

unsigned long currReadTime = 0;

int clk_pin = A7;

void setup() {

Spark.variable("Energy", &energy, INT);

Serial.begin(9600);

Serial1.begin(9600);

energy = 0;

tone(clk_pin, 32000, 0);

}

void loop() {

if(Serial1.available())

{

int inByte = Serial1.read();

int val = inByte-128; //get rid of stop bit

Serial.write(val);

currReadTime = millis();

if (lastReadTime == 0)

{

//ignore first read

lastReadTime = currReadTime;

return;

}

float diff = (currReadTime - lastReadTime) / 1000.0; // delay in seconds from last transmission

energy = val; //result should be in watt seconds

}

}