Alibaba Cloud IoT smart socket based on EMW3080
Table of contents
1. Physical display
2. APP display
3. Server-side display
4. Project requirements
5. Product demand
6. Component selection and module function description
7. Function description
8. Layout and routing design
9. Large rollover(debug)on site
10. Create a project on Alibaba Cloud Life IoT Platform
11. Areas for improvement
12. Summary
13. Video demonstration (4)
1. Physical display


2. APP display




3. Server-side display




4. Project requirements
Design a circuit board that can control home appliances and on-board LED lights through mobile APP and Tmall Genie.
5. Product demand
5.1 5V supplies power to the entire system, and uses a voltage stabilizing chip to generate a 3.3V stable voltage to power a specific chip;
5.2 The hardware part includes power supply regulator, WIFI module, OLED, USB to TTL module, relay, driver chip, microcontroller, buttons, LED, etc.;
5.3 Embedded software programming, mobile APP, server, etc.
6. Component selection and module function description
6.1 Main control chip
The selected model is STC15W4K56S4 . This chip has 56Kb of ROM used to store programs and 4Kb of data memory RAM. It has abundant resources and is sufficient to solve the main control problem of this project.

6.2 WIFI module
Choose the EMW3080 module produced by Qingke with onboard antenna to avoid the trouble of external antenna. Moreover, it supports Alibaba Cloud Feiyan (ILOP) firmware. There is an official technical manual for reference. The performance can basically meet the needs of this project, and the price is relatively cheap. . EMW3080 purchase address , choose Alibaba Feiyan for firmware , and choose EMW3080V2P for model (onboard antenna)

6.3 USB to TTL chip
Select CH340E and add a 100uf decoupling tantalum capacitor to the VCC pin to provide a strong current to the chip and prevent the CH340E from being reset the moment the relay is opened. The 330Ω resistor at the TXD end prevents the current from TXD from flowing back into the RXD of the microcontroller, so that the microcontroller can be reset normally.

6.4 LDO low dropout linear regulator
Choose ME6215C33M5G and AMS1117-3.3 to power the microcontroller and EMW3080 module respectively to ensure the normal operation of each communication module. ME6215C33M5G has an enable pin, and a button can be used to reset the system. AMS1117-3.3 can output a maximum current of 1A, which is enough to power the WIFI module during normal communication. [The 2nd and 4th pins of AMS1117-3.3 are internally connected together]

6.5 Relay and its driving circuit
The relay chooses Huike 's DC5V. Since the ordinary IO port of the microcontroller defaults to a high level when powered on, but we hope that the relay will be closed when power is started, so an inverter is connected to the IO port and a dedicated The NUD3124LT1G chip that drives the relay ensures reliable drive of the relay.
Working instructions of the relay: Pins 2 and 5 are normally closed terminals, and pins 1 and 5 are normally open terminals. After the relay is opened, the magnetic force attracts the iron piece to pin 1, turning 1 and 5 into conduction (that is, terminal P2 is conductive), and 2 and 5 are disconnected:

6.6 OLED display
Choose the Zhongjingyuan 4-pin IIC driven OLED, which comes with a detailed demo to facilitate transplantation to different platforms.

6.7 Temperature and humidity module
Using DHT11 module, it has its own pull-up resistor, no need to add an external one. This module has long transmission distance, low power consumption, small size, sufficient accuracy, and low price. Sending and reading data only occupies one IO port of the microcontroller and takes up few resources.

6.8 Independent buttons
Use the 3*6*5 patch tact switch from Lichuang Mall .
SW2 jog is used to control the relay switch status. Press and hold to enter the network distribution mode. In addition, two additional buttons are added to jog the two LEDs on and off. It is necessary to change the number of buttons to 3 in the program and add the jog business of SW3 and SW4.

