I'm trying to connect an Arduino Mega to my WAMP server's database using MySQL connector for attendance logging. But it shows "Connection failed".
What is my WAMP server's IP address?
I connect my laptop and Arduino with a router so:
- My laptop is 192.168.1.2
- My Arduino is 192.168.1.3
I think it is correct.
Sorry for my bad English.
/*
MySQL Connector/Arduino Example: basic insert
This example demonstrates how to issue an INSERT query to store data 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_arduino (
num integer primary key auto_increment,
message char(40),
recorded timestamp
);Here we see one database and a table with three fields; a primary key that
is an auto_increment, a string, and a timestamp. This will demonstrate how
to save a date and time of when the row was inserted, which can help you
determine when data was recorded or updated.INSTRUCTIONS FOR USE
- Create the database and table as shown above.
- Change the address of the server to the IP address of the MySQL server
- Change the user and password to a valid MySQL user and password
- Connect a USB cable to your Arduino
- Select the correct board and port
- Compile and upload the sketch to your Arduino
- Once uploaded, open Serial Monitor (use 115200 speed) and observe
- After the sketch has run for some time, open a mysql client and issue
the command: "SELECT * FROM test_arduino.hello_arduino" 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_arduino".Note: The MAC address can be anything so long as it is unique on your network.
Created by: Dr. Charles A. Bell
*/#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server_addr(192, 168, 1, 3); // IP of the MySQL server here
char user[] = "root"; // MySQL user login username
char password[] = ""; // MySQL user login password// Sample query
char INSERT_SQL[] = "INSERT INTO test_arduino.hello_arduino (message) VALUES ('Hello, Arduino!')";EthernetClient client;
MySQL_Connection conn((Client *)&client);void setup() {
Serial.begin(115200);
while (!Serial); // wait for serial port to connect
Ethernet.begin(mac_addr);
Serial.println("Connecting...");
if (conn.connect(server_addr, 3306, user, password)) {
Serial.println("Connected Successfully...");
delay(1000);
} else
Serial.println("Connection failed.");
}void loop() {
delay(2000);
Serial.println("Recording data.");
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
cur_mem->execute(INSERT_SQL);
// Note: since there are no results, we do not need to read any data
// Deleting the cursor also frees up memory used
delete cur_mem;
}