Learn IoT Functional Requirements From Smart Plug Reference Design

Contributed By Digi-Key's North American Editors

Devices intended for Internet of Things (IoT) applications share a few common but critical requirements: simple connectivity, a high level of security, and ease of use. This is why designers should look closely at Atmel's Smart Plug reference design as a comprehensive working guide to wireless IoT design.

While applications for harsh environments typically require wired connectivity, consumers have now come to expect wireless connectivity that is reliable and easy to set up. For security, requirements now extend beyond basic data encryption into more sophisticated policies for safely adding new devices to trusted networks, as well as ensuring secure communications during normal operations. Finally, any IoT device is ultimately expected to meet requirements for application-specific sensor or actuator functionality.

This is why Atmel's Smart Plug reference design serves as a comprehensive working guide to wireless IoT design. For designers, it integrates all the hardware and software required for a secure Internet-connected product. At the same time, the design is a working Wi-Fi-enabled power plug that illustrates the three dominant use-case models: local control using its capacitive touch button; control via a mobile app with Wi-Fi connection with or without cloud connectivity; or timer-based control, using the associated app to schedule on/off timing.

Unlike many emerging IoT starter kits, the Atmel Smart Plug reference design is delivered in a cloudless version. Atmel does provide firmware variants designed for cloud integration, but developers also need to look closely at the trend toward "fog computing”.

In fog computing, increasing levels of data processing, storage and application control are performed locally, at the “edge”, instead of relying solely on the cloud. Besides enabling "always on" service, this approach reduces service latency, improves security, saves power, and can enhance the overall user experience.

Device design

The Smart Plug design itself comprises three printed-circuit boards (pc boards), one each for power, the MCU and touch control. The MCU board is vertically mounted to the power board, and the touch board is connected to the MCU board by a pin connector (Figure 1). Atmel offers the Smart Plug in three versions designed to meet US, European and Chinese standards. The three versions provide each region's standard plug and socket, but use the same pc boards.

Image of Atmel Smart Plug reference design

Figure 1: The Atmel Smart Plug reference design separates digital and power circuits into separate boards with a third for LEDs and the QTouch interface. (Image source: Atmel Corp.)

The touch board contains LEDs for Wi-Fi and power, and touch sensors based on QTouch technology. QTouch is Atmel's capacitive touch-sensing technology designed to provide noise immunity and environmental resilience needed for reliable performance in less-than-ideal environments.

The power board contains an AC/DC converter, a DC/DC converter, the power measurement circuit, relay, and the plug and socket. The AC/DC converter is designed to convert the 100 V/240 VAC input voltage into a 12 V/120 mA (typical) DC output to supply the relay and DC/DC converter. The AC/DC converter combines a custom transformer with a Monolithic Power Systems MP020-5 switching regulator.

The MP020-5 is an offline, primary-side regulator that provides accurate constant voltage and constant current regulation without an optocoupler or a secondary feedback circuit. The DC/DC converter converts the DC voltage level from 12 V to 3.3 V for the SAMW25 wireless module on the MCU board.

The MCU board holds the ATSAMW25 wireless module and 32 KHz crystal. In addition, it provides an available footprint on the pc board for an external flash device, which is not required for this design.

Secure connectivity module

The centerpiece of Atmel's design is the ATSAMW25 module, which combines digital control, connectivity and security functionality. Designed as a drop-in solution for Wi-Fi connectivity, the module combines its extensive hardware functionality with integrated software including application and security protocols such as transport layer security (TLS) and integrated network services including TCP/IP stack. In the Smart Plug design, this module not only provides Wi-Fi connectivity and security but also manages the Smart Plug touch button, relay control, temperature sensing and protection, LED indicators, and timing (Figure 2).

Diagram of Atmel ATSAMW25 module

Figure 2: The Atmel Smart Plug reference design provides a complete IoT solution built around the Atmel ATSAMW25 module, which provides functionality for digital control, connectivity, and security. (Image source: Atmel Corp.)

Internally, this core module takes advantage of the extensive functionality integrated in the three key SoCs — the Atmel ATSAMD21G18A MCU, the Atmel ATWINC1500B-MU-T transceiver, and the Atmel ATECC508A CryptoAuthentication device.

