Commit 0a410e3a authored by KangMin An's avatar KangMin An
Browse files

Arduino Data Sending & Server Data Receiving.

parent f967f7cd
...@@ -5,4 +5,4 @@ ...@@ -5,4 +5,4 @@
.vscode/* .vscode/*
# Arduino File # Arduino File
arduino/ arduino/code/test/*
\ No newline at end of file \ No newline at end of file
#include <SoftwareSerial.h>
#include <DHT.h>
#include <DHT_U.h>
#define ESPPIN_TX 2 // ESP 8266 모듈의 TX 핀
#define ESPPIN_RX 3 // ESP 8266 모듈의 RX 핀
#define DHTPIN 7 // DHT11 핀
#define DHTTYPE DHT11 // DHT 모듈 종류
#define CDS_D 4 // CDS 모듈 디지털 핀
#define CDS_A A0 // CDS 모듈 아날로그 핀
uint32_t delayMS; // 센서를 읽는 지연시간
SoftwareSerial esp(ESPPIN_TX,ESPPIN_RX);
DHT_Unified dht(DHTPIN, DHTTYPE);
String SSID = "Wifi_SSID"; // Wifi - SSID
String SSPW = "Wifi_SSPW"; // Wifi - SSPW
String EUEIP = "EUE_IP"; // Web Server - IP
int EUEPORT = 8081; // Web Server - Port
// 함수 선언부
void connectESP();
void connectWifi();
void sendData(String vars);
void setup() {
pinMode(CDS_D, INPUT); // CDS모듈의 Pinmode를 Input으로 설정
Serial.begin(9600); // Serial monitor의 통신 속도 9600으로 설정
esp.begin(9600); // esp모듈의 통신 속도 9600으로 설정
connectESP(); // ESP 모듈 탐색
connectWifi(); // ESP 모듈 wifi 연결
dht.begin(); // DHT11 센서 작동 시작
Serial.println("DHT11 Unified Sensor Start.");
sensor_t sensor; // dht11 구조체 생성
// 온도센서 정보 출력
dht.temperature().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Temperature(온도)");
Serial.print("Sensor : ");Serial.println(sensor.name);
Serial.print("Drive Ver : ");Serial.println(sensor.version);
Serial.print("Unique ID : ");Serial.println(sensor.sensor_id);
Serial.print ("Max Value : "); Serial.print(sensor.max_value); Serial.println(" *C");
Serial.print ("Min Value : "); Serial.print(sensor.min_value); Serial.println(" *C");
Serial.print ("Resolution : "); Serial.print(sensor.resolution); Serial.println(" *C");
Serial.println("------------------------------------");
// 습도센서 정보 프린트
dht.humidity().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Humidity(습도)");
Serial.print ("Sensor : "); Serial.println(sensor.name);
Serial.print ("Driver Ver : "); Serial.println(sensor.version);
Serial.print ("Unique ID : "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value : "); Serial.print(sensor.max_value); Serial.println("%");
Serial.print ("Min Value : "); Serial.print(sensor.min_value); Serial.println("%");
Serial.print ("Resolution : "); Serial.print(sensor.resolution); Serial.println("%");
Serial.println("------------------------------------");
delayMS =sensor.min_delay /1000; // 센서를 읽는 시간을 최소로 설정
}
void loop() {
String input = "";
sensors_event_t event;
dht.temperature().getEvent(&event);
float temp = event.temperature;
String str_Temp = String(temp);
dht.humidity().getEvent(&event);
float humi = event.relative_humidity;
String str_Humi = String(humi);
int lights = analogRead(CDS_A);
String str_Lights = String(lights);
String ID = "Admin";
input += "id=" + ID;
input += "&temp=" + str_Temp;
input += "&humi=" + str_Humi;
input += "&lights=" + str_Lights;
Serial.println(input);
sendData(input);
}
// 함수 정의부
void connectESP(){
esp.println("AT");
Serial.println("AT Sent");
while(!esp.find("OK")){
esp.println("AT");
Serial.println("ESP8266 Not Found.");
}
Serial.println("OK Command Received.");
}
void connectWifi(){
String cmd = "AT+CWMODE=1"; // Client로 설정
esp.println(cmd);
Serial.println("Set ESP8266 to client.");
Serial.println("Connecting to Wifi...");
cmd = "AT+CWJAP=\"" + SSID + "\"," + SSPW + "\"";
esp.println(cmd);
while(!esp.find("OK"));
Serial.println("Wifi Connected");
cmd = "AT+CWJAP?";
esp.println(cmd);
Serial.write(esp.read());
}
void sendData(String input){
// ESP 모듈을 통해 Server로 데이터 전송
esp.println("AT+CIPSTART=\"TCP\",\"" + EUEIP + "\"," + EUEPORT);
if(esp.find("Error")){
Serial.println("AT+CIPSTART Error...");
}
String vars = input;
String msg = "GET /data/input?";
msg += vars;
msg += " HTTP/1.0\r\n\r\n";
esp.print("AT+CIPSEND=");
esp.println(msg.length());
delay(2000);
if(esp.find(">")){
esp.print(msg);
Serial.println("Data sent.");
delay(1000);
}
Serial.println("Connection Closed.");
esp.println("AT+CIPCLOSE");
delay(5000);
}
# Arduino 정보
## Goal
온도, 습도, 조도 값을 wifi를 통해 server로 전송하도록 하는 하드웨어 제작.
## 사용 모듈
- [아두이노 우노 R3 호환보드](https://smartstore.naver.com/mechasolution_com/products/4864858307?NaPm=ct%3Dkh5sht9x%7Cci%3Dcheckout%7Ctr%3Dppc%7Ctrx%3D%7Chk%3D263e55077065b7e821aab4cb5c2c549835202409) : 메인 보드
- [DHT11 온습도 센서](https://smartstore.naver.com/mechasolution_com/products/3126779187?NaPm=ct%3Dkh5sckx3%7Cci%3Dcheckout%7Ctr%3Dppc%7Ctrx%3D%7Chk%3D24efda3bfdd3136f882e6254c93b68e6497e1750) : 온도와 습도를 측정하기 위한 모듈
- DHT11 라이브러리 및 설명 출처 : [https://cafe.naver.com/mechawiki?iframe_url=/MyCafeIntro.nhn%3Fclubid=29397234](https://cafe.naver.com/mechawiki?iframe_url=/MyCafeIntro.nhn%3Fclubid=29397234)
- [CDS 포토셀 광 조도센서 모듈](https://smartstore.naver.com/mechasolution_com/products/2940398248?NaPm=ct%3Dkh5skvb2%7Cci%3Dcheckout%7Ctr%3Dppc%7Ctrx%3D%7Chk%3D3b2b0f3e4509d7b7987fc4d02326b431c9dc5d10) : 조도를 측정하기 위한 모듈
- CDS 포토셀 광 조도 센서 설명 출처 : [https://cafe.naver.com/mechawiki](https://cafe.naver.com/mechawiki)
- [ESP8266 와이파이 모듈](https://smartstore.naver.com/mechasolution_com/products/3412252175?NaPm=ct%3Dkh5sisz4%7Cci%3Dcheckout%7Ctr%3Dppc%7Ctrx%3D%7Chk%3D782dcfa869b54343ad9113b3089fe8b7e1455a90) : wifi 연결 모듈
- [ESP8266 와이파이 모듈 전용 어댑터](https://smartstore.naver.com/mechasolution_com/products/3448897447?NaPm=ct%3Dkh5sj8sw%7Cci%3Dcheckout%7Ctr%3Dppc%7Ctrx%3D%7Chk%3D967033f4922b6288b4279fe63965a7511c1ecf4b) : ESP8266 wifi 모듈을 쉽게 사용하기 위한 어댑터
## 사용 라이브러리
- Adafruit Sensor Library : [https://github.com/adafruit/Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor)
- DHT Sensor Library : [https://github.com/adafruit/DHT-sensor-library](https://github.com/adafruit/DHT-sensor-library)
## Arduino 참고 자료
- [https://it-g-house.tistory.com/entry/%EC%95%84%EB%91%90%EC%9D%B4%EB%85%B8Arduino-%EC%9D%B8%ED%84%B0%EB%84%B7-%ED%95%98%EA%B8%B0-Wifi-ESP-01%EC%97%B0%EA%B2%B0%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95](https://it-g-house.tistory.com/entry/%EC%95%84%EB%91%90%EC%9D%B4%EB%85%B8Arduino-%EC%9D%B8%ED%84%B0%EB%84%B7-%ED%95%98%EA%B8%B0-Wifi-ESP-01%EC%97%B0%EA%B2%B0%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95)
- [https://cafe.naver.com/mechawiki/122](https://cafe.naver.com/mechawiki/122)
import routes from "../routes"; import routes from "../routes";
export const getDataInput = (req, res) => { export const getDataInput = (req, res) => {
res.render("datainput", { pagename: "Data Input" }); console.log(req.query);
}; res.status(200).send("<p>OK</p>");
export const postDataInput = (req, res) => {
res.redirect(routes.home);
}; };
...@@ -5,7 +5,7 @@ dotenv.config(); ...@@ -5,7 +5,7 @@ dotenv.config();
const PORT = process.env.PORT || 4500; const PORT = process.env.PORT || 4500;
const handleListening = () => { const handleListening = () => {
console.log(`✅ Listening on : http://localhost:${PORT}}`); console.log(`✅ Listening on : http://localhost:${PORT}`);
}; };
app.listen(PORT, handleListening); app.listen(PORT, handleListening);
import express from "express"; import express from "express";
import routes from "../routes"; import routes from "../routes";
import { getDataInput, postDataInput } from "../controllers/dataController"; import { getDataInput } from "../controllers/dataController";
const dataRouter = express.Router(); const dataRouter = express.Router();
dataRouter.get(routes.dataInput, getDataInput); dataRouter.get(routes.dataInput, getDataInput);
dataRouter.post(routes.dataInput, postDataInput);
export default dataRouter; export default dataRouter;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment