1.新增esp-idf设备端sdk

This commit is contained in:
zhuangpeng.li
2024-08-05 15:46:10 +08:00
parent 642cf21648
commit 7983f72429
425 changed files with 113377 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
set(COMPONENT_ADD_INCLUDEDIRS .)
set(COMPONENT_SRCS "factory_restore.c")
set(COMPONENT_REQUIRES "esp-aliyun")
register_component()

View File

@@ -0,0 +1,15 @@
menu "[Aliyun]Factory restore"
config FACTORY_QUICK_REBOOT_TIMEOUT
int "Quick reboot timeout, unit second"
default 6
help
Timeout for exiting quick reboot timer, users should reboot device before this timeout.
config FACTORY_QUICK_REBOOT_MAX_TIMES
int "Quick reboot times max record"
default 5
help
If quick reboot times reach this max value, device will do factory restore.
endmenu

View File

@@ -0,0 +1,2 @@
COMPONENT_ADD_INCLUDEDIRS := ./
COMPONENT_SRCDIRS := ./

View File

@@ -0,0 +1,103 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <string.h>
#include <esp_log.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/timers.h"
#include "esp_sleep.h"
#include "dm_wrapper.h"
#include "conn_mgr.h"
#define FACTORY_QUICK_REBOOT_TIMEOUT (CONFIG_FACTORY_QUICK_REBOOT_TIMEOUT * 1000)
#define FACTORY_QUICK_REBOOT_MAX_TIMES CONFIG_FACTORY_QUICK_REBOOT_MAX_TIMES
#define FACTORY_QUICK_REBOOT_TIMES "q_rt"
#define AWSS_KV_RST "awss.rst"
static const char *TAG = "factory_rst";
static esp_err_t factory_restore_handle(void)
{
esp_err_t ret = ESP_OK;
int quick_reboot_times = 0;
/**< If the device restarts within the instruction time, the event_mdoe value will be incremented by one */
int length = sizeof(int);
ret = HAL_Kv_Get(FACTORY_QUICK_REBOOT_TIMES, &quick_reboot_times, &length);
quick_reboot_times++;
ret = HAL_Kv_Set(FACTORY_QUICK_REBOOT_TIMES, &quick_reboot_times, sizeof(int), 0);
if (quick_reboot_times >= FACTORY_QUICK_REBOOT_MAX_TIMES) {
char rst = 0x01;
/* since we cannot report reset status to cloud in this stage, just set the reset flag.
when connects to cloud, awss will do the reset report. */
ret = HAL_Kv_Set(AWSS_KV_RST, &rst, sizeof(rst), 0);
ret = HAL_Kv_Del(FACTORY_QUICK_REBOOT_TIMES);
ESP_LOGW(TAG, "factory restore");
conn_mgr_reset_wifi_config();
} else {
ESP_LOGI(TAG, "quick reboot times %d, don't need to restore", quick_reboot_times);
}
return ret;
}
static void factory_restore_timer_handler(void *timer)
{
if (!xTimerStop(timer, 0)) {
ESP_LOGE(TAG, "xTimerStop timer %p", timer);
}
if (!xTimerDelete(timer, 0)) {
ESP_LOGE(TAG, "xTimerDelete timer %p", timer);
}
/* erase reboot times record */
HAL_Kv_Del(FACTORY_QUICK_REBOOT_TIMES);
ESP_LOGI(TAG, "Quick reboot timeout, clear reboot times");
}
esp_err_t factory_restore_init(void)
{
#ifndef CONFIG_IDF_TARGET_ESP8266
if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_UNDEFINED) {
HAL_Kv_Del(FACTORY_QUICK_REBOOT_TIMES);
return ESP_OK;
}
#endif
TimerHandle_t timer = xTimerCreate("factory_clear", FACTORY_QUICK_REBOOT_TIMEOUT / portTICK_RATE_MS,
false, NULL, factory_restore_timer_handler);
xTimerStart(timer, portMAX_DELAY);
return factory_restore_handle();
}

View File