6.9LED
Use SMD LED, the package is 0805, which is easier to solder.
LED has many uses and can be debugged together with serial port printf during debugging. In this project, the functions of the four OLEDs are:
LED1: Slow flashing indicates that the program is running normally; fast flashing indicates that the network is being configured;
LED2, LED3: used to be controlled by buttons or APP to turn on and off;
LED4: Flashing quickly indicates that the network configuration is successful.

7. Function description
7.1 There are 4 touch buttons on the board to control the circuit:
SW1 controls system reset;
Short press SW2 to control the switch of the relay, and report the switch status of the relay and the temperature and humidity read by DHT11 to the server and APP. Press and hold to configure the network;
SW3 controls the on and off of LED2 and uploads the on and off status to the server and APP;
SW4 controls the on and off of LED3 and uploads the on and off status to the server and APP.
7.2 One-click network configuration
Before the board is powered on for the first time, the product configuration guide on the Alibaba Cloud IoT official website is set to one-click network configuration . After power on, the onboard LED1 flashes slowly to indicate that the program is executing normally. Press and hold SW2, and LED1 flashes quickly to enter the configuration network. mode, use the mobile APP to scan the QR code to configure the network. After the network is successfully configured, LED4 will flash quickly and LED1 will flash slowly again. Afterwards, the network will be automatically configured when the power is turned on again. There is no need to long press SW2.
7.3 Data interaction
After the board is successfully connected to the Internet, the APP, server cloud, and real devices can interact in real time. After setting up the APP configuration panel on the server web page, the temperature and humidity read by the microcontroller through DHT11 can be displayed on the OLED, and the temperature and humidity data and relay switches can be reported on time. The status and the on/off status of the 2 LEDs can also be triggered to report instantly by pressing the button. The APP and cloud will receive corresponding data for display, and users can view data tables, charts, etc. for result analysis. The APP can also control the switches of relays and LED lights.
7.4 Access Tmall Genie control
First delete the bound device from the Cloud Smart APP (long press SW2 to enter the network configuration mode, it will automatically unbind and then configure the network), and configure the EMW3080 to one-click network configuration mode. Tmall Genie is also connected to the former Same WIFI. Afterwards, through the voice "Tmall Elf, find teammates", at this time, the board synchronizes and long presses SW2 to enter the network configuration mode. After about a few seconds, Tmall starts to configure the network, and the network configuration is successful after more than ten seconds.
You can turn on the small fan by saying "Tmall Elf, turn on the socket";
You can turn off the small fan by saying "Tmall Elf, turn off the socket".
8. Layout and routing design
8.1 Strong current part
The high-current parts are not covered with copper . This part often has high voltage, strong current, and strong electromagnetic field, which can easily cause interference, voltage breakdown, or current burnout. It is a dangerous part on the PCB board. High-voltage wiring must be well insulated and isolated. In order to prevent breakdown, the PCB board can be hollowed out, grooved, and the path lengthened in some places. Creepage, etc. must also be considered. Wiring with larger currents should be thicker:

Figure 8-1-1 Precautions for strong current parts
8.2 USB-Micro interface and CH340E chip
Use a slotted USB port as close to the edge of the board as possible to facilitate plugging; the USB port and the D+ and D- data lines of CH340E must use differential wiring and the wiring distance should be as short as possible. When differential wiring is used in Lichuang EDA, the suffixes of adjacent differential lines need to be added with "+" and "-" or * xx_N and xx_P* so that the system can recognize them:

Figure 8-2-1 Differential wiring
8.3 Filter capacitor
电源VCC要先经过滤波电容再到达需要用到电的模组,否则可能会导致供电不稳定,模组不能正常工作。如下图,从USB口过来的VCC不能直接连绿色的线到达3.3V稳压芯片,要根据橙色的路线连线,电容才可以起到作用,其他部分同理:

图8-3-1 滤波电容的正确利用
8.4 WIFI模块
EMW3080附近需要净空区,尽量不要铺铜和放置元器件,并且把板载天线部分伸出板子外面,尽可能降低外部因素对WIFI通信的干扰:

