Arduino Uno와 Ethernet shield를 이용하여 측정한 센서값을 PC에 설치된 DB에 전송하려고 한다.

 

사용 제품

 

1.Arduino Uno

2.Arduino Ethernet shield W5100

 

선행 설치

1.Arduino IDE

2.Mysql or MariaDB

 

---------------------------------------------------------------------------------------------------

1. 아두이노 우노 보드에 아두이노 이더넷 쉴드를 적층 후, 아두이노는 PC와, 이더넷 쉴드는 랜선과 연결시켜준다.

아두이노 Uno + w5100 이더넷 쉴드

 

2. Arduino Mysql connector 라이브러리 다운로드

 

https://www.arduino.cc/reference/en/libraries/mysql-connector-arduino/

 

위 주소에서 라이브러리를 다운로드 후 IDE에 추가한다.

 

 

3. 아두이노 코드 작성

 

라이브러리에 다양한 예제파일이 있으니 쉽게 사용할 수 있다.

 

필자는 예제코드 중 basic_insert 예제를 사용하였다.

 

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
/*
  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.
 
  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_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[] = { 0xDE0xAD0xBE0xEF0xFE0xED };  
 
IPAddress server_addr(xxx,xxx,xxx,xxx);  // mysql 서버 주소를 입력
char user[] = "user";              // mysql user 입력
char password[] = "password";        // mysql password 입력
 
// Sample query
char INSERT_SQL[] = "INSERT INTO TestDB.TestTable (Num,Data) VALUES ('1','100')";
 
EthernetClient client;
MySQL_Connection conn((Client *)&client);
 
void setup() {
  Serial.begin(9600);
  while (!Serial); // wait for serial port to connect
  Ethernet.begin(mac_addr);
  Serial.println("Connecting...");
  if (conn.connect(server_addr, 3306, user, password)) {
    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;
}
cs

 

사용하는 DB의 주소, 아이디, 패스워드를 입력해주고, 사용하고자 하는 쿼리를 작성한다.

(Mac 주소는 어떤 값으로 입력해도 무방하지만 사용하는 네트워크상에서 유일해야한다.)

 

 

4.업로드 후 결과확인

 

아두이노에 업로드 후, 해당 DB에 접속하여 데이터 전송을 확인한다.

DB 데이터

 

다음에는 고정된 데이터 전송이 아닌, 계속하여 변동되는 센서값을 DB로 전송해보려고 한다.

 

아두이노에서 DB로 데이터전송

아두이노 센서값 웹페이지에서 보기

아두이노 센서값 db전송

  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기