@@ -0,0 +1,50 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS products only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#pragma once
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Initialize factory restore
*
* @note For some devices which don't have a physical restore key or button, such as light, we use
* a quick reboot method to record the reboot times, if users quickly reboot the device within
* a specific time, configured by FACTORY_QUICK_REBOOT_TIMEOUT, and reboot several times,
* configured by FACTORY_QUICK_REBOOT_MAX_TIMES, the device will do a factory restore, clear
* stored ssid and password.
*
* @return
* - ESP_OK : OK
* - others : fail
*/
esp_err_t factory_restore_init(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,7 @@
set(COMPONENT_ADD_INCLUDEDIRS "./include")
set(COMPONENT_SRCS "fastbee.c" "Config.c" "Mqtt.c")
set(COMPONENT_REQUIRES "esp-aliyun" "mqtt" "json")
register_component()

View File

@@ -0,0 +1,174 @@
/*********************************************************************
* function 设备交互
* board: esp8266,esp32,esp32s2,esp32s3 core for esp-idf v4.4.7
* source: https://gitee.com/zhuangpengli/IOTDeviceSDK
* copyright: zhuangpeng.li all rights reserved.
********************************************************************/
#include "Config.h"
#include "esp_err.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "nvs.h"
#include "dm_wrapper.h"
// 设备信息配置
char g_userId[USER_ID_LEN + 1] = {0};
char g_productId[PRODUCT_ID_LEN + 1] = {0};
char g_deviceSN[DEVICE_SN_LEN + 1] = {0};
// Mqtt配置
char g_mqtt_url[MQTT_URL_LEN + 1] = {0};
char g_mqtt_username[MQTT_USERNAME_LEN + 1] = {0};
char g_mqtt_password[MQTT_PASSWORD_LEN + 1] = {0};
char g_mqtt_clientid[MQTT_CLIENTID_LEN + 1] = {0};
char g_mqtt_prefix[MQTT_PREFIX_LEN + 1] = {0};
float rssi = 0;
char wumei_iv[17] = "wumei-smart-open";
int monitorCount = 0;
long monitorInterval = 1000;
bool isApMode = false;
/********************************** begin 可配置的项 **********************************/
// wifi信息
char *wifiSsid = "";
char *wifiPwd = "";
// 产品启用授权码,则授权码不能为空
char *authCode = "";
// 设备信息配置
float firmwareVersion = 1.0;
// 经度和纬度可选,如果产品使用设备定位,则必须传
float latitude = 0;
float longitude = 0;
// Mqtt配置
char mqttSecret[17] = "KV52PPZ813EFCQD8";
// NTP地址用于获取时间,修改为自己部署项目的接口地址)
char *ntpServer = "http://fastbee.cn:8080/iot/tool/ntp?deviceSendTime=";
/********************************** end 可配置的项 **********************************/
static const char *TAG = "FASTBEE_CONFIG";
static bool s_part_init_flag;
static esp_err_t HAL_ProductParam_init(void)
{
esp_err_t ret = ESP_OK;
do {
if (s_part_init_flag == false) {
if ((ret = nvs_flash_init_partition(MFG_FASTBEE_PARTITION_NAME)) != ESP_OK) {
ESP_LOGE(TAG, "NVS Flash init %s failed, Please check that you have flashed fctry partition!!!", MFG_FASTBEE_PARTITION_NAME);
break;
}
s_part_init_flag = true;
}
} while (0);
return ret;
}
static int HAL_GetProductParam(char *param_name, const char *param_name_str)
{
esp_err_t ret;
size_t read_len = 0;
nvs_handle handle;
do {
if (HAL_ProductParam_init() != ESP_OK) {
break;
}
if (param_name == NULL) {
ESP_LOGE(TAG, "%s param %s NULL", __func__, param_name);
break;
}
ret = nvs_open_from_partition(MFG_FASTBEE_PARTITION_NAME, NVS_FASTBEE_PRODUCT, NVS_READONLY, &handle);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "%s nvs_open failed with %x", __func__, ret);
break;
}
ret = nvs_get_str(handle, param_name_str, NULL, (size_t *)&read_len);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "%s nvs_get_str get %s failed with %x", __func__, param_name_str, ret);
break;
}
ret = nvs_get_str(handle, param_name_str, param_name, (size_t *)&read_len);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "%s nvs_get_str get %s failed with %x", __func__, param_name_str, ret);
} else {
ESP_LOGV(TAG, "%s %s %s", __func__, param_name_str, param_name);
}
nvs_close(handle);
} while (0);
return read_len;
}
int HAL_GetMqttUrl(char mqtt_url[MQTT_URL_LEN + 1])
{
return HAL_GetProductParam(mqtt_url, "MQTTUrl");
}
int HAL_GetMqttUsername(char mqtt_username[MQTT_USERNAME_LEN + 1])
{
return HAL_GetProductParam(mqtt_username, "MQTTUsername");
}
int HAL_GetMqttPassword(char mqtt_password[MQTT_PASSWORD_LEN + 1])
{
return HAL_GetProductParam(mqtt_password, "MQTTPassword");
}
int HAL_GetMqttClientId(char mqtt_clientid[MQTT_CLIENTID_LEN + 1])
{
return HAL_GetProductParam(mqtt_clientid, "MQTTClientId");
}
int HAL_GetUserId(char userid[USER_ID_LEN + 1])
{
return HAL_GetProductParam(userid, "UserId");
}
int HAL_GetDeviceSN(char deviceSN[DEVICE_SN_LEN + 1])
{
return HAL_GetProductParam(deviceSN, "DeviceSN");
}
int HAL_GetProductId(char productId[PRODUCT_ID_LEN + 1])
{
return HAL_GetProductParam(productId, "ProductId");
}
void loadConfig() {
HAL_GetMqttUrl(g_mqtt_url);
if (strlen(g_mqtt_url) == 0) {
//加载默认配置
memcpy(g_mqtt_url, CONFIG_ESP_MQTT_PLATFORM_FASTBEE_URL, strlen(CONFIG_ESP_MQTT_PLATFORM_FASTBEE_URL));
}
HAL_GetMqttUsername(g_mqtt_username);
if (strlen(g_mqtt_username) == 0) {
memcpy(g_mqtt_username, CONFIG_ESP_MQTT_PLATFORM_FASTBEE_USERNAME, strlen(CONFIG_ESP_MQTT_PLATFORM_FASTBEE_USERNAME));
}
HAL_GetMqttPassword(g_mqtt_password);
if (strlen(g_mqtt_password) == 0) {
memcpy(g_mqtt_password, CONFIG_ESP_MQTT_PLATFORM_FASTBEE_PASSWORD, strlen(CONFIG_ESP_MQTT_PLATFORM_FASTBEE_PASSWORD));
}
HAL_GetMqttClientId(g_mqtt_clientid);
if (strlen(g_mqtt_clientid) == 0) {
HAL_Snprintf(g_mqtt_clientid, sizeof(g_mqtt_clientid), "S&%s&%s&%s", CONFIG_ESP_MQTT_PLATFORM_FASTBEE_DEVICESN,CONFIG_ESP_MQTT_PLATFORM_FASTBEE_PRODICTID,CONFIG_ESP_MQTT_PLATFORM_FASTBEE_USERID);
}
HAL_GetUserId(g_userId);
if (strlen(g_userId) == 0) {
memcpy(g_userId, CONFIG_ESP_MQTT_PLATFORM_FASTBEE_USERID, strlen(CONFIG_ESP_MQTT_PLATFORM_FASTBEE_USERID));
}
HAL_GetDeviceSN(g_productId);
if (strlen(g_productId) == 0) {
memcpy(g_productId, CONFIG_ESP_MQTT_PLATFORM_FASTBEE_PRODICTID, strlen(CONFIG_ESP_MQTT_PLATFORM_FASTBEE_PRODICTID));
}
HAL_GetProductId(g_deviceSN);
if (strlen(g_deviceSN) == 0) {
memcpy(g_deviceSN, CONFIG_ESP_MQTT_PLATFORM_FASTBEE_DEVICESN, strlen(CONFIG_ESP_MQTT_PLATFORM_FASTBEE_DEVICESN));
}
HAL_Snprintf(g_mqtt_prefix, sizeof(g_mqtt_prefix), "/%s/%s", g_productId,g_deviceSN);
}

View File