图8-4-1 WIFI模块在布线时的注意事项
8.5 线宽、铜厚与电流的关系
设计中需要注意那个部分通过的电流比较大,需要参考下表进行线宽的修改:

图8-5-1 合理参考电流与线宽、铜厚关系表格
8.6 合理放置丝印,便于后期焊接调试

图8-6-1 用丝印标注参数
8.7 焊接时,注意芯片的方向的标志,避免焊反

图8-7-1 注意防焊反
8.8 PCB的DRC检查

图8-8-1 无DRC错误
九、大型翻车(调试)现场
9.1 测试电源
画完PCB、嘉立创打样、立创商城BOM表下单后,板子、元器件都到了,第一步先把USB接口、LED灯和它的限流电阻、两个3.3V稳压芯片给焊上,并测试电压值,基本稳在3.3V左右,核心电源部分OK:

图9-1-1 ME6215C33M5G成功输出3.28V

图9-1-2 AMS1117-3.3成功输出3.29V
9.2 测试LED及单片机
把剩余部分焊好,用万用表蜂鸣档测试电路有无短路、虚焊,然后想测试下载进去的程序是否正常运行:
```
LED1 = 0 ; LED2 = 0 ;
LED3 = 0 ; LED4 = 0 ; //函数主体部分
```
正常来说,四个灯都会亮,因为LED的正极连到了3.3V,负极连到IO口,IO口给低电平,LED导通,但程序烧录完成后,发现只有LED3和LED4亮, LED1和LED2是熄灭的状态。用万用表检测完LED并没有焊坏后,查看原理图和手册后,发现连在LED1和LED2的两个引脚都是单片机的PWM输出引脚,上电复位后是高阻输入状态,要对外能输出,要软件将其改为强推挽输出或准双向口/弱上拉。

图9-2-1 LED1、LED2对应的引脚

图9-2-2 PWM输出引脚默认为高阻输入状态

图9-2-3 STC15W4K56S4 IO口模式设置
这里,我将全部IO口初始化为普通IO口(准双向口),重新烧录程序,成功点亮所有LED灯:
```
P0M1=0;P0M0=0;P1M1=0;P1M0=0;
P2M1=0;P2M0=0;P3M1=0;P3M0=0;
P4M1=0;P4M0=0;P5M1=0;P5M0=0; //上电初始化所有IO口为普通IO
LED1 = 0 ; LED2 = 0 ;
LED3 = 0 ; LED4 = 0 ;
```

图9-2-4 成功点灯
9.3 调试串口1
单片机读取DHT11温湿度数据(测试时将返回值均设为0),并使用串口1发送数据,调试效果如下:
PS:一定要注意选择IRC频率为22.1184MHz,每次打开软件默认为11.0592MHz,另外串口波特率为115200,若使用默认频率,串口助手会收到乱码!!翻了好几次车!!所以这个频率要根据具体芯片的手册来选择。

图9-3-1 串口1成功接收数据(注意切换IRC频率)
9.4 调试WIFI串口
用烧录器直连EMW3080,RX1->TX2 ,TX1->RX2 ,之后利用格西烽火串口调试助手,波特率选择115200,给EMW3080发送模块AT指令,WIFI模组可以返回正确的数据:


图9-4-1 利用格西烽火执行AT指令,并返回连接成功的数据
9.5 调试OLED
店家给的4脚IIC的OLED是STM32的例程,故有些地方要稍作修改,比如SDA(P34)和SCL(P35)脚要设为推挽输出模式,按照上面的IO口模式设置表格,即有:P3M1的对应脚设为0,P3M0的对应脚设为1, 即:
P3M1 = 0000 0000 -> P3M1 = 0
P3M0 = 0011 0000 -> P3M0 = 0x30
但是在编译程序时提示如下错误:

图9-5-1 编译错误
然后去百度,发现128字节的data数据空间已经用完,需要使用xdata数据空间:

