NodeMCU wifi 연결 및 led 동작 예제
정보2020. 7. 22. 06:00
NodeMCU wifi 연결 및 led 동작 예제
아두이노에 wifi를 연결을 할려면 wifi 쉴드를 장착을 해야 연결을 할 수 있습니다. wifi 쉴드는 처음 아두이노에 입문을 한다면 비용이 더 나갈 수도 있습니다. 비용 나가는 것 대신에 애초에 wifi 모듈이 같이 있는 NodeMCU도 좋은 대안이 될 수도 있습니다.
wifi 쉴드가 내장이 되어 있어 집에 공유기가 설치되어 있으면 연결을 할 수도 있습니다. wifi를 활용을 하여 다양하게 프로젝트를 진행해볼 수도 있습니다.
NodeMCU wifi 연결 및 led 동작 소스
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #include <SoftwareSerial.h> #include <WiFiClient.h> #include <ESP8266mDNS.h> #include <ESP8266HTTPClient.h> const char* ssid = "와이파이 이름"; const char* password = "패스워드"; ESP8266WebServer server(80); SoftwareSerial s(D6,D5); int led = 14; void setup() { pinMode(led, OUTPUT); digitalWrite(led, LOW); Serial.begin(9600); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status( ) != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP( )); // 웹브라우저에서 ip 주소로 접속했을 때 보여주는 문구 server.on("/", [ ]( ){ server.send(200, "text/html", "<a href='/on'>on</a><br><a href='/off'>off</a>"); }); // LED 켜기 server.on("/on", [ ]( ){ digitalWrite(led, HIGH); server.send(200, "text/html", "<a href='/on'>[on]</a><br><a href='/off'>off</a>"); }); // LED 끄기 server.on("/off", [ ]( ){ digitalWrite(led, LOW); server.send(200, "text/html", "<a href='/on'>on</a><br><a href='/off'>[off]</a>"); }); //웹서버 시작 server.begin(); Serial.println("HTTP server started"); } void loop() { HTTPClient http; //Declare object of class HTTPClient String ADCData, station, postData; int adcvalue=analogRead(A0); //Read Analog value of LDR ADCData = String(adcvalue); //String to interger conversion station = "A"; //Post Data postData = "status=" + ADCData + "&station=" + station ; http.begin("http://192.168.1.91/c4yforum/postdemo.php"); //Specify request destination http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header int httpCode = http.POST(postData); //Send the request String payload = http.getString(); //Get the response payload Serial.println(httpCode); //Print HTTP return code Serial.println(payload); //Print request response payload http.end(); //Close connection delay(5000); //Post Data at every 5 seconds server.handleClient( );//클라이언트 접속을 관리합니다. } | cs |
'정보' 카테고리의 다른 글
php 최고값 얻는 함수 max 사용하는 방법 (0) | 2020.07.28 |
---|---|
php 날짜 함수 date 사용하는 방법 (0) | 2020.07.26 |
php 비어있는 변수인지 확인하는 함수 empty 사용하는 방법 (0) | 2020.07.21 |
php 변수 존재 유무 확인하는 함수 isset 사용하는 방법 (0) | 2020.07.20 |
php 정수 확인하는 함수 is_int 사용하는 방법 (0) | 2020.07.17 |
댓글()