아두이노 온도/습도 센서에서 (센서 모델 DHT11)에서 온도/습도 값을 읽어 mysql DBMS에 다이렉트 입력하는 소스 코드를작성 했다
아두이노 보드 종류에 따라 에더넷 및 와이파이 통신이 가능한데 두 가지 case 모두 구현했다
TCP/UDP 계층까지 connect 명령으로 연결한 후 sql문을 발행하는 구조이다
1. 에더넷 프로토콜 활용한 구현
#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <DFRobot_DHT11.h>
DFRobot_DHT11 DHT;
#define DHT11_PIN 10 // 센서 데이타가 10번 pin으로 입력됨(?)
byte mac_addr[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress server_addr(***,***,***,***); // mysql 서버 주소를 입력. 노드는 쉼표로 분리
char user[] = "*****"; // mysql user 입력
char password[] = "*****"; // mysql password 입력
// Sample query
char INSERT_SQL[] = "INSERT INTO mysql.tb_temperature_humidity (temperature,humidity) VALUES (%d,%d)";
char query[128];
//char INSERT_SQL[] = "INSERT INTO TestDB.TestTable (Temp,Humid) VALUES (%d,%d)";
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() {
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
Serial.println("Recording data.");
DHT.read(DHT11_PIN);
Serial.print("temp:");
Serial.print(DHT.temperature);
Serial.print(" humi:");
Serial.println(DHT.humidity);
int temp = DHT.temperature;
int humid = DHT.humidity;
sprintf(query, INSERT_SQL, temp, humid);
Serial.println(query);
cur_mem->execute(query);
delete cur_mem;
delay(1000);
}
2. 와이파이 프로토콜 활용한 구현
#include <WiFiS3.h> // Use this for WiFi instead of Ethernet.h
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <time.h>
#include <DFRobot_DHT11.h>
DFRobot_DHT11 DHT;
#define DHT11_PIN 10 // 센서 데이타가 10번 pin으로 입력됨(?)
//byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server_addr(***,***,***,***); // IP of the MySQL *server* here
char user[] = "*****"; // MySQL user login username
char password[] = "*****"; // MySQL user login password
// Sample query
char INSERT_SQL[] = "INSERT INTO mysql.tb_temperature_humidity (device_id,location_id,temperature,humidity,device_timer) VALUES (%d,%d,%d,%d,%d)";
char query[128];
// WiFi card example
char ssid[] = "*****"; // your SSID
char pass[] = "*****"; // your SSID Password
WiFiClient client; // Use this for WiFi instead of EthernetClient
MySQL_Connection conn((Client *)&client);
//set interval for sending messages (milliseconds)
const long interval = 60000; // 60초에 1건씩 데이타 생성
unsigned long previousMillis = 0;
void setup() {
Serial.begin(9600);
// Begin WiFi section
int status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
// print out info about the connection:
else {
Serial.println("Connected to network");
IPAddress ip = WiFi.localIP();
Serial.print("My IP address is: ");
Serial.println(ip);
}
// End WiFi section
Serial.println("Connecting...");
if (conn.connect(server_addr, 3306, user, password)) {
delay(1000);
}
else
Serial.println("Connection failed.");
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
//save the last time a message was sent
previousMillis = currentMillis;
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
DHT.read(DHT11_PIN);
Serial.print("temp:");
Serial.print(DHT.temperature);
Serial.print(" humi:");
Serial.println(DHT.humidity);
int temp = DHT.temperature;
int humid = DHT.humidity;
int device_id = 10;
int location_id = 1968;
time_t timer = time(NULL);
sprintf(query, INSERT_SQL, device_id, location_id, temp, humid, timer);
Serial.println(query);
cur_mem->execute(query);
delete cur_mem;
delay(1000);
}
}
'Life Work' 카테고리의 다른 글
[IT 서비스] 모스키토 플랫폼 (0) | 2024.07.13 |
---|---|
[IT 서비스] MQTT 프로토콜 개요 (0) | 2024.07.13 |
[IT 서비스] End To End 데이터 파이프 라인 구축 (0) | 2024.06.28 |
[파이썬] 상황에 따른 import 명령의 활용 (0) | 2024.06.28 |
[파이썬] 인스턴스 변수 vs 클래스 변수 (0) | 2024.06.28 |