qoistoochee128

[Training Camp_Advanced Class] ESP8266 Socket Development Board (Detailed Tutorial)

 
Overview

Smart socket test board based on ESP-12F

Function description

Access the Internet through ESP8266 to achieve remote control, and can be connected to Mijia to achieve voice control of Xiaoai classmates (Baidu Xiaodu and Tmall Genie cannot be tested because they do not have equipment, and theoretically can support the three major elves at the same time). There is a ch340 download circuit on the board, and it is designed with an automatic download circuit to facilitate programming and debugging.

designing process

1. Component selection

1. Choose ESP-12F as the main control. Supporting SOC can save PCB space and reduce circuit complexity. At the same time, ESP8266 supports arduino development, which can greatly reduce programming difficulty. Anxin’s ESP-12F (ESP8266 module) is also very cheap, so Choose ESP-12F.

2. The download part adopts CH340C, which has good compatibility, simple peripheral circuit and low cost.

3. The relay driver uses ON Semiconductor's inductive load driver NUD3124.

2. PCB design (omitted)

There is nothing much to say, just pay attention to the isolation of strong and weak electricity and the layout of the antenna .

3. Development environment

Developed using arduino IDE, using the library of Blinker, and building the arduino environment

blinker library file

Windows: Unzip the downloaded blinker library to the My Computer>Documents>Arduino>libraries folder

Mac OS: Unzip the downloaded blinker library into the Document>Arduino>libraries folder

After the environment is set up, select NodeMCU, as shown in the figure image.png

NodeMCU pin definition

D0~D8: Digital input pins.

PWM: All digital I/O can be used for PWM output

Serial port: Serial (TX-D10/RX0-D9) can be used to communicate with computers or other devices. Serial1 (TX1-D4) only has an output port and can be used to send data to other devices.

SPI: The MISOMOSISCK pin can be used for SPI communication.

IIC: In fact, D0~D8 can be used for IIC communication.

A0: Can only be used as analog input, the input voltage range is 0~3.3V, 8-bit accuracy.

L: The LED located on the 8266 module. This LED is connected to the D2 pin and can be programmed and controlled using the macro LED_BUILTIN


You can use Dx or the GPIO number corresponding to the pin to control the pin, such as

a=digitalRead(D1)

a=digitalRead(5)

When programming the pin program that is not recommended , the three pins TXD0RXD0GPIO0 will be used. If you need to use them, please complete the programming before connecting the peripherals for use. The esp8266 module uses some IO internally, such as S1S2S3SCSOSKGPIO15. Please do not call it during programming, otherwise it may cause program running errors.

IO mode settings: INPUT (input mode), OUTPUT (output mode), INPUT_PULLUP (pull-up input mode) 4456352-8938615d16aa9d71.webp.jpg

4. Procedure

#define BLINKER_WIFI               //官方wifi协议库
#define BLINKER_MIOT_OUTLET        //设置小爱插座类库
#define BLINKER_ALIGENIE_OUTLET    //设置天猫插座类库
#define BLINKER_DUEROS_OUTLET      //设置小度插座类库
#define BLINKER_PRINT Serial       //串口协议库
#define BLINKER_ESP_SMARTCONFIG    //SmartConfig配网

#include <Blinker.h>

#define LED_BUILTIN D4             //LED
#define RELAY_PIN   4              //继电器
const int  buttonPin = D1;         //按键
int lastButtonState = 0;

// 新建组件对象
BlinkerButton Button1("btn-abc");  //设置app按键的键名

char auth[] = "f3fd6e961589";      //Your Device Secret Key
//char ssid[] = "CU_buce";           //你的Wi-Fi网络的SSID或名称(SmartConfig自动配网时可忽略)
//char pswd[] = "zj.629858";         //您的WiFi网络WPA密码或WEP密钥(SmartConfig自动配网时可忽略)

bool oState = false;

