This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathQuery_Progmem.ino
190 lines (146 loc) · 5.41 KB
/
Query_Progmem.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
/*********************************************************************************************************************************
Query_Progmem.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
**********************************************************************************************************************************/
/*
MySQL Connector/Arduino Example : query with PROGMEM strings
This example demonstrates how to issue queries using strings stored in
PROGMEM. As you will see, you need only add a parameter to the execute()
method in the cursor class, const and PROGMEM to the string declaration
and add the #include <avr/pgmspace.h> directive.
For more information and documentation, visit the wiki:
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
NOTICE: You must download and install the World sample database to run
this sketch unaltered. See http://dev.mysql.com/doc/index-other.html.
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
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.h>
#include <avr/pgmspace.h>
#define USING_HOST_NAME true
#if USING_HOST_NAME
// Optional using hostname, and Ethernet built-in DNS lookup
char server[] = "your_account.ddns.net"; // change to your server's hostname/URL
#else
IPAddress server(192, 168, 2, 112);
#endif
uint16_t server_port = 5698; //3306;
char user[] = "invited-guest"; // MySQL user login username
char password[] = "the-invited-guest"; // MySQL user login password
// Sample query
const char PROGMEM query[] = "SELECT * FROM test_arduino.hello_arduino LIMIT 6";
MySQL_Connection conn((Client *)&client);
void initEthernet()
{
#if USE_NATIVE_ETHERNET
MYSQL_DISPLAY(F("======== USE_NATIVE_ETHERNET ========"));
#elif USE_QN_ETHERNET
MYSQL_DISPLAY(F("=========== USE_QN_ETHERNET ==========="));
#endif
#if USE_NATIVE_ETHERNET
// 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]);
MYSQL_DISPLAY(F("========================="));
MYSQL_DISPLAY1("Using mac index =", index);
MYSQL_DISPLAY1("Connected! IP address:", Ethernet.localIP());
// give the Ethernet shield 2 seconds to initialize:
delay(2000);
#else
#if USING_DHCP
// Start the Ethernet connection, using DHCP
Serial.print("Initialize Ethernet using DHCP => ");
Ethernet.begin();
#else
// Start the Ethernet connection, using static IP
Serial.print("Initialize Ethernet using static IP => ");
Ethernet.begin(myIP, myNetmask, myGW);
Ethernet.setDNSServerIP(mydnsServer);
#endif
if (!Ethernet.waitForLocalIP(5000))
{
Serial.println("Failed to configure Ethernet");
if (!Ethernet.linkStatus())
{
Serial.println("Ethernet cable is not connected.");
}
// Stay here forever
while (true)
{
delay(1);
}
}
if (!Ethernet.waitForLink(5000))
{
Serial.println(F("Failed to wait for Link"));
}
else
{
Serial.print("IP Address = ");
Serial.println(Ethernet.localIP());
}
#endif
}
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000); // wait for serial port to connect
MYSQL_DISPLAY2("\nStarting Query_Progmem on", BOARD_NAME, SHIELD_TYPE);
MYSQL_DISPLAY(MYSQL_MARIADB_GENERIC_VERSION);
initEthernet();
MYSQL_DISPLAY3("Connecting to SQL Server @", server, ", Port =", server_port);
MYSQL_DISPLAY3("User =", user, ", PW =", password);
}
void runQuery()
{
MYSQL_DISPLAY("\nRunning SELECT from PROGMEM and printing results\n");
MYSQL_DISPLAY(query);
// Initiate the query class instance
MySQL_Query query_mem = MySQL_Query(&conn);
// Execute the query with the PROGMEM option
// KH, check if valid before fetching
if ( !query_mem.execute(query, true) )
{
MYSQL_DISPLAY("Querying error");
return;
}
// Show the results
query_mem.show_results();
// close the query
query_mem.close();
}
void loop()
{
MYSQL_DISPLAY("Connecting...");
//if (conn.connect(server, server_port, user, password))
if (conn.connectNonBlocking(server, server_port, user, password) != RESULT_FAIL)
{
delay(500);
runQuery();
conn.close(); // close the connection
}
else
{
MYSQL_DISPLAY("\nConnect failed. Trying again on next iteration.");
}
MYSQL_DISPLAY("\nSleeping...");
MYSQL_DISPLAY("================================================");
delay(10000);
}