#include "tos_k.h"
#include "esp8266_tencent_firmware.h"
#include "tencent_firmware_module_wrapper.h"
#include "cJSON.h"
#include "string.h"
#include "usart.h"
#include "ch32v30x_it.h"
#define PRODUCT_ID "U5FOC4ZH3C"
#define DEVICE_NAME "test"
#define DEVICE_KEY "DRjJa88UARCe0psnttX15w=="
#define REPORT_DATA_TEMPLATE "{\\\"method\\\":\\\"report\\\"\\,\\\"clientToken\\\":\\\"00000001\\\"\\,\\\"params\\\":{\\\"push3_roll\\\":%d}}"
char string[3]={'0','0','0'};
char json_string[256];
int i;
uint8_t ask_data[8]={0x54,0x03,0x00,0x3d,0x00,0x03,0x98,0x02};
int16_t sReg;
int fAcc;
extern char rec_data[11];
extern uint8_t uart6_flag;
extern uint8_t U6_RxCounter;
void default_message_handler(mqtt_message_t* msg)
{
strcpy(json_string,msg->payload);
for(i=0;i<sizeof(json_string)-1;i++)
{
json_string[i]=json_string[i+1];
}
cJSON* pload = cJSON_Parse(json_string);
if(pload == NULL) //判断转换是否成功
{
return;
//printf("cjson error...\r\n");
}
else if(pload != NULL)
{
//printf("\r\n %s \r\n",cJSON_Print(pload));//打包成功调用cJSON_Print打印输出
cJSON* method = cJSON_GetObjectItem(pload, "method");
if (0!=strncmp(method->valuestring , "control" , sizeof("control") - 1))
{
cJSON_Delete(pload);
//printf("Invalid method data\r\n");
return;
}
cJSON* params = cJSON_GetObjectItem(pload, "params");
if(!params)
{
cJSON_Delete(pload);
printf("Invalid params data\r\n");
return;
}
// cJSON* push1 = cJSON_GetObjectItem(params, "push1");
// if(push1)string[0] = push1->valueint+'0';
cJSON* push2 = cJSON_GetObjectItem(params, "push2");
if(push2)string[0] = push2->valueint+'0';
cJSON* push3 = cJSON_GetObjectItem(params, "push3");
if(push3)string[1] = push3->valueint+'0';
cJSON* push4 = cJSON_GetObjectItem(params, "push4");
if(push4)string[2] = push4->valueint+'0';
cJSON_Delete(pload);
usart_send_bytes(UART7, string, sizeof(string));//发送数据
string[0] = '0';
string[1] = '0';
string[2] = '0';
// string[4] = '0';
}
}
char payload[256] = {0};
static char report_topic_name[TOPIC_NAME_MAX_SIZE] = {0};
static char report_reply_topic_name[TOPIC_NAME_MAX_SIZE] = {0};
void mqtt_demo_task(void)
{
int ret = 0;
int size = 0;
mqtt_state_t state;
char *product_id = PRODUCT_ID;
char *device_name = DEVICE_NAME;
char *key = DEVICE_KEY;
device_info_t dev_info;
memset(&dev_info, 0, sizeof(device_info_t));
/**
* Please Choose your AT Port first, default is HAL_UART_2(USART2)
*/
ret = esp8266_tencent_firmware_sal_init(HAL_UART_PORT_2);
if (ret < 0) {
printf("esp8266 tencent firmware sal init fail, ret is %d\r\n", ret);
}
esp8266_tencent_firmware_join_ap("HZZ", "15380816993");
strncpy(dev_info.product_id, product_id, PRODUCT_ID_MAX_SIZE);
strncpy(dev_info.device_name, device_name, DEVICE_NAME_MAX_SIZE);
strncpy(dev_info.device_serc, key, DEVICE_SERC_MAX_SIZE);
tos_tf_module_info_set(&dev_info, TLS_MODE_PSK);
mqtt_param_t init_params = DEFAULT_MQTT_PARAMS;
if (tos_tf_module_mqtt_conn(init_params) != 0) {
printf("module mqtt conn fail\n");
} else {
printf("module mqtt conn success\n");
}
if (tos_tf_module_mqtt_state_get(&state) != -1) {
printf("MQTT: %s\n", state == MQTT_STATE_CONNECTED ? "CONNECTED" : "DISCONNECTED");
}
size = snprintf(report_reply_topic_name, TOPIC_NAME_MAX_SIZE, "$thing/down/property/%s/%s", product_id, device_name);
if (size < 0 || size > sizeof(report_reply_topic_name) - 1) {
printf("sub topic content length not enough! content size:%d buf size:%d", size, (int)sizeof(report_reply_topic_name));
}
if (tos_tf_module_mqtt_sub(report_reply_topic_name, QOS0, default_message_handler) != 0) {
printf("module mqtt sub fail\n");
} else {
printf("module mqtt sub success\n");
}
memset(report_topic_name, 0, sizeof(report_topic_name));
size = snprintf(report_topic_name, TOPIC_NAME_MAX_SIZE, "$thing/up/property/%s/%s", product_id, device_name);
if (size < 0 || size > sizeof(report_topic_name) - 1) {
printf("pub topic content length not enough! content size:%d buf size:%d", size, (int)sizeof(report_topic_name));
}
while (1) {
usart_send_bytes(UART6, ask_data,8);//发送数据
if(uart6_flag==1)
{
//printf("X_data is %s \r\n",rec_data);
//printf("%c%c\r\n",rec_data[3],rec_data[4]);
uart6_flag =0;
sReg = ((uint16_t)rec_data[3]<<8)|rec_data[4];
fAcc = sReg * 180 / 32768 ;
//printf("X_data is %.3f \r\n", fAcc);
}
if(fAcc>1)
{
memset(payload, 0, sizeof(payload));
snprintf(payload, sizeof(payload), REPORT_DATA_TEMPLATE, fAcc);
/* use AT+PUB AT command */
if (tos_tf_module_mqtt_pub(report_topic_name, QOS0, payload) != 0) {
printf("module mqtt pub fail\n");
break;
} else {
printf("module mqtt pub success\n");
}
}
tos_sleep_ms(100);
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。