Based on the low-power ARM Cortex-M0+ core, the ATSAMD21G18A integrates 256 KB flash and 32 KB SRAM with an extensive set of peripherals including serial, USB, 14-channel 12-bit analog-to-digital converter (ADC), and 10-bit digital-to-analog converter (DAC). The Atmel ATWINC1500B-MU-T is a low-power, single-chip IEEE 802.11 b/g/n, 2.4 GHz network controller. Designed specifically to simplify integration of Wi-Fi connectivity in MCU-based designs, the ATWINC1500 integrates a full transceiver signal chain and provides multiple peripheral interfaces including UART, SPI, and I2C for interfacing with a host MCU. The ATECC508A crypto device is described below.

For wireless communications, these devices require only a few connections between the SAMD21G MCU and the ATWINC1500 network controller SoC (Figure 3). While the SPI connection provides bidirectional data exchange, the design helps reduce power consumption during quiet periods. For incoming data, the ATWINC1500 can wake the MCU using an interrupt line (IRQn) to signal the MCU to process received data. For outgoing data, the MCU can wake the ATWINC1500 using the Wake line to handle transmissions.

Diagram of Atmel ATSAMW25 module

Figure 3: The Atmel ATSAMW25 module integrates an MCU with SoCs for Wi-Fi connectivity and security, providing a drop-in solution for many IoT applications. (Image source: Atmel Corp.)

The ATSAMW25 module requires few additional components to control the Smart Plug power relay and to interact with the power measurement circuitry (Figure 4). As shown in the figure, the 52-pin ATSAMW25 module provides a simple interface for each key function that is handled external to the module. For example, pin 24 brings out the reset pin for the included Atmel ATSAMD21G18A MCU, and pins 40 and 41 are used to interface with the power measurement subsystem. In addition, the module provides nine general-purpose input/output (GPIO) pins that developers can program to bring out application-specific functions.

Diagram of Atmel ATSAMW25 module (click for full-size)

Figure 4: Designed to simplify integration of Wi-Fi functionality in designs, the Atmel ATSAMW25 module requires only a few external components and provides the minimal external connections needed to implement a design. (Image source: Atmel Corp.)

Unlike Bluetooth's one-to-one pairing connectivity, Wi-Fi connectivity enables device sharing so multiple users can share access and control the device. Still, designers can build custom Wi-Fi/Bluetooth designs by augmenting the base Smart Plug reference design with a dedicated Bluetooth device such as the Atmel ATSAMB11 Bluetooth SoC and associated software. The ATWINC1500 supports 2-wire and 3-wire Wi-Fi/Bluetooth Coexistence signaling, conforming to the IEEE 802.15.2-2003 standard.

Finally, the power measurement circuit implemented on the power board takes advantage of the Atmel ATM90E26. Designed specifically for energy metering, the ATM90E26 integrates a complete analog front-end (AFE) signal chain with a digital-signal processor (DSP). Using voltage and current measurements performed by the AFE, the DSP calculates instantaneous power, RMS current, RMS voltage, and accumulated electrical energy. The results are delivered to the ATSAMW25 through the UART port. To isolate the line voltage and internal supply power domains, the design includes two optocouplers to protect the connection between the ATM90E26 and the ATSAMW25 (pins 40 and 41 on the ATSAMW25 module).

Software design

In the smart plug reference design, the main program runs on the ATSAMD21G18 MCU embedded in the ATSAMW25 module. The software design does not use a real-time operating system (RTOS). Instead, all of the software function blocks run in an endless loop after initialization. The key MCU software functions include Wi-Fi connectivity provided through the ATWINC1500 driver; security through the ATECC508A driver and software AES library; energy measurement through the ATM90E26 driver; and touch button control through the Atmel QTouch library.

In the smart plug reference design, the application firmware area is divided into two partitions — Application 1 and Application 2 (Figure 5). Along with these application partitions, the internal flash includes an emulated EEPROM used to store user information, Wi-Fi network information, and usage data.

Image of memory footprint of the Smart Plug firmware

Figure 5: Because the memory footprint of the Smart Plug firmware is relatively small, two copies of the application can coexist in the ATSAMD21G18 MCU's 256KB flash, allowing over-the-air upgrades without the need for external memory. (Image source: Atmel Corp.)

