Closed ztb8r3 closed 2 years ago
Here is all the code if needed. I don't need to make adjustments to the .h files at all do I?
IPAddress server_addr(10,3,0,32); // IP of the MySQL server here char user[] = "MYUSER"; // MySQL user login username char password[] = "MYPASS"; // MySQL user login password
// Sample query char INSERT_SQL[] = "INSERT INTO test_arduino.hello_arduino (message) VALUES ('Hello, Arduino!')";
// WiFi card example char ssid[] = "MySpectrumWiFi28-2G"; // your SSID char pass[] = "smartairplane997"; // your SSID Password
WiFiClient client; // Use this for WiFi instead of EthernetClient MySQL_Connection conn(&client); MySQL_Cursor* cursor;
void setup() { Serial.begin(115200); while (!Serial); // wait for serial port to connect. Needed for Leonardo only
// Begin WiFi section Serial.printf("\nConnecting to %s", ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
// print out info about the connection: Serial.println("\nConnected to network"); Serial.print("My IP address is: "); Serial.println(WiFi.localIP());
Serial.print("Connecting to SQL... "); if (conn.connect(server_addr, 3306, user, password)) Serial.println("OK."); else Serial.println("FAILED.");
// create MySQL cursor object cursor = new MySQL_Cursor(&conn); }
void loop() { if (conn.connected()) cursor->execute(INSERT_SQL);
delay(5000); }
Please ensure the MySQL you're using is an Oracle-released MySQL. The connector may not work with forks of MySQL. Beyond that, ensure your MySQL permissions are setup correctly to allow the remote user to connect.
You also may want to follow the example in the documentation and example sketches to connect, query, then disconnect rather than holding the connection open.
Otherwise, I am not sure what the exception is or why it is being thrown. Sorry. Perhaps if you posted the output in the Serial Monitor and/or try connecting to a local MySQL server to ensure you have a viable script working on a viable board. Remember the golden rule: keep it simple. Minimizing as many things as you can will help you get a working solution faster. ;)
I experience the esp8266 needs some time between receiving the ip address and initiating the mysql connnection. (Still digging into the libraries to understand the behaviour) Try moving the connection code to the loop and/or adding some delay in between.
But a simple mistake (non-standard port, etc) is also still possible.
Here is all the code if needed. I don't need to make adjustments to the .h files at all do I?
include
// Use this for WiFi instead of Ethernet.h include
include
IPAddress server_addr(10,3,0,32); // IP of the MySQL server here char user[] = "MYUSER"; // MySQL user login username char password[] = "MYPASS"; // MySQL user login password
// Sample query char INSERT_SQL[] = "INSERT INTO test_arduino.hello_arduino (message) VALUES ('Hello, Arduino!')";
// WiFi card example char ssid[] = "MySpectrumWiFi28-2G"; // your SSID char pass[] = "smartairplane997"; // your SSID Password
WiFiClient client; // Use this for WiFi instead of EthernetClient MySQL_Connection conn(&client); MySQL_Cursor* cursor;
void setup() { Serial.begin(115200); while (!Serial); // wait for serial port to connect. Needed for Leonardo only
// Begin WiFi section Serial.printf("\nConnecting to %s", ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
// print out info about the connection: Serial.println("\nConnected to network"); Serial.print("My IP address is: "); Serial.println(WiFi.localIP());
Serial.print("Connecting to SQL... "); if (conn.connect(server_addr, 3306, user, password)) Serial.println("OK."); else Serial.println("FAILED.");
// create MySQL cursor object cursor = new MySQL_Cursor(&conn); }
void loop() { if (conn.connected()) cursor->execute(INSERT_SQL);
delay(5000); }
add a delay of 1 second if (conn.connect(server_addr, 3306, user, password)) delay(1000); else Serial.println("FAILED.");
//In loop MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); cur_mem->execute(INSERT_SQL); delete cur_mem; conn.close();
To check whether it solves your problem, you should insert the delay before the connect (after the wifi) and not after a succesful connection
‘delay(1000); If (conn.connect( etc’
To see where the code crashes you should use the ESP Exception Decoder, see here https://github.com/me-no-dev/EspExceptionDecoder When you copy the trace information ("stack" etc) into it you will see in which tab/line your code has an issue.
I am using the basic insert example on a NodeMCU and I am getting to this point:
Connecting to MySpectrumWiFi28-2G. Connected to network My IP address is: 192.168.1.5 Connecting to SQL... FAILED.
Exception (3): epc1=0x40205a06 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4023820c depc=0x00000000
ctx: cont sp: 3ffefc60 end: 3ffefea0 offset: 01a0
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 1384, room 16 tail 8 chksum 0x2d csum 0x2d v614f7c32 ~ld
I am trying to connect to a MySQL instance that is hosted by AWS. I have a MySQL workbench open and can connect to my db through it. I have a master user and pass set up. Not sure what this exception is.
Any help appreciated.