ATSAMW25
AT16267: Firmware User Guide on JD Smart Cloud
Service Integration with Smart Plug
APPLICATION NOTE
Introduction
®
Atmel is always powering up the IoT design for unlimited possibilities. This
reference design, Atmel smart plug, is a highly integrated typical IoT
solution. Atmel brings innovative, ease of use MCU, Connectivity, Security,
and Sensing portfolio into this reference design.
This document focuses on smart plug firmware design based on JD smart
cloud, provided by JD, a Chinese retail giant. It explains the smart plug
functions, firmware structure, and communication process, etc.
Features
•
•
•
•
•
•
•
MCU + Connectivity + Security + Sensing
Comprehensive system solution integrated with JD Smart Cloud and
Weilian APP
Smart plug HTML5 UI/lua script included in JD Weilian APP
®
®
ATSAMW25 – Fully-integrated MCU + IEEE 802.11 b/g/n Wi-Fi
solution
ATECC508A – Elliptic Curve Cryptography (ECC) based crypto device
ATM90E26 – High-performance wide-span metering AFEs
US, CN, and EU plug standard compliant.
Figure -1. Atmel Smart Plug - US, CN, and EU Standard
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application
Note-04/2016
Table of Contents
Introduction......................................................................................................................1
Features.......................................................................................................................... 1
1. Overview....................................................................................................................4
1.1.
1.2.
Smart Plug Block Diagram........................................................................................................... 4
Smart Plug Functions................................................................................................................... 5
2. Development Tools.................................................................................................... 7
3. Firmware Architecture................................................................................................8
3.1.
3.2.
3.3.
3.4.
Wi-Fi Connectivity.........................................................................................................................9
Sensor Control..............................................................................................................................9
QTouch PTC Library.....................................................................................................................9
JD Protocol Library.......................................................................................................................9
4. Function Blocks........................................................................................................11
4.1.
4.2.
4.3.
4.4.
Wi-Fi Function.............................................................................................................................11
4.1.1.
Wi-Fi Provision............................................................................................................. 11
4.1.2.
Communication with JD Smart Cloud and APP........................................................... 11
4.1.3.
UDP Device Discovery................................................................................................ 12
4.1.4.
Local Control................................................................................................................13
4.1.5.
Cloud Control...............................................................................................................13
4.1.6.
Local Stream Data....................................................................................................... 14
Sensor Reading..........................................................................................................................15
4.2.1.
Internal Temperature Sensor....................................................................................... 15
4.2.2.
Energy Measurement.................................................................................................. 15
User Interface.............................................................................................................................16
4.3.1.
Touch Button................................................................................................................16
4.3.2.
LED Indicator............................................................................................................... 16
OTAU..........................................................................................................................................16
4.4.1.
MCU Memory Map.......................................................................................................16
4.4.2.
OTAU Flow...................................................................................................................17
4.4.3.
Bootloader................................................................................................................... 18
5. Source Project Structure..........................................................................................19
6. Main API Introduction.............................................................................................. 20
6.1.
6.2.
6.3.
6.4.
6.5.
6.6.
6.7.
6.8.
Wi-Fi Connectivity API................................................................................................................20
Smart Config Library APIs..........................................................................................................20
JD Protocol Library APIs............................................................................................................ 20
JD Communication APIs.............................................................................................................21
Smart Plug APIs......................................................................................................................... 21
OTAU APIs................................................................................................................................. 22
Sensor Reading APIs................................................................................................................. 22
UI APIs....................................................................................................................................... 22
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
2
6.9.
Software Timer APIs...................................................................................................................23
7. Memory Footprint.....................................................................................................24
8. Revision History.......................................................................................................25
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
3
1.
Overview
The smart plug showcases a typical IoT application by integrating Atmel MCU, Connectivity, Security, and
Sensing devices. It works with JD smart cloud and its Weilian APP, following JD Joylink Protocol, which
defines communication protocol, security mechanism, network entry, and device discovery. The source
projects with image/HTM5 UI files are attached with this document which can be downloaded from Atmel
web site.
JD smart cloud provides the customers with Wi-Fi or sensor solutions, Cloud services, user data analytic
service, etc.
Below is the smart plug work-flow which is for JD smart cloud based IoT system designs.
Figure 1-1. Smart Plug Work Flow
As shown in the figure above, there are two working stages. The first one is adding new plug stage, which
is also known as provisioning stage. The second one is data exchange stage, in which the plug has been
added into any existing Wi-Fi network and can be operated via the JD Weilian APP.
1.1.
Smart Plug Block Diagram
Smart plug usually works as an edge node in a typical IoT scenario. The block diagram of smart plug is
shown in the figure below.
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
4
Figure 1-2. Smart Plug Block Diagram
The key part of this reference design is ATSAMW25-MR510PB – a fully-integrated MCU + IEEE 802.11
b/g/n Wi-Fi module. In this compact-sized module, Atmel provides a value added system solution with
industry leading MCU (ATSAMD21G18) + Connectivity (ATWINC1500) + Security (ATECC508A) devices.
Note: ATECC508A is not used in this JD based firmware which follows JD's software based security
mechanism.
Beside the module, a hardware metrology chip (ATM90E26) is used to measure electrical output of the
plug. The other functions like LED indicator, touch button, output control, and temperature sensor are fully
taken care of by the MCU embedded in the module.
For more details about the hardware design, refer to AT16225 - Atmel Smart Plug Hardware User Guide.
1.2.
Smart Plug Functions
As a plug, it has the basic ON/OFF control and indication functions. Furthermore, Atmel smart plug also
showcases some advanced functions:
•
•
•
•
•
•
•
ON/OFF local/remote control by JD Weilian APP
Dedicated HTML5 UI in JD Weilian APP
Connection status indication (online/offline)
Touch button control
Programmable 7-day/week schedules (day/hour/minute)
Energy/Voltage/Current/Power measurement
Device temperature monitor and alarm
As a typical IoT application, the following basic functions are seamlessly implemented in Atmel smart plug
and JD Weilian APP:
•
•
•
One-step Wi-Fi configuration to add new smart plug based on JD protocol
Share Smart Plug to multiple users
Smart Plug firmware OTAU
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
5
•
•
Security based on JD security mechanism
IFTTT(if this then that) feature supported by JD cloud
For instructions on using the above features (both hardware usage as well and the JD Weilian app), refer
to AT16268: JD Smart Cloud Based Smart Plug Getting Started Guide.
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
6
2.
Development Tools
To download or debug the firmware, the following development toolchains are used:
•
•
•
•
Atmel Studio 7. Version: 7.0.634 - or above
Atmel START, Atmel START is an innovative online tool for intuitive, graphical configuration of
embedded software projects
™
Programming and debugging tool: Atmel SAM-ICE
SAM-ICE Adaptor: a minimized (1.27mm pitch 10-pin header) adaptor for Atmel SAM-ICE. For
®
more details, refer to Atmel AVR 2033: SAM-ICE Adapter - Hardware User Manual.
For more compatible development tools, refer to: http://www.atmel.com/devices/ATSAMD21G18.aspx?
tab=tools
Warning: Atmel smart plug is designed as a real product reference design. To avoid electrical
hazard, it's strictly prohibited to open the closure when it's connected to the AC power supply.
Make sure you understand the connections are safe before connecting any development tools
to the plug.
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
7
3.
Firmware Architecture
The reference design firmware runs on the ATSAMD21G18 MCU embedded in the ATSAMW25 module performing the following tasks:
•
•
•
•
•
•
Wi-Fi connectivity – ATWINC1500 driver
JD Joylink communication protocol
– One-step network entry configuration
– UDP device discovery
– Local/cloud query/control
– Security mechanism
Sensor control
– Energy measurement – ATM90E26 driver
– MCU internal temperature sensor
®
Touch Button - QTouch PTC library
LED indicator – Wi-Fi status and ON/OFF status
Services
– software timer service
– http client service
– cJSON library
As there is no RTOS in the firmware, all these function blocks are running in an endless loop after the
initialization function call. Depending on different requirement, the function blocks are called at predefined
intervals.
The software timer service is used for application timing usage. It uses system tick as the tick reference,
initialized to generate 1ms interrupt with a software timer on top to handle the individual timing
requirements.
http client service is used for OTAU in order to download the binary image from JD Smart Cloud.
cJSON library is used to encode/decode JSON format data used in UDP discovery process.
The low level drivers are based on Atmel Software Framework V4.
Figure 3-1. Smart Plug Firmware Structure
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
8
3.1.
Wi-Fi Connectivity
Smart plug Wi-Fi function is based on the Atmel industry-leading low-power 2.4GHz IEEE 802.11 b/g/n
Wi-Fi ATWINC1500 SoC (embedded inside ATSAMW25 module). It provides an integrated software
solution with application and network services (TCP/IP stack).
The Wi-Fi module firmware and drivers are part of Atmel Software Framework. The Wi-Fi firmware and
driver version used in the smart plug is 19.4.4.
For more details about the Wi-Fi firmware development, refer to Software Programming Guide for SAM
W25 Xplained Pro and ATWINC1500 Wi-Fi Network Controller - Software Design Guide
3.2.
Sensor Control
There are two sensors in this smart plug reference design. One is the energy measurement sensor
ATME90E26, the other is the internal temperature sensor inside the MCU.
The ATM90E26 is a high-performance energy metering device, which can be easily accessed by the
MCU via the UART interface. It's used to measure the real-time output current, voltage, power, and
accumulated electrical energy of the smart plug. For more information about the ATM90E26 usage, see
the device usage application note:
http://www.atmel.com/Images/Atmel-46102-SE-M90E26-ApplicationNote.pdf.
The temperature sensor is integrated within the MCU's ADC peripheral. It's easy to read the ADC result
and calculate the temperature. For more information about the temperature sensor, refer to the
ATSAMD21G18 device datasheet:
http://www.atmel.com/devices/ATSAMD21G18.aspx.
3.3.
QTouch PTC Library
The MCU (ATSAMD21G18) embedded inside the ATSAMW25 module, is equipped with a PTC
(peripheral touch controller) module. Due to the autonomous operation, the PTC uses very little CPU
resources and provides high quality of touch performance. Atmel provides the QTouch library which
makes it easy to add a touch button to the design.
For more information about the PTC touch implementation, refer to:
http://www.atmel.com/tools/QTOUCHLIBRARYPTC.aspx
3.4.
JD Protocol Library
JD Joylink protocol defines the packet structure, security mechanism, communication protocol of network
entry, device discovery, and query/control, etc.
Security is fully considered from the very beginning of the smart plug reference design, as it's vital for any
IoT applications. In JD based application, it follows the JD's security mechanism encapsulated in the JD
protocol library.
For hardware crypto device and firmware crypto lib, refer to the documents of the standard version plug:
Atmel AT15735: Atmel Smart Plug Firmware User Guide
The implementation of this part is encapsulated in library libjdv2.a.
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
9
For detailed protocol information, please contact JD for the corresponding document..
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
10
4.
Function Blocks
In this chapter, the main function blocks of the smart plug are introduced in detail. To know how to use the
smart plug with JD Weilian APP, refer to AT16268: JD Based Smart Plug Getting Started Guide.
4.1.
Wi-Fi Function
4.1.1.
Wi-Fi Provision
By default, a new smart plug is set to configure mode from factory. In order to connect it to the user's WiFi, it will have to be set up using provision mode through the JD Weilian APP. After setting the router
SSID and password, JD Weilian APP will broadcast this information over radio and the credentials can
then be picked up by the smart plug. Once it is decoded successfully, the smart plug will connect to this
router with the given credentials.
The implementation of this part is encapsulated in the library libJDOneKeyConfig.a.
4.1.2.
Communication with JD Smart Cloud and APP
JD Joylink protocol defines the packet structure, security mechanism, communication protocol of network
entry, device discovery, and query/control, etc. For detail information, refer to the JD protocol document.
The whole detailed process diagram is as below:
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
11
Figure 4-1. Smart Plug Working Process Diagram
After power-up, the smart plug will check if any cloud configurations are pending to decide whether to
enter configuration or operation mode. In configuration mode, it will start the Wi-Fi provisioning process
and UDP discovery process. In operation mode, it will connect to the JD smart cloud, start the
authentication process, and enter operation mode for query and control. The smart plug will send the
heart beat packet to the cloud every 50s to indicate the cloud that the plug is online.
In case of a heart beat response is not received within 5 seconds, or for any other form of communication
failure, the plug will try to reconnect to the server and restart the same process.
4.1.3.
UDP Device Discovery
Once a plug has been provisioned successfully, it works in STA mode and receives UDP broadcast
messages from the Weilian APP. This process is called UDP device discovery. Smart plug will tell APP its
product UUID, MAC address, and cloud configuration if ever configured. This makes the Weilian APP
able to find the smart plug and provide it with cloud configurations, like feed id, access keys, etc. This
information will be used in the following communication process for query and control.
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
12
4.1.4.
Local Control
If the Weilian APP and the smart plug are in the same network, local control via UDP is performed instead
of cloud control to get better performance . The Weilian APP will poll the plug status every 3 seconds. If
actions performed on the UI, UDP control commands will be sent to the smart plug.
Figure 4-2. Local Control Diagram
4.1.5.
Cloud Control
If the Weilian APP and the smart plug are not on the same network, but both of them are connected to the
Internet, cloud control will be utilized in stead. In this case, the smart plug will send its status to the JD
smart cloud every 2 seconds. Power consumption for the last 2 hours will be updated every 2nd hour.
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
13
Figure 4-3. Cloud Control Diagram
4.1.6.
Local Stream Data
When creating product on JD develop center, parameters of stream data are created, such as ON/OFF
state, output power, etc., stored on the JD smart cloud.
Corresponding to the stream parameters, there is the local binary stream data in the firmware, which is
defined as below:
typedef struct lua_bin_ctrl{
/** stream id which is valid*/
uint16_t stream_id;
/** plug on/ff status */
uint8_t onoff;
/* plug output voltage */
uint8_toutput_voltage;
/* plug output current */
uint16_t output_current;
/* plug output power */
uint16_t output_power;
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
14
/* plug power consumption since powered on */
uint16_t active_energy;
/* plug te merature from internal sensor */
int16_t temperature;
/* plug power consumption increment */
int16_t history_energy;
/* reserved for temperature threshold */
int16_t temp_h;
/* error code */
uint8_t err_code;
}lua_bin_ctrl_t;
This struct instance saves the commands from the JD smart cloud and also the status data to be
uploaded for the JD protocol API to construct the final TCP/UDP packets.
There is a lua script file written and uploaded to the smart plug product page of the JD developer center
along with the plug HTML5 UI files. It has two main functions; translate the cloud JSON format to local
binary data and vice versa.
4.2.
Sensor Reading
4.2.1.
Internal Temperature Sensor
The MCU's internal temperature sensor is used to monitor the smart plug operating temperature. In case
of over-temperature, the output will be shut off to prevent damage to the smart plug or connected device.
4.2.2.
Energy Measurement
The energy measurement is done by the Atmel metering AFE (Analog Front End) chip ATM90E26. The
MCU interfaces the ATM90E26 via UART and the default connection parameters are: baud 9600, no
parity, one stop bit. The following results are read out and displayed in the phone APP every 3 seconds.
•
•
•
•
Current – instant RMS current
Voltage – instant RMS voltage
Power – instant active power
Electrical energy – accumulated electrical energy
For electrical energy, the register inside ATM90E26 will be cleared each time when it’s read. So it must be
accumulated in the MCU firmware to get long-term electrical energy data. To record historic electrical
energy, the data is reported to the JD smart cloud every 2 hours.
To achieve better accuracy, the ATM90E26 is calibrated from the factory and the calibration data is stored
in the MCU. These data should be written back to the ATM90E26 calibration register during initialization
before reading any measurements from the device.
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
15
4.3.
User Interface
4.3.1.
Touch Button
A touch button is added to the smart plug design utilizing the PTC module embedded inside the
ATSAMD21G18.. Each time the touch button is pressed, the mains output of the smart plug is toggled. If
the button is kept pressed for more than 5 seconds, the smart plug will reset and enter scan and
configuration mode.
4.3.2.
LED Indicator
There are five LEDs on the smart plug. Three of them are for indicating the Wi-Fi status, while the other
two are for plug output indication.
Wi-Fi indicator function:
•
Yellow blink: Plug in scan and configuration mode, not connected
•
•
•
Yellow: Not connected
Green blink: Plug in STA mode, connecting to home Access Point (AP)
Green: Plug in STA mode, connected to home AP, connection is good
Smart plug output indicator function:
•
•
Green: ON
Red: OFF
The output status and LED colors are the same as shown in the Weilian APP.
4.4.
OTAU
For a typical IoT edge node, over-the-air upgrade (OTAU) is quite necessary. The Atmel smart plug also
includes this feature, following the JD protocol. The MCU (ATSAMD21G18) has an internal flash of
256KB, so it's quite enough for new firmware image downloading while keeping the original firmware
unchanged. In this reference design, no external serial flash is used.
4.4.1.
MCU Memory Map
The MCU internal flash is divided into different sections as shown in the memory map below.
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
16
Figure 4-4. MCU Internal Flash Memory Map
In this reference design, the application firmware area is divided into two parts – Application 1 and
Application 2. bootloader, as described in Section 4.4.3, is used to decide which application firmware to
run after reset. The last part the of internal flash is the emulated EEPROM used to store user information,
Wi-Fi network information (SSID/password/security type), and the JD configuration data, etc.
The two application areas start at 0x0004000 and 0x00020000 respectively. When generating a new
application firmware, make sure the binary code size is not higher than 112KB (128KB half the internal
flash size – 16KB Bootloader or EEPROM area).
4.4.2.
OTAU Flow
The OTAU function is enabled when the smart plug operates in STA mode. The new firmware image is
uploaded to the JD product management web page. The version number is saved in the firmware, and
sent to the JD smart cloud. The JD cloud will compare the given version number against the newest
available firmware. If the firmware available in the JD smart cloud is newer, a notification is sent to the
user who can choose whether or not to upgrade the smart plug. Once the OTAU is triggered, an OTAU
packet will be sent from the JD smart cloud to the plug containing firmware URL and other information
needed to download and upgrade to the new firmware.
After a new image is downloaded into the host MCU successfully, the smart plug will do soft reset to
switch to the new firmware. In this reference design, the application firmware can be upgraded only.
Downgrading is not permitted. The running firmware version can be checked from the JD Weilian APP.
For detailed protocol information, please contact JD for the corresponding document.
The OTAU flow is shown below:
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
17
Figure 4-5. OTAU Flow
4.4.3.
Bootloader
As no external serial flash is used for image storage during OTAU, the bootloader is quite simple. After
reset, it will check the flags in EEPROM and decide which application firmware to run. The bootloader
project files are also included in the software package released together with this doc.
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
18
5.
Source Project Structure
User can get this project from examples of Atmel START or the attachment with this document. The
exported project from Atmel START is an atzip file, which can be opened and extracted by Atmel Studio
7. The project structure is as shown in the Atmel Studio 7 screen-shot below:
The project is based on Atmel Software Framework V4.
Figure 5-1. Smart Plug Project Structure
Figure 5-2. Smart Plug Application Sources
The above folders and files give a basic function division in this project. As some of the function blocks
are tightly integrated, several function blocks may be mixed in application layer files.
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
19
6.
Main API Introduction
In this chapter, the main APIs are introduced for better understanding of key function blocks. For more
details about the function parameters and return values etc., refer to comments in source files.
6.1.
Wi-Fi Connectivity API
These APIs can be found in winc1500.c.
•
•
•
•
•
6.2.
void configure_winc1500(void);
Initialize winc1500 Wi-Fi SoC.
static void m2m_wifi_state_handler(uint8_t u8MsgType, void *pvMsg);
Callback to get the Wi-Fi status update.
static void m2m_wifi_socket_handler(SOCKET sock, uint8 u8Msg, void
*pvMsg);
Callback to get the message and status from socket.
NMI_API sint8 m2m_wifi_connect(char *pcSsid, uint8 u8SsidLen, uint8
u8SecType, void *pvAuthInfo, uint16 u16Ch);
Connect to the AP with security information.
NMI_API sint8 m2m_wifi_get_mac_address(uint8 *pu8MacAddr);
Get the ATWINC1500 MAC address.
Smart Config Library APIs
These APIs are implemented in libJDOneKeyConfig.a.
•
•
6.3.
void smart_config_mode_check(void);
Check if any router (AP) configuration (SSID&PASSWORD) exists, if yes, initialize ATWINC1500,
otherwise enter configure mode.
void smart_config_task(void);
Called by main function, ATWINC1500 is in listening mode, monitoring the data from the Weilian
APP to get the SSID and password. If parsed successfully, ATWINC1500 connects to this router or
AP.
JD Protocol Library APIs
These APIs are implemented in libjdv2.a.
•
•
•
•
int packetAnalyseV1(packetparam_t *pParam, const uint8_t *pIn, int
length, uint8_t* pOut, int maxlen);
UDP packet decode.
int serverAnalyse(packetparam_t *pParam, const uint8_t *pIn, int length,
uint8_t* pOut, int maxlen);
TCP packet decode.
int packetBuildV1(uint8_t* pBuf, int buflen, EencType enctype,
EpacketType cmd, uint8_t* key, const uint8_t* payload, int length);
UDP packet encode.
int serverPacketBuild(uint8_t* pBuf, int buflen, EpacketType cmd,
uint8_t* key, const uint8_t* payload, int payloadLen);
TCP packet encode.
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
20
6.4.
JD Communication APIs
These APIs can be found in winc1500.c.
•
•
•
•
•
•
void handle_wifi_rd_packet(data_packet_t* rd);
Parse received TCP or UDP packetss, call handle_cloud_request() and handle_local_request() to
handle corresponding packet. Parse received TCP or UDP packets, call handle_cloud_request()
and handle_local_request() to handle corresponding packet.
void handle_wifi_wr_packet(data_packet_t *wr);
Send the packet via UDP or TCP by its type.
void handle_cloud_request(uint8_t *cloud_packet, int32_t packet_len);
Handle TCP packets, including authentication response, heartbeat response, upload response,
control, and query commands.
void handle_local_request(uint8_t *local_packet, int32_t packet_len);
Handle UDP packets, including device discovery, plug status query, and control.
static void resolve_cb(uint8_t *hostName, uint32_t hostIp);
DNS callback, get JD server IP from its URL.
int Server_Auth(uint8_t *pbuf);
int Server_HB(uint8_t *pbuf);
int Upload_Data(uint8_t *pbuf, bool isHistoryEnergySent);
•
•
•
•
6.5.
Packets construction functions, for authentication/upload/heartbeat packets.
void winc1500_task(void);
Called by main function. It runs m2m_wifi_handle_events() to handle the Wi-Fi data from
ATWINC1500. Manage connection status, including TCP/UDP sockets creation, JD server
connection, and authentication.
void sw_timer_jd_hb_callback(struct sw_timer_module *const module, int
timer_id, void *context, int period);
Send JD heart beat packet every 50 seconds.
void sw_timer_jd_hb_resp_callback(struct sw_timer_module *const module,
int timer_id, void *context, int period);
After sending the heart beat packet, start this 5 seconds timer to check if heart beat response is
received in time. If not, reset current connection.
void sw_timer_state_upload_callback(struct sw_timer_module *const module,
int timer_id, void *context, int period);
Send the plug status (output voltage, current, power, consumption) every 3 seconds. History
consumption increment is uploaded every 2 hours.
Smart Plug APIs
These APIs can be found in node_plug.c.
•
•
•
int32_t do_ctrl(void *command);
Perform control action and return the result.
int32_t do_snapshot(void *command, bool isHistoryEnergySent);
Perform plug status query and return the result.
bool set_relay_state(plug_state_t state);
Control plug relay state, and set corresponding LED indication.
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
21
6.6.
OTAU APIs
The OTAU APIs can be found in otau.c.
•
•
•
•
6.7.
static void configure_http_client(void);
Configure HTTP client to download remote firmware binary file.
static void http_client_callback(struct http_client_module *module_inst,
int type, union http_client_data *data);
HTTP download callback function, download remote firmware binary file and program flash.
void otau_task(void);
Called by main function to update OTAU status.
static uint32_t program_buffer_to_flash(uint32_t* address, uint8_t
*buffer, uint16_t len, bool is_last_packet);
Program downloaded data to flash, it will keep the received data page size aligned in the buffer.
Sensor Reading APIs
The internal temperature sensor APIs can be found in adc_sensor.c.
•
•
•
•
6.8.
void configure_adc(void);
Initialize the ADC module to read the internal sensor.
float get_internal_temperature(void);
Start ADC conversion to get ADC result and convert the ADC data to temperature. The ATM90E26
sensor reading APIs can be found in m90e26.c.
void configure_m90e26(void);
Initialize UART interface and write calibration data to ATM90E26.
void read_data_m90e26(void);
Read measurement results from ATM90E26.
UI APIs
The UI includes a QTouch button and LED indicator.
QTouch library is part of ASF and its APIs can be found in ASF. For library details, refer to Atmel Qtouch
Library User Guide. The application layer APIs are in button.c and main_samd21.c.
•
•
•
•
•
static void button_handler(buttons_state_code_t button_status);
Handles the button action.
void timer_init(void);
Used to initialize the timers needed by QTouch library.
void touch_button_handler(void);
Reads the touch sensor result from QTouch library API and outputs the button status (short press or
long press). LED indicator APIs can be found in LED.c. As the output indicator works together with
the relay output, the APIs are for Wi-Fi indicators.
void set_wifi_led_mode(led_mode_t led_mode, led_index_t led_pin_num);
Set LED indication to reflect current operational status (on, off, or toggle).
void exec_wifi_led_mode(void);
Refresh the LED indicator status at predefined interval.
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
22
6.9.
Software Timer APIs
Software timer APIs can be found in sw_timer.c.
•
•
•
•
•
void sw_timer_init(struct sw_timer_module *const module_inst, struct
sw_timer_config *const config);
Initialize software timer service and configure timer.
void sw_timer_enable(struct sw_timer_module *const module_inst);
Start software timer service.
int sw_timer_register_callback(struct sw_timer_module *const module_inst,
sw_timer_callback_t callback, void *context, uint32_t period);
Register new software timer, callback is the timer handler, period is the timer interval, timer ID is
returned.
void sw_timer_enable_callback(struct sw_timer_module *const module_inst,
int timer_id, uint32_t delay);
Enable timer with ID timer_id, delay is the delay interval to trigger the handler.
void sw_timer_disable_callback(struct sw_timer_module *const module_inst,
int timer_id);
Stop timer with ID timer_id.
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
23
7.
Memory Footprint
Here is the smart plug project overall memory size generated in Atmel Studio 7 with GCC optimization
option set to -O1.
•
•
Program Memory Usage: 107236 bytes 40.9% full
Data Memory Usage: 23272 bytes 71.0% full
The following figures show the memory maps by smart plug function blocks.
Figure 7-1. Flash Memory Footprint [Byte]
Figure 7-2. SRAM Memory Footprint [Byte]
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
24
8.
Revision History
Doc Rev.
Date
Comments
42720A
04/2016
Initial document release
Atmel AT16267: Firmware User Guide on JD Smart Cloud Service Integration with Smart Plug
[APPLICATION NOTE]
Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
25
Atmel Corporation
©
1600 Technology Drive, San Jose, CA 95110 USA
T: (+1)(408) 441.0311
F: (+1)(408) 436.4200
|
www.atmel.com
2016 Atmel Corporation. / Rev.: Atmel-42720A-Firmware-User-Guide-on-JD-Smart-Cloud-Service-Integration-with-Smart-Plug_AT16267_Application Note-04/2016
®
®
®
®
Atmel , Atmel logo and combinations thereof, Enabling Unlimited Possibilities , AVR , QTouch , and others are registered trademarks or trademarks of Atmel
®
®
Corporation in U.S. and other countries. ARM , ARM Connected logo and others are the registered trademarks or trademarks of ARM Ltd Other terms and product
names may be trademarks of others.
DISCLAIMER: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any
intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN THE ATMEL TERMS AND
CONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED
OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS AND PROFITS, BUSINESS
INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED
OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this
document and reserves the right to make changes to specifications and products descriptions at any time without notice. Atmel does not make any commitment to
update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive
applications. Atmel products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life.
SAFETY-CRITICAL, MILITARY, AND AUTOMOTIVE APPLICATIONS DISCLAIMER: Atmel products are not designed for and will not be used in connection with any
applications where the failure of such products would reasonably be expected to result in significant personal injury or death (“Safety-Critical Applications”) without
an Atmel officer's specific written consent. Safety-Critical Applications include, without limitation, life support devices and systems, equipment or systems for the
operation of nuclear facilities and weapons systems. Atmel products are not designed nor intended for use in military or aerospace applications or environments
unless specifically designated by Atmel as military-grade. Atmel products are not designed nor intended for use in automotive applications unless specifically
designated by Atmel as automotive-grade.
© Copyright 2026 Paperzz