BL602&BL604综合项目1:Dohome智能全彩灯具

通过BL602/BL604芯片作为系统主控,实现智能全彩灯的控制,可通过wifi接入Dohome平台,同时DoHome APP已经对接了各大智能音箱(小米小爱,百度小度,阿里天猫精灵,Amazon,Google,京东叮咚)。可根据需求通过BL602模组制作智能灯具和其他相关产品。

 

项目地址:

https://github.com/SmartArduino/Doiting_BL/tree/master/examples/bl602_Dohome_Light_RGB

 

在app_config.h中定义了软件的版本号,开发板产生的热点名,我们需连接通信的DoHome服务器,和我们udp,tcp通信使用的端口号,以及使用到的IO口:

/*
 * Log config
*/
#define USE_UART_LOG        1
#define LOG_COLOR_ENABLE    0
#define USE_UDP_LOG         0

#if USE_UART_LOG
#define LOG_MIAN_EN       1
#define LOG_WIFI_EN       1
#define LOG_FLASH_EN      1
#define LOG_UTILS_EN      0
#define LOG_PROTOCOL_EN   1
#define LOG_DEVICE_EN     1
#define LOG_UDP_EN        1
#define LOG_LIGHT_EN      0
#define LOG_TCP_EN        0
#define LOG_TCP_CONFIG_EN 1
#define LOG_HTTP_EN       1
#define LOG_UPLOAD_EN     1
#define LOG_SNTP_EN       1
#define LOG_TIMER_EN      1
#define LOG_PROCES_EN     0
#define LOG_LED_DRIVE_EN  0
#define LOG_LED_BOARD_EN  0
#define LOG_OTA_EN        1
#define LOG_UDPLOG_EN     0
#define LOG_FACTORY_EN    1
#define LOG_KEY_EN        1
#define LOG_LOG_EN        1
#define LOG_IR_EN         1
#define LOG_PRODUCT_EN    1
#endif


/*
 * product config
*/
#define IS_WYRGB
// #define IS_WY
// #define IS_STRIP
#if defined IS_STRIP
#define HARDWARE_TYPE           "_STRIPE"
#elif defined IS_WYRGB
#define DEVIVE_NAME_PREFIX      "Light_"
#define HARDWARE_TYPE           "_DT-WYRGB"
#define PIN_LED_R       27
#define PIN_LED_G       13
#define PIN_LED_B       26
#define PIN_LED_W       4
#define PIN_LED_Y       16
#elif defined IS_WY
#define HARDWARE_TYPE           "_DT-WY"
#else
#define HARDWARE_TYPE           "_LED"
#endif


/*
 * doit config
*/
#define FW_VERSION              "1.1.0"
#define DEF_SSID_PREFIX         "DoHome_"
#define DEF_HOSTNAME            "DoHome"

#define ORGANIZATION            "_DOIT"
#define CHIP_TYPE               "_ESP32"

#define REMOTE_SERVER_IP        "115.28.78.23"      

#define REMOTE_SRV_HOST         "led_iot.doit.am"
#define REMOTE_SRV_PORT         8899//6007

#define UPLOAD_SRV_HOST         "dohome.doit.am"
#define UPLOAD_SRV_PORT         8008

#define NTP_SERVER_HOST         "xinfeng.doit.am"
#define NTP_SERVER_PORT         80

#define UPLOAD_SRV_URL          "http://dohome.doit.am:8008"
#define NTP_SERVER_URL          "http://xinfeng.doit.am/iot_api/get_iot_time.php"

#define MAX_STA_NUM             3
#define TCP_SRV_PORT            5555

#define SPI_FLASH_SEC_SIZE      4096

#define UDP_BROADCAST_INFO_PORT 6095
#define UDP_SRV_PORT            6091

#define WEB_SRV_PORT            80
#define DNS_SRV_PORT            53

#define OTA_TCP_PORT            6093
#define OTA_TCP_ECHO_PORT       6094

#define HTTP_UPO
#define WIFI_DEF_MAGIC          0x03

#define KEEPALIVE_TIME          60*1000

#define RESTART_TRIGGER_COUNT      3

