下载本工程
git clone https://github.com/espressif/esp-aliyun.git
cd esp-aliyun
烧录三元组信息
文档烧录三元组 NVS 分区.如果执行了 make erase_flash, 需要重新烧录三元组.
配置 smart light example
RGB 灯分别接 ESP32/ESP8266 开发板上 GPIO0, GPIO2, GPIO4 (可在 lightbulb.c 中修改)
编译 smart light 并烧录运行
cd examples/solutions/smart_light
make chip=esp32 defconfig 或者 make chip=esp8266 defconfig
make -j8 flash monitor
公版APP实现配网
天猫精灵零配(将天猫精灵所连接的路由账号密码发送给设备)
使设备进入配网状态, 对天猫精灵说 “天猫精灵,发现设备”
天猫精灵回复 “正在为您扫描, 发现了智能灯, 现在连接吗”
对天猫精灵说 “连接” 或者 “是的”
天猫精灵回复 “好的, 设备连接中, 稍等一下下哦”
设备收到天猫精灵发送的管理帧配网信息, 进行联网:
天猫精灵和设备绑定
等待联网成功, 天猫精灵说 “智能设备联网成功, 现在用语音控制它试试”, 这时您可以通过天猫精灵音箱绑定了您就可以控制设备了.
如果您之前通过云智能 APP 配网, 天猫精灵配网成功后, 云智能 APP 将不再显示设备. 如果继续通过云智能 APP 配网, APP 会配网失败, 显示 “设备添加失败, 设备已被管理员绑定, 请联系管理员解绑或将设备分享给您”.
static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
{
int res = 0, value = 0;
cJSON *root = NULL, *LightSwitch = NULL, *LightColor = NULL;
ESP_LOGE(TAG, "Property Set Received, Devid: %d, Request: %s", devid, request);
if (!request)
{
return NULL_VALUE_ERROR;
}
/* 整体判断是否为json */
root = cJSON_Parse(request);
if (!root)
{
ESP_LOGI(TAG, "JSON Parse Error");
return FAIL_RETURN;
}
LightSwitch = cJSON_GetObjectItem(root, "powerstate");
//是否为开关指令
if (LightSwitch)
{
ESP_LOGE(TAG, "Property LightSwitch->valueint: %d", LightSwitch->valueint);
if (LightSwitch->valueint == 1)
{
rgb_set_pwm(0, 100, 0);
}
else
{
rgb_set_pwm(0, 0, 0);
}
}
LightColor = cJSON_GetObjectItem(root, "color");
if (LightColor)
{
ESP_LOGE(TAG, "Property LightColor->valueint: %d", LightColor->valueint);
value = LightColor->valueint;
switch (value)
{
case 16711680: //红色
rgb_set_pwm(100, 0, 0);
break;
case 255: //蓝色:
rgb_set_pwm(0, 0, 100);
break;
case 65280: //绿色
rgb_set_pwm(0, 100, 0);
break;
case 65535: //青色
rgb_set_pwm(0, 100, 100);
break;
case 8388736: //紫色
rgb_set_pwm(50, 0, 50);
break;
case 16753920: //橙色
rgb_set_pwm(100, 50, 0);
break;
case 16776960: //黄色
rgb_set_pwm(100, 100, 99);
break;
default:
break;
}
}
cJSON_Delete(root);
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY, (unsigned char *)request, request_len);
return SUCCESS_RETURN;
}
static bool linkkit_connected = false;
//短按函数
static void ButtonShortPressCallBack(void *arg)
{
char property_payload[50] = {0};
if (strcmp((char *)arg, "Button0") == 0)
{
ESP_LOGI(TAG, "button0");
ws2812_setColor(254, 0, 0);
if (linkkit_connected)
{
HAL_Snprintf(property_payload, sizeof(property_payload), "{\"color\":16711680,\"powerstate\":1}");
}
}
else if (strcmp((char *)arg, "Button1") == 0)
{
ESP_LOGI(TAG, "button1");
ws2812_setColor(0, 254, 0);
if (linkkit_connected)
{
HAL_Snprintf(property_payload, sizeof(property_payload), "{\"color\":65280,\"powerstate\":1}");
}
}
else if (strcmp((char *)arg, "Button2") == 0)
{
ESP_LOGI(TAG, "button2");
ws2812_setColor(0, 0, 254);
if (linkkit_connected)
{
HAL_Snprintf(property_payload, sizeof(property_payload), "{\"color\":255,\"powerstate\":1}");
}
}
IOT_Linkkit_Report(0, ITM_MSG_POST_PROPERTY, (unsigned char *)property_payload, strlen(property_payload));
//ESP_LOGI(TAG, "ButtonShortPressCallBack type %s %d esp_get_free_heap_size(): %d ", type, strlen(type), esp_get_free_heap_size());
}
/**
* @description: 按键驱动
* @param {type}
* @return:
*/
void TaskButton()
{
//定义一个 gpio 下降沿触发
button_handle_t btn_handle = button_dev_init(0, 0, BUTTON_ACTIVE_LOW);
button_dev_add_tap_cb(BUTTON_PUSH_CB, ButtonShortPressCallBack, "Button0", 50 / portTICK_PERIOD_MS, btn_handle);
//定义一个 gpio 下降沿触发
btn_handle = button_dev_init(4, 0, BUTTON_ACTIVE_LOW);
button_dev_add_tap_cb(BUTTON_PUSH_CB, ButtonShortPressCallBack, "Button1", 50 / portTICK_PERIOD_MS, btn_handle); //定义一个 gpio 下降沿触发
btn_handle = button_dev_init(5, 0, BUTTON_ACTIVE_LOW);
button_dev_add_tap_cb(BUTTON_PUSH_CB, ButtonShortPressCallBack, "Button2", 50 / portTICK_PERIOD_MS, btn_handle);
vTaskDelete(NULL);
}