Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and...

33
© 2017 NXP B.V. Smart Plug Software Design Reference Manual 1. Introduction This design reference manual describes a solution for a smart plug based on the MKM34Z64 microcontroller. This micro controller is part of the Kinetis-M microcontroller family. The smart plug reference design is intended for measurement of energies in single-phase, electronic power monitor, and wireless control. The solution can be extended to various areas, including lighting monitor, machine room power distribution, and so on. It can help the customer develop a competitive product by providing design documents. The Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive energies. With the Wi-Fi chip, use UART, and predefined command protocol to implement status display and function setting from mobile application to socket. NXP Semiconductors Document Number: DRM158 Design Reference Manual Rev. 0 , 03/2017 Contents 1. Introduction .................................................................... 1 2. Features .......................................................................... 2 3. Software module ............................................................. 2 3.1 Software environment introduction ........................ 2 3.2. Packet communication ........................................ 10 3.3 Communication protocol ..................................... 10 3.4 Power metering................................................... 28 3.5 Others................................................................ 32 4. Revision history ............................................................ 32

Transcript of Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and...

Page 1: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

© 2017 NXP B.V.

Smart Plug Software Design Reference

Manual

1. Introduction

This design reference manual describes a solution for a

smart plug based on the MKM34Z64 microcontroller.

This micro controller is part of the Kinetis-M

microcontroller family. The smart plug reference design

is intended for measurement of energies in single-phase,

electronic power monitor, and wireless control. The

solution can be extended to various areas, including

lighting monitor, machine room power distribution, and

so on. It can help the customer develop a competitive

product by providing design documents.

The Smart Plug Solution is based on the MKM14Z64

and implements measurement of grid voltage, current,

frequency, and active and reactive energies. With the

Wi-Fi chip, use UART, and predefined command

protocol to implement status display and function

setting from mobile application to socket.

NXP Semiconductors Document Number: DRM158

Design Reference Manual Rev. 0 , 03/2017

Contents

1. Introduction .................................................................... 1 2. Features .......................................................................... 2 3. Software module ............................................................. 2

3.1 Software environment introduction ........................ 2 3.2. Packet communication ........................................ 10 3.3 Communication protocol ..................................... 10 3.4 Power metering ................................................... 28 3.5 Others................................................................ 32

4. Revision history ............................................................ 32

Page 2: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

2 NXP Semiconductors

2. Features

NXP Smart Plug Solution Software functions:

Socket on/off status display and setting up

Display of current active and reactive power, apparent power, and power consumption history

Display of historic running time

Time and timer setting up

3. Software module

The software module consists of three main parts. These are command communication, communication

protocol, and power measurement.

3.1 Software environment introduction

3.1.1. Development Tool

Compiling: IAR 7.2

Debug: JTag SWD

3.1.2. Source code structure

Root directory description:

Figure 1. Root directory

Table 1. Root directory description

Root directory Description

Projects IAR project files of Smart Plug project

Src Source code of drivers and smart plug for MKM14Z64

Page 3: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 3

Project source code directory description:

Figure 2. Project source code directory

Table 2. Project source code directory description

Project source code Description

Common Initialization and common code for MKM14Z64

Drivers Driver code for MKM14Z64

Fraclib Float mathematics library

Meterlib Metering library

Projects Source code of smart plug project

Source code description of smart plug project:

Figure 3. Source code

Table 3. Source code description

Source code Description

Local Driver code for SPI and UART

Appconfig.h Macro and interrupt settings for ADC, SIM, CMP, TMR

Config.c, config.h Global structure processing for metering

Fsl_smart_plug_cmd.h Smart Plug serial communication protocol header file

Mk141ph.h Metering parameter configurations

Nvm.c nvm.h Driver for writing nvm flash

Smart_plug.c Smart plug main file for metering and socket control

Smart_plug.h Smart plug software configurations

Page 4: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

4 NXP Semiconductors

3.1.3 Using Alljoyn APP

Smart Plug uses Alljoyn APP in mobile devices to communicate with the smart plug. The current

version of the Alljoyn APP apk is 1.3.2.

3.1.3.1 APP Installation

Copy AllJoynOn-1.3.2.apk to your Android mobile device and install it.

3.1.3.2 APP Usage

1. Plug the smart plug to a power supply.

2. Open the Alljoyn application by clicking on the icon in the following figure:

Figure 4. ALLJoyn ON icon

3. Click the “Configure” button at the top right hand corner of the device list page:

