IntelliChargingPile/Core/Src/OneNet.c

237 lines
5.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <string.h>
#include <stdio.h>
#include "esp8266.h"
#include "main.h"
#include "onenet.h"
#include "mqttkit.h"
#include "stm32f1xx_hal_gpio.h"
// #include "main.h"
#define PRODUCT_ID "zzS53oqy5l"
#define DEVICE_ID "ESP_67EAF3"
#define SIGNATURE "version=2018-10-31&res=products%2FzzS53oqy5l%2Fdevices%2FESP_67EAF3&et=1766658508&method=md5&sign=r2rPhQdTZ3vpkxJTbgN5NQ%3D%3D"
extern unsigned char esp8266_buf[128];
//==========================================================
// 函数名称: OneNet_DevLink
//
// 函数功能: 与onenet创建连接
//
// 入口参数: 无
//
// 返回参数: 1-成功 0-失败
//
// 说明: 与onenet平台建立连接
//==========================================================
_Bool OneNet_DevLink(void)
{
MQTT_PACKET_STRUCTURE mqttPacket = {NULL, 0, 0, 0}; //协议包
unsigned char* dataPtr;
_Bool status = 1;
// printf("OneNet_DevLink\r\nPROID: %s\tAUIF: %s\tDEVID:%s\r\n", PRODUCT_ID, SIGNATURE, DEVICE_ID);
if (MQTT_PacketConnect(PRODUCT_ID, SIGNATURE, DEVICE_ID, 256, 1, MQTT_QOS_LEVEL0, NULL, NULL, 0, &mqttPacket) == 0)
{
ESP8266_SendData(mqttPacket._data, mqttPacket._len); //上传平台
dataPtr = ESP8266_GetIPD(250); //等待平台响应
if (dataPtr != NULL)
{
if (MQTT_UnPacketRecv(dataPtr) == MQTT_PKT_CONNACK)
{
switch (MQTT_UnPacketConnectAck(dataPtr))
{
case 0:
status = 0;
break;
// case 1: printf("WARN: 连接失败:协议错误\r\n");
// break;
// case 2: printf("WARN: 连接失败非法的clientid\r\n");
// break;
// case 3: printf("WARN: 连接失败:服务器失败\r\n");
// break;
// case 4: printf("WARN: 连接失败:用户名或密码错误\r\n");
// break;
// case 5: printf("WARN: 连接失败:非法链接(比如token非法)\r\n");
// break;
//
default:
break;
}
}
}
MQTT_DeleteBuffer(&mqttPacket); //删包
}
// else
// printf("WARN: MQTT_PacketConnect Failed\r\n");
return status;
}
unsigned char OneNet_FillBuf(char* buf)
{
char text[48];
strcpy(buf, "{\"id\":\"1743082944\",\"params\":{");
memset(text, 0, sizeof(text));
sprintf(text, "\"volt\":{\"value\":%f},", 218.3);
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);
}
//==========================================================
// 函数名称: OneNet_SendData
//
// 函数功能: 上传数据到平台
//
// 入口参数: type发送数据的格式
//
// 返回参数: 无
//
// 说明:
//==========================================================
void OneNet_SendData(void)
{
MQTT_PACKET_STRUCTURE mqttPacket = {NULL, 0, 0, 0}; //协议包
char buf[128];
short body_len = 0, i = 0;
memset(buf, 0, sizeof(buf)); //清空数组内容
body_len = OneNet_FillBuf(buf); //获取当前需要发送的数据流的总长度
if (body_len)
{
if (MQTT_PacketSaveData(DEVICE_ID, body_len, NULL, 5, &mqttPacket) == 0) //封包
{
for (; i < body_len; i++)
{
mqttPacket._data[mqttPacket._len++] = buf[i];
}
ESP8266_SendData(mqttPacket._data, mqttPacket._len); //上传数据到平台
MQTT_DeleteBuffer(&mqttPacket); //删包
}
else;
// printf( "WARN:EDP_NewBuffer Failed\r\n");
}
}
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); //删包
}
}
//==========================================================
// 函数名称: OneNet_RevPro
//
// 函数功能: 平台返回数据检测
//
// 入口参数: dataPtr平台返回的数据
//
// 返回参数: 无
//
// 说明:
//==========================================================
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 *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;
if(type == MQTT_PKT_CMD || type == MQTT_PKT_PUBLISH)
{
MQTT_FreeBuffer(cmdid_topic);
MQTT_FreeBuffer(req_payload);
}
}
uint8_t onenet_state(){
return OneNet_DevLink();
}