본문 바로가기
Development/WIZnet

#HomeAutomation Step1 초음파센서와 터치버튼

by 루카오리 2014. 11. 26.


Ultrasonic_Touch button.fzz

ArduinoSource_WebServer.zip


먼저, 홈오토메이션은 단순히 저의 필요에 의해 만들어진다는 점 양해해 주세요.

대부분의 전문용어가 그러하듯이, 영어나, 일본어를 우리말로 옮기는 과정에서 가독성이 떨어지게 만드는 먼가가  있지 않나 싶습니다. 지금부터는 영어 용어가 있는 경우에는 영어로 쓰는 것이 좋을 것 같습니다. 원문에 충실하도록!! 전문용어를 남발하더라도 이해해 주세요.

Homeautomation 하면 떠오르는 생각은 손가락 하나만으로 집 전체가 제어가 되고, 카메라를 설치해서 외부에서도 집안을 볼수 있지 않을까 하는 생각입니다.

저의 경우는 단순히 무거운 택배를 받기 위해서 시작됩니다. 저희집을 들어오기 위해서는 외부에서 들어오는문(열쇠), 외부문에서 집으로 들어오는 문(비밀번호) 이렇게 두개입니다. 

열쇠로 잠기는 문은 회사에서 제가 제어하고 열어준다면, 무거운 택배를 경비실에서부터 들고오지 않아도 된다는 생각에서 착안된것입니다. 서론이 너무 길죠? 본격적으로 Homeautomation의 세계로 빠져봅시다.

#HomeAutomation Step1 에서 다룰 내용은 현관문에 방문자를 어떻게 구분할것인가 입니다.

그래서 현관문의 부저(button)와 무언가의 접근을 알 수 있는 센서(Ultrasonic)을 사용하려고합니다.

사용제품 및 프로그램 

  • Arduino Uno(MCU)
  • Ethernet Shield(ioShield-A, WIZnet)
  • Ultrasonic(NT-TS601, OEM)
  • 종이 터치 센서
  • LED, 저항
  • 점퍼선
  • Fritzing Program
  • Arduino Sketch Program

아래의 그림은 Fritzing프로그램을 사용하여 회로를 도식화 한 것입니다.

해당 파일은 포스트의 맨위맨위의 첨부파일을 받으시면 있어요.

※※※※참고 사항※※※※

실제 사용된 초음파센서는 그림과 달라요!! 실제 사용된 초음파센서는 다리가 세개이며, GND/VCC/SIG순입니다. 또한 스위치는 종이로 만든 스위치를 사용해서  맨위로 연결된쪽은 실제 사용할떄 제거하면 됩니다.

 아래는 Arduino Sketch Source 코드입니다. 첨부파일과 동일해요

#define sonicPin 7
#define ledPin 8
#define ledPin_touch 9
long cmtomicro(long a){
  return a/29/2;
}
long inchtomicro(long b){
  return b/74/2;
}
#include 
#include 
#include
CapacitiveSensor sensor = CapacitiveSensor(4,2);
int th = 30;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 13, 5);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  // Open serial communications and wait for port to open:

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection and the server:
    if (Ethernet.begin(mac) == 0) {
        Serial.println("Failed to configure Ethernet using DHCP");
       Ethernet.begin(mac, ip);
    }

  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  pinMode(sonicPin, OUTPUT);
  pinMode(ledPin,OUTPUT);
  pinMode(ledPin_touch,OUTPUT);
  digitalWrite(sonicPin, HIGH);// 소닉핀 초기화
  digitalWrite(ledPin, LOW);
  digitalWrite(ledPin_touch, LOW);

}


void loop() {

  int sonicval;
  int cm;
  int inch;
  int val;

   long sensorValue = sensor.capacitiveSensor(30);
   digitalWrite(sonicPin,LOW);
   delayMicroseconds(2);
   digitalWrite(sonicPin,HIGH);
   delayMicroseconds(5);
   digitalWrite(sonicPin,LOW);
   pinMode(sonicPin, INPUT);

      sonicval = pulseIn(sonicPin,HIGH); // 초음파가 반사하여 되돌아온 시간을 저장
      pinMode(sonicPin, OUTPUT);
      digitalWrite(ledPin, LOW);
      digitalWrite(ledPin_touch, LOW);

  // listen for incoming clients
         EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
        while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
         // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 1");  // refresh the page automatically every 5 sec
          client.println();
          client.println("");
          client.println("");

          // output the value of each analog input pin
          client.println("sonicval = ");
          val= cmtomicro(sonicval); // cm거리계산
          client.print(val);
          client.println("
"); if(val >= 50){ client.println("[Safety]"); client.println("
"); } else if(val < 50 && val > 10){ client.println("[Be careful]"); client.println("
"); } else if(val <= 10 && sensorValue >30){ client.println("[Danger]"); client.println("
"); client.println(" Button Push "); client.println("
"); digitalWrite(ledPin, HIGH); digitalWrite(ledPin_touch, HIGH); } if (val > 10 && sensorValue > th) { client.println("Only Button Push "); client.println("
"); digitalWrite(ledPin_touch, HIGH); } client.println("==============================="); client.println("
"); client.println(""); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println("client disconnected"); } delay(2); }

아래는 실제 연결한 것을 찍은 것인데. 없어보이기는 하지만 제대로 동작합니다.


실제 종이로 만든 터치 센서입니다. 연필의 흑심은 전도성이 있어 가능합니다. 저와 같이 사용하시려거든 꼭 4B이상을 사용하시기바랍니다. 2B로 해봤는데, 잘안되더라고요. 조금 없어보이긴 하지만, 신기한 경험이였습니다. 이 재료의 단점은....

헉4

흑연이 손이 뭍어놔서 지워질때마다 다시 색칠을 해줘야한다는.. 

 자세한 설명은 인체종이센서만들기 에서 참고하시길 바랍니다.

다음은 Webserver에서의 동작을 영상으로 찍은것을 볼수 있습니다. 볼때마다 신기하고 뿌듯하네요.