宋元浩

Tencent Cloud IoT Smart Socket

 
Overview

Introduction

Tencent Cloud IoT smart socket supports remote control via WeChat applet, allowing you to view voltage, current, power, accumulated power consumption and other information.

warn

This project contains strong current circuits, please pay attention to safety during the development process!

Open source information

Video demonstration
https://www.bilibili.com/video/BV1qK4y1d7XK/

Circuit diagram and PCB
https://oshwhub.com/Code504/tencent-iot-smart-socket

Engineering code
https://gitee.com/dma/tencent-cloud-iot-smart-socket

Instructions for use

image display

Socket front

Front side of PCB after soldering is completed

The back of the PCB after soldering is completed

Wiring reference

Parameter Description

  • Maximum input voltage: 250V
  • Maximum load current: 5A
  • Maximum load power: 1100w
  • Standby power: less than 2w

Features

  • WeChat applet remote control
  • Support timing switch
  • Information such as voltage, current, power, cumulative power consumption, etc. can be viewed
  • When the accumulated power consumption exceeds 10,000 degrees, it will automatically clear and start counting again.
  • Overload protection, when the load current exceeds 5A, the power is forced to cut off and an alarm message is sent.
  • Breathing light visually indicates load power
  • When measuring low-power electrical appliances (such as TVs on standby, night lights, chargers not connected to mobile phones, etc.), the error is large, and the measurement results are for reference only.

Instructions for use

  1. Connect the smart socket to power and wait for the green indicator light to turn on
  2. Open WeChat, scroll down on the main page, and search for "Tencent Lianlian Mini Program"
  3. Click the plus sign to add the device, select "Socket" in Electrical Lighting (or scan the QR code), and follow the prompts
  4. Press and hold the smart socket button for 5 seconds and then release it. The blue indicator light will flash and network configuration will begin.
  5. After the network configuration is completed, the device automatically restarts, and the newly added device can be seen on the main page of the mini program.
  6. If the network configuration fails, please repeat steps 2, 3, and 4.
  7. Enter the main page of the device to control the switch, set the countdown, and view information such as voltage, current, power, cumulative power consumption, etc.
  8. Set the countdown. When the countdown reaches 0, the status of the switch will flip.
  9. If the load current exceeds 5A, the smart socket will be forced to cut off the power and push an alarm message. It can return to normal after turning it on and off again.

Key Description

  • Click the control switch
  • Press and hold for 5 seconds to configure network
  • Press and hold for 10 seconds to restore factory settings and clear all data including accumulated power consumption.

Indicator light description

color state
Steady green The equipment is working normally, in standby, and the load is powered off.
colorful breath The equipment is working normally and the load power supply is connected.
flashing blue Network connection, distribution network
white flash reset
Flashing red The load current is too large and the power is forced to be cut off.

After the load power is turned on, the indicator light changes to a colorful breathing state. The color of the indicator light is related to the load power. The power from small to large is blue, green, yellow, and red. Color and power reference

power color
0-100w blue
100-300w blue
300-500w green
500-800w yellow
800w and above red

Hardware development

For schematics and PCB, please refer to the open source link above and related information in the doc directory.

Component selection:

  1. I used STM32G070KBT6. It can replace other STM32G0 series LQFP32 packaged chips without changing the PCB, such as STM32G030K8T6, STM32G071KBT6, STM32G081KBT6, STM32G431KBT6, etc. Of course, some changes are required in the software, which will be explained in detail in the software development chapter. .
  2. The wifi chip uses an ESP-12F module. Due to limited space, an adapter board has to be used here with a 20mm long pin row to be welded on top of the power module. The ESP module requires Tencent Cloud firmware, which requires Flash to be greater than or equal to 2MB. You can purchase other ESP8266 modules whose Flash meets the requirements to use with the adapter board. The pin header of the adapter board is fully compatible with the ESP-01 module. If you plan to use the ESP-01 module, in addition to replacing the long pin header, you must also confirm whether the Flash meets the requirements. As far as I know, the ESP-01 module It has 1MB Flash. You need to replace it with 2MB Flash or buy a model with larger Flash.
  3. The relay model is SRD-05VDC-SL-C, and the rated load current is 5A. If a larger load current is required, other models can be replaced. The current transformer should also be replaced and the PCB should be modified to bold the relevant lines.
  4. The current transformer model is ZMCT103C, with a rated current of 5A. If a larger load current is required, other models can be replaced, and the relay should be replaced at the same time.
  5. The fuel gauge chip model is HLW8032, which can be replaced by compatible chips such as HLW8012 and BL0937. The other chips are PWM outputs. The PCB has reserved compatible designs. You can modify the SJ3 jumper and weld the R14 resistor to use the input capture pin of the microcontroller to receive Signal. Note: The programs for several other chips have not been developed, please adapt by yourself!