#define EXT_UPLOAD_PERIOD           (60)        //second        
#define BASE_UPLOAD_PERIOD          (50)        //second

#define SYNC_TIME_PERIOD            (30*60)         //second

#define MIN_TIMEZONE_OFFSET     0
#define MAX_TIMEZONE_OFFSET     23

#define TIMER_NUM               7       // you can setup at most TIMER_NUM timer

#define SEQ_SIZE                (16 * 5)
#define POWERUP_COLOR_MAX_NUM   16
#define MAX_CUSTOM_COLOR        16

#define OTA_PKG_SIZE            240
#define HB_SEND_MAX_COUNT       8

#define SSID_PASS_LEN           64
#define SSID_SSID_LEN           32

#define COLOR_MAX               1000
#define LIGHT_GRA_STEP          10

#define USE_WM_HTTP             0
#ifndef IS_STRIP_WS2812
#define USE_RTC_TIME            1
#endif

#define RESTART_REASON_RESET_NULL                   0
#define RESTART_REASON_RESET_DEV                    1
#define RESTART_REASON_REBOOT_REQ                   2

 

为了稳定的配网,此方案使用ap配网,通过连接开发板产生的热点,手机app把路由器wifi名称密码发送给开发板完成配网:

int wifi_setup_ap(void)
{    
    LOGI(TAG, "wifi_setup_ap");
    char def_ap_ssid[33] = {"BL602_AP"};
    memset(def_ap_ssid, 0x00, sizeof(def_ap_ssid));
    get_default_ssid(def_ap_ssid);

    wifi_interface_t wifi_interface_ap = wifi_mgmr_ap_enable();
    dhcpd_set_server_gw("192.168.4.1");
    wifi_mgmr_ap_start(wifi_interface_ap, def_ap_ssid, 0, NULL, 1);
    return 1;
}

 

连上服务器后开始发送心跳包:

static void vTimerCallback(TimerHandle_t timer)
{
    //服务器连接上了,开始发送心跳
    if (b_start_keep_alive) {
        char keep_msg[256];
        memset(keep_msg, 0x00, sizeof(keep_msg));
        sprintf(keep_msg, "cmd=keep&device_id=%s&device_key=%s\r\n", dev_id, dev_key);
        send(g_client_fd, keep_msg, strlen(keep_msg), 0);
        LOGI(TAG, "keepalive buff: %s", keep_msg);
    } else {
        // LOGI(TAG, "tcp client disconnect=====");
    }
}

处理下发的数据进行设备控制:

/*
    处理tcp服务下发的数据
 */
static void parse(int socket, char *buff, int len)
{
    // LOGI(TAG, "tcp_package: %s", buff);
    if (strstr(buff, "cmd=subscribe") != NULL) {
        char *pstart = strstr(buff, "res=");
        if (pstart != NULL) {
            pstart += strlen("res=");
            int r = *pstart - '0';
            if (r == 1) {
                LOGI(TAG, "register device on remote server OK");
            } else {
                LOGI(TAG, "register device failed!");
            }
        } else {
            LOGI(TAG, "result not found!");
        }
    } else if (strstr(buff, "cmd=publish") != NULL) {
        char *pstart = strstr(buff, "message=");
        if (pstart != NULL) {
            pstart += strlen("message=");
            char ret_buf[256];
            parse_dohome_protocol(pstart, len - (pstart - buff), ret_buf);
        } else {
            LOGI(TAG, "message not found!");
        }
    } else if (strstr(buff, "cmd=keep") != NULL) {
        LOGI(TAG, "send heartbeat OK");
    } else {
        LOGI(TAG, "no such command");
    }
}

将程序编译好后烧录到芯片中,手机下载Dohome APP,添加设备,按提示进行设备的配网

1.扫码下载APP

 

 2.进入APP点击右上角添加设备,选择彩色灯

 

 3.按APP提示进行配网

 

 

4.添加设备成功,能从Dohome  APP控制设备

 

 

配网成功后,可使用app控制,也可以绑定智能音箱控制。

 

posted @ 2020-12-14 09:54  深圳四博智联科技有限公司  阅读(1253)  评论(0编辑  收藏  举报