728x90
MariaDB를 설치합니다.
MariaDB는 오픈 소스의 관계형 데이터베이스 관리 시스템이다. MySQL과 동일한 소스 코드를 기반으로 하며, GPL v2 라이선스를 따른다. 오라클 소유의 현재 불확실한 MySQL의 라이선스 상태에 반발하여 만들어졌으며, 배포자는 몬티 프로그램 AB와 저작권을 공유해야 한다. 위키백과 |
MariaDB Server | mariadb.org/ |
무료 SQL DB Tool - DBeaver를 설치합니다.
무료 SQL DT Tool -- DBeaver 사용법 | miniweb4u.tistory.com/225 |
DBeaver Community | dbeaver.io/download/ |
MariaDB에 smartfarm 데이터베이스를 생성합니다.
Columns를 추가합니다.
CREATE TABLE `sensor` (
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`temp` int(11) DEFAULT 0,
`humidity` int(10) unsigned DEFAULT 0,
`cdc` int(10) unsigned DEFAULT 0,
`water` int(10) unsigned DEFAULT 0,
`co2` int(10) unsigned DEFAULT 0,
`date` timestamp NULL DEFAULT current_timestamp(),
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8mb4
Node-RED
Node-RED에서 스마트팜보드로부터 데이터 수신를 수신합니다.
스마트팜 보드에서 와이파이를 이용하여 실시간 데이터가 업로드됩니다.
데이터 수신 간격은 10초로 설정하였습니다.
node-red-contrib-stackhero-mysql | flows.nodered.org/node/node-red-contrib-stackhero-mysql |
|
|
노드 불러오기
mariaDB와 연동기능을 포함한 노드를 불러옵니다.
노드 가져오기 메뉴 항목 선택 | |
노드불러오기 | |
select json file | |
json 파일 가져오기 |
Function "Insert an Item into DB" 내용입니다.
node-red-contrib-stackhero-mysql 노드를 이용하여 MariaDB에 접속합니다.
MariaDB 에 센서 값을 추가합니다.
Node-RED MySQL MariaDB 연동 | m.blog.naver.com/PostView.nhn?blogId=edblab&logNo=221425543479&categoryNo=12&proxyReferer=https:%2F%2Fwww.google.com%2F |
Node-RED Dashboard
스마트팜 센서데이터를 텍스트 와 그래프로 표시합니다.
DBeaver를 이용하여 데이터베이스를 확인합니다.
스마트팜 보드 소스코드
#include <Servo.h>
#include <DHT.h>
#define DHTPIN 12
#define DHTTYPE DHT11
#define SERVOPIN 9
#define LIGHTPIN 4
#define FAN_PIN 32
#define WATER_PUMP_PIN 31
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define USE_NETWORK 1
#define USE_BLUETOOTH 1
#define DEBUG 1
float temperature, humidity;
int angle = 0;
int get_co2_ppm = 0;
int RBG_R = 4;
int RBG_G = 35;
int RBG_B = 36;
int cdcValue = 0;
int waterValue = 0;
int lightOutput = 0;
int fanOutput = 0;
int waterPumpPin = 0;
int timeout = 0;
char sData[64] = { 0x00, };
char rData[32] = { 0x00, };
char nData[32] = { 0x00, };
int rPos = 0;
int nPos = 0;
int right = 10;
int displayToggle = 1;
//========================================================
#include "WiFiEsp.h"
//char ssid[] = "U+Net6AD4"; // your network SSID (name)
//char pass[] = "1159001503"; // your network password
//char ssid[] = "blueinno"; // your network SSID (name)
//char pass[] = "12345678**"; // your network password
char ssid[] = "ZEROWIN"; // your network SSID (name)
char pass[] = "zzzzzzzz"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
WiFiEspServer server_f(400);
//========================================================
DHT dht(DHTPIN, DHTTYPE);
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void printLCD(int col, int row , char *str) {
for(int i=0 ; i < strlen(str) ; i++){
lcd.setCursor(col+i , row);
lcd.print(str[i]);
}}
void printWifiStatus(){
#if 1 // #if DEBUG
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
#endif
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
delay(10);
#if 1 // #if DEBUG
Serial.print("IP Address: ");
Serial.println(ip);
#endif
char ipno2[26] ;
sprintf(ipno2, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
printLCD(0, 1, ipno2);
// print the received signal strength
long rssi = WiFi.RSSI();
#if 1 // #if DEBUG
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
#endif
}
void setup() {
pinMode(LIGHTPIN, OUTPUT);
pinMode(FAN_PIN, OUTPUT);
pinMode(WATER_PUMP_PIN, OUTPUT);
pinMode(RBG_R,OUTPUT);
pinMode(RBG_G,OUTPUT);
pinMode(RBG_B,OUTPUT);
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
dht.begin();
analogWrite(LIGHTPIN, 255);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
lcd.init();
lcd.backlight();
printLCD(0, 0, "MMB_SmartFarm");
printLCD(0, 1, "NETWORKING...");
#if USE_NETWORK
// initialize serial for ESP module
Serial2.begin(9600);
// initialize ESP module
WiFi.init(&Serial2);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
#if DEBUG
Serial.println("WiFi shield not present");
#endif
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
#if DEBUG
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
#endif
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
#if DEBUG
Serial.println("You're connected to the network");
#endif
printWifiStatus(); // display IP address on LCD
delay(2000);
server_f.begin();
#endif
#if DEBUG
Serial.println("START");
#endif
}
void loop() {
// put your main code here, to run repeatedly:
timeout += 1;
if(timeout % 10 == 0) {
cdcValue = analogRead(0);
cdcValue /= 10;
//Serial.print(cdcValue); Serial.print(",");
waterValue = analogRead(1);
waterValue /= 10;
//Serial.print(waterValue); Serial.print(",");
humidity = dht.readHumidity();
temperature = dht.readTemperature();
lcd.clear();
displayToggle = !displayToggle;
if(displayToggle == 1) {
memset(sData, 0x00, 64);
sprintf(sData, "temp %02dC humi %02d%%", (int)temperature,
(int)humidity);
printLCD(0, 0, sData);
memset(sData, 0x00, 64);
sprintf(sData, "cdc%-04d soil%-04d", cdcValue, waterValue);
printLCD(0, 1, sData);
}
else {
memset(sData, 0x00, 64);
sprintf(sData, "temp %02dC humi %02d%%", (int)temperature,
(int)humidity);
printLCD(0, 0, sData);
memset(sData, 0x00, 64);
sprintf(sData, "co2 %d ppm", get_co2_ppm);
printLCD(0, 1, sData);
}
sprintf(sData, "{ \"temp\":%02d,\"humidity\":%02d,\"cdc\":%-04d,\"water\":%-04d,\"co2\":%-04d }",
(int)temperature, (int)humidity,
cdcValue, waterValue, get_co2_ppm);
// Serial.println(sData);
Serial1.println(sData);
}
while(0 < Serial1.available()) {
char ch = Serial1.read();
rData[rPos] = ch; rPos += 1;
Serial.print(ch);
if(ch == '\n')
{
#if DEBUG
Serial.print("rPos=");
Serial.print(rPos);
Serial.print(" ");
Serial.println(rData);
#endif
if(memcmp(rData, "C_S-", 4) == 0)
{
if(rData[4] == '0') angle = 10;
else angle = 80;
servo.attach(SERVOPIN);
servo.write(angle);
delay(500);
servo.detach();
#if DEBUG
Serial.print("server_f_MOTOR=");
Serial.println(angle);
#endif
}
if(memcmp(rData, "C_F-", 4) == 0)
{
if(rData[4] == '0') digitalWrite(FAN_PIN, 0);
else digitalWrite(FAN_PIN, 1);
#if DEBUG
Serial.print("FAN=");
Serial.println(rData[4]);
#endif
}
if(memcmp(rData, "C_L-", 4) == 0)
{
int light = atoi(rData+4);
analogWrite(LIGHTPIN, (int)(25 * light));
#if DEBUG
Serial.print("LIGHT=");
Serial.println(25 * light); // light);
#endif
}
if(memcmp(rData, "C_W-", 4) == 0)
{
if(rData[4] == '0') digitalWrite(WATER_PUMP_PIN, 0);
else digitalWrite(WATER_PUMP_PIN, 1);
#if DEBUG
Serial.print("WATER=");
Serial.println(rData[4]);
#endif
}
rPos = 0;
memset(rData, 0x00,32);
break;
}
delay(10);
}
#if USE_NETWORK
WiFiEspClient c = server_f.available();
if(c)
{
#if DEBUG
Serial.println("N#RECV: ");
#endif
boolean bDataRead = false;
nPos = 0;
while (0 < c.available()) {
char ch = c.read();
#if DEBUG
Serial.write(ch);
#endif
bDataRead = true;
nData[nPos] = ch; nPos += 1;
}
Serial.print("nData=");
Serial.println(nData);
if(bDataRead == true) {
#if DEBUG
Serial.println();
#endif
// sprintf(sData, "timeout %d", timeout);
c.print(sData);
if(5 <= nPos)
{
#if DEBUG
Serial.println(nData);
#endif
if(memcmp(nData, "C_S-", 4) == 0)
{
if(nData[4] == '0') angle = 10;
else angle = 80;
servo.attach(SERVOPIN);
servo.write(angle);
delay(500);
servo.detach();
#if DEBUG
Serial.print("N#server_f_MOTOR=");
Serial.println(angle);
#endif
}
if(memcmp(nData, "C_F-", 4) == 0)
{
if(nData[4] == '0') digitalWrite(FAN_PIN, 0);
else digitalWrite(FAN_PIN, 1);
#if DEBUG
Serial.print("N#FAN=");
Serial.println(nData[4]);
#endif
}
if(memcmp(nData, "C_L-", 4) == 0)
{
int light = atoi(nData+4);
analogWrite(LIGHTPIN, (int)(25 * light));
analogWrite(RBG_R, (int)(25 * light));
analogWrite(RBG_G, (int)(25 * light));
analogWrite(RBG_B, (int)(25 * light));
#if 1 // #if DEBUG
Serial.print("N#LIGHT=");
Serial.println(light);
#endif
}
if(memcmp(nData, "C_W-", 4) == 0)
{
if(nData[4] == '0') digitalWrite(WATER_PUMP_PIN, 0);
else digitalWrite(WATER_PUMP_PIN, 1);
#if DEBUG
Serial.print("N#WATER=");
Serial.println(nData[4]);
#endif
}
nPos = 0;
memset(nData, 0x00, 32);
}
}
delay(10);
// close the connection:
// c.stop();
// Serial.println("Client disconnected");
}
#endif
delay(100);
}
'Node-RED 우노빅보드 스마트팜 > 우노빅보드와 Node-RED 연동' 카테고리의 다른 글
UI Table in Node-RED (0) | 2021.01.30 |
---|---|
Database Date Picker (0) | 2021.01.16 |
Node-RED 스마트팜 로그 파일 저장 (0) | 2020.07.10 |
앱인벤터2 사진촬영 & FTP 업로드 (0) | 2020.07.09 |
Slider LED 밝기 제어 on AppInventor2 (0) | 2020.07.08 |