// app上按下按键即会执行该函数
void button1_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);  //串口打印
        if (state=="on") {                     //如果state是on状态
        digitalWrite(LED_BUILTIN, LOW);        //LED开
        digitalWrite(RELAY_PIN, LOW);          //继电器开
        Button1.color("#00BB00");              //设置app按键是绿色
        // 反馈开关状态
        Button1.print("on");
        oState = true;
    } else if(state=="off"){
        digitalWrite(LED_BUILTIN, HIGH);       //LED关
        digitalWrite(RELAY_PIN, HIGH);         //继电器关
        Button1.color("#FF0000");              //设置app按键是红色
        // 反馈开关状态
        Button1.print("off");
        oState = false;
    }
}

//*******如果小爱有对设备进行操作就执行下面
void miotPowerState(const String & state)
{
    BLINKER_LOG("need set power state: ", state);

    if (state == BLINKER_CMD_ON) {
        digitalWrite(LED_BUILTIN, LOW);
        digitalWrite(RELAY_PIN, LOW);
        Button1.color("#00BB00");              //设置app按键是绿色

        BlinkerMIOT.powerState("on");
        BlinkerMIOT.print();

        Button1.print("on");
        oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {
        digitalWrite(LED_BUILTIN, HIGH);
        digitalWrite(RELAY_PIN, HIGH);
        Button1.color("#FF0000");              //设置app按键是红色

        BlinkerMIOT.powerState("off");
        BlinkerMIOT.print();

        Button1.print("off");
        oState = false;
    }
}

void miotQuery(int32_t queryCode)
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);

    switch (queryCode)
    {
        case BLINKER_CMD_QUERY_ALL_NUMBER :
            BLINKER_LOG("MIOT Query All");
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
        case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
            BLINKER_LOG("MIOT Query Power State");
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
        default :
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
    }
}

