일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Login
- javascipt
- sns로그인
- IntelliJ
- 콘솔한글깨짐
- KAKAO
- Reduce
- getters
- react
- Express.js
- node.js
- javscript
- azure
- 연동
- includes
- v-for
- v-on
- Emit
- Vue.js
- axios
- 음양더하기
- v-if
- programmers
- mutations
- 템플릿문법
- state
- vuex
- JavaScript
- mixins
- kibana
- Today
- Total
공부용
jsn270으로 5v릴레이모듈 켜기 본문
//참고
http://gothiczzang.tistory.com/10
http://gothiczzang.tistory.com/13
2편
수정할부분을 빨간색으로 고치겠습니다
//JSN270_Simple_Web_Server
#include <Debug.h>
#include <JSN270.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#define SSID "JSN270_2G" // 접속할 WiFi의 아이디 -> 예를들어 gothiczzang 이면 "gothiczzang"으로 수정
#define KEY "12345678" // 접속할 WiFi의 비밀번호 -> 예를들어 gothiczzang 이면 "gothiczzang"으로 수정
#define AUTH "WPA2" // your wifi network security (NONE, WEP, WPA, WPA2)
#define USE_DHCP_IP 1
#if !USE_DHCP_IP
#define MY_IP "192.168.1.133"
#define SUBNET "255.255.255.0"
#define GATEWAY "192.168.1.254"
#endif
#define SERVER_PORT 80
#define PROTOCOL "TCP"
int led_status = 0;
SoftwareSerial mySerial(3, 2); // RX, TX
JSN270 JSN270(&mySerial);
void setup() {
char c;
mySerial.begin(9600);
Serial.begin(9600);
Serial.println("--------- JSN270 Simple HTTP server Test --------");
// wait for initilization of JSN270
delay(5000);
//JSN270.reset();
delay(1000);
//JSN270.prompt();
JSN270.sendCommand("at+ver\r");
delay(5);
while(JSN270.receive((uint8_t *)&c, 1, 1000) > 0) {
Serial.print((char)c);
}
delay(1000);
#if USE_DHCP_IP
JSN270.dynamicIP();
#else
JSN270.staticIP(MY_IP, SUBNET, GATEWAY);
#endif
if (JSN270.join(SSID, KEY, AUTH)) {
Serial.println("WiFi connect to " SSID);
}
else {
Serial.println("Failed WiFi connect to " SSID);
Serial.println("Restart System");
return;
}
delay(1000);
JSN270.sendCommand("at+wstat\r");
delay(5);
while(JSN270.receive((uint8_t *)&c, 1, 1000) > 0) {
Serial.print((char)c);
}
delay(1000);
JSN270.sendCommand("at+nstat\r");
delay(5);
while(JSN270.receive((uint8_t *)&c, 1, 1000) > 0) {
Serial.print((char)c);
}
delay(1000);
}
void loop() {
if (!JSN270.server(SERVER_PORT, PROTOCOL)) {
Serial.println("Failed connect ");
Serial.println("Restart System");
} else {
Serial.println("Waiting for connection...");
}
String currentLine = ""; // make a String to hold incoming data from the client
int get_http_request = 0;
while (1) {
if (mySerial.overflow()) {
Serial.println("SoftwareSerial overflow!");
}
if (JSN270.available() > 0) {
char c = JSN270.read();
Serial.print(c);
if (c == '\n') { // if the byte is a newline character
if (currentLine.length() == 0) {
if (get_http_request) {
Serial.println("new client");
//Serial.println("HTTP RESPONSE");
// Enter data mode
JSN270.sendCommand("at+exit\r");
delay(100);
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
JSN270.println("HTTP/1.1 200 OK");
JSN270.println("Content-type:text/html");
JSN270.println();
// the content of the HTTP response follows the header:
JSN270.print("Click <a href=\"/H\">here</a> turn the relay on<br>"); //켜짐 전송
JSN270.print("Click <a href=\"/L\">here</a> turn the relay off<br>"); //꺼짐 전송
// The HTTP response ends with another blank line:
JSN270.println();
// wait until all http response data sent:
delay(1000);
// Enter command mode:
JSN270.print("+++");
delay(100);
// break out of the while loop:
break;
}
}
// if you got a newline, then clear currentLine:
else {
// Check the client request:
if (currentLine.startsWith("GET / HTTP")) {
Serial.println("HTTP REQUEST");
get_http_request = 1;
}
else if (currentLine.startsWith("GET /H")) {
//Serial.println("GET HIGH");
get_http_request = 1;
digitalWrite(relay, HIGH); //켜짐
}
else if (currentLine.startsWith("GET /L")) {
//Serial.println("GET LOW");
get_http_request = 1;
digitalWrite(relay, ROW); //꺼짐
}
currentLine = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
if (led_status) {
JSN270.sendCommand("at+gpioset=15,1\r"); // GET /H turns the LED on
}
else {
JSN270.sendCommand("at+gpioset=15,0\r"); // GET /H turns the LED off
}
// close the connection
JSN270.sendCommand("at+nclose\r");
Serial.println("client disonnected");
}
'2018-2 IoT 가습기 > 아두이노' 카테고리의 다른 글
hm-10 블루투스 모듈 사용하기 (0) | 2018.11.16 |
---|---|
jsn270 사용하기 (2) | 2018.11.16 |
DHT11 & 릴레이모듈 설정하기 (0) | 2018.11.16 |
부품내용 (0) | 2018.11.16 |