본문으로 건너뛰기

uno_ethernet_channel_txrx

1. 부품 준비

웹앱으로 LED 원격제어 실습만 해 보실 경우 버튼은 생략하셔도 됩니다.

  • 아두이노 UNO
  • 아두이노 이더넷쉴드(W5100)
  • 브레드보드, 점퍼선
  • 푸시버튼
  • LED
  • 저항 약 330옴

2. 부품 연결하기

본 예제는 간단히 LED 1개와 전류 제한 용 저항, 그리고 버튼이 1개 연결되었습니다 저항 크기는 약 220~560옴 정도로 바꿔도 무방합니다.

3. 예제소스


/*
Arduino Uno R3 + Ethernet shield W5100 + LED

https://github.com/remocons/iosignal-arduino

An example of communicating between Arduinos and controlling them with a webapp.
1. change the LED_PIN and BUTTON_PIN numbers according to your board.
2. set WIFI_SSID and KEY when using wifi.
3. iosignal server information is used without modification. If you use another server, change the server address and port number.
4. compile and upload to Arduino.
5. Access http://test.iosignal.net with a modern web browser.

[kr] 아두이노들간의 통신과 웹앱으로 제어하는 예제입니다.
1. 보드에 따라 LED_PIN과 BUTTON_PIN 번호를 변경하세요.
2. wifi 사용시 WIFI_SSID와 KEY 를 설정하세요.
3. iosignal 서버 정보는 수정 없이 그대로 사용합니다. 다른 서버를 사용할 경우 서버 주소와 포트번호를 변경해줍니다.
4. 아두이노에 컴파일하고 업로딩합니다.
5. 모던 웹브라우저로 http://test.iosignal.net 에 접속하세요.

*/

#include <SPI.h>
#include <Ethernet.h>
#include <string.h>
#include <IOSignal.h>
#include <Bounce2.h>

#define LED_PIN 3
#define BUTTON_PIN 2

// If you have multiple devices, you'll need to change the MAC address.
byte mac[]{0, 0, 0, 0, 0, 0x81};
EthernetClient client;
IOSignal io;
Bounce2::Button downBtn = Bounce2::Button();

void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // active High
downBtn.attach(BUTTON_PIN, INPUT_PULLUP);
downBtn.interval(5);
downBtn.setPressedState(LOW);

Serial.begin(115200);
Serial.println(F("Init.."));

Ethernet.init(10);
Ethernet.begin(mac); // DHCP
Serial.print(F("IP:"));
Serial.println(Ethernet.localIP());

io.setRxBuffer( 80 );
io.begin( &client , "io.iosignal.net", 55488);
// io.begin( &client , "192.168.0.204", 55488);
io.onReady( &onReady );
io.onMessage( &onMessage );
}


void onReady()
{
Serial.print("onReady cid: ");
Serial.println( io.cid );
io.subscribe("#homeButton");
}

void onMessage( char *tag, uint8_t payloadType, uint8_t* payload, size_t payloadSize)
{

Serial.print(">> signal tag: " );
Serial.print( tag );
Serial.print(" type: " );
Serial.print( payloadType );
Serial.print(" size: " );
Serial.println( payloadSize );

if( strcmp(tag, "#homeButton") == 0){
if( digitalRead(LED_PIN ) == HIGH ){
digitalWrite( LED_PIN , LOW );
}else{
digitalWrite( LED_PIN , HIGH );
}
}

}


void loop() {
io.loop();
downBtn.update();
if (downBtn.pressed()) {
Serial.println("down");
io.signal("#homeButton", "uno3-eth" );
}
}


4. 웹앱 테스트

위 예제를 아두이노에 업로딩하면 서버에 연결되고, 서버에 연결된 다른 아두이노 장치들과 통신이 가능합니다. 게다가 웹브라우저에서 실행되는 웹앱도 아두이노와 통신이 가능해집니다.

테스트 웹앱을 실행해보시려면, 아두이노와 동일한 공유기에 연결된 장치에서 모던 웹브라우저를 열고 http://test.iosignal.net 에 접속합니다.

  • 기본 서버 상태에서 connect 버튼을 눌러 서버에 접속합니다.
    • 직접 iosignal 서버를 운영중이라면 서버 정보를 변경 후 접속합니다.
  • rx 수신 메시지창과 send message 버튼 모음 창이 보입니다.
  • 아두이노의 버튼을 눌러서 다른 아두이노 장치에 연결된 LED 가 토글되는지 확인해봅니다.
  • 아두이노의 버튼이 눌릴때 웹앱에서 시그널 메시지를 수신하는지 확인합니다.
  • 웹앱에서 시그널 송신 버튼을 누를때 아두이노 보드들의 LED 가 토글되는지 확인해봅니다.