오늘은 WIFI 쉴드를 사용하여 웹서버를 만들어 서버에 누군가 접속을하면 불이 켜지도록 해볼겁니다.

해당 프로젝트는  https://www.youtube.com/watch?t=90&v=iOran8zx1yQ 이것을 응용한것입니다. 시작하기전 동영상을 먼저 보신뒤

하시는걸 추천합니다.

필요한 것들은 아래와 같습니다.

 

옥션기준

와이파이 쉴드 가격이 상당이 비쌉니다. 의도치 않게 와이파이 쉴드가 있으신분들만 해당 글을 따라하실수 있게 됬네요.. ㅠㅠ 죄송합니다..이렇게 비싼줄 몰랐어요ㅠㅠ

아두이노 개발을 위해선 아두이노 공식사이트에서 제공하는 sketch 라는 프로그램이 필요합니다. 아두이노 개발환경 설정은 시간이 나면 해드리겠습니다.

일단 스케치를 실행 하시고 해당코드를 입력해주시기 바랍니다.

WIFI WEB SERVER[MEGA]

/*
  WiFi Web Server

A simple web server that shows the value of the analog input pins.
using a WiFi shield.

This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.

Circuit:
* WiFi shield attached
* Analog inputs attached to pins A0 through A5 (optional)

created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe

*/

#include <SPI.h>
#include <WiFi.h>
#include <Wire.h>

char ssid[] = "와이파이명";      // your network SSID (name)
//char pass[] = "와이파이 비밀번호";

// 공유기에서 와이파이 설정을 WEP 방식으로 하신분들은 암호사용안함으로 한뒤 사용해주시기 바랍니다.

 //int keyIndex = 0; 
int status = WL_IDLE_STATUS;

WiFiServer server(80);

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  Wire.begin();
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }



  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid);
   
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();
  // you're connected now, so print out the status:
  printWifiStatus();
}


void loop() {
  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    Serial.println("new client");
          Wire.beginTransmission(5);
          Wire.write('H');
          Wire.endTransmission();

    // 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: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");

          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("<br />");
          }
          client.println("</html>");                
          Wire.beginTransmission(5);
          Wire.write('L');
          Wire.endTransmission();
          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 disonnected");
  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

위의 코드를 컴파일 하신뒤 MEGA에 업로드 시켜주시기 바랍니다.

 

LED CONTROL [UNO]

#include <Wire.h>
#include <SPI.h>
#include <SD.h>

File myFile;
void setup() {
  // put your setup code here, to run once:
  Wire.begin(5);
  Wire.onReceive(ReceiveEvent);

  pinMode(8, OUTPUT);
  digitalWrite(8, LOW);
}

void loop() {
  // put your main code here, to run repeatedly:

}

void ReceiveEvent(int howMany){
  while(Wire.available()){
    char c = Wire.read();

    if(c == 'H')
    {
      digitalWrite(8, HIGH);
    }
    else if(c == 'L')
    {
      digitalWrite(8, LOW);
    }
  }

 

}

위의 코드를 입력하신뒤 우노에 업로드 해주시기 바랍니다.

회로 연결도

회로연결까지 끝나셨다면 와이파이쉴드 웹서버에 접속해보세요. LED가 반짝 반짝 거리는것을 확인할수 있습니다.

해당동영상은 빨른시일내에 올리도록 하겠습니다.

 

 

'ARDUINO' 카테고리의 다른 글

아두이노 키트 빌려옴  (0) 2015.10.04
Posted by MAESTRO.
,
ㅠㅠ
Posted by MAESTRO.
,


왼쪽부터 브레드보드(빵판ㅋㅋ) 메가, 우노, 와이파이 실드, 이더넷보드 입니다. 이거 가지고 재미나게 놀아야지ㅋㅋ

'ARDUINO' 카테고리의 다른 글

아두이노 WIFI WEB SERVER 를 활용해 보자  (0) 2015.10.05
Posted by MAESTRO.
,