Pin layout instructions:

  1. As shown in the figure, H4 is the 5V power supply, H1 is the STLINK interface, H2 is the microcontroller debugging serial port, and H3 is the ESP8266 module serial port. 
  2. Three programming jumpers for the ESP8266 module are reserved on the board. Above the "run" and "flash" silkscreens, short-circuit the left and middle pads of all jumpers to enter the run mode, and short-circuit the two middle and right pads of all jumpers to enter the programming mode. . In programming mode, please connect the serial port to ESP TRthe two pin headers marked by silk screen.
  3. SJ1 is the microcontroller BOOT jumper; SJ3 is the fuel gauge chip compatible jumper. Short-circuit the output signals of the fuel gauge on the left and middle pads and connect them to the input capture pin of the microcontroller. Short-circuit the fuel gauge on the middle and right pads. The output signal is connected to the microcontroller serial port input pin. 

other instructions:

  1. Please refer to the previous picture for the connection method of live wire and neutral wire. The output live wire must pass through the current transformer. If the input live wire passes through the current transformer, the standby current of the smart socket itself will also be measured at the same time.
  2. The reset button and its related resistors and capacitors are mainly for convenience of debugging and can be omitted.
  3. For ease of use, the indicator light uses WS2812, which does not match the reserved indicator hole on the casing. It is best to seal it with glue with good light transmittance such as hot melt glue or epoxy glue to avoid the risk of someone’s hands. It's very dangerous to poke things around because you're right next to a high-voltage circuit!

Cost Estimate

The following is the price when I purchased it, for reference only

name model unit price
Shell and hardware none 5.50
Power module HLK-PM01 11.00
master control STM32G070KBT6 12.50
Current Transformer ZMCT103C 2.72
voltage transformer ZMPT107-1 4.11
fuel gauge chip HLW8032 3.6
relay SRD-05VDC-SL-C 2.36
WIFI module ESP-12F 8.83
PCB none 3.00
Other components Wires, capacitors, resistors, pin headers, buttons, LEDs, etc. 10.00
Total 63.62 yuan    

Cost reduction plan

The following cost reduction plan only provides ideas, and all software and hardware need to be developed by yourself.

  • STM32G070KBT6 can be replaced by STM32G030K8T6, or other cheaper microcontrollers, but the code needs to be greatly optimized for memory
  • Remove the microcontroller and carry out secondary development directly on ESP8266. This method requires the largest development workload, but also the largest cost reduction.
  • You can find power modules on a certain website. The cheapest 5V 3W power module only costs 6 yuan.
  • Using a single power supply non-isolated solution can save the cost of current transformers and voltage transformers, at the expense of the entire circuit board carrying strong current
  • Using a dual power supply non-isolated solution can also save the cost of current transformers and voltage transformers, but the cost saved may be similar to adding a non-isolated power supply.

Backend development

New projects and products

  1. Register a developer account on  Tencent Cloud  official website (please skip if you have already registered)
  2. Log in to  the IoT development platform and refer to  Project Management to  create a new project.
  3. Refer to  the product definition  to create a new product and fill in the product information. Select [Smart Life]->[Electrical Lighting]->[Socket] for [Product Category], select [Device] for [Equipment Type], and select [Key Authentication] for [Authentication Method] ], select [Wi-Fi] for [Communication Method], select [Data Template] for [Data Protocol], and click Save after creation is completed.

