Skip to content

Commit dfd1528

Browse files
Charles BellCharles Bell
authored andcommitted
Added source files
1 parent 56607b8 commit dfd1528

11 files changed

+1541
-0
lines changed

keywords.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
connect KEYWORD1
2+
execute KEYWORD2
3+
show_results KEYWORD3
4+
connected KEYWORD4
5+
field_struct KEYWORD5

libraries.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=MySQL Connector Arduino
2+
version=1.1.0
3+
author=Dr. Charles Bell
4+
maintainer=Dr. Charles Bell <chuck.bell@oracle.com>
5+
sentence=Connects Arduino using Arduino Ethernet-compatible shields including the Ethernet Shield and WiFi Shield.
6+
paragraph=You can use this library to connect your Arduino project directly to a MySQL server without using an intermediate computer or a web- or cloud-based service. Having direct access to a database server means you can store data acquired from your project as well as check values stored in tables on the server. This also means you can setup your own, local MySQL server to store your data further removing the need for Internet connectivity. If that is not an issue, you can still connect to and store data on a MySQL server via your network, Internet, or even in the cloud!
7+
category=Communication
8+
url=https://github.com/ChuckBell/MySQL_Connector_Arduino
9+
architectures=*

src/.DS_Store

6 KB
Binary file not shown.

src/MySQL_Connection.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Copyright (c) 2012, 2016 Oracle and/or its affiliates. All rights reserved.
3+
4+
This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; version 2 of the License.
7+
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with this program; if not, write to the Free Software
15+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
17+
MySQL_Connection.cpp - Library for communicating with a MySQL Server over
18+
Ethernet. (formerly mysql.cpp)
19+
20+
Change History:
21+
22+
Version 1.0.0a Created by Dr. Charles A. Bell, April 2012.
23+
Version 1.0.0b Updated by Dr. Charles A. Bell, October 2013.
24+
Version 1.0.1b Updated by Dr. Charles A. Bell, February 2014.
25+
Version 1.0.2b Updated by Dr. Charles A. Bell, April 2014.
26+
Version 1.0.3rc Updated by Dr. Charles A. Bell, March 2015.
27+
Version 1.0.4ga Updated by Dr. Charles A. Bell, July 2015.
28+
Version 1.1.0a Created by Dr. Charles A. Bell, January 2016.
29+
*/
30+
#include <Arduino.h>
31+
#include <MySQL_Connection.h>
32+
#include <MySQL_Encrypt_Sha1.h>
33+
34+
#define MAX_CONNECT_ATTEMPTS 3
35+
36+
const char CONNECTED[] PROGMEM = "Connected to server version ";
37+
const char DISCONNECTED[] PROGMEM = "Disconnected.";
38+
39+
/*
40+
connect - Connect to a MySQL server.
41+
42+
This method is used to connect to a MySQL server. It will attempt to
43+
connect to the server as a client retrying up to MAX_CONNECT_ATTEMPTS.
44+
This permits the possibility of longer than normal network lag times
45+
for wireless networks. You can adjust MAX_CONNECT_ATTEMPTS to suit
46+
your environment.
47+
48+
server[in] IP address of the server as IPAddress type
49+
port[in] port number of the server
50+
user[in] user name
51+
password[in] (optional) user password
52+
53+
Returns boolean - True = connection succeeded
54+
*/
55+
boolean MySQL_Connection::connect(IPAddress server, int port, char *user,
56+
char *password)
57+
{
58+
int connected = 0;
59+
int i = -1;
60+
61+
// Retry up to MAX_CONNECT_ATTEMPTS times 1 second apart.
62+
do {
63+
delay(1000);
64+
connected = client->connect(server, port);
65+
i++;
66+
} while (i < MAX_CONNECT_ATTEMPTS && !connected);
67+
68+
if (connected) {
69+
read_packet();
70+
parse_handshake_packet();
71+
send_authentication_packet(user, password);
72+
read_packet();
73+
if (check_ok_packet() != 0) {
74+
parse_error_packet();
75+
return false;
76+
}
77+
show_error(CONNECTED);
78+
Serial.println(server_version);
79+
free(server_version); // don't need it anymore
80+
return true;
81+
}
82+
return false;
83+
}
84+
85+
/*
86+
close - cancel the connection
87+
88+
This method closes the connection to the server and frees up any memory
89+
used in the buffer.
90+
*/
91+
void MySQL_Connection::close()
92+
{
93+
if (connected())
94+
{
95+
client->flush();
96+
client->stop();
97+
show_error(DISCONNECTED, true);
98+
}
99+
}

src/MySQL_Connection.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright (c) 2012, 2016 Oracle and/or its affiliates. All rights reserved.
3+
4+
This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; version 2 of the License.
7+
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with this program; if not, write to the Free Software
15+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
17+
MySQL_Connection.h - Library for communicating with a MySQL Server over
18+
Ethernet. (formerly mysql.h)
19+
20+
This header file defines a connection class for connecting to a MySQL
21+
server.
22+
23+
Change History:
24+
25+
Version 1.0.0a Created by Dr. Charles A. Bell, April 2012.
26+
Version 1.0.0b Updated by Dr. Charles A. Bell, October 2013.
27+
Version 1.0.1b Updated by Dr. Charles A. Bell, February 2014.
28+
Version 1.0.2b Updated by Dr. Charles A. Bell, April 2014.
29+
Version 1.0.3rc Updated by Dr. Charles A. Bell, March 2015.
30+
Version 1.0.4ga Updated by Dr. Charles A. Bell, July 2015.
31+
Version 1.1.0a Created by Dr. Charles A. Bell, January 2016.
32+
*/
33+
#ifndef MYSQL_CONNECTION_H
34+
#define MYSQL_CONNECTION_H
35+
36+
#include <MySQL_Packet.h>
37+
38+
class MySQL_Connection : public MySQL_Packet {
39+
public:
40+
MySQL_Connection(Client *client_instance) :
41+
MySQL_Packet(client_instance) {}
42+
boolean connect(IPAddress server, int port, char *user, char *password);
43+
int connected() { return client->connected(); }
44+
const char *version() { return MYSQL_VERSION_STR; }
45+
void close();
46+
};
47+
48+
#endif

0 commit comments

Comments
 (0)