ChuckBell / MySQL_Connector_Arduino

Database connector library for using MySQL with your Arduino projects.
332 stars 133 forks source link

Can't connect to mySQL in combination with mqtt client #57

Closed topdancer closed 6 years ago

topdancer commented 6 years ago

Hi,

As mentioned in issue #56, I was trying to use the asynchronous mitt client with the mySQL connector on an ESP8266 system. Compiling the code was successful, but I can’t connect to mySQL, log-on immediately fails.

Please find below the sketch. It is the „FullyFeatured-ESP8266“ example supplied with the AsyncMqttClient library. I only added the two connector includes, the log-on credentials for the connector and the connect statements, these additions are marked in the sketch.

The connector log-on always fails immediately, without the normal log on delay. I did the same with the mqtt-esp8266 example from the PubSubClient library, the connector connects without any problem.

Since I see some advantages with the async client, I would like to use it.

Does anybody understand why the connector fails with the async client ?

Thanks Kurt

I use the connector 1.1.1a

My sketch:

`#include

include

include

/ added to the example /

include

include

/ added to the example end /

define WIFI_SSID "my-WLAN"

define WIFI_PASSWORD "my-pw"

define MQTT_HOST IPAddress(192, 168, 2, 102)

define MQTT_PORT 1883

AsyncMqttClient mqttClient; Ticker mqttReconnectTimer;

WiFiEventHandler wifiConnectHandler; WiFiEventHandler wifiDisconnectHandler; Ticker wifiReconnectTimer;

/ added to the example / WiFiClient client; // Use this for WiFi instead of EthernetClient MySQL_Connection conn(&client); char user[] = "arduino"; // MySQL user login username char password[] = "arduino"; // MySQL user login password / added to the example end /

void connectToWifi() { Serial.println("Connecting to Wi-Fi..."); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); }

void onWifiConnect(const WiFiEventStationModeGotIP& event) { Serial.println("Connected to Wi-Fi."); connectToMqtt(); }

void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { Serial.println("Disconnected from Wi-Fi."); mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi wifiReconnectTimer.once(2, connectToWifi); }

void connectToMqtt() { Serial.println("Connecting to MQTT..."); mqttClient.connect(); }

void onMqttConnect(bool sessionPresent) { Serial.println("Connected to MQTT."); Serial.print("Session present: "); Serial.println(sessionPresent); uint16_t packetIdSub = mqttClient.subscribe("test/lol", 2); Serial.print("Subscribing at QoS 2, packetId: "); Serial.println(packetIdSub); mqttClient.publish("test/lol", 0, true, "test 1"); Serial.println("Publishing at QoS 0"); uint16_t packetIdPub1 = mqttClient.publish("test/lol", 1, true, "test 2"); Serial.print("Publishing at QoS 1, packetId: "); Serial.println(packetIdPub1); uint16_t packetIdPub2 = mqttClient.publish("test/lol", 2, true, "test 3"); Serial.print("Publishing at QoS 2, packetId: "); Serial.println(packetIdPub2); }

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { Serial.println("Disconnected from MQTT.");

if (WiFi.isConnected()) { mqttReconnectTimer.once(2, connectToMqtt); } }

void onMqttSubscribe(uint16_t packetId, uint8_t qos) { Serial.println("Subscribe acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); Serial.print(" qos: "); Serial.println(qos); }

void onMqttUnsubscribe(uint16_t packetId) { Serial.println("Unsubscribe acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); }

void onMqttMessage(char topic, char payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { Serial.println("Publish received."); Serial.print(" topic: "); Serial.println(topic); Serial.print(" qos: "); Serial.println(properties.qos); Serial.print(" dup: "); Serial.println(properties.dup); Serial.print(" retain: "); Serial.println(properties.retain); Serial.print(" len: "); Serial.println(len); Serial.print(" index: "); Serial.println(index); Serial.print(" total: "); Serial.println(total); }

void onMqttPublish(uint16_t packetId) { Serial.println("Publish acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); }

void setup() { Serial.begin(115200); Serial.println(); Serial.println();

wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect); wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);

mqttClient.onConnect(onMqttConnect); mqttClient.onDisconnect(onMqttDisconnect); mqttClient.onSubscribe(onMqttSubscribe); mqttClient.onUnsubscribe(onMqttUnsubscribe); mqttClient.onMessage(onMqttMessage); mqttClient.onPublish(onMqttPublish); mqttClient.setServer(MQTT_HOST, MQTT_PORT);

connectToWifi();

/ added to the example / Serial.print("\nConnecting to mySQL... "); if (conn.connect( MQTT_HOST, 3306, user, password)) Serial.println("OK."); else Serial.println("FAILED."); / added to the example end /

}

void loop() { }`

ChuckBell commented 6 years ago

I’ve seen something similar when the sketch + libraries exceeds available memory. Try a systematic elimination strategy by commenting out bits of the mqtt code until the connection works. If you get down to only the bare MySQL code and it still doesn’t connect, comment out the mqtt library. If it then connects, you most likely need more memory (larger micro controller).

