Skip to content

Commit

Permalink
mqtt test (experimental)
Browse files Browse the repository at this point in the history
  • Loading branch information
Klaus K Wilting committed May 20, 2020
1 parent 98789a7 commit 9c0b504
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion include/mqttclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern TaskHandle_t mqttTask;
void mqtt_enqueuedata(MessageBuffer_t *message);
void mqtt_queuereset(void);
void mqtt_client_task(void *param);
int mqtt_connect(IPAddress mqtt_host, uint16_t mqtt_port);
int mqtt_connect(const char *my_host, const uint16_t my_port);
void mqtt_callback(char *topic, byte *payload, unsigned int length);
void NetworkEvent(WiFiEvent_t event);
esp_err_t mqtt_init(void);
Expand Down
38 changes: 20 additions & 18 deletions src/mqttclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

static const char TAG[] = __FILE__;

IPAddress mqtt_server_ip;

QueueHandle_t MQTTSendQueue;
TaskHandle_t mqttTask;

WiFiClient ipClient;
PubSubClient mqttClient(ipClient);
WiFiClient EthClient;
PubSubClient mqttClient(EthClient);

void NetworkEvent(WiFiEvent_t event) {
switch (event) {
Expand All @@ -24,9 +22,9 @@ void NetworkEvent(WiFiEvent_t event) {
case SYSTEM_EVENT_ETH_GOT_IP:
ESP_LOGI(TAG, "ETH MAC: %s", ETH.macAddress().c_str());
ESP_LOGI(TAG, "IPv4: %s", ETH.localIP().toString().c_str());
ESP_LOGI(TAG, "Link Speed %d Mbps %s", ETH.linkSpeed(),
ESP_LOGI(TAG, "Link Speed: %d Mbps %s", ETH.linkSpeed(),
ETH.fullDuplex() ? "full duplex" : "half duplex");
mqtt_connect(mqtt_server_ip, MQTT_PORT);
mqtt_connect(MQTT_SERVER, MQTT_PORT);
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
ESP_LOGI(TAG, "Network link disconnected");
Expand All @@ -39,29 +37,32 @@ void NetworkEvent(WiFiEvent_t event) {
}
}

int mqtt_connect(IPAddress mqtt_host, uint16_t mqtt_port) {
int mqtt_connect(const char *my_host, const uint16_t my_port) {
IPAddress mqtt_server_ip;

static String clientId = "paxcounter-" + String(random(0xffff), HEX);
ESP_LOGI(TAG, "MQTT name is %s", clientId.c_str());

// resolve server
if (WiFi.hostByName(MQTT_SERVER, mqtt_server_ip)) {
ESP_LOGI(TAG, "Attempting to connect to %s [%s]", MQTT_SERVER,
if (WiFi.hostByName(my_host, mqtt_server_ip)) {
ESP_LOGI(TAG, "Attempting to connect to %s [%s]", my_host,
mqtt_server_ip.toString().c_str());
} else {
ESP_LOGI(TAG, "Could not resolve %s", MQTT_SERVER);
ESP_LOGI(TAG, "Could not resolve %s", my_host);
return -1;
}

// attempt to connect to MQTT server
if (ipClient.connect(mqtt_host, mqtt_port)) {
if (EthClient.connect(mqtt_server_ip, my_port)) {

mqttClient.setServer(mqtt_server_ip, MQTT_PORT);
mqttClient.setServer(mqtt_server_ip, my_port);
mqttClient.setCallback(mqtt_callback);

String clientId = "Paxcounter-";
clientId += String(random(0xffff), HEX);

if (mqttClient.connect(clientId.c_str())) {
ESP_LOGI(TAG, "MQTT server connected, subscribing...");
mqttClient.publish(MQTT_OUTTOPIC, "hello world");
mqttClient.subscribe(MQTT_INTOPIC);
mqttClient.loop();
ESP_LOGI(TAG, "MQTT topic subscribed");
} else {
ESP_LOGW(TAG, "MQTT server not responding, retrying later");
Expand All @@ -88,16 +89,17 @@ void mqtt_client_task(void *param) {

// send data to mqtt server
if (mqttClient.connected()) {
snprintf(cPort, sizeof(cPort), "Port_%d", msg.MessagePort);
snprintf(cMsg, sizeof(cMsg), "%s", msg.Message);
snprintf(cPort, sizeof(cPort), "%d", msg.MessagePort);
snprintf(cMsg, sizeof(cMsg), "%0X", msg.Message);
ESP_LOGI(TAG, "Topic=%s | Message=%s", cPort, cMsg);
mqttClient.publish(cPort, cMsg);
mqttClient.loop();
ESP_LOGI(TAG, "%d byte(s) sent to MQTT", msg.MessageSize);
} else {
mqtt_enqueuedata(&msg); // re-enqueue the undelivered message
delay(10000);
// attempt to reconnect to MQTT server
mqtt_connect(mqtt_server_ip, MQTT_PORT);
mqtt_connect(MQTT_SERVER, MQTT_PORT);
}
} // while(1)
}
Expand Down

0 comments on commit 9c0b504

Please sign in to comment.