Figure 5. Configure button on device list page

Page 5: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 5

Figure 6. Application settings

4. Configure WIFI router and password.

Figure 7. Setup home Wi-Fi network

Page 6: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

6 NXP Semiconductors

5. Return to the device list page and check if any of the devices have a yellow icon next to them,

this indicates that they are unconfigured.

Figure 8. Unconfigured devices

Page 7: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 7

6. Click the yellow unconfigured device and enter the device configuration page.

Figure 9. Setup device step 1

7. Click on Next.

Figure 10. Setup device step 2 - configuration

8. Wait for connection.

Figure 11. Setup device step 3 - connection

9. If you are prompted to enter the phrase password, enter “123456” (the password can be changed

in the options section of the device control page).

Page 8: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

8 NXP Semiconductors

10. Enter the device name, then click on “finish”.

Figure 12. Setup device step 3 - enter device name

11. Configuration is now complete and you will see the available device with a green icon next to it

indicating that it is available. The configured but not unavailable devices have grey icons.

Figure 13. Nearby devices list

Page 9: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 9

12. Click the device and open the device control page.

Figure 14. Device control page

The device has the following features:

1. Current active power

2. Current reactive power

3. Current apparent power

4. History active energy consumption until now

5. Current grid frequency

6. History running time

7. Current on/off status of plug and on/off control

8. Wi-Fi power save mode

9. Timer control

10. Smart plug current time set-up.

Page 10: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

10 NXP Semiconductors

3.2. Packet communication

3.2.1 Communication hardware settings

MKM14Z64 uses UART1 as the communication port to connect with the external Wi-Fi chip. The uart

settings are:

• Bit width 9-bit (1 start bit, 8 data bit)

• Baud rate: 9600

• No parity check

• Full duplex

3.2.2 Communication port parameters configuration

Port parameters can be configured in smart_plug.h.

Macros that can be configured are:

• SMTPLUG_SYS_CLK_FREQ KM

Current system clock setting. By default the system clock is set to 24M.

• SMTPLUG_COMM_BAUDRATE

Port baud rate setting. By default this is set to 9600.

• SMTPLUG_COMM_IRQ_MODE

Use IRQ mode for UART communication. This macro cannot be set simultaneously with

SMTPLUG_COMM_USE_DMA.

• SMTPLUG_COMM_USE_DMA

Use DMA mode for UART communication. This macro cannot be set simultaneously with

SMTPLUG_COMM_IRQ_MODE.

3.3 Communication protocol

3.3.1 Command and response structure

A command is composed of a command header and a data body, sent from the KM to the Wi-Fi chip, or

vice versa.

As the communicated data packages are not too large, the current design uses a fixed 8 bytes data

transfer, therefore every message header and data body are 8 bytes. The application can cut out data

from the data body according to the data length in the command header.

Magic Num 1 Magic Num 2 LCParameter 3Parameter 2Parameter 1Command

ClassOp Code

1 Byte 1 Byte 1 Byte 1 Byte 1 Byte 1 Byte 1 Byte 1 Byte

Figure 15. Command header

Page 11: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 11

Send Data

8 Bytes

Figure 16. Data body

NOTE

1. Two magic numbers (0x73 and 0x70) are added at the top of the

command header. These two magic numbers will be ignored in the

below command explanation.

2. Command response structure also uses this structure.

3.3.2 Wi-Fi control command

The Wi-Fi control command is used by the KM to control the Wi-Fi chip. The Wi-Fi chip need to parse

the command and execute accordingly the command. The following figure uses the K20 QCA board as

an example.

KM BoardK20 WIFI QCA

Board

Command

Response

Figure 17. Wi-Fi control command

3.3.2.1 SMTPLG_CMD_OP_WIFI_INIT

This command initializes the Wi-Fi board and get the initialization status.

NOTE

The execution of this command is decided by the Wi-Fi board. If the

Wi-Fi board needs the KM to control initialization, it can do initialization

after getting this command; if the Wi-Fi board will initialize itself at

power up, this command can just get the initialization status.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_WIFI_CTL

Operation code SMTPLG_CMD_OP_WIFI_INIT

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

Page 12: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

12 NXP Semiconductors

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_WIFI_CTL

Operation code SMTPLG_CMD_OP_WIFI_INIT

Parameter 1 1 - Initialization is done

0 - Initializing

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body: No data, ignored.

3.3.2.2 SMTPLG_CMD_OP_WIFI_RESET

This command resets the Wi-Fi board.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_WIFI_CTL