数据模板

  1. 点击刚刚创建的产品,点击【数据模板】
  2. 点击【导入JSON】,你也可以在下方定义自己的数据模板 
  3. 在这里将 project智能插座.json 的内容粘贴进去,完成后点击【保存】 
  4. 更多内容请参考官网文档 数据模板

设备开发

  1. 在【设备开发】标签页下可以选择【基于OS开发】或跳过,不影响本项目后续开发流程
  2. 在【Topic列表】标签页下可以查看已有的Topic或添加自定义Topic,本项目不需要,可跳过
  3. 更多内容请参考官网文档 设备开发

交互开发

  1. 依次点击【交互开发】、【配置小程序】、【面板配置】 
  2. 如图所示,这里可以配置小程序的页面,你可参考我的布局,或按自己喜欢的方式配置 
  3. 依次点击【交互开发】、【配置小程序】、【配网引导】,在【选择配网方式】标签页下参照图片进行设置,【芯片方案选择 】设置为【乐鑫】,【首选配网方式】设置为【Smart Config】,【次选配网方式】设置为【Soft Ap】或【无】 
  4. 更多内容请参考官网文档 交互开发

设备调试

  1. 设备端代码编译并下载后你可以在这里进行在线调试,
  2. 点击【设备信息】可以看到【设备名称】、【设备密钥】、【产品ID】,后续开发过程会用到这些信息 
  3. 更多内容请参考官网文档 设备调试

批量投产

个人DIY玩家可以忽略,正式量产请参考官网文档 批量投产

数据开发

当智能插座检检测到负载电流过大时会强制断电并上报告警消息,下面进行相关功能设置

  1. 在左侧选择【数据开发】点击【新建数据流】,在弹出的串口内填入必要信息 
  2. 拖入所需的功能模块并进行配置、连接
  3. 如图所示,在设备事件中选择你的智能插座产品,在【选择事件】中选择【DeviceStatus】,在【选择事件属性】中选择【Message】 
  4. 如图所示。在【数据过滤】的【条件组合方式】选择【全部或】,过滤条件设置为【Message】、【等于】、【overcurrent】 
  5. 如图所示。在【APP推送】或【公众号推送】中填入推送标题及推送内容 
  6. 设置完成后点击【保存】、点击【启用】
  7. 更多内容请参考官网文档 数据开发

软件开发

ESP8266模组烧录固件

  1. 建议提前烧好固件然后再焊接,如果已经焊接可以通过设置板上预留的跳线进行烧写,烧写完成后记得将跳线改回去
  2. 获取 ESP8266腾讯云定制固件,可以去 乐鑫官网 或 腾讯github 下载
  3. 获取乐鑫官方Flash下载工具,下载地址 Flash下载工具
  4. 运行 flash_download_tool_v3.8.5.exe,依次点击【Developer Mode】、【ESP8266 DownloadTool】
  5. 如图所示进行设置,在第一栏点击【...】打开刚刚下载的固件,勾选左边的方框,在右边填入烧写地址“0x0”;CrystalFreq(晶振频率)请根据实际情况选择,部分型号可能是26M或24M,下载后如果串口出现乱码现象请重新修改晶振频率,这里设置为【40M】;【SPI SPEED】选择【40MHz】;【SPI MODE】选择【QIO】,【FLASH SIZE】请根据实际情况选择,一般 ESP-12F 模组为 【32Mbit】;右下角设置串口号和波特率,如果下载出错可尝试降低波特率;点击【START】开始下载 
  6. 有关烧写的更多内容请查阅“Flash下载工具”内相关文档,或访问 乐鑫官网

单片机软件开发

前提条件:已安装Keil—MDK,至少有ST-Link、JLink等调试器之一

懒人版

适用条件:最纯粹的懒人方案,硬件方案完全照搬,仅修改一些不重要的元件,单片机、ESP8266模组、电量计芯片、电流互感器、电压互感器、采样电阻的参数必须完全一致

开发步骤:

  1. 修改 projectqcloud-iot-sdk-tencent-at-basedincludeconfig.h,开启 #define DEBUG_DEV_INFO_USED 宏
  2. 在 projectqcloud-iot-sdk-tencent-at-basedporthal_os.c 中填入“后台开发”章节创建的 产品ID、设备名、设备密钥,代码如下
