나의 이야기/삼성 SCSA 파이널 프로젝트
[BustaBusta | 버스타버스타] 아두이노 설계도 및 소스코드
불타는홍당무
2017. 4. 20. 23:22
#include <SPI.h> #include <Ethernet.h> //이더넷 통신을 위한 라이브러리 #include <SoftwareSerial.h> //블루투스 통신을 위한 라이브러리 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF }; //MAC Address IPAddress server(70,12,109,133); IPAddress ip(70,12,109,133); EthernetClient client; int conn; int reqInterval=3000; SoftwareSerial BTSerial(2, 3); //블루투스 모듈이 연결된 핀번호 byte buffer[1024]; //데이터 수신 버퍼 int bufferPosition; //버퍼에 기록할 위치 //각 압력센서에 연결된 핀번호 int fsrPin0 = A0; int fsrPin1 = A1; int fsrPin2 = A2; int fsrPin3 = A3; int fsrPin4 = A4; //각 압력센서에서 읽은 데이터를 저장할 변수 int fsrReading0 = 0; int fsrReading1 = 0; int fsrReading2 = 0; int fsrReading3 = 0; int fsrReading4 = 0; //좌석에 사람 착석여부를 판단하는 압력센서의 값 int threshold = 500; //잔여좌석에 대한 정보를 저장하는 배열. 전체좌석은 45석. int seatStatus[45] = {0,0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; //각각의 LED와 연결된 핀번호 int redLed = 6; int redLed2 = 7; //스피커와 연결된 핀번호 int speaker = 5; //하차벨과 연결된 핀번호 int redState = 0; int redState2 = 0; //냉/난방장치와 연결된 핀번호 int motorPin = 9; //냉/난방장치의 on/off상태 int motorState = 0; int motorM; unsigned long time = 0; boolean connectStatus = false; void sendRequest() { if((millis() - time) < reqInterval) { return; } if(!client.connected()) { client.stop(); if(client.connect(server, 80)) { Serial.println("connected"); } else { Serial.println("connection failed"); } } else { if (client.connected()) { //서버로 HTTP Request 전송 client.print("GET /Busta/saveSeat.do?routeid=1111&seats="); //좌석에 대한 정보를 가진 배열(seatStatus)을 문자열로 변환하여 seats라는 값으로 전송 for(int i=0; i<45; i++) { client.print(seatStatus[i]); } client.println(" HTTP/1.1"); client.println("Host: 70.12.109.137"); client.println("Connection: close"); client.println(); } else { Serial.println("skip this turn.."); } } } void beep(unsigned char delayms) { analogWrite(speaker, 100); delay(delayms); //대기 analogWrite(speaker, 0); //사운드OFF } void setup () { //하차벨(redLed, redLed2) pinMode(redLed, OUTPUT); pinMode(redLed2, OUTPUT); //하차벨 스피커(speaker) pinMode(speaker, OUTPUT); //냉난방장치(motorPin) 설정 pinMode(motorPin, OUTPUT); digitalWrite(motorPin, LOW); //블루투스를 통해 데이터를 주고 받기 위해 시리얼 통신BTSerial) 시작 BTSerial.begin(9600); bufferPosition = 0; Serial.begin(115200); Serial.println("trying to start Ethernet..."); // start the Ethernet connection: conn = Ethernet.begin(mac) == 0; Serial.println(conn); if (conn) { Serial.println("failed to configure Ethernet using DHCP"); // no point in carrying on, so do nothing forevermore: // try to congifure using IP address instead of DHCP: Ethernet.begin(mac, ip); } time = millis(); } void loop () { while(client.available()) { char c = client.read(); Serial.print(c); } fsrReading0 = analogRead(fsrPin0); fsrReading1 = analogRead(fsrPin1); fsrReading2 = analogRead(fsrPin2); fsrReading3 = analogRead(fsrPin3); fsrReading4 = analogRead(fsrPin4); Serial.println(fsrReading0); Serial.println(fsrReading1); Serial.println(fsrReading2); Serial.println(fsrReading3); Serial.println(fsrReading4); if(fsrReading0 > threshold) //좌석에 설치된 압력센서가 threshold의 값(650)을 넘으면 { Serial.println("person seated"); seatStatus[0] = 1; //사람이 앉은 것으로 인식하여 좌석정보 변경. sendRequest(); //변경된 좌석값을 Http Request를 통해 전송 } else { Serial.println("seat empty"); seatStatus[0] = 0; //좌석에 설치된 압력센서가 threshold의 값(650)을 넘지 않으면 sendRequest(); //변경된 좌석값을 Http Request를 통해 전송 } if(fsrReading1 > threshold) { Serial.println("person seated"); seatStatus[1] = 1; sendRequest(); } else { Serial.println("seat empty"); seatStatus[1] = 0; sendRequest(); } if(fsrReading2 > threshold) { Serial.println("person seated"); seatStatus[2] = 1; sendRequest(); } else { Serial.println("seat empty"); seatStatus[2] = 0; sendRequest(); } if(fsrReading3 > 950) { Serial.println("person seated"); seatStatus[3] = 1; sendRequest(); } else { Serial.println("seat empty"); seatStatus[3] = 0; sendRequest(); } if(fsrReading4 > threshold) { Serial.println("person seated"); seatStatus[5] = 1; sendRequest(); } else { Serial.println("seat empty"); seatStatus[5] = 0; sendRequest(); } if (BTSerial.available()) //블루투스로 데이터 수신 { byte data = BTSerial.read(); Serial.write(data); //수신된 데이터 시리얼 모니터로 출력 if(data == '1') //하차벨 버튼을 눌렀을 경우 { if(redState == 0) //하차벨이 꺼져 있을 경우 { digitalWrite(redLed, HIGH); //하차벨의 불을 킴 digitalWrite(redLed2, HIGH); redState = 1; //하차벨의 상태를 켜진 상태로 세팅 redState2 = 1; delay(150); beep(9000); //스피커에서 소리 발생시킴 } else // 하차벨이 켜져 있을 경우 { digitalWrite(redLed, LOW); //하차벨을 끔 digitalWrite(redLed2, LOW); redState = 0; //하차벨의 상태를 꺼진 상태로 세팅 redState2 = 0; delay(150); } } if(data == '2') //냉난방 켜기 버튼을 눌렀을 경우 { Serial.println("motor on"); if(motorState == 0) { digitalWrite(motorPin, HIGH); //냉난방 켬 motorState = 1; delay(150); } } if(data == '3') //냉난방 끄기 버튼을 눌렀을 경우 { Serial.println("motor off"); if(motorState == 1) { analogWrite(motorPin, LOW); //냉난방 끔 motorState = 0; delay(150); } } Serial.println("--------------------"); }