ChuckBell / MySQL_Connector_Arduino

Database connector library for using MySQL with your Arduino projects.
331 stars 132 forks source link

Hardware Serial1 on Arduino Mega with ESP8266 using WiFiEsp library #97

Closed karagonat closed 2 years ago

karagonat commented 5 years ago

I can successfully connect to MySQL with an ESP8266 using SoftwareSerial on an Arduino Uno.

[WiFiEsp] Initializing ESP module [WiFiEsp] Initilization successful - 1.5.4 [WiFiEsp] Connected to BTHub4-5H9P Connected to network My IP address is: 192.168.1.75 Connecting... [WiFiEsp] Connecting to 192.168.1.74 Connected to server version 5.5.52-0+deb8u1 [WiFiEsp] Disconnecting 3 Disconnected.

When I try to use Hardware Serial1 on an Arduino Mega I get timeouts with access denied by the MySQL server.

Screenshot Timeout Error

The MySQL server is running on a Raspberry Pi 3B. Server Version: 5.5.52-0+deb8u1 (Raspbian).

MySQL Connector Arduino version 1.1.1

WiFiEsp by bportaluri version 2.2.2

Arduino Mega 2560 R3.

I'm using an ESP-01 module:

ESP01

Here is the version info for the ESP-01:

AT version:1.1.0.0(May 11 2016 18:09:56) SDK version:1.5.4(baaeaebb) Ai-Thinker Technology Co. Ltd. Jun 13 2016 11:29:20

I have tried this with 2 Arduino Megas and 2 ESP-01 modules. It always fails to connect to the MySQL server using Hardware Serial1. I have tried using Serial2 which also fails. I have tried a 10 second delay before the conn.connect() call.

I have also tried SoftwareSerial on the Mega which does connect successfully.

Any help or advice to get this working on Hardware Serial would be appreciated.

Here is the code I'm using:

//------------------------------------- // MySQL_connect_WiFiEsp_Mega_test

include

include

include

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(192,168,1,74); // IP of the MySQL server char user[] = "user"; // MySQL user login username char password[] = "user_passwd"; // MySQL user login password

char ssid[] = "ssid"; // SSID char pass[] = "ssid_passwd"; // SSID Password

WiFiEspClient client; MySQL_Connection conn((Client *)&client);

void setup() { Serial.begin(115200); while (!Serial);

// initialize serial for ESP module Serial1.begin(115200); // initialize ESP module WiFi.init(&Serial1);

// check for the presence of the ESP8266 if (WiFi.status() == WL_NO_SHIELD) { Serial.println("ESP8266 not present"); // don't continue while (true); }

// Begin WiFi section int status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); while(true); } else { // print out info about the connection: Serial.println("Connected to network"); IPAddress ip = WiFi.localIP(); Serial.print("My IP address is: "); Serial.println(ip); } // End WiFi section

// Connect to MySQL server Serial.println("Connecting..."); if (conn.connect(server_addr, 3306, user, password)) { delay(1000); } else Serial.println("Connection failed."); conn.close();

}

void loop() { } //-------------------------------------

Circuit connections are:

ESP-01 (Tx) -> Mega 19(Rx1) ESP-01 (Rx) <- Mega 18(Tx1) via resistor voltage divider (1k : 2.2k) ESP-01 (CH_PD) +3.3V ESP-01 powered from Mega +5v via AMS1117 +3.3v

I have also tried an external 3.3v supply for the ESP-01 with common GND to the Mega.

karagonat commented 5 years ago

I have tried inserting delays in the connect() method in MySql_Connection.cpp. This did not resolve the problem.

I have also used the print_packet() method to print out the data exchanged with the server:

if (connected) { Serial.println("Read packet 0"); read_packet(); print_packet(); Serial.println("Parse handshake"); parse_handshake_packet(); Serial.println("Send auth"); send_authentication_packet(user, password); print_packet(); Serial.println("Read packet 2"); read_packet(); print_packet(); if (check_ok_packet() != 0) { parse_error_packet(); return false; } }

Read packet 0 Packet: 0 contains 86 bytes. HEX: 53 0 0 0 A 35 2E 35 2E 35 32 2D 30 2B 64 65 62 38 75 31 0 E0 4 0 0 68 63 3A 35 3D 70 3D 7C 0 72 20 75 73 65 72 20 27 6A 6F 68 6E 27 40 27 45 53 50 5F 44 42 43 45 34 30 2E 68 6F 6D 65 27 20 28 75 73 69 6E 67 20 70 61 73 73 77 6F 72 64 3A 20 59 45 53

Send auth Packet: 1 contains 86 bytes. HEX: 3C 0 0 1 85 A6 3 0 0 0 0 1 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6A 6F 68 6E 0 14 42 14 55 A5 4 92 1D 7C AE B2 C0 DB 5F 53 72 C9 18 A A0 B2 0 FB 0 EE F6 9F BD BB EF DE FE FB FF 9F EA 5F B8 8B B5 ED D5 BD 96 FB

Read packet 2 Packet: 2 contains 81 bytes. HEX: 4E 0 0 2 FF 15 4 23 32 38 30 30 30 41 63 63 65 73 73 20 64 65 6E 69 65 64 20 66 6F 72 20 75 73 0 72 20 27 6A 6F 68 6E 27 40 27 45 53 50 5F 44 42 43 45 34 30 2E 68 6F 6D 65 27 14 0 0 0 55 A5 4 92 1D 7C AE B2 C0 DB 5F 53 72 C9 18 A A0

ChuckBell commented 5 years ago

I don't think timing is the issue. The timeout errors suggest it isn't communicating.

By the output you're seeing and the packets shown, it appears that the connector is not talking to the ESP module correctly. I wonder then if there is some incompatibility with the WiFiEspClient library and the connector. In the past, this has been a problem and it turns out there are several such ESP client libraries - not all of which are 100% compatible with the Arduino Ethernet classes, which is required for the connector.

I might suggest four things. First, try using the print_packet() with the working configurations and compare the packets. I suspect you're going to see some significant differences. Second, try another ESP library - one that mentions it is not only 100% compatible with the Arduino Ethernet classes but also works with the Mega. Third (and this is just a suggestion) try acquiring an Arduino WiFi shield and try that. Suffice to say I've seen a lot of issues with ESP modules most of which are compatibility related. Fourth and finally, consider upgrading your MySQL to 5.7 or later - be sure to use the Oracle version, not a variant. This won't fix your problem, but may eliminate issues down the road.

Ok, now a dumb question: Have you tried using pins 0 and 1 on the Mega?

karagonat commented 5 years ago

I haven't tried pins 0 and 1 on the Mega. I'll try that and your other suggestions.

Thanks very much for your reply and for making this library available and supporting it.