//*******如果天猫精灵有对设备进行操作就执行下面
void aligeniePowerState(const String & state)
{
    BLINKER_LOG("need set power state: ", state);

    if (state == BLINKER_CMD_ON) {
        digitalWrite(LED_BUILTIN, LOW);
        digitalWrite(RELAY_PIN, LOW);
        Button1.color("#00BB00");              //设置app按键是绿色

        BlinkerAliGenie.powerState("on");
        BlinkerAliGenie.print();

        Button1.print("on");
        oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {
        digitalWrite(LED_BUILTIN, HIGH);
        digitalWrite(RELAY_PIN, HIGH);
        Button1.color("#FF0000");              //设置app按键是红色

        BlinkerAliGenie.powerState("off");
        BlinkerAliGenie.print();

        Button1.print("off");
        oState = false;
    }
}

void aligenieQuery(int32_t queryCode)
{
    BLINKER_LOG("AliGenie Query codes: ", queryCode);

    switch (queryCode)
    {
        case BLINKER_CMD_QUERY_ALL_NUMBER :
            BLINKER_LOG("AliGenie Query All");
            BlinkerAliGenie.powerState(oState ? "on" : "off");
            BlinkerAliGenie.print();
            break;
        case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
            BLINKER_LOG("AliGenie Query Power State");
            BlinkerAliGenie.powerState(oState ? "on" : "off");
            BlinkerAliGenie.print();
            break;
        default :
            BlinkerAliGenie.powerState(oState ? "on" : "off");
            BlinkerAliGenie.print();
            break;
    }
}

//*******如果小度有对设备进行操作就执行下面
void duerPowerState(const String & state)
{
    BLINKER_LOG("need set power state: ", state);

    if (state == BLINKER_CMD_ON) {
        digitalWrite(LED_BUILTIN, LOW);
        digitalWrite(RELAY_PIN, LOW);
        Button1.color("#00BB00");              //设置app按键是绿色

        BlinkerDuerOS.powerState("on");
        BlinkerDuerOS.print();

        Button1.print("on");
        oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {
        digitalWrite(LED_BUILTIN, HIGH);
        digitalWrite(RELAY_PIN, HIGH);
        Button1.color("#FF0000");              //设置app按键是红色

        BlinkerDuerOS.powerState("off");
        BlinkerDuerOS.print();

        Button1.print("off");
        oState = false;
    }
}

void duerQuery(int32_t queryCode)
{
    BLINKER_LOG("DuerOS Query codes: ", queryCode);

    switch (queryCode)
    {
        case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
            BLINKER_LOG("DuerOS Query power state");
            BlinkerDuerOS.powerState(oState ? "on" : "off");
            BlinkerDuerOS.print();
            break;
        case BLINKER_CMD_QUERY_TIME_NUMBER :
            BLINKER_LOG("DuerOS Query time");
            BlinkerDuerOS.time(millis());
            BlinkerDuerOS.print();
            break;
        default :
            BlinkerDuerOS.powerState(oState ? "on" : "off");
            BlinkerDuerOS.print();
            break;
    }
}

//如果本地开关有动作执行下面手动模式
void KeyDetection(void){
  if((digitalRead(buttonPin)==LOW)&&lastButtonState==0){
      Blinker.delay(15);                                //延时15ms消抖
      if((digitalRead(buttonPin)==LOW)){                 //确定按键按下
        digitalWrite(LED_BUILTIN, oState);
        digitalWrite(RELAY_PIN,oState);
        Button1.color(oState ? "#FF0000" : "#00BB00");
        Button1.print(oState ? "off" : "on");
        oState = !oState;
        lastButtonState = !lastButtonState;
      }
   }else if(digitalRead(buttonPin)==HIGH&&lastButtonState==1){
    lastButtonState = !lastButtonState;
   }
}

// 心跳包函数
void heartbeat() {
        if (digitalRead(RELAY_PIN)==LOW) {
        // 反馈开关状态
        Button1.print("on");
        Button1.color("#00BB00");              //设置app按键是绿色
        oState = true;
    } else if(digitalRead(RELAY_PIN)==HIGH){
        // 反馈开关状态
        Button1.print("off");
        Button1.color("#FF0000");              //设置app按键是红色
        oState = false;
    }
}

void setup()
{
    Serial.begin(115200);             //初始化串口
    BLINKER_DEBUG.stream(Serial);     //输出调试信息

    pinMode(LED_BUILTIN, OUTPUT);     //初始化有LED的IO
    digitalWrite(LED_BUILTIN, HIGH);  //默认上电时关

    pinMode(buttonPin, INPUT_PULLUP); //初始化按键IO为内部上拉
    pinMode(RELAY_PIN, OUTPUT);       //初始化继电器IO
    digitalWrite(RELAY_PIN, HIGH);    //默认上电时关

    Blinker.begin(auth);              //初始化bliker
    Button1.attach(button1_callback); //app上操作必须的注册回调函数关联按键名“Button1”和判断程序“button1_callback

    BlinkerMIOT.attachPowerState(miotPowerState);        //小爱语音操作注册函数
    BlinkerMIOT.attachQuery(miotQuery);                  //小爱语音操作回调函数
    BlinkerAliGenie.attachPowerState(aligeniePowerState);//天猫语音操作注册函数
    BlinkerAliGenie.attachQuery(aligenieQuery);          //天猫语音操作回调函数
    BlinkerDuerOS.attachPowerState(duerPowerState);      //小度语音操作注册函数
    BlinkerDuerOS.attachQuery(duerQuery);                //小度语音操作回调函数
    Blinker.attachHeartbeat(heartbeat);//app定时向设备发送心跳包, 设备收到心跳包后会返回设备当前状态进行语音操作和app操作同步。
}

void loop()
{
    Blinker.run();
    KeyDetection();
}



5. One-click network configuration

The SmartConfig distribution network used by the program can be used with AIRKISS , ESPTOUCH , etc. To access Mijia, you need to add it in Mijia-My-Other Platform Devices and bind your Dian Deng Technology account.

6. Use demonstration

The program has built-in voice control for three major elves (Xiaomi, Tmall Genie, and Baidu Xiaodu). Since I only have Xiaoai, I have not tested the other two. If there are problems when testing the other two, you can give me feedback. .

参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2025-06-22 19:31:33

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号