@@ -0,0 +1,158 @@
/*********************************************************************
* function 设备交互
* board: esp8266,esp32,esp32s2,esp32s3 core for esp-idf v4.4.7
* source: https://gitee.com/zhuangpengli/IOTDeviceSDK
* copyright: zhuangpeng.li all rights reserved.
********************************************************************/
#include "Mqtt.h"
#include "Config.h"
#include "cJSON.h"
#include "esp_log.h"
#include "dm_wrapper.h"
char *sInfoTopic = "/info/get";
char *sOtaTopic = "/ota/get";
char *sNtpTopic = "/ntp/get";
char *sPropertyTopic = "/property/get";
char *sFunctionTopic = "/function/get";
char *sPropertyOnline = "/property-online/get";
char *sFunctionOnline = "/function-online/get";
char *sMonitorTopic = "/monitor/get";
// Mqtt发布的主题
char *pInfoTopic = "/info/post";
char *pNtpTopic = "/ntp/post";
char *pPropertyTopic = "/property/post";
char *pFunctionTopic = "/function/post";
char *pMonitorTopic = "/monitor/post";
char *pEventTopic = "/event/post";
static const char *TAG = "FASTBEE_MQTT";
// 订阅系统主题
void subscribeTopic(esp_mqtt_client_handle_t client) {
char topic[128] = {0};
int msg_id;
HAL_Snprintf(topic, sizeof(topic), "%s%s", g_mqtt_prefix, sInfoTopic);
msg_id = esp_mqtt_client_subscribe(client, topic, 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
memset(topic, 0, sizeof(topic));
HAL_Snprintf(topic, sizeof(topic), "%s%s", g_mqtt_prefix, sOtaTopic);
msg_id = esp_mqtt_client_subscribe(client, topic, 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
memset(topic, 0, sizeof(topic));
HAL_Snprintf(topic, sizeof(topic), "%s%s", g_mqtt_prefix, sNtpTopic);
msg_id = esp_mqtt_client_subscribe(client, topic, 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
memset(topic, 0, sizeof(topic));
HAL_Snprintf(topic, sizeof(topic), "%s%s", g_mqtt_prefix, sPropertyTopic);
msg_id = esp_mqtt_client_subscribe(client, topic, 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
memset(topic, 0, sizeof(topic));
HAL_Snprintf(topic, sizeof(topic), "%s%s", g_mqtt_prefix, sFunctionTopic);
msg_id = esp_mqtt_client_subscribe(client, topic, 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
memset(topic, 0, sizeof(topic));
HAL_Snprintf(topic, sizeof(topic), "%s%s", g_mqtt_prefix, sPropertyOnline);
msg_id = esp_mqtt_client_subscribe(client, topic, 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
memset(topic, 0, sizeof(topic));
HAL_Snprintf(topic, sizeof(topic), "%s%s", g_mqtt_prefix, sFunctionOnline);
msg_id = esp_mqtt_client_subscribe(client, topic, 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
memset(topic, 0, sizeof(topic));
HAL_Snprintf(topic, sizeof(topic), "%s%s", g_mqtt_prefix, sMonitorTopic);
msg_id = esp_mqtt_client_subscribe(client, topic, 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
}
// 1.发布设备信息
void publishInfo(esp_mqtt_client_handle_t client) {
char topic[128] = {0};
int msg_id;
cJSON *json = cJSON_CreateObject();
HAL_Snprintf(topic, sizeof(topic), "%s%s", g_mqtt_prefix, pInfoTopic);
// 向JSON对象中添加数据
cJSON_AddNumberToObject(json, "rssi", 3);
cJSON_AddNumberToObject(json, "firmwareVersion", firmwareVersion);
cJSON_AddNumberToObject(json, "status", 3);
cJSON_AddStringToObject(json, "userId", g_userId);
cJSON_AddNumberToObject(json, "longitude", longitude);
cJSON_AddNumberToObject(json, "latitude", latitude);
cJSON *summary = cJSON_CreateObject();
cJSON_AddStringToObject(summary, "name", "fastbee");
cJSON_AddStringToObject(summary, "chip", "esp32");
cJSON_AddStringToObject(summary, "author", "zhuangpeng.li");
cJSON_AddNumberToObject(summary, "version", 1.6);
cJSON_AddStringToObject(summary, "create", "2024-07-07");
cJSON_AddItemToObject(json, "summary", summary);
char *json_string = cJSON_Print(json);
msg_id = esp_mqtt_client_publish(client, topic, json_string, 0, 0, 0);
ESP_LOGI(TAG, "sent publish %s, msg_id= %d, msg: %s", topic, msg_id, json_string);
cJSON_Delete(json);
free(json_string);
}
// 2.发布时钟同步信,用于获取当前时间(可选)
void publishNtp(esp_mqtt_client_handle_t client) {
char topic[128] = {0};
int msg_id;
cJSON *json = cJSON_CreateObject();
HAL_Snprintf(topic, sizeof(topic), "%s%s", g_mqtt_prefix, pNtpTopic);
cJSON_AddStringToObject(json, "deviceSendTime", "test");
char *json_string = cJSON_Print(json);
msg_id = esp_mqtt_client_publish(client, topic, json_string, 0, 0, 0);
ESP_LOGI(TAG, "sent publish %s, msg_id= %d, msg: %s", topic, msg_id, json_string);
cJSON_Delete(json);
free(json_string);
}
// 3.发布属性
void publishProperty(esp_mqtt_client_handle_t client, const char *msg) {
char topic[128] = {0};
int msg_id;
HAL_Snprintf(topic, sizeof(topic), "%s%s", g_mqtt_prefix, pPropertyTopic);
msg_id = esp_mqtt_client_publish(client, topic, msg, 0, 0, 0);
ESP_LOGI(TAG, "发布属性主题: %s", topic);
ESP_LOGI(TAG, "消息id= %d, 消息: %s", msg_id, msg);
}
// 4.发布功能
void publishFunction(esp_mqtt_client_handle_t client, const char *msg) {
char topic[128] = {0};
int msg_id;
HAL_Snprintf(topic, sizeof(topic), "%s%s", g_mqtt_prefix, pFunctionTopic);
msg_id = esp_mqtt_client_publish(client, topic, msg, 0, 0, 0);
ESP_LOGI(TAG, "发布功能主题: %s", topic);
ESP_LOGI(TAG, "消息id= %d, 消息: %s", msg_id, msg);
}
// 5.发布事件
void publishEvent(esp_mqtt_client_handle_t client, const char *msg) {
char topic[128] = {0};
int msg_id;
HAL_Snprintf(topic, sizeof(topic), "%s%s", g_mqtt_prefix, pEventTopic);
msg_id = esp_mqtt_client_publish(client, topic, msg, 0, 0, 0);
ESP_LOGI(TAG, "发布事件主题: %s", topic);
ESP_LOGI(TAG, "消息id= %d, 消息: %s", msg_id, msg);
}
// 6.发布实时监测数据
void publishMonitor(esp_mqtt_client_handle_t client, const char *msg) {
char topic[128] = {0};
int msg_id;
HAL_Snprintf(topic, sizeof(topic), "%s%s", g_mqtt_prefix, pMonitorTopic);
// 发布实时监测数据(不会存储,需要实时存储则发布为属性)
msg_id = esp_mqtt_client_publish(client, topic, msg, 0, 0, 0);
ESP_LOGI(TAG, "发布实时监测主题: %s", topic);
ESP_LOGI(TAG, "消息id= %d, 消息: %s", msg_id, msg);
}

View File

@@ -0,0 +1,2 @@
COMPONENT_ADD_INCLUDEDIRS := ./include
COMPONENT_SRCDIRS := ./

View File

@@ -0,0 +1,8 @@
fastbee-key,namespace,
MQTTUrl,data,string
MQTTUsername,data,string
MQTTPassword,data,string
MQTTClientId,data,string
UserId,data,string
DeviceSN,data,string
ProductId,data,string
1 fastbee-key namespace
2 MQTTUrl data string
3 MQTTUsername data string
4 MQTTPassword data string
5 MQTTClientId data string
6 UserId data string
7 DeviceSN data string
8 ProductId data string

View File

@@ -0,0 +1,4 @@
id,MQTTUrl,MQTTUsername,MQTTPassword,MQTTClientId,UserId,DeviceSN,ProductId
1,fastbee.cn:1883,FastBee,P63653937TRQ8F27,S&DCDA0C11FEBC&588&1,1,DCDA0C11FEBC,588
2,fastbee.cn:1883,FastBee,P63653937TRQ8F27,S&DCDA0C11FEBC&588&1,1,DCDA0C11FEBC,588
3,fastbee.cn:1883,FastBee,P63653937TRQ8F27,S&DCDA0C11FEBC&588&1,1,DCDA0C11FEBC,588
1 id MQTTUrl MQTTUsername MQTTPassword MQTTClientId UserId DeviceSN ProductId
2 1 fastbee.cn:1883 FastBee P63653937TRQ8F27 S&DCDA0C11FEBC&588&1 1 DCDA0C11FEBC 588
3 2 fastbee.cn:1883 FastBee P63653937TRQ8F27 S&DCDA0C11FEBC&588&1 1 DCDA0C11FEBC 588
4 3 fastbee.cn:1883 FastBee P63653937TRQ8F27 S&DCDA0C11FEBC&588&1 1 DCDA0C11FEBC 588

View File

@@ -0,0 +1,9 @@
key,type,encoding,value
fastbee-key,namespace,,
MQTTUrl,data,string,fastbee.cn:1883
MQTTUsername,data,string,FastBee
MQTTPassword,data,string,P63653937TRQ8F27
MQTTClientId,data,string,S&DCDA0C11FEBC&588&1
UserId,data,string,1
DeviceSN,data,string,DCDA0C11FEBC
ProductId,data,string,588
1 key type encoding value
2 fastbee-key namespace
3 MQTTUrl data string fastbee.cn:1883
4 MQTTUsername data string FastBee
5 MQTTPassword data string P63653937TRQ8F27
6 MQTTClientId data string S&DCDA0C11FEBC&588&1
7 UserId data string 1
8 DeviceSN data string DCDA0C11FEBC
9 ProductId data string 588

View File

@@ -0,0 +1,98 @@
/*********************************************************************
* function 设备交互
* board: esp8266,esp32,esp32s2,esp32s3 core for esp-idf v4.4.7
* source: https://gitee.com/zhuangpengli/IOTDeviceSDK
* copyright: zhuangpeng.li all rights reserved.
********************************************************************/
#include "fastbee.h"
#include "config.h"
#include "mqtt.h"
#include "esp_log.h"
#include "dm_wrapper.h"
static const char *TAG = "FASTBEE";
static void log_error_if_nonzero(const char *message, int error_code)
{
if (error_code != 0) {
ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code);
}
}
/*
* @brief Event handler registered to receive MQTT events
*
* This function is called by the MQTT client event loop.
*
* @param handler_args user data registered to the event.
* @param base Event base for the handler(always MQTT Base in this example).
* @param event_id The id for the received event.
* @param event_data The data for the event, esp_mqtt_event_handle_t.
*/
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
esp_mqtt_event_handle_t event = event_data;
esp_mqtt_client_handle_t client = event->client;
int msg_id;
switch ((esp_mqtt_event_id_t)event_id) {
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
subscribeTopic(client);
publishInfo(client);
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
break;
case MQTT_EVENT_SUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0);
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
break;
case MQTT_EVENT_UNSUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_PUBLISHED:
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_DATA:
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
printf("DATA=%.*s\r\n", event->data_len, event->data);
break;
case MQTT_EVENT_ERROR:
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err);
log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno);
ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
}
break;
default:
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
break;
}
}
static int fastbee_thread(void *paras){
loadConfig();
esp_mqtt_client_config_t mqtt_cfg = {
.uri = g_mqtt_url,
.client_id = g_mqtt_clientid,
.username = g_mqtt_username,
.password = g_mqtt_password,
};
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
/* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
esp_mqtt_client_start(client);
return 0;
}
void fastbee_main(void *paras)
{
while (1) {
fastbee_thread(NULL);
}
}

View File

@@ -0,0 +1,58 @@
/*********************************************************************
* function 设备交互
* board: esp8266,esp32,esp32s2,esp32s3 core for esp-idf v4.4.7
* source: https://gitee.com/zhuangpengli/IOTDeviceSDK
* copyright: zhuangpeng.li all rights reserved.
********************************************************************/
#ifndef _CONFIG_H
#define _CONFIG_H
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define MQTT_URL_LEN (64)
#define MQTT_USERNAME_LEN (64)
#define MQTT_PASSWORD_LEN (64)
#define MQTT_CLIENTID_LEN (64)
#define USER_ID_LEN (16)
#define DEVICE_SN_LEN (64)
#define PRODUCT_ID_LEN (16)
#define MQTT_PREFIX_LEN (128)
#define MFG_FASTBEE_PARTITION_NAME "fctry"
#define NVS_FASTBEE_PRODUCT "fastbee-key"
extern char g_userId[USER_ID_LEN + 1]; // 用户ID
extern char g_productId[PRODUCT_ID_LEN + 1]; // 产品ID
extern char g_deviceSN[DEVICE_SN_LEN + 1]; // 设备编号重要同时是Mqtt的clientId
// Mqtt配置
extern char g_mqtt_url[MQTT_URL_LEN + 1];
extern char g_mqtt_username[MQTT_USERNAME_LEN + 1];
extern char g_mqtt_password[MQTT_PASSWORD_LEN + 1];
extern char g_mqtt_clientid[MQTT_CLIENTID_LEN + 1];
extern char g_mqtt_prefix[MQTT_PREFIX_LEN + 1];
extern float rssi; // 信号强度信号极好4格[-55— 0]信号好3格[-70— -55]信号一般2格[-85— -70]信号差1格[-100— -85]
extern char wumei_iv[17]; // AES加密偏移量固定值16位
extern int monitorCount; // 发布监测数据的最大次数
extern long monitorInterval; // 发布监测数据的间隔默认1000毫秒
extern bool isApMode; // 是否进入AP配网模式
/********************************** begin 可配置的项 **********************************/
extern char *wifiSsid; // WIFI的SSID
extern char *wifiPwd; // WIFI的密码
extern char *authCode; // 产品授权码,产品未启用时为空字符串
extern float firmwareVersion; // 固件版本
extern float latitude; // 设备精度
extern float longitude; // 设备维度
extern char mqttSecret[17]; // Mqtt秘钥,16位
extern char *ntpServer; // NTP服务地址用于获取当前时间
/********************************** end 可配置的项 **********************************/
// 加载配置
void loadConfig();
#endif

View File

@@ -0,0 +1,43 @@
/*********************************************************************
* function 设备交互
* board: esp8266,esp32,esp32s2,esp32s3 core for esp-idf v4.4.7
* source: https://gitee.com/zhuangpengli/IOTDeviceSDK
* copyright: zhuangpeng.li all rights reserved.
********************************************************************/
#ifndef _FASTBEE_MQTT_H
#define _FASTBEE_MQTT_H
#include "mqtt_client.h"
// 订阅的主题
extern char *sInfoTopic; // 订阅设备信息
extern char *sOtaTopic; // 订阅OTA升级
extern char *sNtpTopic; // 订阅NTP时间
extern char *sPropertyTopic; // 订阅属性
extern char *sFunctionTopic; // 订阅功能
extern char *sPropertyOnline; // 订阅属性-在线模式
extern char *sFunctionOnline; // 订阅功能-在线模式
extern char *sMonitorTopic; // 订阅实时监测
// 发布的主题
extern char *pInfoTopic; // 发布设备信息
extern char *pNtpTopic; // 发布NTP时间
extern char *pPropertyTopic; // 发布属性
extern char *pFunctionTopic; // 发布功能
extern char *pMonitorTopic; // 发布实时监测数据
extern char *pEventTopic; // 发布事件
// 订阅系统主题
void subscribeTopic(esp_mqtt_client_handle_t client);
// 发布设备信息
void publishInfo(esp_mqtt_client_handle_t client);
// 发布时钟同步信息
void publishNtp(esp_mqtt_client_handle_t client);
// 发布事件
void publishEvent(esp_mqtt_client_handle_t client, const char *msg);
// 发布实时监测数据(不会存储,需要实时存储则发布属性)
void publishMonitor(esp_mqtt_client_handle_t client, const char *msg);
// 发布属性
void publishProperty(esp_mqtt_client_handle_t client, const char *msg);
// 发布功能
void publishFunction(esp_mqtt_client_handle_t client, const char *msg);
#endif

View File

@@ -0,0 +1,12 @@
/*********************************************************************
* function 设备交互
* board: esp8266,esp32,esp32s2,esp32s3 core for esp-idf v4.4.7
* source: https://gitee.com/zhuangpengli/IOTDeviceSDK
* copyright: zhuangpeng.li all rights reserved.
********************************************************************/
#ifndef _FASTBEE_H
#define _FASTBEE_H
void fastbee_main(void *paras);
#endif

View File

@@ -0,0 +1,7 @@
set(COMPONENT_ADD_INCLUDEDIRS .)
set(COMPONENT_SRCS "lightbulb.c")
set(COMPONENT_REQUIRES)
register_component()

View File

@@ -0,0 +1,2 @@
COMPONENT_ADD_INCLUDEDIRS := ./
COMPONENT_SRCDIRS := ./

View File

@@ -0,0 +1,327 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <stdio.h>
#include <sdkconfig.h>
#ifdef CONFIG_IDF_TARGET_ESP8266
#include "driver/pwm.h"
#else
#include "driver/ledc.h"
#endif
#include "esp_log.h"
typedef struct rgb {
uint8_t r; // 0-100 %
uint8_t g; // 0-100 %
uint8_t b; // 0-100 %
} rgb_t;
typedef struct hsp {
uint16_t h; // 0-360
uint16_t s; // 0-100
uint16_t b; // 0-100
} hsp_t;
/* LED numbers below are for ESP-WROVER-KIT */
/* Red LED */
#define LEDC_IO_0 (0)
/* Green LED */
#define LEDC_IO_1 (2)
/* Blued LED */
#define LEDC_IO_2 (4)
#define PWM_DEPTH (1023)
#define PWM_TARGET_DUTY 8192
static hsp_t s_hsb_val;
static uint16_t s_brightness;
static bool s_on = false;
static const char *TAG = "lightbulb";
#ifdef CONFIG_IDF_TARGET_ESP8266
#define PWM_PERIOD (500)
#define PWM_IO_NUM 3
// pwm pin number
const uint32_t pin_num[PWM_IO_NUM] = {
LEDC_IO_0,
LEDC_IO_1,
LEDC_IO_2
};
// dutys table, (duty/PERIOD)*depth
uint32_t duties[PWM_IO_NUM] = {
250, 250, 250,
};
// phase table, (phase/180)*depth
int16_t phase[PWM_IO_NUM] = {
0, 0, 50,
};
#define LEDC_CHANNEL_0 0
#define LEDC_CHANNEL_1 1
#define LEDC_CHANNEL_2 2
#endif
/**
* @brief transform lightbulb's "RGB" and other parameter
*/
static void lightbulb_set_aim(uint32_t r, uint32_t g, uint32_t b, uint32_t cw, uint32_t ww, uint32_t period)
{
#ifdef CONFIG_IDF_TARGET_ESP8266
pwm_stop(0x3);
pwm_set_duty(LEDC_CHANNEL_0, r);
pwm_set_duty(LEDC_CHANNEL_1, g);
pwm_set_duty(LEDC_CHANNEL_2, b);
pwm_start();
#else
ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, r);
ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, g);
ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, b);
ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1);
ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2);
#endif
}
/**
* @brief transform lightbulb's "HSV" to "RGB"
*/
static bool lightbulb_set_hsb2rgb(uint16_t h, uint16_t s, uint16_t v, rgb_t *rgb)
{
bool res = true;
uint16_t hi, F, P, Q, T;
if (!rgb)
return false;
if (h > 360) return false;
if (s > 100) return false;
if (v > 100) return false;
hi = (h / 60) % 6;
F = 100 * h / 60 - 100 * hi;
P = v * (100 - s) / 100;
Q = v * (10000 - F * s) / 10000;
T = v * (10000 - s * (100 - F)) / 10000;
switch (hi) {
case 0:
rgb->r = v;
rgb->g = T;
rgb->b = P;
break;
case 1:
rgb->r = Q;
rgb->g = v;
rgb->b = P;
break;
case 2:
rgb->r = P;
rgb->g = v;
rgb->b = T;
break;
case 3:
rgb->r = P;
rgb->g = Q;
rgb->b = v;
break;
case 4:
rgb->r = T;
rgb->g = P;
rgb->b = v;
break;
case 5:
rgb->r = v;
rgb->g = P;
rgb->b = Q;
break;
default:
return false;
}
return res;
}
/**
* @brief set the lightbulb's "HSV"
*/
static bool lightbulb_set_aim_hsv(uint16_t h, uint16_t s, uint16_t v)
{
rgb_t rgb_tmp;
bool ret = lightbulb_set_hsb2rgb(h, s, v, &rgb_tmp);
if (ret == false)
return false;
lightbulb_set_aim(rgb_tmp.r * PWM_TARGET_DUTY / 100, rgb_tmp.g * PWM_TARGET_DUTY / 100,
rgb_tmp.b * PWM_TARGET_DUTY / 100, (100 - s) * 5000 / 100, v * 2000 / 100, 1000);
return true;
}
/**
* @brief update the lightbulb's state
*/
static void lightbulb_update()
{
lightbulb_set_aim_hsv(s_hsb_val.h, s_hsb_val.s, s_hsb_val.b);
}
/**
* @brief initialize the lightbulb lowlevel module
*/
void lightbulb_init(void)
{
#ifdef CONFIG_IDF_TARGET_ESP8266
pwm_init(PWM_PERIOD, duties, PWM_IO_NUM, pin_num);
pwm_set_channel_invert(0x1 << 0);
pwm_set_phases(phase);
pwm_start();
#else
// enable ledc module
periph_module_enable(PERIPH_LEDC_MODULE);
// config the timer
ledc_timer_config_t ledc_timer = {
//set timer counter bit number
.bit_num = LEDC_TIMER_13_BIT,
//set frequency of pwm
.freq_hz = 5000,
//timer mode,
.speed_mode = LEDC_LOW_SPEED_MODE,
//timer index
.timer_num = LEDC_TIMER_0
};
ledc_timer_config(&ledc_timer);
//config the channel
ledc_channel_config_t ledc_channel = {
//set LEDC channel 0
.channel = LEDC_CHANNEL_0,
//set the duty for initialization.(duty range is 0 ~ ((2**bit_num)-1)
.duty = 100,
//GPIO number
.gpio_num = LEDC_IO_0,
//GPIO INTR TYPE, as an example, we enable fade_end interrupt here.
.intr_type = LEDC_INTR_FADE_END,
//set LEDC mode, from ledc_mode_t
.speed_mode = LEDC_LOW_SPEED_MODE,
//set LEDC timer source, if different channel use one timer,
//the frequency and bit_num of these channels should be the same
.timer_sel = LEDC_TIMER_0
};
//set the configuration
ledc_channel_config(&ledc_channel);
//config ledc channel1
ledc_channel.channel = LEDC_CHANNEL_1;
ledc_channel.gpio_num = LEDC_IO_1;
ledc_channel_config(&ledc_channel);
//config ledc channel2
ledc_channel.channel = LEDC_CHANNEL_2;
ledc_channel.gpio_num = LEDC_IO_2;
ledc_channel_config(&ledc_channel);
#endif
}
/**
* @brief deinitialize the lightbulb's lowlevel module
*/
void lightbulb_deinit(void)
{
#ifdef CONFIG_IDF_TARGET_ESP8266
#else
ledc_stop(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0);
ledc_stop(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0);
ledc_stop(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0);
#endif
}
/**
* @brief turn on/off the lowlevel lightbulb
*/
int lightbulb_set_on(bool value)
{
ESP_LOGI(TAG, "lightbulb_set_on : %s", value == true ? "true" : "false");
if (value == true) {
s_hsb_val.b = s_brightness;
s_on = true;
} else {
s_brightness = s_hsb_val.b;
s_hsb_val.b = 0;
s_on = false;
}
lightbulb_update();
return 0;
}
/**
* @brief set the saturation of the lowlevel lightbulb
*/
int lightbulb_set_saturation(float value)
{
ESP_LOGI(TAG, "lightbulb_set_saturation : %f", value);
s_hsb_val.s = value;
if (true == s_on)
lightbulb_update();
return 0;
}
/**
* @brief set the hue of the lowlevel lightbulb
*/
int lightbulb_set_hue(float value)
{
ESP_LOGI(TAG, "lightbulb_set_hue : %f", value);
s_hsb_val.h = value;
if (true == s_on)
lightbulb_update();
return 0;
}
/**
* @brief set the brightness of the lowlevel lightbulb
*/
int lightbulb_set_brightness(int value)
{
ESP_LOGI(TAG, "lightbulb_set_brightness : %d", value);
s_hsb_val.b = value;
s_brightness = s_hsb_val.b;
if (true == s_on)
lightbulb_update();
return 0;
}

