mqtt_client.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * mqtt_client.c
  3. *
  4. * Created on: Jun 10, 2024
  5. * Author: jakubski
  6. */
  7. #include <stdio.h>
  8. #include "FreeRTOS.h"
  9. #include "task.h"
  10. #include "main.h"
  11. #include "cmsis_os.h"
  12. #include <string.h>
  13. #include "lwip.h"
  14. #include "lwip/api.h"
  15. #include "MQTTClient.h"
  16. #include "MQTTInterface.h"
  17. #include "node-red-config.h"
  18. #include "measurements.h"
  19. #include "cJSON.h"
  20. #include "interprocess_data.h"
  21. #include "uart_tasks.h"
  22. #define MQTT_BUFSIZE 1024
  23. char* const subscribeTopicNames[MASTER_BOARD + SLAVES_COUNT] = { "Set/0", "Set/1", "Set/2", "Set/3", "Set/4" };
  24. #define MAX_COMMANDS_IN_MQTT_PAYLOAD 17
  25. char* const topicCommands[MAX_COMMANDS_IN_MQTT_PAYLOAD] = { "fanSpeed", "motorXon", "motorYon", "diode", "motorXMaxCurrent", "motorYMaxCurrent", "clearPeakElectricalMeasurements", "mainBoardRelay",
  26. "setEncoderXValue", "setEncoderYValue", "setVoltageMeasGains", "setVoltageMeasOffsets", "setCurrentMeasGains", "setCurrentMeasOffsets", "resetSystem", "setPositionX", "setPositionY" };
  27. enum _Topics
  28. {
  29. fanSpeedTopic = 0,
  30. motorXonTopic,
  31. motorYonTopic,
  32. diodeTopic,
  33. motorXMaxCurrentTopic,
  34. motorYMaxCurrentTopic,
  35. clearPeakElectricalMeasurementsTopic,
  36. mainBoardRelayTopic,
  37. setEncoderXValue,
  38. setEncoderYValue,
  39. setVoltageMeasGains,
  40. setVoltageMeasOffsets,
  41. setCurrentMeasGains,
  42. setCurrentMeasOffsets,
  43. resetSystem,
  44. setPositionX,
  45. setPositionY
  46. };
  47. enum _BoardNoOverTopic
  48. {
  49. main_board = 0,
  50. board_1,
  51. board_2,
  52. board_3,
  53. board_4,
  54. unknownBoard
  55. };
  56. typedef enum _BoardNoOverTopic BoardNoOverTopic;
  57. extern struct netif gnetif; //extern gnetif
  58. extern UartTaskData uart1TaskData; // Board 1
  59. extern UartTaskData uart3TaskData; // Board 2
  60. extern UartTaskData uart6TaskData; // Board 3
  61. extern UartTaskData uart2TaskData; // Board 4
  62. extern UartTaskData uart8TaskData; // Debug
  63. extern osTimerId_t relay1TimerHandle;
  64. extern osTimerId_t relay2TimerHandle;
  65. extern osTimerId_t relay3TimerHandle;
  66. extern osTimerId_t relay4TimerHandle;
  67. const osThreadAttr_t mqttClientSubTaskAttr =
  68. { .name = "mqttClientSubTask", .stack_size = configMINIMAL_STACK_SIZE * 4,
  69. .priority = (osPriority_t) osPriorityNormal, };
  70. const osThreadAttr_t mqttClientPubTaskAttr =
  71. { .name = "mqttClientPsubTask", .stack_size = configMINIMAL_STACK_SIZE * 4,
  72. .priority = (osPriority_t) osPriorityNormal, };
  73. osThreadId mqttClientSubTaskHandle; //mqtt client task handle
  74. osThreadId mqttClientPubTaskHandle; //mqtt client task handle
  75. Network net; //mqtt network
  76. MQTTClient mqttClient; //mqtt client
  77. uint8_t sndBuffer[MQTT_BUFSIZE]; //mqtt send buffer
  78. uint8_t rcvBuffer[MQTT_BUFSIZE]; //mqtt receive buffer
  79. void RelayCtrl(int32_t relayNumber, int32_t relayTimeOn);
  80. void MqttClientSubTask (void* argument); // mqtt client subscribe task function
  81. void MqttClientPubTask (void* argument); // mqtt client publish task function
  82. uint32_t MqttConnectBroker (void); // mqtt broker connect function
  83. void MqttMessageArrived (MessageData* msg); // mqtt message callback function
  84. void MqttClientSubTask (void* argument) {
  85. uint8_t connectionTryCounter = 0;
  86. while (1) {
  87. // waiting for valid ip address
  88. if (gnetif.ip_addr.addr == 0 || gnetif.netmask.addr == 0 || gnetif.gw.addr == 0) // system has no valid ip address
  89. {
  90. osDelay (pdMS_TO_TICKS (1000));
  91. continue;
  92. } else {
  93. printf ("DHCP/Static IP O.K.\n");
  94. break;
  95. }
  96. }
  97. while (1) {
  98. if (!mqttClient.isconnected) {
  99. // try to connect to the broker
  100. if (MqttConnectBroker () != MQTT_SUCCESS) {
  101. osDelay (pdMS_TO_TICKS (1000));
  102. connectionTryCounter++;
  103. if (connectionTryCounter > 10) {
  104. __disable_irq ();
  105. NVIC_SystemReset ();
  106. }
  107. }
  108. } else {
  109. connectionTryCounter = 0;
  110. MQTTYield (&mqttClient, 500); // handle timer
  111. osDelay (pdMS_TO_TICKS (100));
  112. }
  113. }
  114. }
  115. void MqttClientPubTask (void* argument) {
  116. char messageBuffer[512] = { 0x00 };
  117. char topicTextBuffer[32] = { 0x00 };
  118. uint32_t bytesInBuffer = 0;
  119. uint8_t boardNumber = 0;
  120. MQTTMessage message;
  121. while (1) {
  122. if (mqttClient.isconnected) {
  123. if (is_link_up ()) {
  124. for (boardNumber = 0; boardNumber < SLAVES_COUNT; boardNumber++) {
  125. osMutexAcquire (resMeasurementsMutex, osWaitForever);
  126. RESMeasurements* resMeas = &resMeasurements[boardNumber];
  127. sprintf (topicTextBuffer, "RESmeasurments/%d", boardNumber + 1);
  128. bytesInBuffer = sprintf (messageBuffer, "{\"voltageRMS\":[%.2f, %.2f, %.2f], ", resMeas->voltageRMS[0], resMeas->voltageRMS[1], resMeas->voltageRMS[2]);
  129. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"voltagePeak\":[%.2f, %.2f, %.2f], ", resMeas->voltagePeak[0], resMeas->voltagePeak[1], resMeas->voltagePeak[2]);
  130. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"currentRMS\":[%.3f, %.3f, %.3f], ", resMeas->currentRMS[0], resMeas->currentRMS[1], resMeas->currentRMS[2]);
  131. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"currentPeak\":[%.3f, %.3f, %.3f], ", resMeas->currentPeak[0], resMeas->currentPeak[1], resMeas->currentPeak[2]);
  132. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"power\":[%.2f, %.2f, %.2f], ", resMeas->power[0], resMeas->power[1], resMeas->power[2]);
  133. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"lastSeen\": %ld}", slaveLastSeen[boardNumber]);
  134. osMutexRelease (resMeasurementsMutex);
  135. message.payload = (void*)messageBuffer;
  136. message.payloadlen = strlen (messageBuffer);
  137. MQTTPublish (&mqttClient, topicTextBuffer, &message); // publish a message
  138. }
  139. for (boardNumber = 0; boardNumber < SLAVES_COUNT + 1; boardNumber++) {
  140. if (boardNumber > 0) {
  141. osMutexAcquire (sensorsInfoMutex, osWaitForever);
  142. SesnorsInfo* sensors = &sensorsInfo[boardNumber - 1];
  143. sprintf (topicTextBuffer, "Sensors/%d", boardNumber);
  144. bytesInBuffer = sprintf (messageBuffer, "{\"pvTemperature\":[%.1f, %.1f], ", sensors->pvTemperature[0], sensors->pvTemperature[1]);
  145. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"fanVoltage\":%.2f, ", sensors->fanVoltage);
  146. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"pvEncoderX\":%.2f, ", sensors->pvEncoderX);
  147. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"pvEncoderY\":%.2f, ", sensors->pvEncoderY);
  148. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"motorXStatus\":%d, ", sensors->motorXStatus);
  149. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"motorYStatus\":%d, ", sensors->motorYStatus);
  150. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"motorXAveCurrent\":%.3f, ", sensors->motorXAveCurrent);
  151. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"motorYAveCurrent\":%.3f, ", sensors->motorYAveCurrent);
  152. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"motorXPeakCurrent\":%.3f, ", sensors->motorXPeakCurrent);
  153. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"motorYPeakCurrent\":%.3f, ", sensors->motorYPeakCurrent);
  154. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"limitXSwitchUp\":%d, ", sensors->limitXSwitchUp);
  155. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"limitXSwitchDown\":%d, ", sensors->limitXSwitchDown);
  156. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"limitXSwitchCenter\":%d, ", sensors->limitXSwitchCenter);
  157. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"limitYSwitchUp\":%d, ", sensors->limitYSwitchUp);
  158. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"limitYSwitchDown\":%d, ", sensors->limitYSwitchDown);
  159. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"limitYSwitchCenter\":%d, ", sensors->limitYSwitchCenter);
  160. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"currentXPosition\":%.2f, ", sensors->currentXPosition);
  161. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"currentYPosition\":%.2f, ", sensors->currentYPosition);
  162. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"positionXWeak\":%d, ", sensors->positionXWeak);
  163. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"positionYWeak\":%d, ", sensors->positionYWeak);
  164. bytesInBuffer += sprintf (&messageBuffer[bytesInBuffer], "\"powerSupplyFailMask\":%d}", sensors->powerSupplyFailMask);
  165. osMutexRelease (sensorsInfoMutex);
  166. } else {
  167. sprintf (topicTextBuffer, "Sensors/%d", boardNumber);
  168. uint8_t mainBoardPowerSupplyFailMask = ~((HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_4) << 1) | HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_2)) & 0x3;
  169. bytesInBuffer = sprintf (messageBuffer, "{\"powerSupplyFailMask\":%d}", mainBoardPowerSupplyFailMask);
  170. }
  171. message.payload = (void*)messageBuffer;
  172. message.payloadlen = strlen (messageBuffer);
  173. MQTTPublish (&mqttClient, topicTextBuffer, &message); // publish a message
  174. }
  175. }
  176. }
  177. osDelay (pdMS_TO_TICKS (1000));
  178. }
  179. }
  180. uint32_t MqttConnectBroker () {
  181. uint8_t boardNumber = 0;
  182. int ret;
  183. NewNetwork (&net);
  184. ret = ConnectNetwork (&net, BROKER_IP, MQTT_PORT);
  185. if (ret != MQTT_SUCCESS) {
  186. printf ("ConnectNetwork failed.\n");
  187. return -1;
  188. }
  189. MQTTClientInit (&mqttClient, &net, 1000, sndBuffer, sizeof (sndBuffer), rcvBuffer, sizeof (rcvBuffer));
  190. MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
  191. data.willFlag = 0;
  192. data.MQTTVersion = 3;
  193. data.clientID.cstring = "test_user1";
  194. data.username.cstring = "test_user1";
  195. data.password.cstring = "1234";
  196. data.keepAliveInterval = 100;
  197. data.cleansession = 1;
  198. ret = MQTTConnect (&mqttClient, &data);
  199. if (ret != MQTT_SUCCESS) {
  200. net_disconnect (&net);
  201. printf ("MQTTConnect failed. Code %d\n", ret);
  202. return ret;
  203. }
  204. for (boardNumber = 0; boardNumber < SLAVES_COUNT; boardNumber++) {
  205. ret = MQTTSubscribe (&mqttClient, subscribeTopicNames[boardNumber], QOS0, MqttMessageArrived);
  206. if (ret != MQTT_SUCCESS) {
  207. net_disconnect (&net);
  208. printf ("MQTTSubscribe failed.\n");
  209. return ret;
  210. }
  211. }
  212. printf ("MQTT_ConnectBroker O.K.\n");
  213. return MQTT_SUCCESS;
  214. }
  215. void RelayCtrl (int32_t relayNumber, int32_t relayTimeOn) {
  216. switch (relayNumber) {
  217. case 1:
  218. if (relayTimeOn > 0) {
  219. osTimerStart (relay1TimerHandle, relayTimeOn * 1000);
  220. } else {
  221. osTimerStop (relay1TimerHandle);
  222. }
  223. if (relayTimeOn != 0) {
  224. HAL_GPIO_WritePin (GPIOE, GPIO_PIN_5, GPIO_PIN_SET);
  225. } else {
  226. HAL_GPIO_WritePin (GPIOE, GPIO_PIN_5, GPIO_PIN_RESET);
  227. }
  228. break;
  229. case 2:
  230. if (relayTimeOn > 0) {
  231. osTimerStart (relay2TimerHandle, relayTimeOn * 1000);
  232. } else {
  233. osTimerStop (relay2TimerHandle);
  234. }
  235. if (relayTimeOn != 0) {
  236. HAL_GPIO_WritePin (GPIOE, GPIO_PIN_3, GPIO_PIN_SET);
  237. } else {
  238. HAL_GPIO_WritePin (GPIOE, GPIO_PIN_3, GPIO_PIN_RESET);
  239. }
  240. break;
  241. case 3:
  242. if (relayTimeOn > 0) {
  243. osTimerStart (relay3TimerHandle, relayTimeOn * 1000);
  244. } else {
  245. osTimerStop (relay3TimerHandle);
  246. }
  247. if (relayTimeOn != 0) {
  248. HAL_GPIO_WritePin (GPIOE, GPIO_PIN_4, GPIO_PIN_SET);
  249. } else {
  250. HAL_GPIO_WritePin (GPIOE, GPIO_PIN_4, GPIO_PIN_RESET);
  251. }
  252. break;
  253. case 4:
  254. if (relayTimeOn > 0) {
  255. osTimerStart (relay4TimerHandle, relayTimeOn * 1000);
  256. } else {
  257. osTimerStop (relay4TimerHandle);
  258. }
  259. if (relayTimeOn != 0) {
  260. HAL_GPIO_WritePin (GPIOE, GPIO_PIN_2, GPIO_PIN_SET);
  261. } else {
  262. HAL_GPIO_WritePin (GPIOE, GPIO_PIN_2, GPIO_PIN_RESET);
  263. }
  264. break;
  265. default: break;
  266. }
  267. }
  268. void MqttMessageArrived (MessageData* msg) {
  269. SerialProtocolCommands spCommand = spUnknown;
  270. BoardNoOverTopic topicForBoard = unknownBoard;
  271. uint8_t boardNumber = 0;
  272. MQTTMessage* message = msg->message;
  273. char topicName[32] = { 0 };
  274. memcpy (topicName, msg->topicName->lenstring.data, msg->topicName->lenstring.len);
  275. for (boardNumber = 0; boardNumber < SLAVES_COUNT; boardNumber++) {
  276. if (strcmp (topicName, subscribeTopicNames[boardNumber]) == 0) {
  277. topicForBoard = (BoardNoOverTopic)(boardNumber);
  278. break;
  279. }
  280. }
  281. if (topicForBoard == unknownBoard) {
  282. return;
  283. }
  284. cJSON* json = cJSON_Parse (message->payload);
  285. const cJSON* objectItem = NULL;
  286. InterProcessData data = { 0 };
  287. uint32_t arraySize = 0;
  288. if (topicForBoard != main_board) {
  289. for (int topicCmdNumber = 0; topicCmdNumber < MAX_COMMANDS_IN_MQTT_PAYLOAD; topicCmdNumber++) {
  290. spCommand = spUnknown;
  291. objectItem = cJSON_GetObjectItemCaseSensitive (json, topicCommands[topicCmdNumber]);
  292. if (objectItem != NULL) {
  293. switch (topicCmdNumber) {
  294. case fanSpeedTopic: spCommand = spSetFanSpeed;
  295. case motorXonTopic:
  296. if (spCommand == spUnknown) {
  297. spCommand = spSetMotorXOn;
  298. }
  299. case motorYonTopic:
  300. if (spCommand == spUnknown) {
  301. spCommand = spSetMotorYOn;
  302. }
  303. if (cJSON_IsArray (objectItem)) {
  304. data.spCommand = spCommand;
  305. arraySize = cJSON_GetArraySize (objectItem);
  306. if (arraySize == 2) {
  307. for (int i = 0; i < arraySize; i++) {
  308. cJSON* item = cJSON_GetArrayItem (objectItem, i);
  309. if (cJSON_IsNumber (item)) {
  310. data.values.integerValues.value[i] = item->valueint;
  311. }
  312. }
  313. }
  314. }
  315. break;
  316. case diodeTopic:
  317. if (cJSON_IsNumber (objectItem)) {
  318. data.spCommand = spSetDiodeOn;
  319. data.values.integerValues.value[0] = objectItem->valueint;
  320. }
  321. break;
  322. case motorXMaxCurrentTopic: spCommand = spSetmotorXMaxCurrent;
  323. case motorYMaxCurrentTopic:
  324. if (cJSON_IsNumber (objectItem)) {
  325. if (spCommand == spUnknown) {
  326. spCommand = spSetmotorYMaxCurrent;
  327. }
  328. data.spCommand = spCommand;
  329. data.values.flaotValues.value[0] = objectItem->valuedouble;
  330. }
  331. break;
  332. case clearPeakElectricalMeasurementsTopic:
  333. if (cJSON_IsNumber (objectItem)) {
  334. if (objectItem->valueint == 1) {
  335. data.spCommand = spClearPeakMeasurments;
  336. }
  337. }
  338. break;
  339. case mainBoardRelayTopic:
  340. if (cJSON_IsArray (objectItem)) {
  341. arraySize = cJSON_GetArraySize (objectItem);
  342. if (arraySize == 2) {
  343. int32_t relayNumber = -1;
  344. cJSON* item = cJSON_GetArrayItem (objectItem, 0);
  345. if (cJSON_IsNumber (item)) {
  346. relayNumber = item->valueint;
  347. }
  348. int32_t relayTimeOn = 0;
  349. item = cJSON_GetArrayItem (objectItem, 1);
  350. if (cJSON_IsNumber (item)) {
  351. relayTimeOn = item->valueint;
  352. }
  353. RelayCtrl (relayNumber, relayTimeOn);
  354. }
  355. }
  356. break;
  357. case setEncoderXValue:
  358. if (cJSON_IsNumber (objectItem)) {
  359. data.spCommand = spSetEncoderXValue;
  360. data.values.flaotValues.value[0] = objectItem->valuedouble;
  361. }
  362. break;
  363. case setEncoderYValue:
  364. if (cJSON_IsNumber (objectItem)) {
  365. data.spCommand = spSetEncoderYValue;
  366. data.values.flaotValues.value[0] = objectItem->valuedouble;
  367. }
  368. break;
  369. case setVoltageMeasGains: spCommand = spSetVoltageMeasGains;
  370. case setVoltageMeasOffsets:
  371. if (spCommand == spUnknown) {
  372. spCommand = spSetVoltageMeasOffsets;
  373. }
  374. case setCurrentMeasGains:
  375. if (spCommand == spUnknown) {
  376. spCommand = spSetCurrentMeasGains;
  377. }
  378. case setCurrentMeasOffsets:
  379. if (spCommand == spUnknown) {
  380. spCommand = spSetCurrentMeasOffsets;
  381. }
  382. if (cJSON_IsArray (objectItem)) {
  383. data.spCommand = spCommand;
  384. arraySize = cJSON_GetArraySize (objectItem);
  385. if (arraySize == 3) {
  386. for (int i = 0; i < arraySize; i++) {
  387. cJSON* item = cJSON_GetArrayItem (objectItem, i);
  388. if (cJSON_IsNumber (item)) {
  389. data.values.flaotValues.value[i] = item->valuedouble;
  390. }
  391. }
  392. }
  393. }
  394. break;
  395. case resetSystem:
  396. if (cJSON_IsNumber (objectItem)) {
  397. if (objectItem->valueint == 1) {
  398. data.spCommand = spResetSystem;
  399. break;
  400. }
  401. }
  402. break;
  403. case setPositionX:
  404. if (cJSON_IsNumber (objectItem)) {
  405. data.spCommand = spSetPositonX;
  406. data.values.flaotValues.value[0] = objectItem->valuedouble;
  407. }
  408. break;
  409. case setPositionY:
  410. if (cJSON_IsNumber (objectItem)) {
  411. data.spCommand = spSetPositonY;
  412. data.values.flaotValues.value[0] = objectItem->valuedouble;
  413. }
  414. break;
  415. default: break;
  416. }
  417. }
  418. }
  419. } else {
  420. for (int topicCmdNumber = 0; topicCmdNumber < MAX_COMMANDS_IN_MQTT_PAYLOAD; topicCmdNumber++) {
  421. objectItem = cJSON_GetObjectItemCaseSensitive (json, topicCommands[topicCmdNumber]);
  422. if (objectItem != NULL) {
  423. if (topicCmdNumber == resetSystem) {
  424. __disable_irq ();
  425. NVIC_SystemReset ();
  426. break;
  427. }
  428. }
  429. }
  430. }
  431. switch (topicForBoard) {
  432. case main_board: break;
  433. case board_1:
  434. #ifdef USE_UART8_INSTEAD_UART1
  435. if (uart8TaskData.sendCmdToSlaveQueue != NULL) {
  436. osMessageQueuePut (uart8TaskData.sendCmdToSlaveQueue, &data, 0, (TickType_t)100);
  437. }
  438. #else
  439. if (uart1TaskData.sendCmdToSlaveQueue != NULL) {
  440. osMessageQueuePut (uart1TaskData.sendCmdToSlaveQueue, &data, 0, (TickType_t)100);
  441. }
  442. #endif
  443. printf ("Send cmd to board 1\n");
  444. break;
  445. case board_2:
  446. if (uart3TaskData.sendCmdToSlaveQueue != NULL) {
  447. osMessageQueuePut (uart3TaskData.sendCmdToSlaveQueue, &data, 0, (TickType_t)100);
  448. }
  449. printf ("Send cmd to board 2\n");
  450. break;
  451. case board_3:
  452. if (uart6TaskData.sendCmdToSlaveQueue != NULL) {
  453. osMessageQueuePut (uart6TaskData.sendCmdToSlaveQueue, &data, 0, (TickType_t)100);
  454. }
  455. printf ("Send cmd to board 3\n");
  456. break;
  457. case board_4:
  458. if (uart2TaskData.sendCmdToSlaveQueue != NULL) {
  459. osMessageQueuePut (uart2TaskData.sendCmdToSlaveQueue, &data, 0, (TickType_t)100);
  460. }
  461. printf ("Send cmd to board 4\n");
  462. break;
  463. default: break;
  464. }
  465. cJSON_Delete (json);
  466. printf ("MQTT Topic:%s, MSG[%d]:%s\n", topicName, (int)message->payloadlen, (char*)message->payload);
  467. }
  468. void mqtt_cli_init (void) {
  469. mqttClientSubTaskHandle = osThreadNew (MqttClientSubTask, NULL, &mqttClientSubTaskAttr); // subscribe task
  470. mqttClientPubTaskHandle = osThreadNew (MqttClientPubTask, NULL, &mqttClientPubTaskAttr); // publish task
  471. }