#ifdef DEBUG_DEV_INFO_USED
/* 产品名称, 与云端同步设备状态时需要  */
static const char sg_product_id[MAX_SIZE_OF_PRODUCT_ID + 1]  = "在这里填你的产品ID";
/* 设备名称, 与云端同步设备状态时需要 */
static const char sg_device_name[MAX_SIZE_OF_DEVICE_NAME + 1]  = "在这里填你的设备名";
/* 设备密钥, TLS PSK认证方式*/
static const char sg_device_secret[MAX_SIZE_OF_DEVICE_SERC + 1] = "在这里填你的设备密钥";
#endif
  1. 启动工程 projectMKD-ARMproject.uvprojx
  2. F7编译,按F8烧录
  3. Enjoy it!

进阶版

适用条件:想要了解本项目的更多软件细节,或出于降本等原因更换了不同型号的单片机,要求有一定软硬件开发经验

这里以将主控更换为 STM32F103C8T6 为例,假设你已经画好了新的原理图和PCB,软件开发过程大致如下:

  1. 使用 STM32CubeMX 生成工程,芯片选择 STM32F103C8T6,中断、引脚等请自行设置
  2. 移植Tencent OS Tiny,详细步骤请参考官方文档。本工程已将Tencent OS Tiny,提取出来并适配完成,你几乎可以直接放在你自己的工程中使用,这里说几个重点需要注意的地方:
    • 本工程使用Keil编译,STM32G070KBT6为cortex-m0+内核,因此使用 projectTencentOS-tinyarcharmarm-v7mcortex-m0+armcc 内的代码,STM32F103C8T6为cortex-m3内核需使用 projectTencentOS-tinyarcharmarm-v7mcortex-m3armcc 内的代码,请自行更换
    • 根据自己的实际需要修改 projectTencentOS-tiny os_config.h,详细内容请参考官方文档
    • 修改 projectSrcstm32g0xx_it.c,该文件由 STM32CubeMX 自动生成,除了需要添加自己的逻辑代码以外,在 void PendSV_Handler(void) 前添加 __weak 关键字,因为Tencent OS Tiny已经实现该函数
    • 同样是 projectSrcstm32g0xx_it.c,在 void SysTick_Handler(void) 内添加 Tencent OS Tiny 任务调度相关代码,如下所示
