-
#7 원격으로 선풍기 제어하기Arduino 2019. 6. 18. 20:14
기본구조
아두이노(esp-01 와이파이모듈(Server)-------공유기(AP)-------Spring(Client)
1.아두이노 코드 - AT Command를 이용하여 서버로 사용했다.
또한 릴레이 모듈을 이용하였음
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081#include <SoftwareSerial.h>#define DEBUG trueSoftwareSerial esp8266(2, 3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.// This means that you need to connect the TX line from the esp to the Arduino's pin 2// and the RX line from the esp to the Arduino's pin 3int Relaypin = 5;void setup() {pinMode(Relaypin, OUTPUT);Serial.begin(9600);esp8266.begin(9600); // your esp's baud rate might be differentsendData("AT+RST\r\n", 2000, DEBUG); // reset modulesendData("AT+CIOBAUD?\r\n", 2000, DEBUG); // check baudrate (redundant)sendData("AT+CWMODE=3\r\n", 1000, DEBUG); // configure as access point (working mode: AP+STA)sendData("AT+CWLAP\r\n", 3000, DEBUG); // list available access pointssendData("AT+CWJAP=\"iptime\",\"a12345678\"\r\n", 6000, DEBUG); // join the access pointsendData("AT+CIFSR\r\n", 1000, DEBUG); // get ip addresssendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple connectionssendData("AT+CIPSERVER=1,7777\r\n", 1000, DEBUG); // turn on server on port 80}void loop() {Serial.print(".");if (esp8266.available()) { // check if the esp is sending a messageSerial.println("Available()");if (esp8266.find("+IPD,")) {delay(1000); // wait for the serial buffer to fill up (read all the serial data)// get the connection id so that we can then disconnectint connectionId = esp8266.read() - 48; // subtract 48 because the read() function returns// the ASCII decimal value and 0 (the first decimal number) starts at 48esp8266.find("pin="); // advance cursor to "pin="int pinNumber = (esp8266.read() - 48) * 10; // get first number i.e. if the pin 13 then the 1st number is 1, then multiply to get 10pinNumber += (esp8266.read() - 48); // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first numberif (pinNumber == 11) {digitalWrite(Relaypin, HIGH);Serial.println("Relay ON");}if(pinNumber == 12) {digitalWrite(Relaypin, LOW);Serial.println("Relay OFF");}// make close commandString closeCommand = "AT+CIPCLOSE=";closeCommand += connectionId; // append connection idcloseCommand += "\r\n";sendData(closeCommand, 1000, DEBUG); // close connection}}delay(1000);}/*Name: sendDataDescription: Function used to send data to ESP8266.Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)Returns: The response from the esp8266 (if there is a reponse)*/String sendData(String command, const int timeout, boolean debug) {String response = "";esp8266.print(command); // send the read character to the esp8266long int time = millis();while ( (time + timeout) > millis()) {while (esp8266.available()) {// The esp has data so display its output to the serial windowchar c = esp8266.read(); // read the next character.response += c;}}if (debug) {Serial.print(response);}return response;}cs Spring 코드 - ajax를 이용하여 http통신을 했다.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@include file="../include/header.jsp" %><link rel="stylesheet" type="text/css" href="/resources/css/manage/remoteMain.css" /><div class="container2"><c:forEach var="myiot" items="${myIot}"><figure><div class="switch"><%-- 보일러 사진 --%><c:if test="${myiot.model_name eq 'boiler' }"><img class="window" src="resources/images/boiler.png" /><br/></c:if><%-- 창문 사진 --%><c:if test="${myiot.model_name eq 'window' }"><img class="window" src="resources/images/window.png" /><br/></c:if><%-- 환풍기 사진 --%><c:if test="${myiot.model_name eq 'ventilator' }"><img class="window" src="resources/images/Ventilator.jpg" /><br/></c:if><%-- 공기청정기 사진 --%><c:if test="${myiot.model_name eq 'aircleaner' }"><img class="window" src="resources/images/airclean.jpg" /><br/></c:if><div class="nickName"><b>${myiot.iot_id }</b></div><div class="local">${myiot.model_name } 원격제어 <br/> 위치: ${myiot.place_name}</div><div class="on_off"><label class="rocker rocker-small"><input type="checkbox" class="onOff" value="check"><span id="11" class="switch-left choice">ON</span><span id="12" class="switch-right choice">OFF</span></label><label class="rocker rocker-small"><input type="checkbox" class="autoManual"><span class="switch-auto choice">자동</span><span class="switch-manual choice">수동</span></label></div></div></figure></c:forEach><button id="btn-reg">IoT 제품 제어 등록</button></div><script type="text/javascript">$(document).ready(function() {$(".choice").click(function() {var p = $(this).attr('id'); // get id value (i.e. pin13, pin12, or pin11)if($(this).attr('id') == 11){alert("ON");}else if($(this).attr('id') == 12){alert("OFF");}// send HTTP GET request to the IP address with the parameter "pin" and value "p", then execute the function$.get("http://39.127.7.97:7777/", {pin : p}); // execute get request (아두이노 웹서버 IP 주소로 고쳐 준다)});});</script><script src='resources/js/manage/remoteMain.js'></script><%@include file="../include/footer.jsp" %>cs 실행 결과 - ON 버튼 클릭하면 미리 비치 된 선풍기가 켜지고 OFF 버튼 클릭하면 선풍기가 꺼지는 것을 알 수 있습니다~
'Arduino' 카테고리의 다른 글
#3 LCD I2C 사용 (0) 2019.06.14 #2 ESP-01 와이파이 모듈을 이용한 미세먼지 측정 (0) 2019.06.12 #1 아두이노란? (0) 2019.06.11