ChuckBell / MySQL_Connector_Arduino

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

Wifi and Ethernet #116

Open Newsoul693 opened 4 years ago

Newsoul693 commented 4 years ago

Hi! Now I use W5500 to connect with ESP8266 so my ESP8266 can connect both LAN cable and Wifi. But when I using Ethenet.begin(mac) without Lan cable => My code will detect LAN cable is not connect => Go to connect via Wifi by Wifi.begin(ssid, pass). The status of Wifi is availble but I can't connect to my database. Can you help me? `Ethernet.init(15); Ethernet.begin(mac_addr); if (Ethernet.hardwareStatus() != '3' || Ethernet.linkStatus() != '2') { Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); WiFi.begin("ssid", "pass"); int c = 0; while ( c < 20 ) { if (WiFi.status() == WL_CONNECTED) { Serial.println("connected"); } delay(500); Serial.print(WiFi.status()); c++; } } Serial.print(WiFi.localIP()); Serial.println("Connecting..."); void loop() { if (conn.connect(server_addr, 3306, user, password)) { Serial.println("connect");

// You would add your code here to run a query once on startup.

} else { Serial.println("Not connect"); }

delay(1000); }`

ChuckBell commented 4 years ago

I will have to confirm, but I’m almost certain you can’t use both at the same time. That aside, how are you initializing the connector? Which library are you specifying in mysql_packet.h? If you want WiFi, you’ll need to change it there, not on the fly as it were.

On Oct 12, 2019, at 2:27 AM, Newsoul693 notifications@github.com wrote:

Hi! Now I use W5500 to connect with ESP8266 so my ESP8266 can connect both LAN cable and Wifi. But when I using Ethenet.begin(mac) without Lan cable => My code will detect LAN cable is not connect => Go to connect via Wifi by Wifi.begin(ssid, pass). The status of Wifi is availble but I can't connect to my database. Can you help me? `Ethernet.init(15); Ethernet.begin(mac_addr); if (Ethernet.hardwareStatus() != '3' || Ethernet.linkStatus() != '2') { Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); WiFi.begin("ssid", "pass"); int c = 0; while ( c < 20 ) { if (WiFi.status() == WL_CONNECTED) { Serial.println("connected"); } delay(500); Serial.print(WiFi.status()); c++; } } Serial.print(WiFi.localIP()); Serial.println("Connecting..."); void loop() { if (conn.connect(server_addr, 3306, user, password)) { Serial.println("connect");

// You would add your code here to run a query once on startup. } else { Serial.println("Not connect"); }

delay(1000); }`

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

Newsoul693 commented 4 years ago
ChuckBell commented 4 years ago

The small microprocessors you’re using aren’t going to be able to perform round trips to MySQL quickly. The hardware is considerably slower than, say a PC. So, 1 transaction per second is doing well.

That said, you can try adjusting the various delays() in your sketch and the connector code. But be forewarned that the delays are there to ensure the various steps have time to be ready such as waiting for bytes in the buffer and so on. So do not be surprised if you adjust the delays only to suffer from hangs or dropped data.

In short, you may be able to sub optimize the code for your use but it isn’t going to be generic enough for any application. And, ubiquitously, YMMV.

On Sat, Oct 12, 2019 at 22:48 Newsoul693 notifications@github.com wrote:

  • Sorry for noob question, now I can connect MySql server via Lan and Wifi (LAN is higher priority, if the hardware detect cable lost connection, it will switch connect to Wifi). I just declare: WiFiClient clients; EthernetClient client; MySQL_Connection conn((Client )&client); MySQL_Connection conns((Client )&clients); to switch connect method :D.
  • Previous time, I only use Wifi to connect my database, still using Ethernet.h library and everything is OK. One more question:
  • I see the delay between get data from database always about 1 seccond. How to reduce this?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/116?email_source=notifications&email_token=AB6SHYEJ2V5OB3DJN5WRHZTQOKEAFA5CNFSM4JABPGTKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBCM4VA#issuecomment-541380180, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6SHYGPVMQF5ZBKSESRPEDQOKEAFANCNFSM4JABPGTA .

Newsoul693 commented 4 years ago

Hello! I have one more question:

ChuckBell commented 4 years ago

Hi,

I am not sure I understand.

If you are running your sketch and you unplug the Ethernet cable, the sketch will hang or fail. The connector code does not include the error handling and retry features needed to support such a condition. Unless you use a watchdog timer, you can expect hangs or inoperability until the Ethernet connection is reestablished and the board is restarted.

Was that your question?

I do not know what you mean by deleting everything related to the library. Do you mean the #include? I’d have to see your sketch to help in that case but if you don’t need the library or networking, yes, you should comment out that code.

Dr. Bell

On Oct 17, 2019, at 4:49 AM, Newsoul693 notifications@github.com wrote:

Hello! I have one more question:

When I un-plug the internet capble, the code is stopped, I try use Serial.print some where but did not show anything. I delete all code related to your library then everything is OK. Can you help me? :( — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/116?email_source=notifications&email_token=AB6SHYEJUJAKAIEYSGQ6AATQPARLFA5CNFSM4JABPGTKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBPKRRA#issuecomment-543074500, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6SHYHBBNYWAH3KK7WKOXDQPARLFANCNFSM4JABPGTA.

Newsoul693 commented 4 years ago

Yes, It's exactly my question. I write code switch connect to mysql via Wifi and Ethernet. If I remove code Select and Insert query and only keeping connection code "test_connect()", I can switch Wifi and Ethernet when I unplug Ethernet cable. I used watch dog timer by the first line and last line in loop() code but still not effect. Here is my code: `void loop() { //ESP.wdtEnable(8000); test_connect(); //Keep this line is OK MySQL_Cursor cur_memss; if (conn.connected()) { cur_memss = new MySQL_Cursor(&conn); ket_noi = true; } else if (conns.connected()) { cur_memss = new MySQL_Cursor(&conns); ket_noi = true; } else { //Code khong the ket noi ket_noi = false; } if (get_time == true && ket_noi == true) { time_t now = time(nullptr); struct tm p_tm = localtime(&now); Serial.print(":"); Serial.print(p_tm->tm_hour); Serial.print(":"); Serial.print(p_tm->tm_min); Serial.print(":"); Serial.print(p_tm->tm_sec); Serial.print(":"); Serial.print(p_tm->tm_mday); Serial.print(":"); Serial.print(p_tm->tm_mon + 1); Serial.print(":"); Serial.print(p_tm->tm_year + 1900); Serial.print(":"); Serial.println(p_tm->tm_wday + 1); get_time = false; }

if (ket_noi == true) { sprintf(query, final_select, esp); cur_memss->execute(query); column_names cols = cur_memss->get_columns(); row_values row = NULL; do { row = cur_memss->get_next_row(); //Doc gia tri tung hang if (row != NULL) { set = row->values[1]; setting = set.toInt(); mod = row->values[2]; _mode = mod.toInt(); loa = row->values[0]; load = loa.toInt(); relayState = loa.toInt(); no = row->values[3]; NoNc = no.toInt(); _time_on = row->values[4]; time_on = _time_on.toInt() 1000; _time_off = row->values[5]; time_off = _time_off.toInt() 1000; } } while (row != NULL); last_send_to_arduino = send_to_arduino; send_to_arduino = "" + set + "" + mod + "" + loa + "" + no + "" + _time_on + "" + _time_off; if (send_to_arduino != last_send_to_arduino || send_init == true) { Serial.println(send_to_arduino); send_init = false; } if (millis() >= 22000) { Serial.println("Send_to_me"); } if (Serial.available()) { receives = Serial.readStringUntil('\n'); receives.trim(); //Serial.println(receives); if (receives.charAt(0) == '^') { for (int i = 0; i < 2; i++) { trang_thai[i] = ""; } count = 0; for (int i = 1; i < receives.length(); i ++) { if (receives[i] != '^') { trang_thai[count] += receives[i]; } if (receives[i] == '^') { count += 1; } } if (count > 0) { cam_bien = trang_thai[0].toInt(); relay = trang_thai[1].toInt(); } } } if (_mode != 0) { sprintf(query, final_sensor, cam_bien, relay, esp); } else { sprintf(query, final_sensors, cam_bien, esp); } cur_memss->execute(query); sprintf(query, INSERT_TREND, esp, cam_bien, relay); cur_memss->execute(query); cur_memss->close(); delete cur_memss; }

if (digitalRead(0) == 0) { setupAP(); } //ESP.wdtFeed(); } void test_connect() { if (Ethernet.hardwareStatus() != EthernetNoHardware && Ethernet.linkStatus() != LinkOFF) {//co mang LAN conns.close(); Ethernet.begin(mac); if (mode_lan == false) { Serial.println("Lan"); if (conn.connect(server_addr, 3306, user, password)) { mode_wifi = false; mode_lan = true; delay(1000); } } } if (Ethernet.hardwareStatus() == EthernetNoHardware || Ethernet.linkStatus() == LinkOFF) { conn.close(); //WiFi.begin(esid.c_str(), epass.c_str()); if (mode_wifi == false) { Serial.println("Wifi"); if (conns.connect(server_addr, 3306, user, password)) { mode_wifi = true; mode_lan = false; delay(1000); // You would add your code here to run a query once on startup. } } } }`

Newsoul693 commented 4 years ago

I use Serial.println to debug your library and see that function curmem -> Execute stuck in my code, then I find in MySQL_packet.cpp at line 289, the code is stucked here. I changed it from while (avail_bytes < 4 ) { avail_bytes = wait_for_client(); } to int dem = 0; while (avail_bytes < 4 && dem < 10) { avail_bytes = wait_for_client(); Serial.println("123");//Just for testing dem ++; } It will show the error: Timeout waiting for client then reset the esp8266. I think maybe have other method that avoid reset. Can you help me to improve it?

ChuckBell commented 4 years ago

Yes. Comment out those lines and see what happens. If that doesn’t work, I’ve got a couple of other tricks.

On Fri, Oct 18, 2019 at 12:16 Newsoul693 notifications@github.com wrote:

I use Serial.println to debug your library and see that function curmem -> Execute stuck in my code, then I find in MySQL_packet.cpp at line 289, the code is stucked here. I changed it from while (avail_bytes < 4 ) { avail_bytes = wait_for_client(); } to int dem = 0; while (avail_bytes < 4 && dem < 10) { avail_bytes = wait_for_client(); Serial.println("123");//Just for testing dem ++; } It will show the error: Timeout waiting for client then reset the esp8266. I think maybe have other method that avoid reset. Can you help me to improve it?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/116?email_source=notifications&email_token=AB6SHYDSPLXZET3ORI5L4UDQPHOMFA5CNFSM4JABPGTKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBU7WQI#issuecomment-543816513, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6SHYC5QZLB2IJU5BKCZTTQPHOMFANCNFSM4JABPGTA .

Newsoul693 commented 4 years ago

I comment those line: while (avail_bytes < 4 ) { avail_bytes = wait_for_client(); } then show the error "Timeout waiting for client" then reset the esp8266. The Exception and stack: 08:47:33.967 -> Exception (3): 08:47:33.967 -> epc1=0x4020ef81 epc2=0x00000000 epc3=0x00000000 excvaddr=0x40031018 depc=0x00000000 08:47:34.001 -> 08:47:34.001 -> >>>stack>>> 08:47:34.001 -> 08:47:34.001 -> ctx: cont 08:47:34.001 -> sp: 3ffffd70 end: 3fffffc0 offset: 01a0 08:47:34.001 -> 3fffff10: 3ffef288 00000302 00000302 4020f070 08:47:34.001 -> 3fffff20: 3ffeecd4 3fff0c64 0000004a 4020f498 08:47:34.001 -> 3fffff30: 00000008 3fff0c64 00000050 4020ba7c 08:47:34.001 -> 3fffff40: 3ffeebdc 3ffeec34 3ffeed54 00000000 08:47:34.001 -> 3fffff50: 00000000 ff000000 00000000 3ffeec2c 08:47:34.035 -> 3fffff60: 3ffeecd4 3ffeec30 3fff0c64 40201faf 08:47:34.035 -> 3fffff70: 00000000 00000000 ff342a31 feefeffe 08:47:34.035 -> 3fffff80: 00000060 feefeffe feefeffe feefeffe 08:47:34.035 -> 3fffff90: 00000000 00000000 00000001 3ffef1e8 08:47:34.035 -> 3fffffa0: 3fffdad0 00000000 3ffef1b8 4020db28 08:47:34.035 -> 3fffffb0: feefeffe feefeffe 3ffe8568 401005b9 08:47:34.035 -> <<<stack<<< The Decoder: `Exception 3: LoadStoreError: Processor internal physical address or data error during load or store PC: 0x4020ef81: umm_assimilate_up(unsigned short) at C:\Users\USER\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266\umm_malloc\umm_malloc.cpp line 1179 EXCVADDR: 0x40031018

Decoding stack results 0x4020f070: _umm_free(void) at C:\Users\USER\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266\umm_malloc\umm_malloc.cpp line 1304 0x4020f498: free(void) at C:\Users\USER\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266\umm_malloc\umm_malloc.cpp line 1764 0x4020ba7c: MySQL_Cursor::execute(char const*, bool) at C:\Users\USER\Documents\Arduino\libraries\MySQL_Connector_Arduino\src\MySQL_Cursor.cpp line 87 0x40201faf: loop() at C:\Users\USER\Desktop\Sync\new_mysql/new_mysql.ino line 500 0x4020db28: loop_wrapper() at C:\Users\USER\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266\core_esp8266_main.cpp line 125`