정보나눔

오픈소스하드웨어 프로젝트에 대한 다양한 정보를 나누는 공간입니다.

클라이언트 아두이노서버 제어
정동아 | 2015-06-08

아두이노를 서버로 만들고 클라이언트가 접속해서 모터를 컨트롤하려고 

저번에 질문올렸는데 breakout.js 를 사용해보니 서버프로그램이 없으면 동작하질않아서..

(라즈베리파이도 없고 노트북으로 사용중이기에) 의도와 약간 달라져서 

 

혹시 아두이노 자체에서 서버를 만들고 , 클라이언트가 서버에 접속해서 (같은 ap일때) 

버튼을 누를때 모터나 led를 제어할 수는 없을까요? 

정보가많이없어서 질문 올려봅니다..

프로필사진

정동아 2015-06-08 15:21:28

아래와 같이 코드를 만들었는데

버튼을 눌렀을때 led는 동작했는데 

비슷한 방법으로 서보모터를 제어해보려고 하니 

스케치 컴파일시키고 시리얼모니터 띄우면 연결이 안되고 그래서 

뭐가문제인지를 몰라서 몇일째 해매고있네요 .. 

servo모터를 버튼을 누를때만 제어하고싶은데 뭐가 문제일까요

 

 

#include <SPI.h>
#include <WiFi.h>
#define LED_PIN  5
#include <Servo.h>

int pos = 1;
Servo servo;
WiFiServer server(80);
int status = WL_IDLE_STATUS;
char server_ip[16];
char ssid[] = "AndroidHotspot3400";      // your network SSID (name)
char pass[] = "gmlqja91";   // your network password

void setup() {
  Serial.begin(9600);
  servo.attach(9);
  pinMode(LED_PIN, OUTPUT);

  status = WiFi.begin(ssid, pass);
  server.begin();
  IPAddress myIP = WiFi.localIP();
  sprintf(server_ip, "%d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
  Serial.print("server is at ");
  Serial.println(server_ip);
}


void loop() {
  // listen for incoming clients
  WiFiClient client = server.available();
  if(client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    String buffer = "";
    while(client.connected()) {
      if(client.available()) {
        char c = client.read();
        Serial.write(c);
        buffer += c;

        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");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
           
          client.println("<body>");
          
          if(digitalRead(LED_PIN))
            client.print("LED is <font color='green'>ON</font>");
          else
            client.print("LED is <font color='red'>OFF</font>");

          client.println("<br />");
          
          client.print("<FORM action=\"http://");
          client.print(server_ip);
          client.print("/\" >");
          client.print("<P> <INPUT type=\"hidden\" name=\"status\" value=\"1\">");
          client.println("<p><input type=\"submit\" value=\"LED ON\"></p>");
          client.println("</form>");

          client.print("<FORM action=\"http://");
          client.print(server_ip);
          client.print("/\" >");
          client.print("<P> <INPUT type=\"hidden\" name=\"status\" value=\"0\">");
          client.println("<p><input type=\"submit\" value=\"LED OFF\"></p>");
          client.println("</form>");
          
          client.print("<FORM action=\"http://");
          client.print(server_ip);
          client.print("/\" >");
          client.print("<P> <INPUT type=\"hidden\" name=\"status\" value=\"3\">");
          client.println("<p><input type=\"submit\" value=\"Servo ON\"></p>");
          client.println("</form>");
          
          client.println("</body>");
          client.println("</html>");
          break;
        }
        
        if (c == '\n') {
          Serial.println(c, DEC);
          // you're starting a new line
          currentLineIsBlank = true;
          buffer = "";
        } else if (c == '\r') {
          Serial.println(c, DEC);
          // you've gotten a character on the current line
          if(buffer.indexOf("GET /?status=1") >= 0)
            digitalWrite(LED_PIN, HIGH);

          if(buffer.indexOf("GET /?status=0") >= 0)
            digitalWrite(LED_PIN, LOW);
            
           if(buffer.indexOf("GET /?status=3") >= 0)
            servo.write(10);
        }else {
          currentLineIsBlank = false;
        }
      }
    }

    delay(1);
    client.stop();
    Serial.println("client disonnected");
  }
}

 

이전글   |    아두이노로 토양습도센서와 모터돌리기 질문입니다~... 2015-06-07
다음글   |    앱인벤터 (계속 질문드려 죄송합니다 ㅜㅜ)... 2015-06-08