우선 영상처리는 visual studio에서 하고 정보를 아두이노로 보내어 제어 할것이다.
그렇기 때문에 먼저 아두이노를 통해 정보를 주고받을 웹서버를 만드는 작업을 한다.
이를위해 아두이노에 와이파이 모듈이 장착되어있는 모델을 사용하여야한다.
필자는 Arduino Nano RP2040를 사용하였다.
아두이노 기본 설정에 대한 정보는 다음 글을 참조하길 바란다.
https://jiwoojung.tistory.com/2
Arduino Nano RP2040 사용법
원본: 아두이노 홈페이지 정보https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-board-manager https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-board-manager docs.arduino.cc 1.[라이브러리 다운받기]우선 좌측에
jiwoojung.tistory.com
그리고 필요한 소자가 서보모터 2개를 이용하여 만든 팬틸트 형식의 소자가 필요하다

1.[아두이노와 웹서버의 연결]
<아두이노 코드>
아래 "network"와 "password"에는 자신이 사용할 와이파이의 이름과, 패스워드를 입력하길 바란다.
#include <SPI.h>
#include <WiFiNINA.h>
#include <Servo.h>
// WiFi network information
char ssid[] = "network"; // your network SSID (name)
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
// Servo objects
Servo myservox; // create Servo object to control a servo
Servo myservoy; // create Servo object to control a servo
void setup() {
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
Serial.begin(9600); // initialize serial communication
// attach the servos to pins
myservox.attach(6); // attaches the servo on pin 6 to the Servo object
myservoy.attach(5); // attaches the servo on pin 5 to the Servo object
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
server.begin();
printWifiStatus();
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
// send a standard HTTP response header
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("<html><body>");
client.print("<button onclick=\"location.href='/RH'\">Red ON</button>");
client.print("<button onclick=\"location.href='/RL'\">Red OFF</button>");
client.print("<button onclick=\"location.href='/GH'\">Green ON</button>");
client.print("<button onclick=\"location.href='/GL'\">Green OFF</button>");
client.print("<button onclick=\"location.href='/BH'\">Blue ON</button>");
client.print("<button onclick=\"location.href='/BL'\">Blue OFF</button>");
client.print("</body></html>");
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
// LED control
if (currentLine.endsWith("GET /RH")) {
digitalWrite(LEDR, HIGH);
}
if (currentLine.endsWith("GET /RL")) {
digitalWrite(LEDR, LOW);
}
if (currentLine.endsWith("GET /GH")) {
digitalWrite(LEDG, HIGH);
}
if (currentLine.endsWith("GET /GL")) {
digitalWrite(LEDG, LOW);
}
if (currentLine.endsWith("GET /BH")) {
digitalWrite(LEDB, HIGH);
}
if (currentLine.endsWith("GET /BL")) {
digitalWrite(LEDB, LOW);
}
// Servo control
if (currentLine.startsWith("GET /X"))
{
int indexX = currentLine.indexOf('X') + 1;
int indexY = currentLine.indexOf('Y');
int posx = currentLine.substring(indexX, indexY).toInt();
int posy = currentLine.substring(indexY + 1).toInt();
myservox.write(posx);
myservoy.write(posy);
}
}
}
client.stop();
Serial.println("client disconnected");
}
}
void printWifiStatus() {
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
이렇게 코드를 입력하고 시행하면, 시리얼 모니터에 ip주소가 나올것이다.
ip주소를 와이파이가 연결되어있는 노트북의 인터넷 주소에 입력하면 제어가 가능한 웹서버가 나온다.

이렇게 하면 visual studio - 웹서버 - 아두이노의 연결에서
아두이노와 웹서버가 연결된것이다.
'아두이노' 카테고리의 다른 글
Arduino Nano RP2040 사용법 (0) | 2024.07.13 |
---|