Operation code SMTPLG_CMD_OP_WIFI_RESET

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_WIFI_CTL

Operation code SMTPLG_CMD_OP_WIFI_RESET

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body: No data, ignored.

3.3.2.3 SMTPLG_CMD_OP_WIFI_LP_MODE

This command sets the Wi-Fi board to low power mode.

In low power mode, Wi-Fi will be disconnected and the mobile APP cannot control the smart plug.

• Send command:

— Command header:

Page 13: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 13

Command class SMTPLG_CMD_CLS_WIFI_CTL

Operation code SMTPLG_CMD_OP_WIFI_LP_MODE

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_WIFI_CTL

Operation code SMTPLG_CMD_OP_WIFI_LP_MODE

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body: No data, ignored.

3.3.2.4 SMTPLG_CMD_OP_WIFI_PWR_SAVE_MODE

This command sets the Wi-Fi board to power saving mode.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_WIFI_CTL

Operation code SMTPLG_CMD_OP_WIFI_PWR_SAVE_MODE

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_WIFI_CTL

Operation code SMTPLG_CMD_OP_WIFI_PWR_SAVE_MODE

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body: No data, ignored.

3.3.2.5 SMTPLG_CMD_OP_WIFI_NORMAL_MODE

This command sets the Wi-Fi board to normal mode.

• Send command:

— Command header:

Page 14: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

14 NXP Semiconductors

Command class SMTPLG_CMD_CLS_WIFI_CTL

Operation code SMTPLG_CMD_OP_WIFI_NORMAL_MODE

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_WIFI_CTL

Operation code SMTPLG_CMD_OP_WIFI_NORMAL_MODE

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body: No data, ignored.

3.3.2.6 Application command

APP commands send procedure (For instance the example below uses K20 QCA Wi-Fi board as an

example):

KM BoardK20 WIFI QCA

BoardPhone APP

Figure 1 Mobile APP command

APP commands are sent from APP. The Wi-Fi board will forward the command to KM directly without

any parsing.

KM will parse and execute the command and send the response back to the APP.

The Wi-Fi board needs to decide whether to do parsing according to the command class in the command

header. For the APP command, no parsing is needed, otherwise, Wi-Fi control commands, parsing is

needed and executes the command.

3.3.2.7 SMTPLG_CMD_OP_SET_PWR_ONOFF

This command turns On/Off the smart plug power supply.

• Send command:

Page 15: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 15

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_SET_PWR_ONOFF

Parameter 1 0×1 - On

0×0 - Off

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_WIFI_APP

Operation code SMTPLG_CMD_OP_SET_PWR_ONOFF

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body: No data, ignored.

3.3.2.8 SMTPLG_CMD_OP_SET_WIFI_RESET

This command resets the Wi-Fi.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_SET_WIFI_RESET

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_SET_WIFI_RESET

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body: No data, ignored.

Page 16: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

16 NXP Semiconductors

3.3.2.9 SMTPLG_CMD_OP_GET_PWR_STS

This command gets the current On/Off status of the smart plug.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_GET_PWR_STS

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_GET_PWR_STS

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body:

Data On/Off status

1 - On

0 - Off

3.3.2.10 SMTPLG_CMD_OP_SET_PWRSAVE_MODE

This command turns On/Off the power saving mode of the smart plug.

This command sets the smart plug to power saving mode when idle.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_SET_PWRSAVE_MODE

Parameter 1 1 - On

0 - Off

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

Page 17: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 17

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_WIFI_SET_PWRSAVE_MODE

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body: No data, ignored.

3.3.2.11 SMTPLG_CMD_OP_GET_PWRSAVE_MODE

This command gets the power saving setting status of the smart plug.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_GET_PWRSAVE_MODE

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_GET_PWRSAVE_MODE

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body:

Data Power saving status

1 - On

0 - Off

Page 18: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

18 NXP Semiconductors

3.3.2.12 SMTPLG_CMD_OP_GET_TOTAL_ACT_PWR_SUM

This command gets the history energy consumption. In the current design, the value of this history

energy consumption indicates energy consumption from the moment that smart plug is plugged to the

socket. When the smart plug is removed, this value will be reset to 0.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_GET_TOTAL_ACT_PWR_SUM

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_WIFI_APP

Operation code SMTPLG_CMD_GET_TOTAL_ACT_PWR_SUM

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 1 – Negative

0 – Positive

Data length 8

— Data body:

Data History active power energy consumption.

