implemented downstream topic subscribe and receive

This commit is contained in:
strawmanbobi
2022-03-19 15:53:39 +08:00
parent 74e9328092
commit 0ef02dcf4b
4 changed files with 47 additions and 25 deletions

View File

@@ -148,7 +148,7 @@ int AliyunIoTSDK::mqttCheckConnect() {
return mqttStatus; return mqttStatus;
} }
void AliyunIoTSDK::begin(Client &espClient, int AliyunIoTSDK::begin(Client &espClient,
const char *_productKey, const char *_productKey,
const char *_deviceName, const char *_deviceName,
const char *_deviceSecret, const char *_deviceSecret,
@@ -189,7 +189,7 @@ void AliyunIoTSDK::begin(Client &espClient,
client->setCallback(callback); client->setCallback(callback);
#endif #endif
mqttCheckConnect(); return mqttCheckConnect();
} }
int AliyunIoTSDK::loop() { int AliyunIoTSDK::loop() {
@@ -236,6 +236,10 @@ void AliyunIoTSDK::sendCustomData(const char *topic, const uint8_t *data, int le
Serial.println(d); Serial.println(d);
} }
boolean AliyunIoTSDK::subscribe(const char* topic, int qos) {
return client->subscribe(topic, qos);
}
void AliyunIoTSDK::registerCustomCallback(MQTT_CALLBACK_SIGNATURE) { void AliyunIoTSDK::registerCustomCallback(MQTT_CALLBACK_SIGNATURE) {
client->setCallback(callback); client->setCallback(callback);
} }

View File

@@ -64,11 +64,11 @@ public:
* @param _deviceSecret : AliyunIoT device secret * @param _deviceSecret : AliyunIoT device secret
* @param _region : AliyunIoT region * @param _region : AliyunIoT region
*/ */
static void begin(Client &espClient, static int begin(Client &espClient,
const char *_productKey, const char *_productKey,
const char *_deviceName, const char *_deviceName,
const char *_deviceSecret, const char *_deviceSecret,
const char *_region); const char *_region);
/** /**
* Send data * Send data
@@ -134,9 +134,18 @@ public:
*/ */
static void sendCustomData(const char *topic, const uint8_t *data, int length); static void sendCustomData(const char *topic, const uint8_t *data, int length);
/**
* Subscribe MQTT topic for Aliot
*
* @param topic : topic in string
* @param qos : MQTT qos param
* @return if succeeded
*/
static boolean subscribe(const char* topic, int qos);
/** /**
* Register customized MQTT message callback * Register customized MQTT message callback
* *
* @param callback : callback pointer * @param callback : callback pointer
*/ */
static void registerCustomCallback(MQTT_CALLBACK_SIGNATURE); static void registerCustomCallback(MQTT_CALLBACK_SIGNATURE);

View File

@@ -18,8 +18,8 @@ platform = espressif8266
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
upload_speed = 115200 upload_speed = 115200
upload_port = COM6 upload_port = COM5
monitor_port = COM6 monitor_port = COM5
board_build.flash_mode = dout board_build.flash_mode = dout
build_flags = build_flags =
-Wno-unused-function -Wno-unused-function

View File

@@ -26,6 +26,7 @@
#include "IRbabySerial.h" #include "IRbabySerial.h"
#include "IRbabyAlink.h" #include "IRbabyAlink.h"
#include "IRbabyIRIS.h"
#include "IRbabyGlobal.h" #include "IRbabyGlobal.h"
#include "IRbaby.h" #include "IRbaby.h"
@@ -38,26 +39,31 @@ String g_device_name = "";
String g_device_secret = ""; String g_device_secret = "";
String g_region_id = "cn-shanghai"; String g_region_id = "cn-shanghai";
String g_upstream_topic = ""; String g_upstream_topic = "";
String g_downstream_topic = "";
int g_app_id = 0; int g_app_id = 0;
static bool downstream_topic_subscribed = false;
static AliyunIoTSDK iot; static AliyunIoTSDK iot;
static ep_state_t endpoint_state = FSM_IDLE; static ep_state_t endpoint_state = FSM_IDLE;
static void registerCallback(); static void registerCallback(const char* topic, int qos);
static void irisAlinkCallback(const char *topic, uint8_t *data, int length); static void irisAlinkCallback(const char *topic, uint8_t *data, int length);
static int iot_retry = 0; static int iot_retry = 0;
static void sendIrisKitHeartBeat();
void connectToAliyunIoT() { void connectToAliyunIoT() {
downstream_topic_subscribed = false;
INFOF("Try connecting to Aliyun IoT : %s, %s, %s, %s\n", 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()); g_product_key.c_str(), g_device_name.c_str(), g_device_secret.c_str(), g_region_id.c_str());
iot.begin(wifi_client, g_product_key.c_str(), g_device_name.c_str(), g_device_secret.c_str(),
g_region_id.c_str()); 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();
}
INFOLN("Aliyun IoT connect done"); INFOLN("Aliyun IoT connect done");
g_upstream_topic = g_product_key + "/" + g_device_name + "/user/iris/upstream"; g_upstream_topic = g_product_key + "/" + g_device_name + "/user/iris/upstream";
registerCallback(); g_downstream_topic = g_product_key + "/" + g_device_name + "/user/iris/downstream";
} }
void checkAlinkMQTT() { void checkAlinkMQTT() {
@@ -66,7 +72,12 @@ void checkAlinkMQTT() {
if (0 == mqttStatus) { if (0 == mqttStatus) {
iot_retry = 0; iot_retry = 0;
sendIrisKitHeartBeat(); if (false == downstream_topic_subscribed) {
registerCallback(g_downstream_topic.c_str(), 0);
downstream_topic_subscribed = true;
} else {
sendIrisKitHeartBeat();
}
} else { } else {
INFOF("Alink MQTT check failed, retry = %d\n", iot_retry); INFOF("Alink MQTT check failed, retry = %d\n", iot_retry);
iot_retry++; iot_retry++;
@@ -86,15 +97,13 @@ AliyunIoTSDK getSession() {
return iot; return iot;
} }
static void registerCallback() { static void registerCallback(const char* topic, int qos) {
iot.registerCustomCallback(irisAlinkCallback); if (iot.subscribe(topic, qos)) {
INFOLN("topic subscribed");
iot.registerCustomCallback(irisAlinkCallback);
}
} }
static void irisAlinkCallback(const char *topic, uint8_t *data, int length) { static void irisAlinkCallback(const char *topic, uint8_t *data, int length) {
INFO("IRIS Alink callback triggerd : topic = "); INFOF("IRIS downstream message : topic = %s, length = %d, data = %s\n", topic, length, (char*) data);
INFO(topic);
INFO(", data = ");
INFO((char*)data);
INFO(", length = ");
INFOLN(length);
} }