概述
在使用阿里云与Nodemcu的交互中,会使用AliyunIoTSDK库进行数据发送和返回。使用这哥加强的SDK能够实现阿里云平台的RRPC和MQTT在自定义topic下的数据发送。
这里是.c文件
#include "AliyunIoTSDK.h"
#include "PubSubClient.h"
#include <SHA256.h>
#define CHECK_INTERVAL 10000
#define MESSAGE_BUFFER_SIZE 10
#define RETRY_CRASH_COUNT 5
static const char *deviceName = NULL;
static const char *productKey = NULL;
static const char *deviceSecret = NULL;
static const char *region = NULL;
struct DeviceProperty
{
String key;
String value;
};
DeviceProperty PropertyMessageBuffer[MESSAGE_BUFFER_SIZE];
#define MQTT_PORT 1883
#define SHA256HMAC_SIZE 32
#define DATA_CALLBACK_SIZE 20
#define ALINK_BODY_FORMAT "{"id":"123","version":"1.0","method":"thing.event.property.post","params":%s}"
#define ALINK_EVENT_BODY_FORMAT "{"id": "123","version": "1.0","params": %s,"method": "thing.event.%s.post"}"
static unsigned long lastMs = 0;
static int retry_count = 0;
static PubSubClient *client = NULL;
char AliyunIoTSDK::clientId[256] = "";
char AliyunIoTSDK::mqttUsername[100] = "";
char AliyunIoTSDK::mqttPwd[256] = "";
char AliyunIoTSDK::domain[150] = "";
char AliyunIoTSDK::ALINK_TOPIC_PROP_POST[150] = "";
char AliyunIoTSDK::ALINK_TOPIC_PROP_SET[150] = "";
char AliyunIoTSDK::ALINK_TOPIC_EVENT[150] = "";
static String hmac256(const String &signcontent, const String &ds)
{
byte hashCode[SHA256HMAC_SIZE];
SHA256 sha256;
const char *key = ds.c_str();
size_t keySize = ds.length();
sha256.resetHMAC(key, keySize);
sha256.update((const byte *)signcontent.c_str(), signcontent.length());
sha256.finalizeHMAC(key, keySize, hashCode, sizeof(hashCode));
String sign = "";
for (byte i = 0; i < SHA256HMAC_SIZE; ++i)
{
sign += "0123456789ABCDEF"[hashCode[i] >> 4];
sign += "0123456789ABCDEF"[hashCode[i] & 0xf];
}
return sign;
}
static void parmPass(JsonVariant parm)
{
// const char *method = parm["method"];
for (int i = 0; i < DATA_CALLBACK_SIZE; i++)
{
if (poniter_array[i].key)
{
bool hasKey = parm["params"].containsKey(poniter_array[i].key);
if (hasKey)
{
poniter_array[i].fp(parm["params"],poniter_array[i].key);
}
}
}
}
static void topicPass(char *topic,JsonVariant parm)
{
for (int i = 0; i < 10; i++)
{
if (poniter_topic[i].key){
if (strstr(topic, poniter_topic[i].key))
{
poniter_topic[i].fp(parm,topic);
}
}
}
}
// 所有云服务的回调都会首先进入这里,例如属性下发
static void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
payload[length] = '