— Data format:

The value of history energy consumption is a float data, which is stored as 8 bytes, big endian. The first

4 bytes indicate the integer part, the latter 4 bytes indicate the fractional part. The sign byte indicates

whether it is a positive value or not.

Fractional Part

4 Bytes 4 Bytes

Integer Part

3.3.2.13 SMTPLG_CMD_OP_GET_HIST_TIME

This command gets the history running time of the smart plug. In the current design, the value of this

time is the duration from the moment that the smart plug is plugged to the socket. When the smart plug

is removed, this value will be reset to 0.

Page 19: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 19

The time value uses a 32-bit integer to indicate total minutes. This data is stored in big endian mode.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_GET_HIST_TIME

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

– Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_SET_WIFI_RESET

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 4

— Data body:

Data History running time, 32-bit integer, big integer.

3.3.2.14 SMTPLG_CMD_OP_GET_ACT_PWR

This command gets the current active power of the smart plug.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_GET_ACT_PWR

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Page 20: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

20 NXP Semiconductors

Operation code SMTPLG_CMD_OP_GET_ACT_PWR

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 1 – Negative

0 – Positive

Data length 8

— Data body:

Data Value of active power

— Data format:

The value of history energy consumption is a float data, which is stored as 8 bytes, big endian. The first

4 bytes indicate the integer part, the latter 4 bytes indicates the fractional part. The sign byte indicates

whether it is a positive value or not.

Fractional Part

4 Bytes 4 Bytes

Integer Part

3.3.2.15 SMTPLG_CMD_OP_GET_REACT_PWR

This command gets the current reactive power of the smart plug.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_GET_REACT_PWR _MODE

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_WIFI_GET_REACT_PWR

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 1 – Negative

0 – Positive

Page 21: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 21

Data length 0

— Data body:

Data Value of reactive power

— Data format:

The value of history energy consumption is a float data, which is stored as 8 bytes, big endian. The first

4 bytes indicate the integer part, the latter 4 bytes indicate the fractional part. The sign byte indicates

whether it is a positive value or not.

Fractional Part

4 Bytes 4 Bytes

Integer Part

3.3.2.16 SMTPLG_CMD_OP_GET_TOTAL_PWR

This command gets the current apparent power of smart plug.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_GET_TOTAL_PWR

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_GET_TOTAL_PWR

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 1 – Negative

0 – Positive

Data length 0

— Data body:

Data Value of apparent power

— Data format:

Page 22: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

22 NXP Semiconductors

The value of history energy consumption is a float data, which is stored as 8 bytes, big endian. The first

4 bytes indicate the integer part, latter 4 bytes indicates the fractional part. The sign byte indicates

whether it is a positive value or not.

Fractional Part

4 Bytes 4 Bytes

Integer Part

3.3.2.17 SMTPLG_CMD_OP_GET_GRID_FREQ

This command gets the current grid frequency.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_GET_GRID_FREQ

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_WIFI_APP

Operation code SMTPLG_CMD_GET_GRID_FREQ

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 8

— Data body:

Data Grid frequency value

— Data format:

The value of history energy consumption is a float data, which is stored as 8 bytes, big endian. The first

4 bytes indicate the integer part, latter 4 bytes indicates the fractional part. The sign byte indicates

whether it is a positive value or not.

Page 23: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 23

Fractional Part

4 Bytes 4 Bytes

Integer Part

3.3.2.18 SMTPLG_CMD_OP_GET_TIMER_BEGIN

This command gets the begin time of a timer by timer index.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_GET_TIMER_BEGIN

Parameter 1 0

Parameter 2 Timer index (value is 0 when there is only one timer)

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_GET_TIMER_BEGIN

Parameter 1 Timer index (value is 0 when there is only one timer)

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 3

— Data body:

Data Time value

— Data format:

Follow hour, min and sec variable in SMTPLUG_TM structure in fsl_smart_plug_cmd.h.

3.3.2.19 SMTPLG_CMD_OP_GET_TIMER_END

This command gets the end time of a timer by timer index.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_GET_TIMER_END

Page 24: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

24 NXP Semiconductors

Parameter 1 0

Parameter 2 Timer index (value is 0 when there is only one timer)

Parameter 3 0

Data length 0

— Data body: No data, ignored.

Response:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_GET_TIMER_END

Parameter 1 Timer index (value is 0 when there is only one timer)

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 3

— Data body:

Data Time value

— Data format:

