이번에 이더넷 모듈을 공부하게 되어 아두이노이더넷 모듈로 LED를 제어해봤습니다.
#include출처 아랫줄에 부분은 PRE버그여서 제외하시고 복사하시면 됩니다.#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
소스에 include한 TextFinder.h는 http://playground.arduino.cc/Code/TextFinder 를 참고하시면 됩니다.
LED를 9번에 사용한 이유는 이더넷포트가 13 - 10번 핀을 사용하고 있기때문에 9번핀을 사용합니다.
이런식으로 연결 하신 뒤에 작동을 시킵니다,.
동영상을 보시면 버튼을 누른뒤 꽤 오랜뒤에 작동하는것을 볼 수 있습니다.
스레드가 아니라 루프문을 돌기때문에 어디서 버튼을 누른것을 캐치했는지, 그리고 루프문 돌아가면서 어느때에 캐치하는지를 잘 파악하는것이 중요하다 생각됩니다.
위 소스를 수정해서 빠르게 작동하도록 설정이 성공하면 그때 다시 올릴 수 있도록 하겠습니다.
'Programming > Arduino' 카테고리의 다른 글
아두이노를 이용한 온도,습도,조도 제어 및 LCD , 7segment 로 출력하기. (1) | 2013.01.29 |
---|---|
아두이노 보드를 이용한 DC모터 제어 (0) | 2013.01.23 |
소리를 이용한 LED를 ON/OFF하기 (0) | 2013.01.22 |
초음파 센서를 이용한 차량 후방 감지기 구현 (2) | 2013.01.22 |
74HC595를 이용한 3핀->8핀 만들기 (1) | 2013.01.17 |