2022-01-12 17:24:18 +08:00
|
|
|
/**
|
|
|
|
|
*
|
2022-02-11 15:06:16 +08:00
|
|
|
* Copyright (c) 2020-2022 IRbaby-IRext
|
2022-01-12 17:24:18 +08:00
|
|
|
*
|
|
|
|
|
* 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>
|
2022-02-16 11:28:46 +08:00
|
|
|
#include <WString.h>
|
2022-01-12 17:24:18 +08:00
|
|
|
|
|
|
|
|
#include "IRbabySerial.h"
|
2022-01-30 11:24:38 +08:00
|
|
|
#include "IRbabyAlink.h"
|
2022-03-19 15:53:39 +08:00
|
|
|
#include "IRbabyIRIS.h"
|
2022-01-12 17:24:18 +08:00
|
|
|
#include "IRbabyGlobal.h"
|
|
|
|
|
|
2022-02-16 11:28:46 +08:00
|
|
|
#include "IRbaby.h"
|
2022-01-12 17:24:18 +08:00
|
|
|
|
2022-05-03 18:48:48 +08:00
|
|
|
#define TOPIC_NAME_MAX (64)
|
|
|
|
|
#define IOT_RETRY_MAX (3)
|
|
|
|
|
|
|
|
|
|
#define IRIS_KIT_PK_DEV "a1WlzsJh50b"
|
|
|
|
|
#define IRIS_KIT_PK_REL "a1ihYt1lqGH"
|
|
|
|
|
#define TOPIC_DOWNSTREAM_DEV "/user/iris/downstream_dev"
|
|
|
|
|
#define TOPIC_UPSTREAM_DEV "/user/iris/upstream_dev"
|
|
|
|
|
#define TOPIC_DOWNSTREAM_REL "/user/iris/downstream"
|
|
|
|
|
#define TOPIC_UPSTREAM_REL "/user/iris/upstream"
|
2022-01-30 10:47:36 +08:00
|
|
|
|
2022-02-16 11:28:46 +08:00
|
|
|
String g_product_key = "";
|
|
|
|
|
String g_device_name = "";
|
|
|
|
|
String g_device_secret = "";
|
|
|
|
|
String g_region_id = "cn-shanghai";
|
2022-03-13 20:04:04 +08:00
|
|
|
String g_upstream_topic = "";
|
2022-03-19 15:53:39 +08:00
|
|
|
String g_downstream_topic = "";
|
2022-03-13 20:04:04 +08:00
|
|
|
int g_app_id = 0;
|
2022-01-12 17:24:18 +08:00
|
|
|
|
2022-03-19 15:53:39 +08:00
|
|
|
|
|
|
|
|
static bool downstream_topic_subscribed = false;
|
2022-01-12 17:24:18 +08:00
|
|
|
static AliyunIoTSDK iot;
|
2022-01-30 10:47:36 +08:00
|
|
|
static ep_state_t endpoint_state = FSM_IDLE;
|
2022-01-12 17:24:18 +08:00
|
|
|
|
2022-03-19 15:53:39 +08:00
|
|
|
static void registerCallback(const char* topic, int qos);
|
2022-01-12 17:24:18 +08:00
|
|
|
static void irisAlinkCallback(const char *topic, uint8_t *data, int length);
|
2022-02-16 11:28:46 +08:00
|
|
|
|
|
|
|
|
static int iot_retry = 0;
|
|
|
|
|
|
2022-01-12 17:24:18 +08:00
|
|
|
void connectToAliyunIoT() {
|
2022-03-19 15:53:39 +08:00
|
|
|
downstream_topic_subscribed = false;
|
2022-05-03 18:48:48 +08:00
|
|
|
|
|
|
|
|
if (g_product_key.equals(IRIS_KIT_PK_DEV)) {
|
|
|
|
|
g_upstream_topic = "/" + g_product_key + "/" + g_device_name + TOPIC_UPSTREAM_DEV;
|
|
|
|
|
g_downstream_topic = "/" + g_product_key + "/" + g_device_name + TOPIC_DOWNSTREAM_DEV;
|
|
|
|
|
} else if (g_product_key.equals(IRIS_KIT_PK_REL)) {
|
|
|
|
|
g_upstream_topic = "/" + g_product_key + "/" + g_device_name + TOPIC_UPSTREAM_REL;
|
|
|
|
|
g_downstream_topic = "/" + g_product_key + "/" + g_device_name + TOPIC_DOWNSTREAM_REL;
|
|
|
|
|
} else {
|
|
|
|
|
ERRORF("IRIS Kit release key is not supported yet\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 11:28:46 +08:00
|
|
|
INFOF("Try connecting to Aliyun IoT : %s, %s, %s, %s\n",
|
|
|
|
|
g_product_key.c_str(), g_device_name.c_str(), g_device_secret.c_str(), g_region_id.c_str());
|
2022-03-19 15:53:39 +08:00
|
|
|
|
|
|
|
|
if (0 == iot.begin(wifi_client, g_product_key.c_str(), g_device_name.c_str(), g_device_secret.c_str(),
|
|
|
|
|
g_region_id.c_str())) {
|
|
|
|
|
sendIrisKitConnect();
|
|
|
|
|
}
|
2022-01-12 17:24:18 +08:00
|
|
|
INFOLN("Aliyun IoT connect done");
|
2022-05-03 18:48:48 +08:00
|
|
|
|
2022-01-12 17:24:18 +08:00
|
|
|
}
|
|
|
|
|
|
2022-09-01 10:53:54 +08:00
|
|
|
void aliotKeepAlive() {
|
|
|
|
|
iot.loop();
|
2022-01-12 17:24:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// not only for IRIS related topic based session
|
|
|
|
|
void sendRawData(const char* topic, const uint8_t *data, int length) {
|
|
|
|
|
iot.sendCustomData(topic, data, length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AliyunIoTSDK getSession() {
|
|
|
|
|
return iot;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-19 15:53:39 +08:00
|
|
|
static void registerCallback(const char* topic, int qos) {
|
|
|
|
|
if (iot.subscribe(topic, qos)) {
|
|
|
|
|
INFOLN("topic subscribed");
|
|
|
|
|
iot.registerCustomCallback(irisAlinkCallback);
|
|
|
|
|
}
|
2022-01-12 17:24:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void irisAlinkCallback(const char *topic, uint8_t *data, int length) {
|
2022-05-03 18:48:48 +08:00
|
|
|
INFOF("downstream message received, topic = %s, length = %d\n", topic, length);
|
2022-03-24 17:25:42 +08:00
|
|
|
if (NULL != g_downstream_topic.c_str() && 0 == strcmp(topic, g_downstream_topic.c_str())) {
|
|
|
|
|
handleIrisKitMessage((const char*) data, length);
|
|
|
|
|
}
|
2022-01-12 17:24:18 +08:00
|
|
|
}
|
2022-09-01 10:53:54 +08:00
|
|
|
|
|
|
|
|
void checkAlinkMQTT() {
|
|
|
|
|
int mqttStatus = 0;
|
|
|
|
|
|
|
|
|
|
if (0 == mqttStatus) {
|
|
|
|
|
iot_retry = 0;
|
|
|
|
|
if (false == downstream_topic_subscribed) {
|
|
|
|
|
INFOF("subscribe topic : %s\n", g_downstream_topic.c_str());
|
|
|
|
|
registerCallback(g_downstream_topic.c_str(), 0);
|
|
|
|
|
downstream_topic_subscribed = true;
|
|
|
|
|
} else {
|
|
|
|
|
sendIrisKitHeartBeat();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
INFOF("Alink MQTT check failed, retry = %d\n", iot_retry);
|
|
|
|
|
iot_retry++;
|
|
|
|
|
}
|
|
|
|
|
if (iot_retry >= IOT_RETRY_MAX) {
|
|
|
|
|
ERRORLN("Alink could not established, something went wrong, reset...");
|
|
|
|
|
factoryReset();
|
|
|
|
|
}
|
|
|
|
|
}
|