Follow the hour, min and sec variable in SMTPLUG_TM structure in fsl_smart_plug_cmd.h.

3.3.2.20 SMTPLG_CMD_OP_SET_TIMER_BEGIN

This command sets the begin time of a timer by timer index.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_SET_TIMER_BEGIN

Parameter 1 0

Parameter 2 Timer index (value is 0 when there is only one timer)

Parameter 3 0

Data length 3

— Data body:

Data Time value

— Data format:

Follow the hour, min and sec variable in SMTPLUG_TM structure in fsl_smart_plug_cmd.h.

Response:

— Command header:

Page 25: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 25

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_SET_TIMER_BEGIN

Parameter 1 Timer index (value is 0 when there is only one timer)

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body: No data, ignored.

3.3.2.21 SMTPLG_CMD_OP_SET_TIMER_END

This command sets the end time of a timer by timer index.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_SET_TIMER_END

Parameter 1 0

Parameter 2 Timer index (value is 0 when there is only one timer)

Parameter 3 0

Data length 3

— Data body:

Data Time value

— Data format:

Follow the hour, min and sec variable in SMTPLUG_TM structure in fsl_smart_plug_cmd.h.

Response:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_SET_TIMER_END

Parameter 1 Timer index (value is 0 when there is only one timer)

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body: No data, ignored.

3.3.2.22 SMTPLG_CMD_OP_SET_TIMER_ONOFF

This command turns On/Off a timer by timer index.

• Send command:

Page 26: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

26 NXP Semiconductors

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_SET_TIMER_ONOFF

Parameter 1 Timer status

1 - On

0 - Off

Parameter 2 Timer index (value is 0 when there is only one timer)

Parameter 3 0

Data length 0

— Data body: No data, ignored.

— Data format:

Follow the hour, min and sec variable in SMTPLUG_TM structure in fsl_smart_plug_cmd.h.

Response:

— Command header:

Command class SMTPLG_CMD_CLS_WIFI_APP

Operation code SMTPLG_CMD_GET_TOTAL_ACT_PWR_SUM

Parameter 1 Timer index (value is 0 when there is only one timer)

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body: No data, ignored.

3.3.2.23 SMTPLG_CMD_OP_SET_CONN_PASSWD

This command sets the connection password of the smart plug.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_SET_CONN_PASSWD

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length Password length (within 8 bytes)

— Data body:

Data Connection password string.

Response:

Page 27: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 27

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_SET_CONN_PASSWD

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body: No data, ignored.

3.3.2.24 SMTPLG_CMD_OP_SET_CUR_TIME

This command sets the current time of the smart plug.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_SET_CUR_TIME

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 7

— Data body:

Data Current time value.

— Data format:

Follow the SMTPLUG_Date_Time structure in fsl_smart_plug_cmd.h.

NOTE

In the current design, variable year is stored in little endian mode.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_SET_CUR_TIME

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 0

— Data body: No data, ignored.

Page 28: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

28 NXP Semiconductors

3.3.2.25 SMTPLG_CMD_OP_GET_CUR_TIME

This command gets the current time of the smart plug.

• Send command:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_GET_CUR_TIME

Parameter 1 0

Parameter 2 0

Parameter 3 0

Data length 0

— Data body: No data, ignored.

• Response:

— Command header:

Command class SMTPLG_CMD_CLS_APP

Operation code SMTPLG_CMD_OP_SET_CUR_TIME

Parameter 1 0

Parameter 2 0×90 - Successful

0×60 - Failed

Parameter 3 0

Data length 7

— Data body:

Data Current time value

— Data format::

Follow the SMTPLUG_Date_Time structure in fsl_smart_plug_cmd.h.

NOTE

In the current design, the variable year is stored in little endian mode.

3.4 Power metering

3.4.1 Metering settings

Smart Plug project provide two metering library, filter library and fft library.

The metering algorithms perform computation in either the time or frequency domain.

Filter lib calculates all billing and non-billing quantities in the time domain and FFT lib in the frequency

domain.

Page 29: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 29

Figure 18. Summary of library

3.4.2 Filter library

Figure 19. Filter library framework

Page 30: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

30 NXP Semiconductors

For further information, see the following link:

http://www.nxp.com/products/microcontrollers-and-processors/arm-processors/kinetis-cortex-m-

mcus/m-series-metrology-m0-plus/kinetis-km3x-5075-mhz-precision-metrology-with-segment-lcd-

mcus-based-on-arm-cortex-m0-plus:KM3x?fpsp=1&tab=Documentation_Tab&lang_cd=en

