forked from khoih-prog/MySQL_MariaDB_Generic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnect.ino
225 lines (171 loc) · 8.55 KB
/
Connect.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*********************************************************************************************************************************
Connect.ino
Library for communicating with a MySQL or MariaDB Server
Based on and modified from Dr. Charles A. Bell's MySQL_Connector_Arduino Library https://github.com/ChuckBell/MySQL_Connector_Arduino
to support nRF52, SAMD21/SAMD51, SAM DUE, STM32F/L/H/G/WB/MP1, ESP8266, ESP32, etc. boards using W5x00, ENC28J60, LAM8742A Ethernet,
WiFiNINA, ESP-AT, built-in ESP8266/ESP32 WiFi.
The library provides simple and easy Client interface to MySQL or MariaDB Server.
Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic
Licensed under MIT license
Version: 1.0.3
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet
(Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields
1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code.
1.0.2 K Hoang 20/08/2020 Fix crashing bug when timeout. Make code more error-proof. Drop support to ESP8266_AT_Webserver.
1.0.3 K Hoang 02/10/2020 Add support to Ethernet ENC28J60 using new EthernetENC library.
**********************************************************************************************************************************/
/*
MySQL Connector/Arduino Example : connect
This example demonstrates how to connect to a MySQL server from an
Arduino using an Arduino-compatible Ethernet shield. Note that "compatible"
means it must conform to the Ethernet class library or be a derivative
with the same classes and methods.
For more information and documentation, visit the wiki:
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
INSTRUCTIONS FOR USE
1) Change the address of the server to the IP address of the MySQL server
2) Change the user and password to a valid MySQL user and password
3) Connect a USB cable to your Arduino
4) Select the correct board and port
5) Compile and upload the sketch to your Arduino
6) Once uploaded, open Serial Monitor (use 115200 speed) and observe
If you do not see messages indicating you have a connection, refer to the
manual for troubleshooting tips. The most common issues are the server is
not accessible from the network or the user name and password is incorrect.
Note: The MAC address can be anything so long as it is unique on your network.
Created by: Dr. Charles A. Bell
*/
#include "defines.h"
#include <MySQL_Generic_Ethernet.h>
// Select the static Local IP address according to your local network
IPAddress ip(192, 168, 2, 222);
IPAddress server_addr(192, 168, 2, 112);
uint16_t server_port = 5698; //3306;
char user[] = "invited-guest"; // MySQL user login username
char password[] = "the-invited-guest"; // MySQL user login password
MySQL_Connection conn((Client *)&client);
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("\nStarting Connect on " + String(BOARD_NAME) + ", with " + String(SHIELD_TYPE));
MYSQL_LOGERROR(F("========================================="));
MYSQL_LOGERROR(F("Default SPI pinout:"));
MYSQL_LOGERROR1(F("MOSI:"), MOSI);
MYSQL_LOGERROR1(F("MISO:"), MISO);
MYSQL_LOGERROR1(F("SCK:"), SCK);
MYSQL_LOGERROR1(F("SS:"), SS);
MYSQL_LOGERROR(F("========================================="));
#if defined(ESP8266)
// For ESP8266, change for other boards if necessary
#ifndef USE_THIS_SS_PIN
#define USE_THIS_SS_PIN D2 // For ESP8266
#endif
MYSQL_LOGERROR1(F("ESP8266 setCsPin:"), USE_THIS_SS_PIN);
#if ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC )
// For ESP8266
// Pin D0(GPIO16) D1(GPIO5) D2(GPIO4) D3(GPIO0) D4(GPIO2) D8
// Ethernet 0 X X X X 0
// Ethernet2 X X X X X 0
// Ethernet3 X X X X X 0
// EthernetLarge X X X X X 0
// Ethernet_ESP8266 0 0 0 0 0 0
// D2 is safe to used for Ethernet, Ethernet2, Ethernet3, EthernetLarge libs
// Must use library patch for Ethernet, EthernetLarge libraries
Ethernet.init (USE_THIS_SS_PIN);
#elif USE_ETHERNET3
// Use MAX_SOCK_NUM = 4 for 4K, 2 for 8K, 1 for 16K RX/TX buffer
#ifndef ETHERNET3_MAX_SOCK_NUM
#define ETHERNET3_MAX_SOCK_NUM 4
#endif
Ethernet.setCsPin (USE_THIS_SS_PIN);
Ethernet.init (ETHERNET3_MAX_SOCK_NUM);
#endif //( USE_ETHERNET || USE_ETHERNET2 || USE_ETHERNET_LARGE || USE_ETHERNET_ENC )
#elif defined(ESP32)
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
#ifndef USE_THIS_SS_PIN
#define USE_THIS_SS_PIN 22 // For ESP32
#endif
MYSQL_LOGERROR1(F("ESP32 setCsPin:"), USE_THIS_SS_PIN);
// For other boards, to change if necessary
#if ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC )
// Must use library patch for Ethernet, EthernetLarge libraries
// ESP32 => GPIO2,4,5,13,15,21,22 OK with Ethernet, Ethernet2, EthernetLarge
// ESP32 => GPIO2,4,5,15,21,22 OK with Ethernet3
//Ethernet.setCsPin (USE_THIS_SS_PIN);
Ethernet.init (USE_THIS_SS_PIN);
#elif USE_ETHERNET3
// Use MAX_SOCK_NUM = 4 for 4K, 2 for 8K, 1 for 16K RX/TX buffer
#ifndef ETHERNET3_MAX_SOCK_NUM
#define ETHERNET3_MAX_SOCK_NUM 4
#endif
Ethernet.setCsPin (USE_THIS_SS_PIN);
Ethernet.init (ETHERNET3_MAX_SOCK_NUM);
#endif //( USE_ETHERNET || USE_ETHERNET2 || USE_ETHERNET_LARGE || USE_ETHERNET_ENC )
#else //defined(ESP8266)
// unknown board, do nothing, use default SS = 10
#ifndef USE_THIS_SS_PIN
#define USE_THIS_SS_PIN 10 // For other boards
#endif
MYSQL_LOGERROR3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN);
// For other boards, to change if necessary
#if ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC )
// Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries
Ethernet.init (USE_THIS_SS_PIN);
#elif USE_ETHERNET3
// Use MAX_SOCK_NUM = 4 for 4K, 2 for 8K, 1 for 16K RX/TX buffer
#ifndef ETHERNET3_MAX_SOCK_NUM
#define ETHERNET3_MAX_SOCK_NUM 4
#endif
Ethernet.setCsPin (USE_THIS_SS_PIN);
Ethernet.init (ETHERNET3_MAX_SOCK_NUM);
#endif //( USE_ETHERNET || USE_ETHERNET2 || USE_ETHERNET_LARGE || USE_ETHERNET_ENC )
#endif //defined(ESP8266)
// start the ethernet connection and the server:
// Use DHCP dynamic IP and random mac
uint16_t index = millis() % NUMBER_OF_MAC;
// Use Static IP
//Ethernet.begin(mac[index], ip);
Ethernet.begin(mac[index]);
// Just info to know how to connect correctly
MYSQL_LOGERROR(F("========================================="));
MYSQL_LOGERROR(F("Currently Used SPI pinout:"));
MYSQL_LOGERROR1(F("MOSI:"), MOSI);
MYSQL_LOGERROR1(F("MISO:"), MISO);
MYSQL_LOGERROR1(F("SCK:"), SCK);
MYSQL_LOGERROR1(F("SS:"), SS);
MYSQL_LOGERROR(F("========================================="));
Serial.print("Using mac index = ");
Serial.println(index);
Serial.print("Connected! IP address: ");
Serial.println(Ethernet.localIP());
Serial.print("Connecting to SQL Server @ ");
Serial.print(server_addr);
Serial.println(String(", Port = ") + server_port);
Serial.println(String("User = ") + user + String(", PW = ") + password);
}
void loop()
{
Serial.println("Connecting...");
//if (conn.connect(server_addr, server_port, user, password))
if (conn.connectNonBlocking(server_addr, server_port, user, password) != RESULT_FAIL)
{
Serial.println("Closing connection...");
conn.close(); // close the connection
}
else
{
Serial.println("\nConnect failed. Trying again on next iteration.");
}
Serial.println("\nSleeping...");
Serial.println("================================================");
delay(60000);
}