블로그 이미지
pgmr이상현
Instagram:sh_lee77 머신비전, YOLO, 영상처리, Deep Learning, 딥러닝

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

앞 포스팅에서 JSN270 Wifi Shield의 문제점에 대해 포스팅했고

이번 포스팅은 그로인해 사용하게된 Ethernet Shield를 이용한 Http서버 연결에 대해

포스팅을 해보도록 하겠습니다. GO GO!!


먼저 아래 이미지와같이 Uno보드에 Ethernet Shield를 연결하고

USB와 LAN선을 연결해 줍니다. 불빛이 잘 들어오네요.

Arduino는 오픈소스가 잘 되어있어, 사용하기가 무척 좋은 것 같습니다.

예제 -> Ethernet에서 WebClient와 WebServer예제를 사용합니다.


Web Client

/*

Web client

This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "www.google.com"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while (true);
}
}


Web Server

/*

Web Server

A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
modified 02 Sept 2015
by Arturo Guadalupi
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// 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>");
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 disconnected");
}
}


WebServer 코드에서

byte mac[];        // PC와 연결된 아두이노의 Mac address

IPAddress ip();    // PC와 연결된 아두이노의 현재 IP 주소

위의 정보가 필요하다 Mac주소 같은경우는 Ethernet Shield의 아랫면에 적혀 있다는데,

이상하게 우리는 그렇지 않았습니다. 우리와 같은 분들은 Web Server예제를 통해서, Mac주소를

알아낼 수 있습니다.


이제 Web Client 에서 수정만 해주면 됩니다.

IPAddress server();

위의 코드의 주석을 풀어서 우리의 서버를 구축한 PC의 IP주소를 적어 줍니다.


그리고 다음과 같이 수정합니다.

Host부분에 Server IP만 바꾸어주면 됩니다. 간단합니다.

그 이후에 Serial모니터를 보면 서버연결이 잘 되는것을 확인할 수 있습니다.


JSN270 WiFi Shield를 사용했을 때는 끊어지던 서버가 안 끊어지는 것이

확인됩니다.

posted by pgmr이상현

서버에서의 좌표를 받아 물총을 제어하기 위해선 먼저 서버연결을 해야합니다.

서버를 연결하기 위해서는 WiFi Shield 또는 Ethernet Shield가 필요합니다.


우리는 먼저 WiFi Shield를 사용해보기로 했습니다.

JSN270이라는 쉴드를 사용해보기로 했습니다.

JSN270 WiFi Shield 는 위와같이 생겼다. 처음 써봐서 어떻게 쓰는건지 몰라서

두리번 두리번 했습니다.

쓰는 방법을 알아냈는데, 위와같이 Uno보드 위에 고대로 겹쳐서 끼워주시면 됩니다.


JSN270 WiFi Shield는 검색해보니까 다른 Shield들 하고는 서버연결하는 코드가,

조금 상이 했습니다. JMP SYSTEMS라는 회사에서 만든건데 예제소스는 이 회사

홈페이지에서 받을수 있습니다. SITE  <-- 여기 클릭!! 

자료를 다운받으면 예제소스를 받을 수 있습니다.


서버 연결을 해보겠습니다.

예제소스에 우리의 서버 IP주소만 넣어주면 간단하게 위와같이 서버가 연결된 것을 확인할 수 있습니다. 생각보다 간단하게 잘되네요. 그런데 문제점이 있었습니다.

다시한번 연결을 시도하니 Timeout Failed,  Failed connect,  Restart System

연결이 실패합니다. Restart하라고 해서 다시 연결을 시도해 봅니다.

역시나 연결이 실패하네요...

위 사진에 표시한 Reset버튼을 눌러보겠습니다. 다시 연결이 잘 됩니다.

우리가 뭔갈 잘못하고 있나 생각을 해서 자료를 찾아봤습니다.


다른분들도 이러한 문제점을 겪고 있다는 문제점을 인지하고, 여분으로 사놓았던

Ethernet Shield를 사용하기로 했습니다. 같이사길 잘했다


다음 포스팅은 Ethernet Shield를 사용한 서버연결에 대해 포스팅하겠습니다.

posted by pgmr이상현

이전 포스팅 까지 YOLO에서의 인식되는,

야생동물Bounding Box의 가운데 좌표값을 서버로 보내주는 것 까지 포스팅 했습니다. 그럼 이제 그 좌표를 Arduino에서 받아오고 물총을 동작시켜야 하는데 그 이전에 Arduino환경부터 설정해 보겠습니다.



1. Arduino IDE 설치하기

Arduino IDE는 검색창에 Arduino검색만으로 손쉽게 사이트에 접속할 수 있습니다.

검색마저 귀찮으실 까봐 Arduino site <-- 링크를 가져왔습니다. ㅎㅎ

저의 경우에는 Windows Installer를 받았습니다. Windows Installer를 클릭하게되면

다음과 같은 화면이 나오는데 JUST DOWNLOAD를 클릭하면 됩니다.




2. Arduino 환경설정


Arduino IDE를 실행하면 다음과 같이 실행됩니다.

자 실행을 했으니 이제 PC와 Arduino를 연결해 보겠습니다.

Arduino는 위와 같이 USB를 통해서 PC와 연결해서 소스코딩을 할 수 있습니다.

연결을 했으니, 이제 포트번호를 확인해야 합니다. 장치관리자를 들어갑니다.

이미지와 같이 Arduino Uno 라고있을 겁니다. 저는 4번포트이네요~

자 그럼 다시 Arduino IDE로 이동합니다.

툴 -> 보드 -> (본인의 보드) 본인은 Uno r3보드를 사용해서 Uno를 선택했습니다.

보드를 선택했다면 바로 아래 포트에서 자신의 포트번호를 선택하면 되겠습니다.


자그러면 Arduino환경설정이 모두 끝났습니다. 다음 포스팅은 Arduino와 서버연결에 대해 포스팅 하겠습니다.



posted by pgmr이상현
prev 1 2 3 4 5 6 7 8 9 next