After reset, a simple bootloader is used to decide which application firmware partition to run. Here, the bootloader code looks at variables defined in the bootloader header file, boot.h (Listing 1).

#define APPLICATION_SIZE            (FLASH_SIZE/2UL) // Maintain two copies in flash

#define APP_PAGE_SIZE               FLASH_PAGE_SIZE

#define APP1_START_ADDRESS          0x00004000

#define APP2_START_ADDRESS          0x00020000

 

/** NVM page to save application area info */

#define NVM_APP_AREA_INFO_PAGE      2

 

typedef enum _valid_app {

       APP1_VALID = 1,

       APP2_VALID,

       NOT_FOUND = 0xFF,

} valid_app_enum;

Listing 1: The bootloader header file defines the few constants and variables required for the simple bootload process. (Code source: Atmel Corp.)

The bootloader code itself checks the APPx_VALID flags (Listing 2)

if(app_info.valid_app == APP1_VALID) {

       app_check_address = APP1_START_ADDRESS;

       app_check_address_ptr = (uint32_t *) app_check_address;

} else if (app_info.valid_app == APP2_VALID) {

       app_check_address = APP2_START_ADDRESS;

       app_check_address_ptr = (uint32_t *) app_check_address;

} else {

       /*

        * Factory new device has no valid app flag in EEPROM.

        * Application code is in sector 2 for factory new device.

        */

       app_check_address = APP2_START_ADDRESS;

       app_check_address_ptr = (uint32_t *) app_check_address;

}

Listing 2: The bootloader code simply checks which application area (APP1 or APP2) is valid and invokes the code in the active application area to start Smart Plug device software execution. (Code source: Atmel Corp.)

Finally, the bootloader sets up to run the code in the appropriate application space and jumps to that application.

Thanks to the high level of integration afforded by the design's SoCs, the memory footprint for the reference design is minimal. Program memory usage is 83244 bytes (31.8% of capacity) and data memory usage is only 15632 bytes (47.7% of capacity). Because of this minimal memory size, the design is able to support over-the-air (OTA) upgrades needed for a typical IoT edge node. The ATSAMD21G18A MCU's 256 KB internal flash leaves enough space to store a downloaded firmware image as well as retaining the original firmware unchanged. Consequently, this reference design does not require external storage for OTA upgrades.

Atmel offers a complete code package for the device as well as a complete Android app. Developers can access the software configuration through Atmel START — the company's online tool for configuring software components and device settings such as clocks and pin layout. The Smart Plug configuration comes prebuilt with middleware for the Wi-Fi stack, crypto services and IO management as well as a full set of software drivers (Figure 6).

Image of Atmel full set of software drivers

Figure 6: Atmel provides a comprehensive set of tools for software development and debugging, including this online tool for configuring software components and device settings in the design's set of hardware devices. (Image source: Atmel Corp.)

After configuration, developers export the project and open it in an IDE for further software development. For application development, Atmel provides an extensive toolchain built around Atmel Studio 7, which combines code development tools with integrated debugging through Atmel debuggers, programmers, and simulators. For the Smart Plug design, the Atmel Software Framework (ASF) provides APIs for Wi-Fi connectivity, security, communication protocols, sensor readings, and the UI. Developers can use Atmel's ASF Explorer to import drivers, communication stacks and services from ASF into their projects. ASF Explorer automatically manages library dependencies to ensure code packages include required drivers.

As with most emerging IoT devices, the Atmel Smart Plug relies on a mobile app for its user interface for monitoring and control. Indeed, the Atmel-provided Android app is an integral feature of this reference design. Through the app, users can manage Wi-Fi provisioning during setup, share the device with multiple users, perform over-the-air upgrades, turn the device on and off, schedule on/off timing, and even examine usage history.

IoT security mechanisms

The Atmel Android app plays a central role in ensuring secure operations. The Atmel Smart Plug reference design implements a robust security policy involving authenticated communications between the device and the app during setup and during normal operation. Because the Smart Plug design is functionally representative of most IoT edge node designs, the reference design offers a detailed guide to implementation of IoT security measures.