图9-5-2 选择XDATA
之后成功编译,执行程序,OLED可以正常工作,显示正确的数据,但是将OLED的驱动程序整合到整个工程时,只有OLED屏幕在闪烁,并且之前正常慢闪的LED1熄灭了,后来发现生成的Hex文件已经超过了56Kb,而 STC15W4K56S4的存储程序用的ROM(FLASH)只有56Kb(查阅手册或观察名字可得),注释掉一些用不到的函数后,Hex文件成功降到56Kb以下,重新烧录测试,测试效果如下,成功在指定位置显示指定的字符串:

图9-5-1 OLED成功显示数据
9.6 调试DHT11模块
移植对应Demo后,将读到的温度、湿度分别转化为字符串输出给OLED,这里要注意湿度只有整数部分,温度有小数部分并且DHT11的数据引脚Dat必须接上拉电阻(可选为5.1KΩ),增强驱动能力,并在空闲状态下保持高电平,另外VCC和GND之间可以 加一个100nF的电容,用于去耦滤波,使接收到的数据更精确:
PS:购买的DHT11已自带上拉电阻,故本次不用外加

图9-6-1 DHT模块原理图

图9-6-2 成功读取温湿度数据
9.7 调试继电器模块
为了使继电器更稳定的工作,给它加上了专门的驱动芯片,并希望它在上电时为关闭的状态,故在IO口(P26上电默认为高电平),加上一个反相器,下面写一个简单的程序测试继电器能否正常工作:
```
......
#define Relay P26
void main()
{
P2M1=0;P2M0=0;
while(1)
{
delay_ms(3000);
Relay = ~Relay; //约3秒翻转一次继电器开关状态
}
}
```
烧录程序后,成功实现3秒翻转一次继电器开关状态。
9.8 调试配网
调试配网之前,需要在IAP_EPPROM.c文件中,将自己设备的四元组信息更改。
图9-8-1 更改四元组信息
所有模块已调试完毕,目前均可正常工作。利用USB线供电,LED1慢闪,表示程序正在执行,然后长按SW2,进入配网模式,打开云智能APP扫码配网,跟着步骤走后,出现以下情况:

图9-8-2 无法发现 adh_xxx_xxx WIFI
然后并没有在WIFI列表找到**_adh_xxx_xxx**,经在群里讨论,发现了不对劲的地方:

图9-8-3 配网方式默认为设备热点配网

图9-8-4 程序默认为一键配网模式
也就是说,两种不同的配网方式混用了,导致无法配网,于是我修改后采用两种方式配网,一种是修改为一键配网,不需要修改程序,设置如下:
图9-8-5 修改配网方式
另一种方法是,配网方式仍为设备热点配网,配网的代码改为将设备设置为AP模式的AT指令:"AT+ILOPWASAP
",EMW3080如果配网成功,服务器会向EMW3080发送回应,表示配网成功,到此处配网成功。

图9-8-6 WIFI模块成功收到连接成功的信息
配网成功后,可以使用APP控制板子继电器开关、LED亮灭;并且操作板子的状态,也会实时传给APP和服务器网页端。只要板子位于原来配网连接的WIFI范围内,我们就可以通过手机APP全球任意范围内,只要有网络,就可以远程控制板子,并获取其状态信息。若要换一台手机控制,因为我设置的分享方式是授权式,故要先解绑,长按SW2即可先解绑后等待重新配网,使用新手机扫码配网即可。
9.9 调试WIFI模组上报继电器状态和温湿度数据到APP和网页端
首先标识符要对应,即在网页选的功能定义的标识符要与程序里的一致,位置如下图



图9-9-1 两者的标识符要一一对应
做完这一步后,发现按动SW2,继电器的开关状态可以正确的上报到APP和网页,但是APP不能控制继电器的状态,就是说WIFI模组没有收到正确的信息,使单片机执行对应的功能,于是我去检查WIFI接收数据的函数,发现有处标识符没有改动,修正后,成功通过APP控制继电器的开关:

