웹을 통한 원격 PC on/off 입니다.
상용 수준은 아니지만 간단하게 사용하실분들은 참고하셔서 만드셔도 될듯 합니다.
* 사용환경
네트워크 : IPTIME WiFi AP
컨트롤 보드 : 아두이노 mega 2560 + 아두이노 이더넷 Ethernet W5100 micro SD소켓 쉴드
사용 환경은 WiFi AP 에서 특정 포트를 포트 포워딩으로 아두이노 보드 로 지정.
하기 코드는 제가 사용한 비번 관련 부분에 대해서는 수정하였으며 이제 막...동작되는 것까지만 확인된 소스입니다.
* 주의 사항
참고로 하기 코드는 http 베이스로 https 로 동작되지 않기에 의심스러운 WiFi AP 네트워크에서 사용시 악의적인 성격을 가진 사람이
tcpdump 로 정보를 갈취할 경우 갈취한 사람이 해당 사이트 이용하여 원치 않는 PC 리붓을 시킬수 있습니다.
* 아두이노 코드 ( 하기 부분 복사해서 사용하시면 될듯 합니다. )
///*
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
//*/
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
IPAddress ip(192,168,0,5); // <========================================================
#define SERVER_PORT 8080 // <========================================================
#define DHTPIN 2 // Pin which is connected to the DHT sensor. // <========================================================
#define SETTED_TEMPERATURE 28 // <========================================================
#define SETTED_HUMIDITY 90 // <========================================================
int Relay8=8; // Ventilator Switch relay PIN 넘버.. // <========================================================
int Relay14=14; // Switch relay PIN 넘버.. // <========================================================
int Relay15=15; // Switch relay PIN 넘버.. // <========================================================
// Uncomment the type of sensor in use:
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//#include <DHT11.h>
//int pin = A2; // 온습도센서(DHT-11) 핀 A2 부여
//DHT11 dht11(pin);
byte mac[] = { 0x74,0x69,0x69,0x2D,0x30,0x5 };
//IPAddress ip(DHCP IP주소 찾기 예제 검색후 아이피주소 입력);
// IP 주소는 " XXX,XXX,XXX,XXX " ',' 주의해주세요.
// 이더넷 라이브러리 초기화
// 사용할 IP 주소 와 포트 입력
// ('port 80' 은 HTTP 의 기본 값 입니다.):
//EthernetServer server(80);
EthernetServer server(SERVER_PORT);
String readString;
void client();
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
//////////////////////////
uint32_t switch_dev=0;
//////////////////////////
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
dht.begin(); // Initialize DHT device.
while (!Serial) {
; // 포트 연결까지 기다리기. 레오나르도 에만 필요합니다.
}
// 이더넷 서버 연결 시작:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.print(Ethernet.localIP());
Serial.print(":");
Serial.println(SERVER_PORT);
///*
// Print temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Temperature");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C");
Serial.println("------------------------------------");
// Print humidity sensor details.
dht.humidity().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Humidity");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%");
Serial.println("------------------------------------");
// Set delay between sensor readings based on sensor details.
delayMS = sensor.min_delay / 1000;
//*/
pinMode(Relay8, OUTPUT);
pinMode(Relay14, OUTPUT);
pinMode(Relay15, OUTPUT);
//////////////////////////
digitalWrite(Relay14, HIGH); // <=== Low level trigger 릴레이 케이스.
// digitalWrite(Relay14, LOW); // <=== High level trigger 릴레이 케이스.
digitalWrite(Relay15, HIGH); // <=== Low level trigger 릴레이 케이스.
// digitalWrite(Relay15, LOW); // <=== High level trigger 릴레이 케이스.
}
void loop() {
///*
// Delay between measurements.
delay(delayMS);
// Get temperature event and print its value.
sensors_event_t event;
//*/
EthernetClient client = server.available();
if (client) {
Serial.println("new client detected!!");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
char c = 0x00;
while (client.connected()) {
if (client.available()) {
//char c = client.read();
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
////////////////////////////////////////////////
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
Serial.print(c);
}
////////////////////////////////////////////////
if (c == '\n' && currentLineIsBlank) {
Serial.println("test1");
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// 브라우저 5초마다 새로고침
if (readString.indexOf("?pw_1powerreset=xxx") >0){
} else if (readString.indexOf("?pw_2powerreset=xxx") >0){
} else {
//client.println("<meta http-equiv=\"refresh\" content=\"5\">");
client.println("<meta http-equiv=\"refresh\" content=\"10\">"); // <========================================================
}
///////////////////////////////////////////////
//client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
//client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
//client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
//client.println("<link rel='stylesheet' type='text/css' href='https://drive.google.com/file/d/1y3dAAuyWUN2v2tn0Gh7mVEkE8llM4qI9/view?usp=sharing' />");
///////////////////////////////////////////////
client.println("<title>");
client.print("Welcome Mining room by ARDUINO");
client.println("</title>");
int A;
float temp =0;
float humi = 0;
//*
//////////////////////////////////////////////
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println("Error reading temperature!");
} else {
temp = event.temperature;
Serial.print("Temperature: ");
Serial.print(event.temperature);
Serial.println(" *C");
//////////////////////////
if(event.temperature > SETTED_TEMPERATURE) {
digitalWrite(Relay8, HIGH);
} else {
digitalWrite(Relay8, LOW);
}
//////////////////////////
}
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println("Error reading humidity!");
}
else {
humi = event.relative_humidity;
Serial.print("Humidity: ");
Serial.print(event.relative_humidity);
Serial.println("%");
//////////////////////////
if(event.relative_humidity > SETTED_HUMIDITY) {
//digitalWrite(Relay, HIGH);
} else {
//digitalWrite(Relay, LOW);
}
//////////////////////////
}
//*/
//int read( float& humidity, float& temperature);
//if((A=dht11.read(humi, temp))==0) {
// 온습도센서(DHT-11)로부터 값 출력
//Serial.println("test2");
//Serial.print(temp);
//}
//////////////////////////////////////////////
client.println("<center>");
client.println("<h1>");
client.print("Mining Room Monitor");
client.println("</h1>");
client.println("<h2>");
client.print("Mining Room Temperature and Humidity status.");
client.println("</h2>");
client.println("<h4>");
client.print("Temperature : ");
client.print(temp);
client.print("<sup>0</sup>");
client.print("C");
client.println("<br />");
client.print("Humidity : ");
client.print(humi);
client.print("%");
client.println("<br />");
client.print("Ventilator status : ");
if(digitalRead(Relay8) == 1) {
client.print("On");
} else {
client.print("Off");
}
client.println("<br />");
client.println("</h4>");
client.println("</center>");
client.println("<br />");
// auto 한/영 변환 자유로움
// active 한글기본, 한/영 변환이 자유로움
// inactive 영문기본, 한/영 변환이 자유로움
// disabled 영문기본, 한/영 변환이 안됨, 영문/숫자만 입력 가능.
// <input type="text" name="" value="" style="ime-mode:active;">
//client.println("<input type=\"submit\" value=\"#1_PowerReset\" />");
client.print("<form>");
client.print("Password : ");
client.println("<input type=\"password\" placeholder=\"Password\" name=\"pw_1powerreset\" value=\"\" style=\"ime-mode:disabled;\"><br />");
client.println("<input type=\"submit\" value=\"worker110_PowerReset\" onclick=\"history.back(-1)\" />");
client.println("<br />");
client.print("</form>");
client.println("<br />");
client.print("<form>");
client.print("Password : ");
client.println("<input type=\"password\" placeholder=\"Password\" name=\"pw_2powerreset\" value=\"\" style=\"ime-mode:disabled;\"><br />");
client.println("<input type=\"submit\" value=\"worker111_PowerReset\" onclick=\"history.back(-1)\" />");
client.println("<br />");
client.print("</form>");
client.println("<br />");
client.println("</html>");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
//Serial.println("test3");
}
else if (c != '\r') {
currentLineIsBlank = false;
//Serial.println("test4");
}
} else {
//Serial.println("test5");
}
}
//브라우저 데이터 받는 시간
delay(1);
//연결 종료:
client.stop();
Serial.println("client disonnected");
///////////////////////////////////////////////////
if (c == '\n') {
Serial.print("readString : ");
Serial.println(readString); //print to serial monitor for debuging
if (readString.indexOf("?pw_1powerreset=xxx") >0){
digitalWrite(Relay14, LOW); // <=== Low level trigger 릴레이 케이스.
//digitalWrite(Relay14, HIGH); // <=== High level trigger 릴레이 케이스.
delay(2000); // 2 secs..
digitalWrite(Relay14, HIGH); // <=== Low level trigger 릴레이 케이스.
//digitalWrite(Relay14, LOW); // <=== High level trigger 릴레이 케이스.
} else if (readString.indexOf("?pw_2powerreset=xxx") >0){
digitalWrite(Relay15, LOW); // <=== Low level trigger 릴레이 케이스.
//digitalWrite(Relay15, HIGH); // <=== High level trigger 릴레이 케이스.
delay(2000); // 2 secs..
digitalWrite(Relay15, HIGH); // <=== Low level trigger 릴레이 케이스.
//digitalWrite(Relay15, LOW); // <=== High level trigger 릴레이 케이스.
}
//clearing string for next read
readString="";
}
///////////////////////////////////////////////////
} else {
//*
Serial.println("client not detected!!");
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println("Error reading temperature!");
} else {
Serial.print("Temperature: ");
Serial.print(event.temperature);
Serial.println(" *C");
//////////////////////////
if(event.temperature > SETTED_TEMPERATURE) {
digitalWrite(Relay8, HIGH);
} else {
digitalWrite(Relay8, LOW);
}
//////////////////////////
}
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println("Error reading humidity!");
}
else {
Serial.print("Humidity: ");
Serial.print(event.relative_humidity);
Serial.println("%");
//////////////////////////
if(event.relative_humidity > SETTED_HUMIDITY) {
//digitalWrite(Relay, HIGH);
} else {
//digitalWrite(Relay, LOW);
}
//////////////////////////
}
Serial.print("Ventilator status : ");
if(digitalRead(Relay8) == 1) {
Serial.print("On");
} else {
Serial.print("Off");
}
Serial.println(".");
//*/
}
//Serial.println("=============================");
}
///////////////////////////////////////////////////////////
파워선 + 와 Reset + 둘을 묶어 주고 파워선 - , Reset - 를 묶어주어서 스위치 하나를 달아주면서
각 + , - 선 두가닥을 릴레이 쪽으로 빼줍니다..
스위치를 달아준것은 수동으로 reset 혹은 power on 을 위해서 입니다.
( 파워선과 Reset 버튼을 동시에 인가 시키게 되면 꺼져 있던 피씨는 켜지며 켜져있던 피씨는 reset 이 되게 됩니다. )
- 제가 사용하는 보드들은 그러했는데요.. 다른 보드들도 모두 동일한지는 모르겠습니다...
참고로 피씨의 On/Off 상태는 Power LED 쪽에 5V 가 인가가 되어 있습니다.
해당 부분에 전선으로 빼서 Digital Input 으로 읽으면 피씨의 On/Off 상태도 웹에 표시해줄수 있을 것으로 생각 됩니다.
- 아직 해당 부품 택배가 안와서... 해당 부분 테스트 전입니다..
참고로 하기 8개 짜리 릴레이 제품은 비추 입니다..
TONGLING 이라는 하나짜리 릴레이는 의도한 바와 정확히 일치 했습니다..
다중 릴레이는 TONGLING 이라는 거 사용한 다른 제품들 더 테스트 진행하려 합니다...
하기 패스워드 창에 입력후 버튼 누르면 피씨를 On 시키거나 켜져 있는 경우 reset 을 시키게 됩니다.
습도가 21% .... --,,,,
방 무진장 건조하네요.. --,,
.... 저는 이만.. 내일을 위해 이제 잠자리로.....
-------------------------------------
꼬리말
* 게시글 내용 삭제시 레벨 강등
* 질문은 각 주제별 게시판에.
비트코인 암호화화폐 커뮤니티 땡글~ 땡글~
-------------------------------------