View File

@@ -0,0 +1,89 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef _LIGHTBULB_H_
#define _LIGHTBULB_H_
#include <stdbool.h>
/**
* @brief initialize the lightbulb lowlevel module
*
* @param none
*
* @return none
*/
void lightbulb_init(void);
/**
* @brief deinitialize the lightbulb's lowlevel module
*
* @param none
*
* @return none
*/
void lightbulb_deinit(void);
/**
* @brief turn on/off the lowlevel lightbulb
*
* @param value The "On" value
*
* @return none
*/
int lightbulb_set_on(bool value);
/**
* @brief set the saturation of the lowlevel lightbulb
*
* @param value The Saturation value
*
* @return
* - 0 : OK
* - others : fail
*/
int lightbulb_set_saturation(float value);
/**
* @brief set the hue of the lowlevel lightbulb
*
* @param value The Hue value
*
* @return
* - 0 : OK
* - others : fail
*/
int lightbulb_set_hue(float value);
/**
* @brief set the brightness of the lowlevel lightbulb
*
* @param value The Brightness value
*
* @return
* - 0 : OK
* - others : fail
*/
int lightbulb_set_brightness(int value);
#endif /* _LIGHTBULB_H_ */

View File

@@ -0,0 +1,7 @@
set(COMPONENT_ADD_INCLUDEDIRS .)
set(COMPONENT_SRCS "linkkit_solo.c")
set(COMPONENT_REQUIRES "esp-aliyun" "json" "lightbulb")
register_component()

