이번에 이더넷 모듈을 공부하게 되어 아두이노이더넷 모듈로 LED를 제어해봤습니다.


 
#include 
#include 
#include 


//Ethernet Shield MAC address 
byte mac[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
IPAddress ip(172,31,10,155);
IPAddress subnet(255,255,0,0);
IPAddress gateway(172,31,0,1);
IPAddress dns_local(168,126,63,1);

char buffer[8];

EthernetServer server(80);

void setup() {
  Serial.begin(9600);
  
              
  //if(Ethernet.begin(mac) == 0) {
  //  Serial.println("Failed to configure Ethernet using DHCP");
    //using fixed ip
    Ethernet.begin(mac, ip, dns_local, gateway, subnet);
  //}
  
   Serial.print("IP address: ");
  ip = Ethernet.localIP();
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(ip[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
  server.begin();
}

void loop() {
  EthernetClient client = server.available();
  if (client) {
    TextFinder finder(client);
    int type = 0;
    while (client.connected()) {
      if (client.available()) {
        //http request start with "GET / HTTP/1.1"
        if (finder.getString("","/", buffer, sizeof(buffer))) {
          if(strcmp(buffer, "POST ") == 0) {
            finder.find("\n\r");
            while (finder.findUntil("pinD", "\n\r")) {
              int pin = finder.getValue();
              int val = finder.getValue();
              pinMode(pin, OUTPUT);
              digitalWrite(pin, val);
              
              
              Serial.print("pin: ");
              Serial.print(pin);
              Serial.print(" Val: ");
              Serial.println(val);
            }
          }
          
          sendHeader(client,"Post example");
          //create HTML button to control pin 9
          client.println("

Click buttons to turn pin 9 on or off

"); client.print("

"); //create HTML button to turn on pin 9 client.print("

"); client.println(""); client.stop(); break; }//if finder }//if client available }//while connected delay(1); client.stop(); }//if client } void sendHeader(EthernetClient client, char *title) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.print(""); client.print(title); client.println(""); } //소스출처 = http://suakii.egloos.com/2976671

출처 아랫줄에 부분은 PRE버그여서 제외하시고 복사하시면 됩니다.

소스에 include한 TextFinder.h는 http://playground.arduino.cc/Code/TextFinder 를 참고하시면 됩니다.

LED를 9번에 사용한 이유는 이더넷포트가 13 - 10번 핀을 사용하고 있기때문에 9번핀을 사용합니다.




이런식으로 연결 하신 뒤에 작동을 시킵니다,.



동영상을 보시면 버튼을 누른뒤 꽤 오랜뒤에 작동하는것을 볼 수 있습니다.


스레드가 아니라 루프문을 돌기때문에 어디서 버튼을 누른것을 캐치했는지, 그리고 루프문 돌아가면서 어느때에 캐치하는지를 잘 파악하는것이 중요하다 생각됩니다.


위 소스를 수정해서 빠르게 작동하도록 설정이 성공하면 그때 다시 올릴 수 있도록 하겠습니다.



CMOS에서 Advanecd   >  Chipset  >  North Bridge Configuration  들어 갑니다.

첫번째 항목에 Memory Remapping Feature 항목이 Disabled 되어 있는것을 Enabled 로 변경하시고 F10 누르고 재부팅 하시면 4G이상을 인식합니다.

 

레이어(Layer)란 ?

그래듀에이션보다 많은 층을 표현할 때 사용하는 것 레이어 : 위가 짧고 아래가 긴 스타일 레이어 : 90도 이상 각도를 들어서 자르는 층이 많은 커트 (출처 - http://kin.naver.com/openkr/detail.nhn?docId=173794 )

라고 합니다.


더 쉽게 예기하면 포토샾을 하신분들은 레이어를 많이사용하시니 같은개념으로 이해하심 됩니다.

즉 하나의 층을 이야기합니다.


css로 2개의 층을 만들고 그것을 띄웠다 사라졌다를 관리할 수 있습니다.


고양이 메일링 서비스 ( http://bubibuba.tistory.com/77 ) 소스를 참고하여 설명 드리겠습니다.


style.css 파일을 보시면 레이어가 구현이 되어있습니다.

이중 Layer1를 띄웠다 사라졌다 하는 부분을 설명드리면


 
//레이어 1번
#Layer1 {
              border:2px solid #ff6600;
              background:white;
              position:absolute; 
              top:50%; 
              left:50%; 
              width:450px; 
              height:500px; 
              margin-left: -225px; 
              margin-top: -300px;
              visibility:hidden;
          }
위처럼 레이어를 css로 구현을 해 둡니다.

그리고 위 이미지의 Join 버튼을 누르면 Layer1이 출력이 되게 되는데 main.php 의 소스를 보면

<input type="button" value="JOIN" class="button" onclick="Layer1.style.visibility='visible'" style="margin-left:30px">

위 부분에서 구현이 되는걸 확인할 수 있습니다.

OnClick를 보시면 Layer1이 hidden -> visible로 변경되면서 화면이 출력됩니다.




이런식으로 말이죠.. 반대로 Back부분을 보면 화면이 사라지는데

그부분은 위와 마찬가지로

<input class="button" type="button" value="BACK" OnClick="Layer1.style.visibility='hidden'" style="margin-left:30px"> 


 Layer1을  visible -> hidden 로 변경 시켜 화면에서 사라지게 합니다.


소스를 꼭 다운받으셔서 보시는것을 추천드립니다.


 
#footer {
              position:absolute; 
              clear:left; 
              float:left; 
              width:100%; 
              
              left:0; 
              bottom:0px;            
          }


아래속성을 참고로하여 필요한 부분만 수정해주면 됩니다.


에프티크레이이티드(http://fdc.ne.kr)에서 제공하는 교육용 아두이노를 이용하여 제작했습니다.

사용한 센서는 

- 조도 센서

- 온도 센서

- 습도 센서

를 사용하여 

버튼을 땐경우에는 - 온도,습도 를 출력

버튼을 누른경우에는 - 조도를 출력합니다.


#include 
#include 
#include  
#define dataPin  22 // 온습도 데이터 입력핀 설정
#define clockPin 23 // 시간 데이터 입력핀 설정
SHT1x sht1x(dataPin, clockPin);
LiquidCrystal lcd(32, 54, 33, 34, 35, 36, 37);
int buttonState = 0;
const int cdsInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogInPin = A1;  // Analog input pin that the potentiometer is attached to

int sensorValue = 0;        // value read from the pot
int cdsValue = 0;        // value read from the pot


void setup()
{
   Serial.begin(9600); // 
   Serial.println("Starting up");
   Wire.begin();        // join i2c bus (address optional for master) 
   delay(500);  
   pinMode(8, INPUT);  //LED Set "Relay Board Cn1"
   lcd.begin(8,2);              // 8열 2줄 LCD 활용 시작
   lcd.clear();                  // LCD 화면 비움
  
}
void loop()
{
  buttonState = digitalRead(8);
  sensorValue = analogRead(analogInPin);
  cdsValue = analogRead(cdsInPin)*10;   
  float sensor;
  float cds;
  
  // Read values from the sensor
  sensor = analogInPin;
  cds = cdsInPin;   
  const int lookup[10] = {0x3F,0x06,0x5B,0x4F,0x66,
                          0x6D,0x7D,0x07,0x7F,0x6F};
  int Count, Thousands, Hundreds, Tens, Base;  
  Wire.beginTransmission(0x38);
  Wire.write((byte) 0);
  Wire.write(B01000111);
  Wire.endTransmission();
  
  float temp_c; // 섭씨 
  float temp_f; // 화씨
  float humidity; // 습도

  // 위의 변수 값에 센서로부터의 값을 입력 받도록 설정한다.
  temp_c = sht1x.readTemperatureC();
  temp_f = sht1x.readTemperatureF();
  humidity = sht1x.readHumidity();

  if(buttonState == HIGH){
  
      //LCD START
      lcd.setCursor(0,0);           // 0, 0 윗줄 시작
      lcd.print("Temp= ");    // 따옴표 사이에 원하는 글을 적는다.
      lcd.print(temp_c, DEC);    // 따옴표 사이에 원하는 글을 적는다.
      
      lcd.setCursor(0,1);           // 0, 1 아랫줄 시작
      lcd.print("Humi= ");
      lcd.print(temp_f, DEC);
      //LCD END
      
      // 7 SEGMENT START
      Wire.beginTransmission(0x38);
      Wire.write(1);
      Thousands = (int)temp_c/10;
      Hundreds = (int)(temp_c - (Thousands*10));
      Tens = (int)temp_f/10;
      Base = (int)(temp_f - (Tens*10));
      
      Wire.write(lookup[Thousands]);
      Wire.write(lookup[Hundreds]);
      Wire.write(lookup[Tens]);
      Wire.write(lookup[Base]);
      Wire.endTransmission();

      
  }else{
      
    if(cdsValue >= 160)
      {
        Serial.print("Relay Cn1 On\n");
        Serial.print("Trun off Light 0n\n");
    
        digitalWrite(33, LOW);
        digitalWrite(32, HIGH);
        
        lcd.begin(8,2); 
        lcd.clear();
        lcd.setCursor(0,0);        
        lcd.print("cds");   
        lcd.print(cdsValue,DEC);
        lcd.setCursor(0,1);          
        lcd.print("lightoff");
        
        Wire.beginTransmission(0x38);
        Wire.write(1);
        if(cdsValue>99 && cdsValue<1000){
          Thousands = 0;
          Hundreds = cdsValue/100;
          Tens = (cdsValue - (Hundreds*100))/10;
          Base = (cdsValue - (Hundreds*100)-(Tens*10));
        
          Wire.write(lookup[Thousands]);
          Wire.write(lookup[Hundreds]);
          Wire.write(lookup[Tens]);
          Wire.write(lookup[Base]);
          Wire.endTransmission();

        }else if(cdsValue>9 && cdsValue<100){
          Thousands = 0;
          Hundreds = 0;
          Tens = cdsValue/10;
          Base = (cdsValue - (Tens*10));
          
          Wire.write(lookup[Thousands]);
          Wire.write(lookup[Hundreds]);
          Wire.write(lookup[Tens]);
          Wire.write(lookup[Base]);
          Wire.endTransmission();

        }else if(cdsValue>10){
          Thousands = 0;
          Hundreds = 0;
          Tens = 0;
          Base = cdsValue;
        
          Wire.write(lookup[Thousands]);
          Wire.write(lookup[Hundreds]);
          Wire.write(lookup[Tens]);
          Wire.write(lookup[Base]);
          Wire.endTransmission();

        }
      }
  
    else
    {
      Serial.print("Relay Cn2 On\n");
      Serial.print("Trun On the light On\n");
  
      digitalWrite(32, LOW);
      digitalWrite(33, HIGH);
      
      lcd.begin(8,2); 
      lcd.clear();
      lcd.setCursor(0,0);        
      lcd.print("cds ");   
      lcd.print(cdsValue,DEC);
      lcd.setCursor(0,1);          
      lcd.print("light on");
    }
  }
  // 7 SEGMENT END
  delay(50);
}






이 프로그램을 응용하여 릴리즈를 활용하여 

조도값을 받아 어두울때는 전등을 키고 밝을때는 전등을끄는 기능과

추울때는 히터를 틀고 더울때는 히터를 키는 소스를 구현하였습니다.



 

#include 
#include 
#include  
#define dataPin  22 // 온습도 데이터 입력핀 설정
#define clockPin 23 // 시간 데이터 입력핀 설정
SHT1x sht1x(dataPin, clockPin);
LiquidCrystal lcd(40, 54, 41, 42, 43, 44, 45);
const int cdsInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogInPin = A1;  // Analog input pin that the potentiometer is attached to

int sensorValue = 0;        // value read from the pot
int cdsValue = 0;        // value read from the pot


void setup()
{
   Serial.begin(9600); // 
   Serial.println("Starting up"); 
   pinMode(32, OUTPUT); 
   pinMode(33, OUTPUT);    
   lcd.begin(8,2);              // 8열 2줄 LCD 활용 시작
   lcd.clear();                  // LCD 화면 비움
  
}
void loop()
{

  sensorValue = analogRead(analogInPin);
  cdsValue = analogRead(cdsInPin)*10;   
  float sensor;
  float cds;
  
  // Read values from the sensor
  sensor = analogInPin;
  cds = cdsInPin;   
  
  float temp_c; // 섭씨 
  float temp_f; // 화씨
  float humidity; // 습도

  // 위의 변수 값에 센서로부터의 값을 입력 받도록 설정한다.
  temp_c = sht1x.readTemperatureC();
  temp_f = sht1x.readTemperatureF();
  humidity = sht1x.readHumidity();

  if(temp_c<20){ // 온도의 기준은 20도
  
      //LCD START
      lcd.setCursor(0,0);           // 0, 0 윗줄 시작
      lcd.print("T=");    // 따옴표 사이에 원하는 글을 적는다.
      lcd.print((int)temp_c, DEC);    // 따옴표 사이에 원하는 글을 적는다.
      digitalWrite(32, HIGH); 
      lcd.print(" ON  ");    // 따옴표 사이에 원하는 글을 적는다.
      
  }else{
    //LCD START
      lcd.setCursor(0,0);           // 0, 0 윗줄 시작
      lcd.print("T=");    // 따옴표 사이에 원하는 글을 적는다.
      lcd.print((int)temp_c, DEC);    // 따옴표 사이에 원하는 글을 적는다.
      digitalWrite(32, LOW); 
      lcd.print(" OFF  ");    // 따옴표 사이에 원하는 글을 적는다.
  }
      
  if(cdsValue >= 160) // 밝기의 기준은 160
    {
      Serial.print("Relay Cn1 On\n");
      Serial.print("Trun off Light 0n\n");
  
      lcd.setCursor(0,1);
      lcd.print("L=");    // 따옴표 사이에 원하는 글을 적는다.
      lcd.print(cdsValue, DEC);    // 따옴표 사이에 원하는 글을 적는다.
      lcd.print(" OFF  ");    // 따옴표 사이에 원하는 글을 적는다.
      digitalWrite(33, LOW); 
  }
  else
  {
    Serial.print("Relay Cn2 On\n");
    Serial.print("Trun On the light On\n");

    lcd.setCursor(0,1);          
    lcd.print("L=");    // 따옴표 사이에 원하는 글을 적는다.
    lcd.print(cdsValue, DEC);    // 따옴표 사이에 원하는 글을 적는다.
    lcd.print(" ON  ");    // 따옴표 사이에 원하는 글을 적는다.
    digitalWrite(33, HIGH); 
  }
  delay(1000);
}




+ Recent posts