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 pathComplex_Insert_WiFi.ino
206 lines (161 loc) · 6.41 KB
/
Complex_Insert_WiFi.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
/*********************************************************************************************************************************
Complex_Insert_WiFi.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 : complex insert
This example demonstrates how to issue an INSERT query to store data in a
table using data from variables in our sketch. In this case, we supply the
values dynamically.
This sketch simulates storing data from a sensor in a table.
For this, we will create a special database and table for testing.
The following are the SQL commands you will need to run in order to setup
your database for running this sketch.
CREATE DATABASE test_arduino;
CREATE TABLE test_arduino.hello_sensor (
num integer primary key auto_increment,
message char(40),
sensor_num integer,
value float,
recorded timestamp
);
Here we have a table that contains an auto_increment primary key, a text
field, a field to identify the sensor, the value read, and timestamp of
the recorded data.
Note: Since this sketch uses test data, we place the INSERT in the setup()
method so that it runs only once. Typically, you would have the
INSERT in the loop() method after your code to read from the sensor.
For more information and documentation, visit the wiki:
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
INSTRUCTIONS FOR USE
1) Create the database and table as shown above.
2) Change the address of the server to the IP address of the MySQL server
3) Change the user and password to a valid MySQL user and password
4) Connect a USB cable to your Arduino
5) Select the correct board and port
6) Compile and upload the sketch to your Arduino
7) Once uploaded, open Serial Monitor (use 115200 speed) and observe
8) After the sketch has run for some time, open a mysql client and issue
the command: "SELECT * FROM test_arduino.hello_sensor" to see the data
recorded. Note the field values and how the database handles both the
auto_increment and timestamp fields for us. You can clear the data with
"DELETE FROM test_arduino.hello_sensor".
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 "Credentials.h"
#include <MySQL_Generic.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 default_database[] = "test_arduino"; //"test_arduino";
char default_table[] = "hello_sensor"; //"test_arduino";
// Sample query
char INSERT_DATA[] = "INSERT INTO %s.%s (message, sensor_num, value) VALUES ('%s',%d,%s)";
char query[128];
char temperature[10];
MySQL_Connection conn((Client *)&client);
#if !( ESP32 || ESP8266 || defined(CORE_TEENSY) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7) || ( defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) ) )
char *dtostrf(double val, signed char width, unsigned char prec, char *sout)
{
char fmt[20];
sprintf(fmt, "%%%d.%df", width, prec);
sprintf(sout, fmt, val);
return sout;
}
#endif
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000); // wait for serial port to connect
MYSQL_DISPLAY1("\nStarting Complex_Insert_WiFi on", BOARD_NAME);
MYSQL_DISPLAY(MYSQL_MARIADB_GENERIC_VERSION);
// Remember to initialize your WiFi module
#if ( USING_WIFI_ESP8266_AT || USING_WIFIESPAT_LIB )
#if ( USING_WIFI_ESP8266_AT )
MYSQL_DISPLAY("Using ESP8266_AT/ESP8266_AT_WebServer Library");
#elif ( USING_WIFIESPAT_LIB )
MYSQL_DISPLAY("Using WiFiEspAT Library");
#endif
// initialize serial for ESP module
EspSerial.begin(115200);
// initialize ESP module
WiFi.init(&EspSerial);
MYSQL_DISPLAY(F("WiFi shield init done"));
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD)
{
MYSQL_DISPLAY(F("WiFi shield not present"));
// don't continue
while (true);
}
#endif
// Begin WiFi section
MYSQL_DISPLAY1("Connecting to", ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
MYSQL_DISPLAY0(".");
}
// print out info about the connection:
MYSQL_DISPLAY1("Connected to network. My IP address is:", WiFi.localIP());
MYSQL_DISPLAY3("Connecting to SQL Server @", server, ", Port =", server_port);
MYSQL_DISPLAY5("User =", user, ", PW =", password, ", DB =", default_database);
}
void runInsert()
{
// Initiate the query class instance
MySQL_Query query_mem = MySQL_Query(&conn);
if (conn.connected())
{
// Save
dtostrf(50.125, 1, 1, temperature);
sprintf(query, INSERT_DATA, default_database, default_table, "test sensor", 24, temperature);
// Execute the query
MYSQL_DISPLAY(query);
// KH, check if valid before fetching
if ( !query_mem.execute(query) )
{
MYSQL_DISPLAY("Complex Insert error");
}
else
{
MYSQL_DISPLAY("Complex Data Inserted.");
}
}
else
{
MYSQL_DISPLAY("Disconnected from Server. Can't insert.");
}
}
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);
runInsert();
conn.close(); // close the connection
}
else
{
MYSQL_DISPLAY("\nConnect failed. Trying again on next iteration.");
}
MYSQL_DISPLAY("\nSleeping...");
MYSQL_DISPLAY("================================================");
delay(60000);
}