View File

@@ -0,0 +1,2 @@
COMPONENT_ADD_INCLUDEDIRS := ./
COMPONENT_SRCDIRS := ./

View File

@@ -0,0 +1,468 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "infra_config.h"
void HAL_Printf(const char *fmt, ...);
int HAL_Snprintf(char *str, const int len, const char *fmt, ...);
#ifdef DEPRECATED_LINKKIT
#include "solo.c"
#else
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "infra_types.h"
#include "infra_defs.h"
#include "infra_compat.h"
#include "infra_compat.h"
#ifdef INFRA_MEM_STATS
#include "infra_mem_stats.h"
#endif
#include "dev_model_api.h"
#include "dm_wrapper.h"
#include "cJSON.h"
#ifdef ATM_ENABLED
#include "at_api.h"
#endif
#include "lightbulb.h"
#include "esp_log.h"
static const char* TAG = "linkkit_example_solo";
#define EXAMPLE_TRACE(...) \
do { \
HAL_Printf("\033[1;32;40m%s.%d: ", __func__, __LINE__); \
HAL_Printf(__VA_ARGS__); \
HAL_Printf("\033[0m\r\n"); \
} while (0)
#define EXAMPLE_MASTER_DEVID (0)
#define EXAMPLE_YIELD_TIMEOUT_MS (200)
typedef struct {
int master_devid;
int cloud_connected;
int master_initialized;
} user_example_ctx_t;
/**
* These PRODUCT_KEY|PRODUCT_SECRET|DEVICE_NAME|DEVICE_SECRET are listed for demo only
*
* When you created your own devices on iot.console.com, you SHOULD replace them with what you got from console
*
*/
char PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0};
char PRODUCT_SECRET[IOTX_PRODUCT_SECRET_LEN + 1] = {0};
char DEVICE_NAME[IOTX_DEVICE_NAME_LEN + 1] = {0};
char DEVICE_SECRET[IOTX_DEVICE_SECRET_LEN + 1] = {0};
static user_example_ctx_t g_user_example_ctx;
/** Awss Status event callback */
static int user_awss_status_event_handler(int status)
{
EXAMPLE_TRACE("Awss Status %d", status);
return SUCCESS_RETURN;
}
/** cloud connected event callback */
static int user_connected_event_handler(void)
{
EXAMPLE_TRACE("Cloud Connected");
g_user_example_ctx.cloud_connected = 1;
return 0;
}
/** cloud connect fail event callback */
static int user_connect_fail_event_handler(void)
{
EXAMPLE_TRACE("Cloud Connect Fail");
return SUCCESS_RETURN;
}
/** cloud disconnected event callback */
static int user_disconnected_event_handler(void)
{
EXAMPLE_TRACE("Cloud Disconnected");
g_user_example_ctx.cloud_connected = 0;
return 0;
}
/** cloud raw_data arrived event callback */
static int user_rawdata_arrived_event_handler(const int devid, const unsigned char *request, const int request_len)
{
EXAMPLE_TRACE("Cloud Rawdata Arrived");
return 0;
}
/* device initialized event callback */
static int user_initialized(const int devid)
{
EXAMPLE_TRACE("Device Initialized");
g_user_example_ctx.master_initialized = 1;
return 0;
}
/** recv property post response message from cloud **/
static int user_report_reply_event_handler(const int devid, const int msgid, const int code, const char *reply,
const int reply_len)
{
EXAMPLE_TRACE("Message Post Reply Received, Message ID: %d, Code: %d, Reply: %.*s", msgid, code,
reply_len,
(reply == NULL)? ("NULL") : (reply));
return 0;
}
/** recv event post response message from cloud **/
static int user_trigger_event_reply_event_handler(const int devid, const int msgid, const int code, const char *eventid,
const int eventid_len, const char *message, const int message_len)
{
EXAMPLE_TRACE("Trigger Event Reply Received, Message ID: %d, Code: %d, EventID: %.*s, Message: %.*s",
msgid, code,
eventid_len,
eventid, message_len, message);
return 0;
}
static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
{
int res = 0;
cJSON *root = NULL, *LightSwitch = NULL, *LightColor = NULL;
ESP_LOGI(TAG,"Property Set Received, Devid: %d, Request: %s", devid, request);
lightbulb_set_brightness(78);
lightbulb_set_saturation(100);
if (!request) {
return NULL_VALUE_ERROR;
}
/* Parse Root */
root = cJSON_Parse(request);
if (!root) {
ESP_LOGI(TAG,"JSON Parse Error");
return FAIL_RETURN;
}
/** Switch Lightbulb On/Off */
LightSwitch = cJSON_GetObjectItem(root, "LightSwitch");
if (LightSwitch) {
lightbulb_set_on(LightSwitch->valueint);
}
/** Switch Lightbulb Hue */
LightSwitch = cJSON_GetObjectItem(root, "RGBColor");
if (LightSwitch) {
LightColor = cJSON_GetObjectItem(LightSwitch, "Red");
lightbulb_set_hue(LightColor ? LightColor->valueint : 0);
LightColor = cJSON_GetObjectItem(LightSwitch, "Green");
lightbulb_set_hue(LightColor ? LightColor->valueint : 120);
LightColor = cJSON_GetObjectItem(LightSwitch, "Blue");
lightbulb_set_hue(LightColor ? LightColor->valueint : 240);
}
cJSON_Delete(root);
res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_POST_PROPERTY,
(unsigned char *)request, request_len);
ESP_LOGI(TAG,"Post Property Message ID: %d", res);
return SUCCESS_RETURN;
}
static int user_property_desired_get_reply_event_handler(const char *serviceid, const int serviceid_len)
{
ESP_LOGI(TAG, "ITE_PROPERTY_DESIRED_GET_REPLY");
return SUCCESS_RETURN;
}
static int user_property_get_event_handler(const int devid, const char *serviceid, const int serviceid_len, char **response, int *response_len)
{
ESP_LOGI(TAG,"Get Property Message ID: %d", devid);
return SUCCESS_RETURN;
}
static int user_service_request_event_handler(const int devid, const char *serviceid, const int serviceid_len,
const char *request, const int request_len,
char **response, int *response_len)
{
int contrastratio = 0, to_cloud = 0;
cJSON *root = NULL, *item_transparency = NULL, *item_from_cloud = NULL;
ESP_LOGI(TAG,"Service Request Received, Devid: %d, Service ID: %.*s, Payload: %s", devid, serviceid_len,
serviceid,
request);
/* Parse Root */
root = cJSON_Parse(request);
if (root == NULL || !cJSON_IsObject(root)) {
ESP_LOGI(TAG,"JSON Parse Error");
return -1;
}
if (strlen("Custom") == serviceid_len && memcmp("Custom", serviceid, serviceid_len) == 0) {
/* Parse Item */
const char *response_fmt = "{\"Contrastratio\":%d}";
item_transparency = cJSON_GetObjectItem(root, "transparency");
if (item_transparency == NULL || !cJSON_IsNumber(item_transparency)) {
cJSON_Delete(root);
return -1;
}
ESP_LOGI(TAG,"transparency: %d", item_transparency->valueint);
contrastratio = item_transparency->valueint + 1;
/* Send Service Response To Cloud */
*response_len = strlen(response_fmt) + 10 + 1;
*response = malloc(*response_len);
if (*response == NULL) {
ESP_LOGW(TAG,"Memory Not Enough");
return -1;
}
memset(*response, 0, *response_len);
snprintf(*response, *response_len, response_fmt, contrastratio);
*response_len = strlen(*response);
} else if (strlen("SyncService") == serviceid_len && memcmp("SyncService", serviceid, serviceid_len) == 0) {
/* Parse Item */
const char *response_fmt = "{\"ToCloud\":%d}";
item_from_cloud = cJSON_GetObjectItem(root, "FromCloud");
if (item_from_cloud == NULL || !cJSON_IsNumber(item_from_cloud)) {
cJSON_Delete(root);
return -1;
}
ESP_LOGI(TAG,"FromCloud: %d", item_from_cloud->valueint);
to_cloud = item_from_cloud->valueint + 1;
/* Send Service Response To Cloud */
*response_len = strlen(response_fmt) + 10 + 1;
*response = malloc(*response_len);
if (*response == NULL) {
ESP_LOGW(TAG,"Memory Not Enough");
return -1;
}
memset(*response, 0, *response_len);
snprintf(*response, *response_len, response_fmt, to_cloud);
*response_len = strlen(*response);
}
cJSON_Delete(root);
return 0;
}
static int user_timestamp_reply_event_handler(const char *timestamp)
{
EXAMPLE_TRACE("Current Timestamp: %s", timestamp);
return SUCCESS_RETURN;
}
static int user_toplist_reply_event_handler(const int devid, const int msgid, const int code, const char *eventid, const int eventid_len)
{
EXAMPLE_TRACE("ITE_TOPOLIST_REPLY");
return SUCCESS_RETURN;
}
static int user_permit_join_event_handler(const int devid, const int msgid, const int code, const char *eventid, const int eventid_len)
{
EXAMPLE_TRACE("ITE_PERMIT_JOIN");
return SUCCESS_RETURN;
}
/** fota event handler **/
static int user_fota_event_handler(int type, const char *version)
{
char buffer[1024] = {0};
int buffer_length = 1024;
/* 0 - new firmware exist, query the new firmware */
if (type == 0) {
EXAMPLE_TRACE("New Firmware Version: %s", version);
IOT_Linkkit_Query(EXAMPLE_MASTER_DEVID, ITM_MSG_QUERY_FOTA_DATA, (unsigned char *)buffer, buffer_length);
}
return 0;
}
/* cota event handler */
static int user_cota_event_handler(int type, const char *config_id, int config_size, const char *get_type,
const char *sign, const char *sign_method, const char *url)
{
char buffer[128] = {0};
int buffer_length = 128;
/* type = 0, new config exist, query the new config */
if (type == 0) {
EXAMPLE_TRACE("New Config ID: %s", config_id);
EXAMPLE_TRACE("New Config Size: %d", config_size);
EXAMPLE_TRACE("New Config Type: %s", get_type);
EXAMPLE_TRACE("New Config Sign: %s", sign);
EXAMPLE_TRACE("New Config Sign Method: %s", sign_method);
EXAMPLE_TRACE("New Config URL: %s", url);
IOT_Linkkit_Query(EXAMPLE_MASTER_DEVID, ITM_MSG_QUERY_COTA_DATA, (unsigned char *)buffer, buffer_length);
}
return 0;
}
static int user_mqtt_connect_succ_event_handler(void)
{
EXAMPLE_TRACE("ITE_MQTT_CONNECT_SUCC");
return SUCCESS_RETURN;
}
static void user_post_property(void)
{
static int cnt = 0;
int res = 0;
char property_payload[30] = {0};
HAL_Snprintf(property_payload, sizeof(property_payload), "{\"Counter\": %d}", cnt++);
res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_POST_PROPERTY,
(unsigned char *)property_payload, strlen(property_payload));
EXAMPLE_TRACE("Post Property Message ID: %d", res);
}
static void user_post_event(void)
{
int res = 0;
char *event_id = "HardwareError";
char *event_payload = "{\"ErrorCode\": 0}";
res = IOT_Linkkit_TriggerEvent(EXAMPLE_MASTER_DEVID, event_id, strlen(event_id),
event_payload, strlen(event_payload));
EXAMPLE_TRACE("Post Event Message ID: %d", res);
}
static void user_deviceinfo_update(void)
{
int res = 0;
char *device_info_update = "[{\"attrKey\":\"abc\",\"attrValue\":\"hello,world\"}]";
res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_DEVICEINFO_UPDATE,
(unsigned char *)device_info_update, strlen(device_info_update));
EXAMPLE_TRACE("Device Info Update Message ID: %d", res);
}
static void user_deviceinfo_delete(void)
{
int res = 0;
char *device_info_delete = "[{\"attrKey\":\"abc\"}]";
res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_DEVICEINFO_DELETE,
(unsigned char *)device_info_delete, strlen(device_info_delete));
EXAMPLE_TRACE("Device Info Delete Message ID: %d", res);
}
static int linkkit_thread(void *paras)
{
int res = 0;
iotx_linkkit_dev_meta_info_t master_meta_info;
int domain_type = 0, dynamic_register = 0, post_reply_need = 0;
#ifdef ATM_ENABLED
if (IOT_ATM_Init() < 0) {
EXAMPLE_TRACE("IOT ATM init failed!\n");
return -1;
}
#endif
memset(&g_user_example_ctx, 0, sizeof(user_example_ctx_t));
HAL_GetProductKey(PRODUCT_KEY);
HAL_GetProductSecret(PRODUCT_SECRET);
HAL_GetDeviceName(DEVICE_NAME);
HAL_GetDeviceSecret(DEVICE_SECRET);
memset(&master_meta_info, 0, sizeof(iotx_linkkit_dev_meta_info_t));
memcpy(master_meta_info.product_key, PRODUCT_KEY, strlen(PRODUCT_KEY));
memcpy(master_meta_info.product_secret, PRODUCT_SECRET, strlen(PRODUCT_SECRET));
memcpy(master_meta_info.device_name, DEVICE_NAME, strlen(DEVICE_NAME));
memcpy(master_meta_info.device_secret, DEVICE_SECRET, strlen(DEVICE_SECRET));
/* Register Callback */
IOT_RegisterCallback(ITE_AWSS_STATUS, user_awss_status_event_handler);
IOT_RegisterCallback(ITE_CONNECT_SUCC, user_connected_event_handler);
IOT_RegisterCallback(ITE_CONNECT_FAIL, user_connect_fail_event_handler);
IOT_RegisterCallback(ITE_DISCONNECTED, user_disconnected_event_handler);
IOT_RegisterCallback(ITE_RAWDATA_ARRIVED, user_rawdata_arrived_event_handler);
IOT_RegisterCallback(ITE_SERVICE_REQUEST, user_service_request_event_handler);
IOT_RegisterCallback(ITE_PROPERTY_SET, user_property_set_event_handler);
IOT_RegisterCallback(ITE_PROPERTY_GET, user_property_get_event_handler);
IOT_RegisterCallback(ITE_PROPERTY_DESIRED_GET_REPLY, user_property_desired_get_reply_event_handler);
IOT_RegisterCallback(ITE_REPORT_REPLY, user_report_reply_event_handler);
IOT_RegisterCallback(ITE_TRIGGER_EVENT_REPLY, user_trigger_event_reply_event_handler);
IOT_RegisterCallback(ITE_TIMESTAMP_REPLY, user_timestamp_reply_event_handler);
IOT_RegisterCallback(ITE_TOPOLIST_REPLY, user_toplist_reply_event_handler);
IOT_RegisterCallback(ITE_PERMIT_JOIN, user_permit_join_event_handler);
IOT_RegisterCallback(ITE_INITIALIZE_COMPLETED, user_initialized);
IOT_RegisterCallback(ITE_FOTA, user_fota_event_handler);
IOT_RegisterCallback(ITE_COTA, user_cota_event_handler);
IOT_RegisterCallback(ITE_MQTT_CONNECT_SUCC, user_mqtt_connect_succ_event_handler);
#if CONFIG_MQTT_DIRECT
domain_type = IOTX_CLOUD_REGION_SHANGHAI;
#else
domain_type = IOTX_CLOUD_REGION_SINGAPORE;
#endif
IOT_Ioctl(IOTX_IOCTL_SET_DOMAIN, (void *)&domain_type);
/* Choose Login Method */
dynamic_register = 0;
IOT_Ioctl(IOTX_IOCTL_SET_DYNAMIC_REGISTER, (void *)&dynamic_register);
/* post reply doesn't need */
post_reply_need = 1;
IOT_Ioctl(IOTX_IOCTL_RECV_EVENT_REPLY, (void *)&post_reply_need);
/* Create Master Device Resources */
g_user_example_ctx.master_devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_MASTER, &master_meta_info);
if (g_user_example_ctx.master_devid < 0) {
EXAMPLE_TRACE("IOT_Linkkit_Open Failed\n");
return -1;
}
/* Start Connect Aliyun Server */
res = IOT_Linkkit_Connect(g_user_example_ctx.master_devid);
if (res < 0) {
EXAMPLE_TRACE("IOT_Linkkit_Connect Failed\n");
IOT_Linkkit_Close(g_user_example_ctx.master_devid);
return -1;
}
while (1) {
IOT_Linkkit_Yield(EXAMPLE_YIELD_TIMEOUT_MS);
}
IOT_Linkkit_Close(g_user_example_ctx.master_devid);
IOT_DumpMemoryStats(IOT_LOG_DEBUG);
IOT_SetLogLevel(IOT_LOG_NONE);
return 0;
}
void linkkit_main(void *paras)
{
while (1) {
linkkit_thread(NULL);
}
}
#endif

View File

@@ -0,0 +1,29 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef _LINKKIT_SOLO_H__
#define _LINKKIT_SOLO_H__
void linkkit_main(void *paras);
#endif