Sent from my iPad

On Jul 20, 2018, at 3:42 AM, topdancer notifications@github.com wrote:

Hi,

As mentioned in issue #56, I was trying to use the asynchronous mitt client with the mySQL connector on an ESP8266 system. Compiling the code was successful, but I can’t connect to mySQL, log-on immediately fails.

Please find below the sketch. It is the „FullyFeatured-ESP8266“ example supplied with the AsyncMqttClient library. I only added the two connector includes, the log-on credentials for the connector and the connect statements, these additions are marked in the sketch.

The connector log-on always fails immediately, without the normal log on delay. I did the same with the mqtt-esp8266 example from the PubSubClient library, the connector connects without any problem.

Since I see some advantages with the async client, I would like to use it.

Does anybody understand why the connector fails with the async client ?

Thanks Kurt

I use the connector 1.1.1a

My sketch:

`#include

include

include

/* added to the example /

include

include

/ added to the example end */

define WIFI_SSID "WLAN-SMF3KX"

define WIFI_PASSWORD "6892439432138709"

define MQTT_HOST IPAddress(192, 168, 2, 202)

define MQTT_PORT 1883

AsyncMqttClient mqttClient; Ticker mqttReconnectTimer;

WiFiEventHandler wifiConnectHandler; WiFiEventHandler wifiDisconnectHandler; Ticker wifiReconnectTimer;

/ added to the example / WiFiClient client; // Use this for WiFi instead of EthernetClient MySQL_Connection conn(&client); char user[] = "arduino"; // MySQL user login username char password[] = "arduino"; // MySQL user login password / added to the example end /

void connectToWifi() { Serial.println("Connecting to Wi-Fi..."); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); }

void onWifiConnect(const WiFiEventStationModeGotIP& event) { Serial.println("Connected to Wi-Fi."); connectToMqtt(); }

void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { Serial.println("Disconnected from Wi-Fi."); mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi wifiReconnectTimer.once(2, connectToWifi); }

void connectToMqtt() { Serial.println("Connecting to MQTT..."); mqttClient.connect(); }

void onMqttConnect(bool sessionPresent) { Serial.println("Connected to MQTT."); Serial.print("Session present: "); Serial.println(sessionPresent); uint16_t packetIdSub = mqttClient.subscribe("test/lol", 2); Serial.print("Subscribing at QoS 2, packetId: "); Serial.println(packetIdSub); mqttClient.publish("test/lol", 0, true, "test 1"); Serial.println("Publishing at QoS 0"); uint16_t packetIdPub1 = mqttClient.publish("test/lol", 1, true, "test 2"); Serial.print("Publishing at QoS 1, packetId: "); Serial.println(packetIdPub1); uint16_t packetIdPub2 = mqttClient.publish("test/lol", 2, true, "test 3"); Serial.print("Publishing at QoS 2, packetId: "); Serial.println(packetIdPub2); }

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { Serial.println("Disconnected from MQTT.");

if (WiFi.isConnected()) { mqttReconnectTimer.once(2, connectToMqtt); } }

void onMqttSubscribe(uint16_t packetId, uint8_t qos) { Serial.println("Subscribe acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); Serial.print(" qos: "); Serial.println(qos); }

void onMqttUnsubscribe(uint16_t packetId) { Serial.println("Unsubscribe acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); }

void onMqttMessage(char topic, char payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { Serial.println("Publish received."); Serial.print(" topic: "); Serial.println(topic); Serial.print(" qos: "); Serial.println(properties.qos); Serial.print(" dup: "); Serial.println(properties.dup); Serial.print(" retain: "); Serial.println(properties.retain); Serial.print(" len: "); Serial.println(len); Serial.print(" index: "); Serial.println(index); Serial.print(" total: "); Serial.println(total); }

void onMqttPublish(uint16_t packetId) { Serial.println("Publish acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); }

void setup() { Serial.begin(115200); Serial.println(); Serial.println();

wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect); wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);

mqttClient.onConnect(onMqttConnect); mqttClient.onDisconnect(onMqttDisconnect); mqttClient.onSubscribe(onMqttSubscribe); mqttClient.onUnsubscribe(onMqttUnsubscribe); mqttClient.onMessage(onMqttMessage); mqttClient.onPublish(onMqttPublish); mqttClient.setServer(MQTT_HOST, MQTT_PORT);

connectToWifi();

/ added to the example / Serial.print("\nConnecting to mySQL... "); if (conn.connect( MQTT_HOST, 3306, user, password)) Serial.println("OK."); else Serial.println("FAILED."); / added to the example end /

}

void loop() { }`

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

topdancer commented 6 years ago

Thanks, Chuck.

Unfortunately I can't reduce the code further down, it is a sample code that comes with the MQTT library. It takes only 26% of program memory and 42% of variable memory.

I will likely go back and use the connector only, eventually use the lightweight PubSubClient library.