2022-01-12 17:24:18 +08:00
|
|
|
/**
|
|
|
|
|
*
|
2024-02-11 12:42:50 +08:00
|
|
|
* Copyright (c) 2020-2024 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
|
|
|
|
2024-02-11 12:42:50 +08:00
|
|
|
#include "serials.h"
|
|
|
|
|
#include "iot_hub.h"
|
2024-02-15 18:27:27 +08:00
|
|
|
#include "iris_client.h"
|
2024-02-11 12:42:50 +08:00
|
|
|
#include "global.h"
|
2022-01-12 17:24:18 +08:00
|
|
|
|
2024-02-11 12:42:50 +08:00
|
|
|
#include "emq_client.h"
|
2024-02-18 14:47:28 +08:00
|
|
|
#include "aliot_client.h"
|
2024-02-11 12:42:50 +08:00
|
|
|
#include "iris_kit.h"
|
2022-01-30 10:47:36 +08:00
|
|
|
|
2024-02-18 14:47:28 +08:00
|
|
|
#include "iot_hub.h"
|
|
|
|
|
|
2024-02-03 14:06:52 +08:00
|
|
|
|
|
|
|
|
// external variable declarations
|
|
|
|
|
extern int g_runtime_env;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// public variable definitions
|
2024-01-11 13:00:45 +08:00
|
|
|
String g_mqtt_server = "";
|
2022-02-16 11:28:46 +08:00
|
|
|
String g_product_key = "";
|
|
|
|
|
String g_device_name = "";
|
|
|
|
|
String g_device_secret = "";
|
2024-02-15 18:27:27 +08:00
|
|
|
String g_device_token = "";
|
2024-01-11 13:00:45 +08:00
|
|
|
|
|
|
|
|
String g_mqtt_client_id = "";
|
|
|
|
|
String g_mqtt_user_name = "";
|
|
|
|
|
String g_mqtt_password = "";
|
2022-03-13 20:04:04 +08:00
|
|
|
String g_upstream_topic = "";
|
2022-03-19 15:53:39 +08:00
|
|
|
String g_downstream_topic = "";
|
2024-02-12 19:26:03 +08:00
|
|
|
|
|
|
|
|
String g_aliot_region = "cn-shanghai";
|
2024-01-11 13:00:45 +08:00
|
|
|
int g_mqtt_port = 1883;
|
2022-01-12 17:24:18 +08:00
|
|
|
|
2024-01-11 13:00:45 +08:00
|
|
|
int g_app_id = 0;
|
2024-02-19 15:20:42 +08:00
|
|
|
mqtt_type_t g_mqtt_type = MQTT_TYPE_MAX;
|
|
|
|
|
boolean g_subscribed = false;
|
2024-04-30 09:51:41 +08:00
|
|
|
unsigned long last_check_time = 0UL;
|
2024-02-03 14:06:52 +08:00
|
|
|
|
|
|
|
|
// private variable definitions
|
2022-03-19 15:53:39 +08:00
|
|
|
static bool downstream_topic_subscribed = false;
|
2022-01-12 17:24:18 +08:00
|
|
|
|
2024-02-03 14:06:52 +08:00
|
|
|
|
|
|
|
|
// private function declarations
|
2024-10-25 17:33:38 +08:00
|
|
|
static void irisIoTCallback(char *topic, uint8_t *data, uint32_t length);
|
2022-02-16 11:28:46 +08:00
|
|
|
|
|
|
|
|
static int iot_retry = 0;
|
|
|
|
|
|
2024-02-19 15:20:42 +08:00
|
|
|
static PubSubClient g_mqtt_client(wifi_client);
|
2024-01-11 13:00:45 +08:00
|
|
|
|
2024-02-03 14:06:52 +08:00
|
|
|
|
|
|
|
|
// public function definitions
|
2024-10-25 17:33:38 +08:00
|
|
|
int connectIot() {
|
2022-03-19 15:53:39 +08:00
|
|
|
downstream_topic_subscribed = false;
|
2024-01-11 13:00:45 +08:00
|
|
|
int conn_ret = -1;
|
2022-05-03 18:48:48 +08:00
|
|
|
|
2024-02-03 14:06:52 +08:00
|
|
|
if (g_runtime_env == RUNTIME_DEBUG) {
|
2022-05-03 18:48:48 +08:00
|
|
|
g_upstream_topic = "/" + g_product_key + "/" + g_device_name + TOPIC_UPSTREAM_DEV;
|
|
|
|
|
g_downstream_topic = "/" + g_product_key + "/" + g_device_name + TOPIC_DOWNSTREAM_DEV;
|
2024-02-03 14:06:52 +08:00
|
|
|
} else if (g_runtime_env == RUNTIME_RELEASE) {
|
2022-05-03 18:48:48 +08:00
|
|
|
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 {
|
2024-10-25 17:33:38 +08:00
|
|
|
ERRORF("Release key is not supported yet\n");
|
2024-01-11 13:00:45 +08:00
|
|
|
factoryReset();
|
|
|
|
|
return -1;
|
2022-05-03 18:48:48 +08:00
|
|
|
}
|
|
|
|
|
|
2024-02-18 14:47:28 +08:00
|
|
|
// g_mqtt_user_name act as clientId for aliot and as userName for EMQX
|
2024-01-11 13:00:45 +08:00
|
|
|
g_mqtt_user_name = getDeviceID();
|
|
|
|
|
|
2024-02-18 14:47:28 +08:00
|
|
|
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());
|
2024-02-19 15:20:42 +08:00
|
|
|
conn_ret = connectToAliot(g_mqtt_client);
|
2024-01-11 13:00:45 +08:00
|
|
|
|
2024-02-18 14:47:28 +08:00
|
|
|
if (0 != conn_ret) {
|
2025-01-03 09:42:38 +08:00
|
|
|
INFOF("Try connecting to EMQX %s:%d, client_id = %s, user_name = %s, password.size = %d\n",
|
2024-02-18 14:47:28 +08:00
|
|
|
g_mqtt_server.c_str(), g_mqtt_port,
|
|
|
|
|
g_mqtt_client_id.c_str(), g_mqtt_user_name.c_str(), g_mqtt_password.length());
|
2024-02-19 15:20:42 +08:00
|
|
|
conn_ret = connectToEMQXBroker(g_mqtt_client);
|
|
|
|
|
if (0 == conn_ret) {
|
|
|
|
|
g_mqtt_type = MQTT_TYPE_EMQX;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
g_mqtt_type = MQTT_TYPE_ALIOT;
|
|
|
|
|
|
2024-02-18 14:47:28 +08:00
|
|
|
}
|
2022-03-19 15:53:39 +08:00
|
|
|
|
2024-01-11 13:00:45 +08:00
|
|
|
if (0 != conn_ret) {
|
2024-10-25 17:33:38 +08:00
|
|
|
ERRORF("Something may went wrong with your credential, please retry connect to Wifi...\n");
|
2024-01-11 13:00:45 +08:00
|
|
|
factoryReset();
|
|
|
|
|
return -1;
|
2022-03-19 15:53:39 +08:00
|
|
|
}
|
2024-01-11 13:00:45 +08:00
|
|
|
|
2024-02-19 15:20:42 +08:00
|
|
|
if (!g_subscribed) {
|
2024-10-25 17:33:38 +08:00
|
|
|
g_mqtt_client.setCallback(iotCallback);
|
2024-02-19 15:20:42 +08:00
|
|
|
g_mqtt_client.subscribe(g_downstream_topic.c_str());
|
|
|
|
|
g_subscribed = true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 13:00:45 +08:00
|
|
|
// send connect request
|
|
|
|
|
sendIrisKitConnect();
|
|
|
|
|
|
2024-02-19 15:20:42 +08:00
|
|
|
return conn_ret;
|
2022-01-12 17:24:18 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-25 17:33:38 +08:00
|
|
|
void keepAliveIot() {
|
|
|
|
|
if (g_mqtt_client.connected()) {
|
|
|
|
|
if (MQTT_TYPE_ALIOT == g_mqtt_type) {
|
|
|
|
|
aliotKeepAlive(g_mqtt_client);
|
|
|
|
|
} else if (MQTT_TYPE_EMQX == g_mqtt_type) {
|
|
|
|
|
emqxClientKeepAlive(g_mqtt_client);
|
|
|
|
|
}
|
2024-02-19 15:20:42 +08:00
|
|
|
}
|
2024-04-30 09:51:41 +08:00
|
|
|
unsigned long current_time = millis();
|
|
|
|
|
if (current_time - last_check_time > 10000) {
|
|
|
|
|
if (!g_mqtt_client.connected()) {
|
|
|
|
|
g_mqtt_client.unsubscribe(g_downstream_topic.c_str());
|
|
|
|
|
g_subscribed = false;
|
2024-10-25 17:33:38 +08:00
|
|
|
connectIot();
|
2024-10-28 16:37:07 +08:00
|
|
|
last_check_time = current_time;
|
2024-04-30 09:51:41 +08:00
|
|
|
}
|
2024-01-11 13:00:45 +08:00
|
|
|
}
|
2022-01-12 17:24:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// not only for IRIS related topic based session
|
2024-01-09 12:33:59 +08:00
|
|
|
void sendData(const char* topic, const uint8_t *data, int length) {
|
2024-02-19 15:20:42 +08:00
|
|
|
g_mqtt_client.publish(topic, data, length);
|
2022-01-12 17:24:18 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-09 12:33:59 +08:00
|
|
|
void* getSession() {
|
2024-02-19 15:20:42 +08:00
|
|
|
return &g_mqtt_client;
|
2022-01-12 17:24:18 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-25 17:33:38 +08:00
|
|
|
void checkIot() {
|
2024-02-19 15:20:42 +08:00
|
|
|
if (g_mqtt_client.connected()) {
|
2024-10-25 17:33:38 +08:00
|
|
|
INFOF("Send iris kit heart beat\n");
|
2024-01-12 10:54:31 +08:00
|
|
|
sendIrisKitHeartBeat();
|
|
|
|
|
}
|
2024-01-11 13:00:45 +08:00
|
|
|
}
|
2024-02-19 15:20:42 +08:00
|
|
|
|
2024-10-25 17:33:38 +08:00
|
|
|
void iotCallback(char *topic, uint8_t *data, uint32_t length) {
|
|
|
|
|
INFOF("Downstream message received, topic = %s, length = %d\n", topic, length);
|
2024-02-19 15:20:42 +08:00
|
|
|
if (NULL != g_downstream_topic.c_str() && 0 == strcmp(topic, g_downstream_topic.c_str())) {
|
|
|
|
|
handleIrisKitMessage((const char*) data, length);
|
|
|
|
|
}
|
|
|
|
|
}
|