Filter-Based Algorithm for Metering Applications (document AN4265).

The filter library is located in Meterlib directory,meterlib.a meterlib.h. The fraction library is located in

the Fraclib directory, fraclib.a fraclib.h.

3.4.2 FFT library

Figure 20. FFT library framework

For further information, see the following link:

http://www.nxp.com/products/microcontrollers-and-processors/arm-processors/kinetis-cortex-m-

mcus/m-series-metrology-m0-plus/kinetis-km3x-5075-mhz-precision-metrology-with-segment-lcd-

mcus-based-on-arm-cortex-m0-plus:KM3x?fpsp=1&tab=Documentation_Tab&lang_cd=en

FFT-Based Algorithm for Metering Applications (document AN4255).

Using an FFT on the Sigma-Delta ADCs (document AN4847).

The FFT library is located in the fftlib directory, meterlibFFT2.a metering2.h. The fraction library is

located in the Fraclib directory, fraclib.a fraclib.h.

Page 31: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Software module

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

NXP Semiconductors 31

3.4.3 Metering library comparison

1. FFT library performance

Number of FFT samples Computing time [ms] MCU execution cycles

8 0.24 11513

16 0.55 26384

32 1.13 54208

64 2.42 116093

128 5.36 257131

• CPU Clock = 47.972352 MHz

• Compiler optimization = high speed

• Grid frequency = 50 Hz

• ARM® Cortex®-M0+ core

2. Filter library performance

Phase number Computing time [ms] MCU execution cycles

1PH 0.20 9586

2PH 0.38 18130

3PH 0.63 30218

• CPU Clock = 47.972352 MHz

• Compiler optimization = high speed

• Grid frequency = 50 Hz

• ARM Cortex-M0+ core

3. Resource usage

As a typical 3PH power meter design, resource usage:

Resource usage

(3PH)

Flash RAM CPU loading CAL period

Filter 34 KB 4.3 KB 75% 1200 Hz

FFT (interpolation)

N=64

29 KB 8 KB 70% 50 Hz

FFT (synchronous)

N=64

22 KB 5 KB 51% 50 Hz

NOTE:

The Flash resource includes application and meter lib code size only.

Page 32: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Revision history

Smart Plug Software Design Reference Manual, Rev. 0, 03/2017

32 NXP Semiconductors

3.5 Others

3.5.1 Cross zero detection

The smart plug will switch the relay when cross zero is detected to reduce spark and increase life of

relay.

Procedure:

1. Enable cross zero detection when CMP module is initialized.

2. When you need to switch to the relay, the plug will check the variable cmp_czd_detected first.

3.5.2 Key behavior

By default, pressing the key on the smart plug will switch the relay and send an Init command to the Wi-

Fi board (please check pulse_output() function for implementation).

This behavior can be changed as per requirements.

4. Revision history Table 4. Revision history

Revision number Date Substantive changes

0 03/2017 Initial release

Page 33: Smart Plug Software Design Reference ManualThe Smart Plug Solution is based on the MKM14Z64 and implements measurement of grid voltage, current, frequency, and active and reactive

Document Number: DRM158 Rev. 0

03/2017

How to Reach Us:

Home Page:

nxp.com

Web Support:

nxp.com/support

Information in this document is provided solely to enable system and software

implementers to use NXP products. There are no express or implied copyright licenses

granted hereunder to design or fabricate any integrated circuits based on the

information in this document. NXP reserves the right to make changes without further

notice to any products herein.

NXP makes no warranty, representation, or guarantee regarding the suitability of its

products for any particular purpose, nor does NXP assume any liability arising out of

the application or use of any product or circuit, and specifically disclaims any and all

liability, including without limitation consequential or incidental damages. “Typical”

parameters that may be provided in NXP data sheets and/or specifications can and do

vary in different applications, and actual performance may vary over time. All operating

parameters, including “typicals,” must be validated for each customer application by

customer’s technical experts. NXP does not convey any license under its patent rights

nor the rights of others. NXP sells products pursuant to standard terms and conditions

of sale, which can be found at the following address:

nxp.com/SalesTermsandConditions.

NXP, the NXP logo, Freescale, and Kinetis are trademarks of NXP B.V. All other

product or service names are the property of their respective owners.

ARM, the ARM Powered logo, and Cortex are registered trademarks of ARM Limited (or

its subsidiaries) in the EU and/or elsewhere. All rights reserved.

© 2017 NXP B.V.