updated iris-kit mqtt to aliot

This commit is contained in:
strawmanbobi
2024-02-18 14:47:28 +08:00
parent 4d54cb273a
commit 95297411a9
9 changed files with 546 additions and 85 deletions

View File

@@ -0,0 +1,398 @@
/**
*
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Author: Strawmanbobi and Caffreyfans
*
* Permission is hereby granted, 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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "aliot_mqtt_sign.h"
// private variable definitions
static char mac_src[SIGN_SOURCE_MAX_LEN] = { 0 };
static uint8_t mac_res[32] = { 0 };
// private function declarations
static int utils_hmac_sha256(const uint8_t *msg, uint32_t msg_len, const uint8_t *key,
uint32_t key_len, uint8_t output[32]);
static void _hex2str(uint8_t *input, uint16_t input_len, char *output);
// public function definitions
int aliot_mqtt_sign(const char *product_key, const char *device_name, const char *device_secret, const char* device_id,
char* client_id, int max_client_id_size, char* user_name, int max_user_name_size, char* password, int max_password_size) {
int res = 0;
/* check parameters */
if (product_key == NULL || device_name == NULL || device_secret == NULL ||
client_id == NULL || user_name == NULL || password == NULL) {
return -1;
}
if ((strlen(product_key) > PRODUCT_KEY_MAX_LEN) || (strlen(device_name) > DEVICE_NAME_MAX_LEN) ||
(strlen(device_secret) > DEVICE_SECRET_MAX_LEN)) {
return -1;
}
/* setup clientid */
memset(client_id, 0 , max_client_id_size);
strncpy(client_id, device_id, max_client_id_size - 1);
strncat(client_id, MQTT_CLINETID_KV, max_client_id_size - 1);
/* setup user_name */
memset(user_name, 0, max_user_name_size);
strncpy(user_name, device_name, max_user_name_size - 1);
strncat(user_name, "&", max_user_name_size - 1);
strncat(user_name, product_key, max_user_name_size - 1);
/* setup password */
memset(mac_src, 0, SIGN_SOURCE_MAX_LEN);
strncpy(mac_src, "clientId", SIGN_SOURCE_MAX_LEN - 1);
strncat(mac_src, device_id, SIGN_SOURCE_MAX_LEN - 1);
strncat(mac_src, "deviceName", SIGN_SOURCE_MAX_LEN - 1);
strncat(mac_src, device_name, SIGN_SOURCE_MAX_LEN - 1);
strncat(mac_src, "productKey", SIGN_SOURCE_MAX_LEN - 1);
strncat(mac_src, product_key, SIGN_SOURCE_MAX_LEN - 1);
strncat(mac_src, "timestamp", SIGN_SOURCE_MAX_LEN - 1);
strncat(mac_src, TIMESTAMP_VALUE, SIGN_SOURCE_MAX_LEN - 1);
Serial.print("DEBUG\t!!! MAC SRC = ");
Serial.println(mac_src);
memset(mac_res, 0 , sizeof(mac_res));
if (0 == utils_hmac_sha256((uint8_t *)mac_src, strlen(mac_src), (uint8_t *)device_secret,
strlen(device_secret), mac_res)) {
memset(password, 0, max_password_size);
_hex2str(mac_res, sizeof(mac_res), password);
res = 0;
} else {
res = -1;
}
return res;
}
/******************************
* hmac-sha256 implement below
******************************/
#define SHA256_KEY_IOPAD_SIZE (64)
#define SHA256_DIGEST_SIZE (32)
/**
* \brief SHA-256 context structure
*/
typedef struct {
uint32_t total[2]; /*!< number of bytes processed */
uint32_t state[8]; /*!< intermediate digest state */
unsigned char buffer[64]; /*!< data block being processed */
int is224; /*!< 0 => SHA-256, else SHA-224 */
} iot_sha256_context;
typedef union {
char sptr[8];
uint64_t lint;
} u_retLen;
/*
* 32-bit integer manipulation macros (big endian)
*/
#ifndef GET_UINT32_BE
#define GET_UINT32_BE(n,b,i) \
do { \
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
| ( (uint32_t) (b)[(i) + 3] ); \
} while( 0 )
#endif
#ifndef PUT_UINT32_BE
#define PUT_UINT32_BE(n,b,i) \
do { \
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
(b)[(i) + 3] = (unsigned char) ( (n) ); \
} while( 0 )
#endif
static void utils_sha256_zeroize(void *v, uint32_t n) {
volatile unsigned char *p = (unsigned char*) v;
while (n--) {
*p++ = 0;
}
}
void utils_sha256_init(iot_sha256_context *ctx) {
memset(ctx, 0, sizeof(iot_sha256_context));
}
void utils_sha256_free(iot_sha256_context *ctx) {
if (NULL == ctx) {
return;
}
utils_sha256_zeroize(ctx, sizeof(iot_sha256_context));
}
void utils_sha256_starts(iot_sha256_context *ctx) {
int is224 = 0;
ctx->total[0] = 0;
ctx->total[1] = 0;
if (is224 == 0) {
/* SHA-256 */
ctx->state[0] = 0x6A09E667;
ctx->state[1] = 0xBB67AE85;
ctx->state[2] = 0x3C6EF372;
ctx->state[3] = 0xA54FF53A;
ctx->state[4] = 0x510E527F;
ctx->state[5] = 0x9B05688C;
ctx->state[6] = 0x1F83D9AB;
ctx->state[7] = 0x5BE0CD19;
}
ctx->is224 = is224;
}
static const uint32_t K[] = {
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
};
#define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
#define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
#define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
#define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
#define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
#define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
#define F0(x,y,z) ((x & y) | (z & (x | y)))
#define F1(x,y,z) (z ^ (x & (y ^ z)))
#define R(t) \
( \
W[t] = S1(W[t - 2]) + W[t - 7] + \
S0(W[t - 15]) + W[t - 16] \
)
#define P(a,b,c,d,e,f,g,h,x,K) \
{ \
temp1 = h + S3(e) + F1(e,f,g) + K + x; \
temp2 = S2(a) + F0(a,b,c); \
d += temp1; h = temp1 + temp2; \
}
void utils_sha256_process(iot_sha256_context *ctx, const unsigned char data[64]) {
uint32_t temp1, temp2, W[64];
uint32_t A[8];
unsigned int i;
for (i = 0; i < 8; i++) {
A[i] = ctx->state[i];
}
for (i = 0; i < 64; i++) {
if (i < 16) {
GET_UINT32_BE(W[i], data, 4 * i);
} else {
R(i);
}
P(A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i]);
temp1 = A[7];
A[7] = A[6];
A[6] = A[5];
A[5] = A[4];
A[4] = A[3];
A[3] = A[2];
A[2] = A[1];
A[1] = A[0];
A[0] = temp1;
}
for (i = 0; i < 8; i++) {
ctx->state[i] += A[i];
}
}
void utils_sha256_update(iot_sha256_context *ctx, const unsigned char *input, uint32_t ilen) {
size_t fill;
uint32_t left;
if (ilen == 0) {
return;
}
left = ctx->total[0] & 0x3F;
fill = 64 - left;
ctx->total[0] += (uint32_t) ilen;
ctx->total[0] &= 0xFFFFFFFF;
if (ctx->total[0] < (uint32_t) ilen) {
ctx->total[1]++;
}
if (left && ilen >= fill) {
memcpy((void *)(ctx->buffer + left), input, fill);
utils_sha256_process(ctx, ctx->buffer);
input += fill;
ilen -= fill;
left = 0;
}
while (ilen >= 64) {
utils_sha256_process(ctx, input);
input += 64;
ilen -= 64;
}
if (ilen > 0) {
memcpy((void *)(ctx->buffer + left), input, ilen);
}
}
static const unsigned char sha256_padding[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
void utils_sha256_finish(iot_sha256_context *ctx, uint8_t output[32]) {
uint32_t last, padn;
uint32_t high, low;
unsigned char msglen[8];
high = (ctx->total[0] >> 29)
| (ctx->total[1] << 3);
low = (ctx->total[0] << 3);
PUT_UINT32_BE(high, msglen, 0);
PUT_UINT32_BE(low, msglen, 4);
last = ctx->total[0] & 0x3F;
padn = (last < 56) ? (56 - last) : (120 - last);
utils_sha256_update(ctx, sha256_padding, padn);
utils_sha256_update(ctx, msglen, 8);
PUT_UINT32_BE(ctx->state[0], output, 0);
PUT_UINT32_BE(ctx->state[1], output, 4);
PUT_UINT32_BE(ctx->state[2], output, 8);
PUT_UINT32_BE(ctx->state[3], output, 12);
PUT_UINT32_BE(ctx->state[4], output, 16);
PUT_UINT32_BE(ctx->state[5], output, 20);
PUT_UINT32_BE(ctx->state[6], output, 24);
if (ctx->is224 == 0) {
PUT_UINT32_BE(ctx->state[7], output, 28);
}
}
void utils_sha256(const uint8_t *input, uint32_t ilen, uint8_t output[32]) {
iot_sha256_context ctx;
utils_sha256_init(&ctx);
utils_sha256_starts(&ctx);
utils_sha256_update(&ctx, input, ilen);
utils_sha256_finish(&ctx, output);
utils_sha256_free(&ctx);
}
static int utils_hmac_sha256(const uint8_t *msg, uint32_t msg_len, const uint8_t *key,
uint32_t key_len, uint8_t* output) {
iot_sha256_context context;
uint8_t k_ipad[SHA256_KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
uint8_t k_opad[SHA256_KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
int32_t i = 0;
if ((NULL == msg) || (NULL == key) || (NULL == output)) {
return -1;
}
if (key_len > SHA256_KEY_IOPAD_SIZE) {
return -1;
}
/* start out by storing key in pads */
memset(k_ipad, 0, sizeof(k_ipad));
memset(k_opad, 0, sizeof(k_opad));
memcpy(k_ipad, key, key_len);
memcpy(k_opad, key, key_len);
/* XOR key with ipad and opad values */
for (i = 0; i < SHA256_KEY_IOPAD_SIZE; i++) {
k_ipad[i] ^= 0x36;
k_opad[i] ^= 0x5c;
}
/* perform inner SHA */
utils_sha256_init(&context); /* init context for 1st pass */
utils_sha256_starts(&context); /* setup context for 1st pass */
utils_sha256_update(&context, k_ipad, SHA256_KEY_IOPAD_SIZE); /* start with inner pad */
utils_sha256_update(&context, msg, msg_len); /* then text of datagram */
utils_sha256_finish(&context, output); /* finish up 1st pass */
/* perform outer SHA */
utils_sha256_init(&context); /* init context for 2nd pass */
utils_sha256_starts(&context); /* setup context for 2nd pass */
utils_sha256_update(&context, k_opad, SHA256_KEY_IOPAD_SIZE); /* start with outer pad */
utils_sha256_update(&context, output, SHA256_DIGEST_SIZE); /* then results of 1st hash */
utils_sha256_finish(&context, output); /* finish up 2nd pass */
return 0;
}
static void _hex2str(uint8_t *input, uint16_t input_len, char *output) {
const char *z_encode = "0123456789ABCDEF";
int i = 0, j = 0;
for (i = 0; i < input_len; i++) {
output[j++] = z_encode[(input[i] >> 4) & 0xf];
output[j++] = z_encode[(input[i]) & 0xf];
}
}

View File

@@ -0,0 +1,56 @@
/**
*
* Copyright (c) 2020-2024 IRbaby-IRext
*
* Author: Strawmanbobi and Caffreyfans
*
* Permission is hereby granted, 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 <Arduino.h>
#ifndef ALIOT_MQTT_SIGN_H
#define ALIOT_MQTT_SIGN_H
#define PRODUCT_KEY_MAX_LEN (20)
#define DEVICE_NAME_MAX_LEN (32)
#define DEVICE_SECRET_MAX_LEN (64)
#define SIGN_SOURCE_MAX_LEN (256)
#define CLIENT_ID_MAX_LEN (256)
#define USER_NAME_MAX_LEN (256)
#define PASSWORD_MAX_LEN (65)
#define TIMESTAMP_VALUE "1708225691253"
#define MQTT_CLINETID_KV "|securemode=2,signmethod=hmacsha256,timestamp=1708225691253|"
#ifdef __cplusplus
extern "C" {
#endif
int aliot_mqtt_sign(const char *product_key, const char *device_name, const char *device_secret, const char* device_id,
char* client_id, int max_client_id_size, char* user_name, int max_user_name_size, char* password, int max_password_size);
#ifdef __cplusplus
}
#endif
#endif // ALIOT_MQTT_SIGN_H

View File

@@ -15,9 +15,9 @@
#include "aliyun_iot_sdk.h"
#define CHECK_INTERVAL 30000
#define MESSAGE_BUFFER_SIZE 10
#define MQTT_CONNECT_RETRY_MAX 3
#define CHECK_INTERVAL (30000)
#define MESSAGE_BUFFER_SIZE (10)
#define MQTT_CONNECT_RETRY_MAX (3)
static const char *deviceName = NULL;
static const char *productKey = NULL;
@@ -32,8 +32,6 @@ struct DeviceProperty {
DeviceProperty PropertyMessageBuffer[MESSAGE_BUFFER_SIZE];
#define MQTT_PORT 1883
#define SHA256HMAC_SIZE 32
#define DATA_CALLBACK_SIZE 20
@@ -50,33 +48,21 @@ static PubSubClient *client = NULL;
static pointerDesc pointerArray[20];
static pPointerDesc pPointerArray;
char AliyunIoTSDK::clientId[256] = "";
char AliyunIoTSDK::mqttUsername[100] = "";
char AliyunIoTSDK::mqttPwd[256] = "";
char AliyunIoTSDK::domain[150] = "";
char AliyunIoTSDK::clientId[CLIENT_ID_MAX_LEN] = "";
char AliyunIoTSDK::mqttUsername[USER_NAME_MAX_LEN] = "";
char AliyunIoTSDK::mqttPwd[PASSWORD_MAX_LEN] = "";
char AliyunIoTSDK::domain[DOMAIN_NAME_MAX_LEN] = "";
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 long long getTimeMills(void) {
static uint32_t low32, high32;
uint32_t new_low32 = millis();
if (new_low32 < low32) high32++;
low32 = new_low32;
return (uint64_t) high32 << 32 | low32;
}
static void parmPass(JsonVariant parm) {
@@ -131,15 +117,19 @@ int AliyunIoTSDK::mqttCheckConnect() {
if (client->connect(clientId, mqttUsername, mqttPwd)) {
Serial.println("INFO:\tMQTT Connected!");
} else {
Serial.print("ERROR:\tMQTT Connect err: ");
Serial.print("ERROR:\tMQTT Connect err : ");
Serial.println(client->state());
Serial.print("ERROR:\tusername : ");
Serial.println(mqttUsername);
Serial.print("ERROR:\tpassword : ");
Serial.println(mqttPwd);
delay(MQTT_WAIT_GENERIC);
connectRetry++;
Serial.print("INFO:\tretry: ");
Serial.print("INFO:\tretry : ");
Serial.println(connectRetry);
mqttStatus = -1;
if (connectRetry > MQTT_CONNECT_RETRY_MAX) {
Serial.println("ERROR:\t max connect retry times reached");
Serial.println("ERROR:\tmax connect retry times reached");
break;
}
}
@@ -151,6 +141,7 @@ int AliyunIoTSDK::mqttCheckConnect() {
}
int AliyunIoTSDK::begin(PubSubClient &mqttClient,
const char *_clientId,
const char *_productKey,
const char *_deviceName,
const char *_deviceSecret,
@@ -162,39 +153,40 @@ int AliyunIoTSDK::begin(PubSubClient &mqttClient,
deviceSecret = _deviceSecret;
iotInstanceId = _iotInstanceId;
region = _region;
int port = 443;
long times = millis();
String timestamp = String(times);
uint16_t port = 443;
sprintf(clientId, "%s|securemode=3,signmethod=hmacsha256,timestamp=%s|", deviceName,
timestamp.c_str());
int res = 0;
String signcontent = "clientId";
signcontent += deviceName;
signcontent += "deviceName";
signcontent += deviceName;
signcontent += "productKey";
signcontent += productKey;
signcontent += "timestamp";
signcontent += timestamp;
res = aliot_mqtt_sign(productKey, deviceName, deviceSecret, _clientId,
clientId, CLIENT_ID_MAX_LEN, mqttUsername, USER_NAME_MAX_LEN, mqttPwd, PASSWORD_MAX_LEN);
String pwd = hmac256(signcontent, deviceSecret);
if (0 != res) {
Serial.println("ERROR\tfailed to sign aliot mqtt params");
return -1;
}
Serial.print("DEBUG\tMQTT clietnId = ");
Serial.println(clientId);
Serial.print("DEBUG\tMQTT userName = ");
Serial.println(mqttUsername);
Serial.print("DEBUG\tMQTT password = ");
Serial.println(mqttPwd);
strcpy(mqttPwd, pwd.c_str());
sprintf(mqttUsername, "%s&%s", deviceName, productKey);
sprintf(ALINK_TOPIC_PROP_POST, "/sys/%s/%s/thing/event/property/post", productKey, deviceName);
sprintf(ALINK_TOPIC_PROP_SET, "/sys/%s/%s/thing/service/property/set", productKey, deviceName);
sprintf(ALINK_TOPIC_EVENT, "/sys/%s/%s/thing/event", productKey, deviceName);
if (NULL != iotInstanceId) {
sprintf(domain, "%s.mqtt.iothub.aliyuncs.com", productKey);
port = 443;
sprintf(domain, "%s.mqtt.iothub.aliyuncs.com", iotInstanceId);
port = 1883;
} else {
sprintf(domain, "%s.iot-as-mqtt.%s.aliyuncs.com", productKey, region);
port = 1883;
}
Serial.print("INFO\tconnect to aliyun : ");
Serial.print(domain);
Serial.print(":");
Serial.println(port);
client->setServer(domain, port);
#if defined USE_STANDARD_THING_MODEL_TOPIC

View File

@@ -18,10 +18,14 @@
#include <PubSubClient.h>
#include "Client.h"
#include "aliot_mqtt_sign.h"
#ifdef __cplusplus
extern "C" {
#endif
#define DOMAIN_NAME_MAX_LEN (256)
typedef void (*funcPointer)(JsonVariant ele);
typedef struct {
@@ -36,10 +40,10 @@ typedef struct {
class AliyunIoTSDK {
private:
// MQTT client related parameters
static char mqttPwd[256];
static char clientId[256];
static char mqttUsername[100];
static char domain[150];
static char mqttPwd[PASSWORD_MAX_LEN];
static char clientId[CLIENT_ID_MAX_LEN];
static char mqttUsername[USER_NAME_MAX_LEN];
static char domain[DOMAIN_NAME_MAX_LEN];
static void messageBufferCheck();
static void sendBuffer();
@@ -66,6 +70,7 @@ public:
* @param _region : AliyunIoT region
*/
static int begin(PubSubClient &mqttClient,
const char *_clientId,
const char *_productKey,
const char *_deviceName,
const char *_deviceSecret,

View File

@@ -21,26 +21,22 @@ upload_speed = 115200
upload_port = /dev/ttyUSB0
monitor_port = /dev/ttyUSB0
board_build.flash_mode = dout
build_flags =
-Wno-unused-function
-Wno-unused-variable
-DLOG_DEBUG=1
-DLOG_INFO=1
-DLOG_ERROR=1
-I${common.workspace}/lib/irext/include
-I${common.workspace}/lib/linksdk
-I${common.workspace}/lib/linksdk/core
-I${common.workspace}/lib/linksdk/core/utils
-I${common.workspace}/lib/linksdk/core/sysdep
build_flags =
-Wno-unused-function
-Wno-unused-variable
-DLOG_DEBUG=1
-DLOG_INFO=1
-DLOG_ERROR=1
-I${common.workspace}/lib/irext/include
[env:esp8266-1m-base]
board = ${common.board_1m}
board_build.ldscript = ${common.ldscript_1m}
lib_deps =
WifiManager@^2.0.11
PubSubClient@^2.8
rweather/Crypto@^0.2.0
sui77/rc-switch@^2.6.4
ArduinoJson@^6.0
IRremoteESP8266@^2.8.6
AliyunIoTSDK@^0.3
lib_deps =
WifiManager@^2.0.11
PubSubClient@^2.8
rweather/Crypto@^0.2.0
sui77/rc-switch@^2.6.4
ArduinoJson@^6.0
IRremoteESP8266@^2.8.6
AliyunIoTSDK@^0.3

View File

@@ -22,7 +22,6 @@
*/
#include "defines.h"
#include "serials.h"
#include "iot_hub.h"
#include "iris_client.h"
@@ -54,14 +53,17 @@ static AliyunIoTSDK iot;
// public function definitions
int connectToAliyunIoT(PubSubClient &mqtt_client) {
int connectToAliot(PubSubClient &mqtt_client) {
String aliot_client_id = g_product_key + "." + g_device_name;
if (0 == iot.begin(mqtt_client, g_product_key.c_str(), g_device_name.c_str(), g_device_secret.c_str(),
g_aliot_instance_id.c_str(), g_aliot_region.c_str())) {
sendIrisKitConnect();
int res = iot.begin(mqtt_client, aliot_client_id.c_str(), g_product_key.c_str(), g_device_name.c_str(), g_device_secret.c_str(),
g_aliot_instance_id.c_str(), g_aliot_region.c_str());
if (0 == res) {
INFOLN("Aliyun IoT connected");
} else {
ERRORLN("Failed to connect to Aliyun IoT");
}
INFOLN("Aliyun IoT connected");
return 0;
return res;
}
void aliotKeepAlive() {

View File

@@ -30,6 +30,6 @@
int connectToAliot(PubSubClient &mqtt_client);
int disconnectFromAliot(PubSubClient &mqtt_client);
void aliotKeepAlive();
#endif // IRIS_KIT_ALIOT_CLIENT_H

View File

@@ -30,9 +30,11 @@
#include "global.h"
#include "emq_client.h"
#include "aliot_client.h"
#include "iris_kit.h"
#include "iot_hub.h"
// external variable declarations
extern int g_runtime_env;
@@ -88,13 +90,19 @@ int connectToIrextIoT() {
return -1;
}
// g_mqtt_user_name act as clientId for aliot and as userName for EMQX
g_mqtt_user_name = getDeviceID();
INFOF("Try connecting to IRext IoT %s:%d, client_id = %s, user_name = %s, password.size = %d\n",
g_mqtt_server.c_str(), g_mqtt_port,
g_mqtt_client_id.c_str(), g_mqtt_user_name.c_str(), g_mqtt_password.length());
INFOF("Try connecting to AliyunIoT, product_key = %s, device_name = %s, device_secret = %s\n",
g_product_key.c_str(), g_device_name.c_str(), g_device_token.c_str());
conn_ret = connectToAliot(mqtt_client);
conn_ret = connectToEMQXBroker(mqtt_client);
if (0 != conn_ret) {
INFOF("Try connecting to IRext IoT %s:%d, client_id = %s, user_name = %s, password.size = %d\n",
g_mqtt_server.c_str(), g_mqtt_port,
g_mqtt_client_id.c_str(), g_mqtt_user_name.c_str(), g_mqtt_password.length());
conn_ret = connectToEMQXBroker(mqtt_client);
}
if (0 != conn_ret) {
ERRORLN("Something may went wrong with your credential, please retry connect to Wifi...");

View File

@@ -26,6 +26,7 @@
#include <Arduino.h>
#include <LittleFS.h>
#include <Ticker.h>
#include <WiFiUdp.h>
#include "defines.h"
#include "ir_emit.h"
@@ -54,6 +55,8 @@ extern String g_mqtt_password;
extern int g_app_id;
// public variable definitions
const unsigned long utcOffsetInMilliSeconds = 3600 * 1000;
int credential_init_retry = 0;
int g_runtime_env = RUNTIME_RELEASE;
iris_kit_settings_t iriskit_settings;
@@ -66,6 +69,7 @@ static Ticker disableIRTask; // disable IR receive
static Ticker disableRFTask; // disable RF receive
static Ticker saveDataTask; // save data
// private function declarations
static void wifiReset();