图9-9-2 接收数据用的标识符均要改成对应的标识符
9.10 增加APP控制LED2、LED3的亮灭功能
修改按键点动函数,成功使得按下SW3可以翻转LED2的亮灭,按下SW4可以翻转LED3的亮灭(LED初始状态均为熄灭),板子上可以实现该功能,但是不能上传数据到APP和网站;如果用APP执行对应操作,板子会响应,这说明设备数据上传出了问题,设备接收数据正常,当时上报的数据如下:

图9-10-1 一次性上报5个数据失败
于是我在想是不是一次不能上报那么多数据,服务器承受不住?本来上报3个数据(继电器状态、温度、湿度)是正常可以互动的,现在5个不行,那4个呢?于是乎我注释掉1个(保留任意4个),重新烧录程序,发现又可以互动了!!经群里老铁点明,可以上报5个数据,但是上报数据用的数组SendBuf和JsonStr要增大才行,我尝试这增大这两个数组,于是乎本来就55KbHex文件,FLASH遭不了重,增大后又超56Kb了。后来我尝试分两次上报,于是再复制一个上报函数,用来上报2个LED灯的状态,继续烧程序,发现5个数据均可上报,互动成功!!!

图9-10-1 2个函数分开上报【注意在.c文件增加函数时,要在对应的.h文件声明】
图9-10-2 服务器网页端接收到的实时数据,经比对,数据正常

图9-10-3 APP接收到的数据,与服务器网页端一致
9.11 二度调试串口2
诡异的事情终究还是发生了......前一天板子还能正常上电就自动配网,第二天早上上电后就毫无反应,无法配网,检查完不是路由器网络的问题后,再反复切换一键配网模式和设备热点配网模式后,发现LED4(配网成功的指示灯)始终不闪,于是我用烧录器直连EMW3080进行串口调试,使用AT指令启动一键配网模式,发现WIFI模块能收到服务器发送回来的成功连接信息,并且操控APP的按键,可以上报到服务器日志里,但是APP和板子毫无互动:
图9-11-1 WIFI模块可以连上服务器
图9-11-2 网页端日志显示成功收到数据
这说明WIFI是接收到服务器给它发送的连接成功的信息,但是单片机没有收到,因为如果单片机收到,LED4灯的闪烁标志位会被置1,置完1后就会快速,表示配网成功:
图9-11-3 单片机收到成功配网信息后会将配置闪烁标志位
所以,我推测,WIFI接收到的表示连接成功的字符串"ILOP,CONNECTED",单片机并没有通过串口2成功读取,而这两者的RX和TX各通过一个330Ω的限流电阻交叉互连,我了解到这2个电阻是起到防电流倒灌到单片机,从而使单片机可以正常复位的作用,我隐约记得零妖说可以把这两个330Ω电阻给拆掉,于是乎我把这两个电阻换成0Ω电阻,重新上电,一切恢复正常...orz...经群里的老铁反映,有可能是单片机发出去的信号太弱了,电流过小。
9.12 调试小风扇
由于宿舍没有什么可以改造的电器,搜刮一顿后 ,发现舍友的散热用小风扇,是DC12V/0.09A,由于板子最大能供电5V,尝试用USB口供电,发现电压可以,但是电流不够,小风扇无法转动,故使用烧录器接上充电宝给小风扇供电。

9.13 调试天猫精灵
在产品管理中的人机交互设置页面中,发现天猫精灵中的插座品类的状态映射属性只有一个,为开关状态,其标识符为**“powerstate”**,正好对应继电器开关的标识符。

Press and hold SW2 to unbind and enter the network configuration mode. The board will first unbind from the Cloud Smart APP. Tmall Elf connects to the same WIFI as the former, and can automatically configure the network by saying "Tmall Elf, find teammates" . Subsequently, use "Tmall Elf, open the socket" to turn on the small fan, and "Tmall Elf, close the socket" to turn off the small fan.
10. Create a project on the Alibaba Cloud platform
10.1 Register an Alibaba Cloud account, perform real-name authentication, and log in to the Alibaba Cloud Life IoT Platform
10.2 Create a new project





