728x90
스마트팜 구현에 필요한 센서 / 제어 / 데이터 통신 를 연결하여 기본 동작 테스트를 진행합니다.
#include <SoftwareSerial.h>
#include <Servo.h>
#define DEBUG true
#define USE_NETWORK false
#define RXPIN 3
#define TXPIN 2
SoftwareSerial esp8266Serial(RXPIN,TXPIN);
// Pin 2 & 3 of Arduino as RX and TX. Connect TX and RX of ESP8266 respectively.
//=====================================================================
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 4
#define DHTTYPE DHT11
#define SERVOPIN 5
int angle = 0;
Servo servo;
#define LIGHTPIN 6
#define FAN_PIN 7
#define WATER_PUMP_PIN 8
DHT dht(DHTPIN, DHTTYPE);
// ?占쏙옙? ???? ?????? ???
int temperature, humidity;
// LCD ????占쏙옙??
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// ?占쏙옙? ?????? ?占쏙옙??? ?????? ?????
int request_humidity_temperature()
{
int err;
float h = dht.readHumidity();
float t = dht.readTemperature();
//Serial.print(h);
//Serial.print(',');
//Serial.println(t);
temperature = (int)t;
humidity = (int)h;
return err;
}
//=====================================================================
String esp8266Data(String command, const int timeout, boolean debug)
{
if (debug)
{
Serial.print("CMD: ");
Serial.println(command);
}
String response = "";
esp8266Serial.print(command);
long int time = millis();
while ( (time + timeout) > millis())
{
while (esp8266Serial.available())
{
char c = esp8266Serial.read();
response += c;
}
}
if (debug)
{
Serial.print(response);
}
return response;
}
bool bConnected = false;
LiquidCrystal_I2C lcd(0x39, 16, 2);
void printLCD(int col, int row , unsigned long value) {
char str[21] = { 0x00, };
sprintf(str, "%lu", value);
for(int i=0 ; i < strlen(str) ; i++){
lcd.setCursor(col+i , row);
lcd.print(str[i]);
}
}
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 printLCD_M(int col, int row , char * str) { // MIDDLE
int i = 0;
i = (20-col-strlen(str)) / 2;
int j = 0;
for( ; j < strlen(str) ; j++){
lcd.setCursor(col+i+j , row);
lcd.print(str[j]);
}
}
String readStr, writeStr;
void SendToClient(String s)
{
writeStr = s;
//Serial.println("\nAT+CIPSEND=" + String(writeStr.length() + 2));
//delay(100);
esp8266Serial.println("AT+CIPSEND=0," + String(writeStr.length() + 2));
// Serial.print(millis()); Serial.print("= ");
String result = "";
char ch = 0x00;
while(ch != 0x00)
{
if(0 < esp8266Serial.available()) {
ch = esp8266Serial.read();
result += ch;
}
delay(1);
};
Serial.print("result=");
Serial.println(result);
//readStr = esp8266Serial.readString();
//Serial.print(readStr);
delay(10);
//Serial.println("\n" + writeStr +"\n\r");
esp8266Serial.println(writeStr);
return;
}
void OpenSession()
{
// esp8266Data("AT+CIPSTART=\"UDP\",\"192.168.0.3\",50000\r\n", 5000, DEBUG);
esp8266Data("AT+CIPSTART=0, \"TCP\",\"192.168.0.3\",60000\r\n", 5000, DEBUG);
bConnected = true;
return;
}
void CloseSession()
{
esp8266Serial.println("AT+CIPCLOSE=0");
return;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("ESP8266 TEST");
// set the data rate for the SoftwareSerial port
lcd.begin();
printLCD(0, 0, "UNO BIGBOARD");
printLCD(0, 1, "SMARTFARM");
#if USE_NETWORK
esp8266Serial.begin(9600);
esp8266Data("AT+RST\r\n", 5000, DEBUG); // Reset the ESP8266
esp8266Data("AT+CWMODE=1\r\n", 5000, DEBUG); //Set station mode Operation
esp8266Data("AT+CWJAP=\"ZEROWIN\",\"zzzzzzzz\"\r\n", 5000, DEBUG);//Enter your WiFi network's SSID and Password.
esp8266Data("AT+CIFSR\r\n", 5000, DEBUG);//You will get the IP Address of the ESP8266 from this command.
esp8266Data("AT+CIPMUX=1\r\n", 5000, DEBUG);
esp8266Data("AT+CIPSERVER=1,500001\r\n", 5000, DEBUG);
#endif
dht.begin();
servo.attach(SERVOPIN);
pinMode(LIGHTPIN, OUTPUT);
pinMode(FAN_PIN, OUTPUT);
pinMode(WATER_PUMP_PIN, OUTPUT);
OpenSession();
}
int cdcValue = 0;
int waterValue = 0;
int lightOutput = 0;
int fanOutput = 0;
int waterPumpPin = 0;
char sendData[64] = "";
void loop() {
// put your main code here, to run repeatedly:
cdcValue = analogRead(0);
//Serial.print(cdcValue); Serial.print(",");
waterValue = analogRead(1);
//Serial.print(waterValue); Serial.print(",");
request_humidity_temperature();
sprintf(sendData, "D_%d,%d,%d,%d", temperature, humidity, cdcValue, waterValue);
SendToClient(sendData);
Serial.println(sendData);
String result2 = "";
while(0 < esp8266Serial.available()) {
char ch = esp8266Serial.read();
result2 += ch;
}
Serial.print("result2=");
Serial.println(result2);
int firstListItem = result2.indexOf("link is not");
if(firstListItem != -1) {
CloseSession();
delay(100);
OpenSession();
}
else {
// +IPD,12:1023,681,310
firstListItem = result2.indexOf("+IPD");
if(firstListItem != -1) {
firstListItem += 5;
int secondListItem = result2.substring(firstListItem).indexOf(":");
if(secondListItem != -1) {
String dataLen = result2.substring(firstListItem, firstListItem + secondListItem);
int len = dataLen.toInt();
Serial.print("dataLen=");
Serial.println(dataLen);
String data = result2.substring(firstListItem + secondListItem + 1,
firstListItem + secondListItem + 1 + len);
Serial.print("data=");
Serial.println(data);
}
}
}
angle += 10;
if(90 < angle) angle = 0;
servo.write(angle);
lightOutput = !lightOutput;
digitalWrite(LIGHTPIN, lightOutput);
fanOutput = !fanOutput;
digitalWrite(FAN_PIN, fanOutput);
waterPumpPin = !waterPumpPin;
digitalWrite(WATER_PUMP_PIN, waterPumpPin);
delay(1000);
}
시리얼 모니터에서 수집된 센서 데이터를 볼 수 있습니다.
또한 네트워크, 블루투스를 이용하여 스마트폰앱, Node-RED 웹서버로 데이터를 전송합니다.
'Node-RED 우노빅보드 스마트팜 > 우노빅보드와 Node-RED 연동' 카테고리의 다른 글
Node-RED 와 Arduino-ESP8266 TCP 통신 (0) | 2020.06.20 |
---|---|
ESP8266 ESP-01 최신 펌웨어 업데이트 (0) | 2020.06.03 |
TCP 통신 Node-RED + Arduino ESP8266 (0) | 2020.05.29 |
Node-Red 대쉬보드로 UDP 데이터 송신 (0) | 2020.05.28 |
ESP8266 연결 & 이더넷 동작 테스트 (0) | 2020.05.27 |