adjust mqtt client
This commit is contained in:
parent
9d7f739be5
commit
07b2ad7a32
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <pubsubc.h>
|
#include <pubsubc.h>
|
||||||
#include <platformAdaption.h>
|
#include <platformAdaption.h>
|
||||||
|
#include <PontCoopScheduler.h>
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -15,21 +16,59 @@ extern const uint8_t MQTT_SOCK;
|
|||||||
client_t client;
|
client_t client;
|
||||||
mqttClient_t mqttClient;
|
mqttClient_t mqttClient;
|
||||||
|
|
||||||
|
uint8_t brokerAddress[] = { 172, 16, 2, 16 };
|
||||||
|
uint16_t brokerPort = 1883;
|
||||||
|
|
||||||
// typedef void (*callback_t)(char*, uint8_t*, uint16_t);
|
// typedef void (*callback_t)(char*, uint8_t*, uint16_t);
|
||||||
static void mqttCallback(char *topic, uint8_t *payload, uint16_t payloadLength) {
|
static void mqttCallback(char *topic, uint8_t *payload, uint16_t payloadLength) {
|
||||||
logMsg("mcb");
|
logMsg("mcb");
|
||||||
}
|
}
|
||||||
|
|
||||||
void mqttTestInit() {
|
|
||||||
logMsg("mti: initializing mqtt client");
|
|
||||||
client.sockNum = MQTT_SOCK;
|
|
||||||
mqttClientInit(&mqttClient, &client, mqttCallback);
|
|
||||||
logMsg("mti: mqtt client initialized");
|
|
||||||
|
|
||||||
logMsg("mti: connecting to broker");
|
void mqttTestHandler(void *handle) {
|
||||||
uint8_t brokerAddress[] = { 172, 16, 2, 16 };
|
static uint8_t state = 0;
|
||||||
bool res = mqttConnect(&mqttClient, brokerAddress, 1883, "mbv3gw-client", NULL, NULL, NULL, 0, false, NULL, false);
|
static uint8_t message[] = "Hello world\n\r";
|
||||||
logMsg("mti: mqttConnect returns %d", res);
|
|
||||||
|
if (isNetworkAvailable()) {
|
||||||
|
switch (state) {
|
||||||
|
case 0:
|
||||||
|
coloredMsg(LOG_YELLOW, "mth, initializing mqtt client");
|
||||||
|
client.sockNum = MQTT_SOCK;
|
||||||
|
mqttClientInit(&mqttClient, &client, mqttCallback);
|
||||||
|
coloredMsg(LOG_YELLOW, "mth: mqtt client initialized");
|
||||||
|
|
||||||
|
state = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
coloredMsg(LOG_YELLOW, "mth, connecting to broker ");
|
||||||
|
bool res = mqttConnect(&mqttClient, brokerAddress, 1883, "mbv3gw-client", NULL, NULL, NULL, 0, false, NULL, false);
|
||||||
|
coloredMsg(LOG_YELLOW, "mth, mqttConnect returns %d", res);
|
||||||
|
|
||||||
|
if (res) {
|
||||||
|
coloredMsg(LOG_YELLOW, "mth, ok, connected");
|
||||||
|
state = 2;
|
||||||
|
} else {
|
||||||
|
state = 255;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
coloredMsg(LOG_YELLOW, "mth, waiting");
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
case 255:
|
||||||
|
coloredMsg(LOG_YELLOW, "mth, error state, will stop here");
|
||||||
|
schDel(mqttTestHandler, NULL);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
coloredMsg(LOG_YELLOW, "mth, network not yet ready");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void mqttTestInit() {
|
||||||
|
schAdd(mqttTestHandler, NULL, 0, 100);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user