mqtt test (experimental)
This commit is contained in:
parent
98789a7dd5
commit
9c0b504841
@ -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);
|
||||
|
@ -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) {
|
||||
@ -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");
|
||||
@ -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");
|
||||
@ -88,8 +89,9 @@ 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);
|
||||
@ -97,7 +99,7 @@ void mqtt_client_task(void *param) {
|
||||
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user