【esp32】esp-aliyun:smart_light解析

阿里飞燕平台
接上个文章,分析下代码:

配网

app_main()

{
    //Initialize NVS
    esp_err_t ret = nvs_flash_init();

    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }

    ESP_ERROR_CHECK(ret);

    ESP_LOGI(TAG, "IDF version: %s", esp_get_idf_version());
    ESP_LOGI(TAG, "esp-aliyun verison: %s", HAL_GetEAVerison());
    ESP_LOGI(TAG, "iotkit-embedded version: %s", HAL_GetIEVerison());
	//
    restore_factory_init();
	//led灯初始化
    led_light_start();
	//wifi初始化,sta模式
    initialise_wifi();

    wifi_config_t wifi_config;
	//读取wifi_config,如果无,创建,ret<0
    ret = esp_info_load(NVS_KEY_WIFI_CONFIG, &wifi_config, sizeof(wifi_config_t));
	//无,创建wifi_config,等待用户扫码二维码,发送wifi密码
    if(ret < 0) {
        // make sure user touches device belong to themselves
        awss_set_config_press(1);

        set_iotx_info();
        // awss callback
        iotx_event_regist_cb(linkkit_event_monitor);

        // awss entry
        awss_start();
    } else {
		//读取到wifi_config,连接wifi
        HAL_Awss_Connect_Ap(CONNECT_AP_TIMEOUT, (char*)(wifi_config.sta.ssid), (char*)(wifi_config.sta.password), 0, 0, NULL, 0);
    }
	//创建线程,产品功能实现
    xTaskCreate(smart_light_example, "smart_light_example", 10240, NULL, 5, NULL);
}

如果想要让设备之间连上wifi,省的每次都要扫描二维码,就要在esp_info_load上做文章;
有几个问题

  1. 把设备解绑,在用手机去连,有时候能在添加设备上显示扫描到本地端有什么设备,但是点击后连接失败
  2. 已连接wifi的并解绑的设备,app有时扫描不到

产品功能

linkkit_main

/* Register Callback */
	//与云端连接成功时
    IOT_RegisterCallback(ITE_CONNECT_SUCC, user_connected_event_handler);
	//与云端连接断开时
    IOT_RegisterCallback(ITE_DISCONNECTED, user_disconnected_event_handler);
	//Linkkit收到收到raw data数据时
    IOT_RegisterCallback(ITE_RAWDATA_ARRIVED, user_down_raw_data_arrived_event_handler);
	//Linkkit收到收到服务(同步/异步)调用请求时
    IOT_RegisterCallback(ITE_SERVICE_REQUST, user_service_request_event_handler);
	//Linkkit收到收到属性设置请求时
    IOT_RegisterCallback(ITE_PROPERTY_SET, user_property_set_event_handler);
	//Linkkit收到收到属性获取的请求时
    IOT_RegisterCallback(ITE_PROPERTY_GET, user_property_get_event_handler);
	//Linkkit收到收到上报消息的应答时
    IOT_RegisterCallback(ITE_REPORT_REPLY, user_report_reply_event_handler);
	//Linkkit收到收到事件上报消息的应答时
    IOT_RegisterCallback(ITE_TRIGGER_EVENT_REPLY, user_trigger_event_reply_event_handler);
	//当Linkkit收到收到查询时间戳请求的应答时
    IOT_RegisterCallback(ITE_TIMESTAMP_REPLY, user_timestamp_reply_event_handler);
	//设备初始化完成时
    IOT_RegisterCallback(ITE_INITIALIZE_COMPLETED, user_initialized);
	//Linkkit收到可用固件的通知时
    IOT_RegisterCallback(ITE_FOTA, user_fota_event_handler);
	//Linkkit收到可用远程配置文件的通知时
    IOT_RegisterCallback(ITE_COTA, user_cota_event_handler);

【esp32】esp-aliyun:smart_light解析_第1张图片
【esp32】esp-aliyun:smart_light解析_第2张图片
主要看下面这个

	//Linkkit收到收到属性设置请求时
    IOT_RegisterCallback(ITE_PROPERTY_SET, user_property_set_event_handler);

user_property_set_event_handler

static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
{
    int res = 0;
    user_example_ctx_t *user_example_ctx = user_example_get_ctx();
    EXAMPLE_TRACE("Property Set Received, Devid: %d, Request: %s", devid, request);

    user_parse_cloud_cmd(request, request_len);

    res = IOT_Linkkit_Report(user_example_ctx->master_devid, ITM_MSG_POST_PROPERTY,
                             (unsigned char *)request, request_len);
    EXAMPLE_TRACE("Post Property Message ID: %d", res);

    return 0;
}

user_parse_cloud_cmd

修改下面这部分就能实现我们的功能

static void user_parse_cloud_cmd(const char *request, const int request_len)
{
    if(request == NULL || request_len <= 0) {
        return;
    }

    char* ptr = NULL;
    // TODO: parse it as fixed format JSON string
    if(strstr(request, "LightSwitch") != NULL) {        // {"LightSwitch":0}
        ptr = strstr(request, ":");
        ptr++;
        bool on = atoi(ptr);
        if(on) {
            ESP_LOGI(TAG, "Set R:%d G:%d B:%d", s_rgb.r, s_rgb.g, s_rgb.b);
            lightbulb_set_aim(s_rgb.r * PWM_TARGET_DUTY / 100, s_rgb.g * PWM_TARGET_DUTY / 100, s_rgb.b * PWM_TARGET_DUTY / 100, 0, 0, 0);
        } else {
            ESP_LOGI(TAG, "Set Light OFF");
            lightbulb_set_aim(0, 0, 0, 0, 0, 0);
        }

    } else if (strstr(request, "RGBColor") != NULL) {   // {"RGBColor":{"Red":120,"Blue":36,"Green":108}}
        ptr = strstr(request, "Red");
        ptr += 5;
        s_rgb.r = atoi(ptr);

        ptr = strstr(request, "Blue");
        ptr += 6;
        s_rgb.g = atoi(ptr);
        
        ptr = strstr(request, "Green");
        ptr += 7;
        s_rgb.b = atoi(ptr);

        ESP_LOGI(TAG, "Set R:%d G:%d B:%d", s_rgb.r, s_rgb.g, s_rgb.b);
        lightbulb_set_aim(s_rgb.r * PWM_TARGET_DUTY / 100, s_rgb.g * PWM_TARGET_DUTY / 100, s_rgb.b * PWM_TARGET_DUTY / 100, 0, 0, 0);

    } else {
        ESP_LOGW(TAG, "unsupported JSON cmd");
    }
}

你可能感兴趣的:(esp32,ESP32)