10.3 Set product function definition

10.4 Configure human-computer interaction interface

It should be noted that the network distribution mode must be the same as the network distribution mode in the program. The description is as follows:


10.5 Add test equipment





At this point, the device has been added successfully, and the EMW3080 can be connected to the Alibaba Cloud Life IoT Platform in subsequent operations.
11. Areas for improvement
11.1 Serial port debugging
Since multiple serial ports were used this time, no consideration was given to selecting the serial port through jumper caps, which made subsequent debugging difficult and required additional soldering of jumpers.
3-1, 4-2 are used to download programs and debug serial port 1
3-5, 4-6 are used to debug the WIFI serial port

11.2 Heat dissipation of WIFI module
When the WIFI module is working normally, I find that the temperature near the module is a bit high to the touch, which may affect the normal operation of the WIFI module. Referring to the heat dissipation methods of other students, I found that grooves can be dug at the bottom to increase the heat dissipation area, which can To a certain effect:

11.3 One relay is improved to a multi-channel relay
Once you understand how to control one relay, you can design multiple relays to control household appliances and realize smart life.
11.4 Power supply section
During debugging, it was found that the current provided by the USB power supply was not large enough, and some modules could not work properly when multiple electrical appliances were connected. A 220Vac-5Vdc module can be used to power the entire board. Be careful to add fuses and safety capacitors.


12. Summary
Speaking of Lichuang EDA, I used to use it for AD packaging. At that time, I didn’t realize that I missed a big treasure (manual dog head). I met Lichuang EDA this semester because Lichuang was doing online training for our school for the school competition. There was a basic training camp later, but because the final exams were piled together and there was also a school competition, I regretted not signing up. But then I found out that the official account promoted the advanced class training camp, so I signed up~
I had never been exposed to the Internet of Things before. I had only been exposed to microcontrollers and embedded systems. It was a new challenge for me. I had almost no experience in actual project implementation. It was a process for a novice to start from scratch. Then I followed Song Gong and Zhou Gong step by step. It was the first time I really drew a board in Lichuang EDA, the first time I placed an order based on the BOM list in Lichuang Mall, the first time I experienced SMT, and the Nth time Jialichuang made a Board, direct one-stop service, very convenient. After the board arrives, it is divided into modules for soldering. After each module is soldered, one is debugged. First, use a multimeter to check whether there are any weak soldering, short circuits, etc., and then power on and test each module one by one. After the headache of the hardware part, it’s time to move on to the software part. I forgot all about pointers after I finished learning them in my freshman year. It’s still a bit difficult to read. After listening to Song Gong’s explanation, I don’t understand it. Then I watch the replay and read the program. , and modified the relevant parameters, downloaded it to the board for testing, and gradually got a clue. There are many questions that you can ask in the group to get advice, but in the end you still have to debug slowly by yourself. The hardware and software are adjusted together, but it is mainly a hardware problem. There are not many things that need to be changed in the software. The identifier is one of them, which will be read later. After understanding the program, I added additional services and successfully reported information on humidity and key-pressing to control the LED on and off, and could use the APP to control the LED on and off. After acquiring Tmall Genie, it successfully used Tmall to distribute the network and control the small fan switch.
Through the actual training camp, my ability to adjust code and hardware has also been improved to a certain extent. Through the actual project, I have also understood the networking, communication, and control processes of the Internet of Things to a certain extent. With a commercial case, I have learned from the needs Begin with analysis, then proceed to solution design, hardware design, and software design, learning how to solve problems step by step. There are also pointers and structures that I don’t quite understand and I didn’t want to understand in depth before, which gave me the urge to learn them again. Come on!
We would like to thank Lichuang EDA for organizing this event, and thank Gong Song, Gong Zhou, Gong Mo, Gong Ye and other staff for providing us with financial and technical support. Friends, see you again in the winter vacation class~
13. Video demonstration (4)
1. Interaction between the board and the web page
2. Interaction between APP and board
3. Visual interface
4. Tmall Elf Control