The design relies on both the Atmel ATECC508A hardware crypto engine and a software crypto library to implement security features, including:

  • FIPS186-3 elliptic curve digital signature algorithm (ECDSA) for Android app and smart plug authentication;
  • FIPS SP800-56A elliptic curve Diffie-Hellman algorithm (ECDH) for session key generation without ever transmitting the secret key outside the secure key storage;
  • Advanced Encryption Standard (AES)-128 cipher block chaining (CBC) mode for data encryption and decryption of communication on the network;
  • SHA-256 based MAC (message authentication code) for data integrity check.

The same security functions are also implemented in software on the Android app side to allow secure communications between the Smart Plug device and app.

Key-based encryption places a critical requirement for key security on any IoT design. In the Smart Plug design, the ATECC508A crypto device addresses this need with its secure hardware-based cryptographic key storage and cryptographic countermeasures. When a design reaches full production, key storage such as that in the ATECC508A would be provisioned in a secure factory environment prior to distribution to ensure protection of secret keys.

In the field, the design uses sophisticated authentication schemes to add a new Smart Plug device to an existing network and to exchange data between authenticated Smart Plug devices and the app. During data exchange, after the Smart Plug and app authenticate each other, the smart plug replies with its MAC address and a random number challenge. Because the Smart Plug receives the Android app public key when it is (securely) added to the network, the ATECC508A can generate an ECDH pre-master key and randomize the key using the MAC algorithm to output a temporary key that remains valid only for the life of the session. In turn, the ATECC508A uses this session key as the AES key to encrypt information transmitted to the Android app in reply. Although the security flow for adding a new Smart Plug device is more complex, it follows a similar security philosophy of random key exchange, challenge and verification.

For the developer, however, the Atmel drivers and APIs handle the details. For example, a simple call to cademo_aes_crypt() in the ecc508a driver returns encrypts or decrypts data in AES-128 CBC mode using the third-party library LibTomCrypt (Listing 3).

static uint8_t cademo_aes_crypt(uint8_t enc_mode, uint8_t* aes_key, uint8_t* plain_txt, uint16_t* plain_txt_size, uint8_t* cypher_txt, uint8_t* iv)

 {

       uint8_t ret = ATCA_SUCCESS;

       uint16_t keysize = AES_KEY_SIZE;

      

       // LibTomCrypt

       register_cipher(&aes_desc);

 

       int32_t cypher_idx = find_cipher("aes");

       symmetric_CBC cbc;

      

       ret = cbc_start(cypher_idx, iv, aes_key, keysize, 0, &cbc);

       if (enc_mode == MODE_ENCRYPT) {

              ret = cbc_encrypt(plain_txt, cypher_txt, *plain_txt_size, &cbc);

       }

       else if (enc_mode == MODE_DECRYPT) {

              /* plain_txt is the encrypted data by calling cademo_aes_crypt(),

                 while cypher_txt is the place to store decrypted data. */

              ret = cbc_decrypt(plain_txt, cypher_txt, *plain_txt_size, &cbc);

       }

      

       return ret;

 }

Listing 3: The Atmel code base for the Smart Plug reference design offers a comprehensive set of routines for security implementation such as this routine for AES-128 CBC mode encryption/decryption. (Code source: Atmel Corp.)

Here, the calling parameters include:

  1. enc_mode — Flag defining whether to encrypt or decrypt
  2. aes_key — The AES cipher
  3. plain_txt — The bytes to encrypt
  4. plain_txt_size — The number of bytes to encrypt
  5. cypher_txt — The encrypted text
  6. iv — The initialization vector for CBC mode

Conclusion

IoT design requires successful integration of functionality including connectivity, security, and application resources. By working with a robust IoT reference design, new IoT developers can quickly gain experience and more experienced developers can accelerate IoT design. The Atmel Smart Plug reference design provides a comprehensive working example of IoT design. Combining software and hardware for wireless connectivity, security, and energy metrology, the design offers deep insight into the interactions between IoT design components and can be an excellent starting point for sophisticated custom IoT designs. 

Disclaimer: The opinions, beliefs, and viewpoints expressed by the various authors and/or forum participants on this website do not necessarily reflect the opinions, beliefs, and viewpoints of Digi-Key Electronics or official policies of Digi-Key Electronics.

About this publisher

Digi-Key's North American Editors