void SysTick_Handler(void) 
{ 
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */ 
  HAL_IncTick(); 
  /* USER CODE BEGIN SysTick_IRQn 1 */ 
  if(tos_knl_is_running()) 
  { 
      tos_knl_irq_enter(); 
      tos_tick_handler(); 
      tos_knl_irq_leave(); 
  } 
  /* USER CODE END SysTick_IRQn 1 * / 
}
  1. Porting qcloud-iot-sdk-tencent-at-based, this project has been adapted. You can use this as a reference to develop your own business logic. For more information, please refer to the documentation in the SDK
  2. The Flash of STM32F103C8T6 is 64KB, the RAM is 20KB, and the write life of Flash is 10k times, while the Flash of STM32G070KBT6 is 128KB, the RAM is 36KB, and the write life of Flash is 1k times. Note that the Flash write life of G0 is an order of magnitude smaller than that of F1! G0's Flash writes 8 bytes each time, and F1's Flash writes 2 bytes each time. Please pay special attention if you use the on-chip Flash to save information such as accumulated power consumption! For more information, please refer to the chip data sheet
  3. Flash space allocation The Flash address allocation in this project is as follows. If you change the chip, please modify  projectuser_codeuser_utils.h the relevant settings | Start address | End address | Size | Description | | :--------- | :------ --- | :---- | :--------- | | 0x08000000 | 0x0801afff | 108KB | Program | | 0x0801b000 | 0x0801bfff | 4KB | Device Information | Quantity |
  4. The [Event Reporting] function is used in the project to report overcurrent messages. Similarly, you can report any events you need, such as high temperature alarms. As mentioned earlier, the [Data Development] filter condition is set to [Message], the message content is [overcurrent], and the relevant code is in projectqcloud-iot-sdk-tencent-at-basedusr_logicdata_template_usr_logic.c

mass production

This project comes with a simple mass production plan. The steps are as follows:

  1. After the product is officially released, log in to the [Internet of Things Development Platform] and generate device information in batches on the [Equipment Mass Production] page. The [one machine, one secret] method is used here. For detailed operation methods, see  Mass Production Management
  2. Modify  toolsdevice_info.txt, fill in the device information generated in the previous step, the format is  PRODUCT_ID DEVICE_NAME DEVICE_SECRET, one per line, without any extra spaces, line breaks, etc., for example
    PVXXXXXXDN my_smart_socket_001 cyBXXXXXXXXXXXXXXXI/z5== 
    PVXXXXXXDN my_smart_socket_002 yGhXXXXXXXXXXXXXXXBtZX== 
    PVXXXXXXDN my_smart_socket_003 gdGXXXXXXXXXXXXXXXYWdl==
  3. Run  the script and the corresponding  file device_info_hex_generate.py will be generated in the current directory. DEVICE_NAME.hex
  4. Burn the same compiled  project.hex files from the "Software Development" chapter for all devices
  5. Burn separate  DEVICE_NAME.hex files for each device individually
  6. Take for  STM32 ST-LINK Utility example, after starting the software, click File->Open file, select project.hex, click [Connect] to connect, click [Program & Verify] to burn, click File->Open file again, select DEVICE_NAME.hex, and click [Program & Verify] to proceed. Burn, click [Disconnect] to disconnect after completion 

Note: Device information must not be burned repeatedly. If two or more devices have burned the same device information, the two devices may kick each other offline during use.

Q&A

Q: The code of Tencent OS Tiny is too large and the domestic network conditions are not good. What should I do if git clone always fails?
A: Use gitee to create a new warehouse, choose to import an existing warehouse and fill in 
https://github.com/Tencent/TencentOS-tiny.git.  After the background synchronization is completed, you can use it like github, which is very fast.

Q: What is the difference between an isolated power supply solution and a non-isolated power supply solution?
A: Please check the HLW8032 information in the doc directory

Q: The device goes offline after the router is restarted or the network is disconnected. What is the solution?
A: The official ESP8266 customized firmware currently does not support automatic reconnection when WiFi is disconnected. You can only restart the device and wait for automatic reconnection, or power on again.

Q: Where can I buy the casing?
A: You can find it by searching for keywords such as "smart socket shell" and "smart socket shell containing hardware" on famous domestic e-commerce websites. What? Can't find the same style? It was probably sold out and taken off the shelves.

Q: Do you plan to release a finished product? Or do small batches?
A: Personal energy is limited, so all design plans are open source. If you are interested, you can make your own. If you feel that my plan is not comfortable to use, you are welcome to modify it yourself.

Q: Can you provide technical support?
A: Personal energy is limited, just let it happen! Leave a message and I'll try my best to reply.

Q: Can this solution be used commercially?
A: Please comply with the open source agreement.

References

Tencent Cloud IoT development platform document  https://cloud.tencent.com/document/product/1081
Tencent Cloud IoT console  https://console.cloud.tencent.com/iotexplorer
Espressif official AT firmware list  https:/ /www.espressif.com/zh-hans/support/download/at
Espressif Flash Download Tool  https://www.espressif.com/sites/default/files/tools/flash_download_tool_v3.8.7_0.zip
Espressif Express official download guide  https://docs.espressif.com/projects/esp-at/zh_CN/latest/Get_Started/Downloading_guide.html
AT command set  https://docs.espressif.com/projects/esp-at/zh_CN/latest/AT_Command_Set/ index.html
ESP8266 Tencent Cloud customized AT firmware download address  http://download.espressif.com/esp_at/firmware/ESP8266/ESP8266-IDF-AT_V2.1.0.0.zip
ESP8266 Tencent Cloud customized AT firmware open source address  https:// github.com/tencentyun/qcloud-iot-esp-wifiTencent
Cloud AT SDK  https://github.com/tencentyun/qcloud-iot-sdk-tencent-at-based
Tencent OS Tiny  https://github.com/Tencent /TencentOS-tiny

参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2025-06-19 05:24:19

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号