break: before freertos
This commit is contained in:
@ -4,7 +4,9 @@
|
||||
#include "main.h"
|
||||
#include "onenet.h"
|
||||
#include "mqttkit.h"
|
||||
#include "oled.h"
|
||||
#include "stm32f1xx_hal_gpio.h"
|
||||
#include "cJSON.h"
|
||||
|
||||
#define PRODUCT_ID "zzS53oqy5l"
|
||||
#define DEVICE_ID "ESP_67EAF3"
|
||||
@ -79,27 +81,28 @@ unsigned char OneNet_FillBuf(char* buf)
|
||||
{
|
||||
char text[48];
|
||||
|
||||
strcpy(buf, "{\"id\":\"1743082944\",\"params\":{");
|
||||
strcpy(buf, "{\"id\":\"1743082944\",\"params\":{");
|
||||
|
||||
memset(text, 0, sizeof(text));
|
||||
sprintf(text, "\"volt\":{\"value\":%.2f},", IM_Volt * 0.0001);
|
||||
strcat(buf, text);
|
||||
memset(text, 0, sizeof(text));
|
||||
sprintf(text, "\"volt\":{\"value\":%.2f},", IM_Volt * 0.0001);
|
||||
strcat(buf, text);
|
||||
|
||||
memset(text, 0, sizeof(text));
|
||||
sprintf(text, "\"current\":{\"value\":%.2f},", IM_Curr * 0.0001);
|
||||
strcat(buf, text);
|
||||
memset(text, 0, sizeof(text));
|
||||
sprintf(text, "\"current\":{\"value\":%.2f},", IM_Curr * 0.0001);
|
||||
strcat(buf, text);
|
||||
|
||||
memset(text, 0, sizeof(text));
|
||||
sprintf(text, "\"watt_P\":{\"value\":%.2f},", IM_Power * 0.0001);
|
||||
strcat(buf, text);
|
||||
memset(text, 0, sizeof(text));
|
||||
sprintf(text, "\"watt_P\":{\"value\":%.2f},", IM_Power * 0.0001);
|
||||
strcat(buf, text);
|
||||
|
||||
memset(text, 0, sizeof(text));
|
||||
sprintf(text, "\"energy\":{\"value\":%.3f},", IM_Energy * 0.0001);
|
||||
strcat(buf, text);
|
||||
memset(text, 0, sizeof(text));
|
||||
sprintf(text, "\"energy\":{\"value\":%.3f},", IM_Energy * 0.0001);
|
||||
strcat(buf, text);
|
||||
|
||||
memset(text, 0, sizeof(text));
|
||||
sprintf(text, "\"output\":{\"value\":%s}", HAL_GPIO_ReadPin(RELAY1_GPIO_Port, RELAY1_Pin) == GPIO_PIN_RESET ? "true" : "false");
|
||||
strcat(buf, text);
|
||||
memset(text, 0, sizeof(text));
|
||||
sprintf(text, "\"output\":{\"value\":%s}",
|
||||
HAL_GPIO_ReadPin(RELAY1_GPIO_Port, RELAY1_Pin) == GPIO_PIN_RESET ? "true" : "false");
|
||||
strcat(buf, text);
|
||||
|
||||
strcat(buf, "}}");
|
||||
return strlen(buf);
|
||||
@ -146,23 +149,21 @@ void OneNet_SendData(void)
|
||||
|
||||
void OneNET_Subscribe(void)
|
||||
{
|
||||
|
||||
MQTT_PACKET_STRUCTURE mqtt_packet = {NULL, 0, 0, 0}; //协议包
|
||||
|
||||
char topic_buf[56];
|
||||
const char *topic = topic_buf;
|
||||
|
||||
snprintf(topic_buf, sizeof(topic_buf), "$sys/%s/%s/thing/property/set", PRODUCT_ID, DEVICE_ID);
|
||||
|
||||
// printf("Subscribe Topic: %s\r\n", topic_buf);
|
||||
|
||||
if(MQTT_PacketSubscribe(MQTT_SUBSCRIBE_ID, MQTT_QOS_LEVEL0, &topic, 1, &mqtt_packet) == 0)
|
||||
{
|
||||
ESP8266_SendData(mqtt_packet._data, mqtt_packet._len); //向平台发送订阅请求
|
||||
|
||||
MQTT_DeleteBuffer(&mqtt_packet); //删包
|
||||
}
|
||||
MQTT_PACKET_STRUCTURE mqtt_packet = {NULL, 0, 0, 0}; //协议包
|
||||
|
||||
char topic_buf[56];
|
||||
const char* topic = topic_buf;
|
||||
|
||||
snprintf(topic_buf, sizeof(topic_buf), "$sys/%s/%s/thing/property/set", PRODUCT_ID, DEVICE_ID);
|
||||
|
||||
// printf("Subscribe Topic: %s\r\n", topic_buf);
|
||||
|
||||
if (MQTT_PacketSubscribe(MQTT_SUBSCRIBE_ID, MQTT_QOS_LEVEL0, &topic, 1, &mqtt_packet) == 0)
|
||||
{
|
||||
ESP8266_SendData(mqtt_packet._data, mqtt_packet._len); //向平台发送订阅请求
|
||||
|
||||
MQTT_DeleteBuffer(&mqtt_packet); //删包
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -177,70 +178,107 @@ void OneNET_Subscribe(void)
|
||||
//
|
||||
// 说明:
|
||||
//==========================================================
|
||||
void OneNet_RevPro(unsigned char *cmd)
|
||||
void OneNet_RevPro(unsigned char* cmd)
|
||||
{
|
||||
|
||||
char *req_payload = NULL;
|
||||
char *cmdid_topic = NULL;
|
||||
|
||||
unsigned short topic_len = 0;
|
||||
unsigned short req_len = 0;
|
||||
|
||||
unsigned char qos = 0;
|
||||
static unsigned short pkt_id = 0;
|
||||
|
||||
unsigned char type = 0;
|
||||
|
||||
short result = 0;
|
||||
char* req_payload = NULL;
|
||||
char* cmdid_topic = NULL;
|
||||
|
||||
char *dataPtr = NULL;
|
||||
char numBuf[10];
|
||||
int num = 0;
|
||||
type = MQTT_UnPacketRecv(cmd);
|
||||
switch(type)
|
||||
{
|
||||
case MQTT_PKT_PUBLISH: //接收的Publish消息
|
||||
|
||||
result = MQTT_UnPacketPublish(cmd, &cmdid_topic, &topic_len, &req_payload, &req_len, &qos, &pkt_id);
|
||||
// if(result == 0)
|
||||
// {
|
||||
// printf("%s\r\n", req_payload);
|
||||
// }
|
||||
break;
|
||||
|
||||
case MQTT_PKT_PUBACK: //发送Publish消息,平台回复的Ack
|
||||
|
||||
if(MQTT_UnPacketPublishAck(cmd) == 0)
|
||||
// UsartPrintf(USART_DEBUG, "Tips: MQTT Publish Send OK\r\n");
|
||||
|
||||
break;
|
||||
|
||||
case MQTT_PKT_SUBACK: //发送Subscribe消息的Ack
|
||||
|
||||
if(MQTT_UnPacketSubscribe(cmd) == 0)
|
||||
;
|
||||
// UsartPrintf(USART_DEBUG, "Tips: MQTT Subscribe OK\r\n");
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
result = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
ESP8266_Clear(); //清空缓存
|
||||
|
||||
if(result == -1)
|
||||
return;
|
||||
unsigned short topic_len = 0;
|
||||
unsigned short req_len = 0;
|
||||
|
||||
if(type == MQTT_PKT_CMD || type == MQTT_PKT_PUBLISH)
|
||||
{
|
||||
MQTT_FreeBuffer(cmdid_topic);
|
||||
MQTT_FreeBuffer(req_payload);
|
||||
}
|
||||
unsigned char qos = 0;
|
||||
static unsigned short pkt_id = 0;
|
||||
|
||||
unsigned char type = 0;
|
||||
|
||||
short result = 0;
|
||||
|
||||
type = MQTT_UnPacketRecv(cmd);
|
||||
switch (type)
|
||||
{
|
||||
case MQTT_PKT_PUBLISH:
|
||||
|
||||
result = MQTT_UnPacketPublish(cmd, &cmdid_topic, &topic_len, &req_payload, &req_len, &qos, &pkt_id);
|
||||
if (result == 0)
|
||||
{
|
||||
cJSON* root = cJSON_Parse(req_payload);
|
||||
if (root == NULL) return;
|
||||
|
||||
const cJSON* id = cJSON_GetObjectItem(root, "id");
|
||||
if (id == NULL) return;
|
||||
|
||||
cJSON* params = cJSON_GetObjectItem(root, "params");
|
||||
if (params == NULL)
|
||||
{
|
||||
cJSON_Delete(root);
|
||||
return;
|
||||
}
|
||||
|
||||
// params process
|
||||
const cJSON* energy = cJSON_GetObjectItem(params, "energy");
|
||||
const cJSON* output = cJSON_GetObjectItem(params, "output");
|
||||
|
||||
if (energy != NULL)
|
||||
{
|
||||
char disp[64];
|
||||
sprintf(disp, "set eng=%d", energy->valueint);
|
||||
OLED_ShowString(0, 8, disp, 12);
|
||||
}
|
||||
|
||||
if (output != NULL)
|
||||
{
|
||||
HAL_GPIO_WritePin(RELAY1_GPIO_Port, RELAY1_Pin, output->valueint ? GPIO_PIN_RESET : GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
MQTT_PACKET_STRUCTURE mqtt_packet = {NULL, 0, 0, 0};
|
||||
|
||||
cJSON* reply = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(reply, "id", cJSON_CreateString(id->valuestring));
|
||||
cJSON_AddItemToObject(reply, "code", cJSON_CreateNumber(200));
|
||||
cJSON_AddItemToObject(reply, "msg", cJSON_CreateString("success"));
|
||||
|
||||
const char* reply_str = cJSON_Print(reply);
|
||||
if (MQTT_PacketPublish(MQTT_PUBLISH_ID, strcat(cmdid_topic, "_reply"),
|
||||
reply_str,
|
||||
strlen(reply_str), MQTT_QOS_LEVEL0, 0, 1, &mqtt_packet) == 0)
|
||||
{
|
||||
ESP8266_SendData(mqtt_packet._data, mqtt_packet._len);
|
||||
MQTT_DeleteBuffer(&mqtt_packet);
|
||||
}
|
||||
|
||||
cJSON_Delete(reply);
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
break;
|
||||
|
||||
case MQTT_PKT_PUBACK: //发送Publish消息,平台回复的Ack
|
||||
|
||||
if (MQTT_UnPacketPublishAck(cmd) == 0)
|
||||
break;
|
||||
|
||||
case MQTT_PKT_SUBACK: //发送Subscribe消息的Ack
|
||||
|
||||
if (MQTT_UnPacketSubscribe(cmd) == 0)
|
||||
break;
|
||||
|
||||
default:
|
||||
result = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
ESP8266_Clear(); //清空缓存
|
||||
|
||||
if (result == -1)
|
||||
return;
|
||||
|
||||
if (type == MQTT_PKT_CMD || type == MQTT_PKT_PUBLISH)
|
||||
{
|
||||
MQTT_FreeBuffer(cmdid_topic);
|
||||
MQTT_FreeBuffer(req_payload);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t onenet_state(){
|
||||
return OneNet_DevLink();
|
||||
uint8_t onenet_state()
|
||||
{
|
||||
return OneNet_DevLink();
|
||||
}
|
||||
|
Reference in New Issue
Block a user