본문 바로가기

IOT(Arduino+Android)

nodeMCU 1.0( ESP8266- Module)

준비

1 : Arduino IDE  툴  설치(arduino.cc)

2 : nodeMCU 1.0( ESP8266- Module) 보드와 USB케이블로 컴퓨터에 연결

3 :메뉴 파일- 환경설정에 다음 URL 추가

 http://arduino.esp8266.com/stable/package_esp8266com_index.json

4:  보드 설정

 

5:  파일- 예제 불러오기

 

6: 사용 중인 wifi 공유기 SSID, PASS를 소스 void setup() 함수에 내에 설정 합니다.  

8: 업로드 실행

A

 

완료 되었습니다.

 

9: 통신 속도를 115200 선택하고,  RST버튼을 동작합니다.

 

사용중인 공유기 명칭과  IP가 나타납니다.

10: http://192.168.0.4에 접속해 봅니다. (아이폰, 안드로이드 폰, 사용 중인 PC)

 

사각형 링크를 크릭하면 보드 LED가 ON, OFF 합니다.

PC 접속

 

폰에서도  접속 해 봅니다.

[주의] 할 점은 모두 같은 공유기에 접속되어야 합니다.

 

 

//예제를 살펴봅나다

 

 

#include <ESP8266WiFi.h>

#ifndef STASSID
#define STASSID "iptime"
#define STAPSK  "12345678"
#endif


const char* ssid = STASSID;
const char* password = STAPSK;

// 클라이언트의 요청을 처리하기 위해 서버 기본 포트 80으로 생성합니다. 
WiFiServer server(80);

 

// WiFiServer server(8050); 이라면 접속시 주소 예 (http://192.168.0.4:8050으로 해야 합니다.

void setup() {
  Serial.begin(115200);

  // prepare LED
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 0);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print(F("Connecting to "));
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
  }
  Serial.println();
  Serial.println(F("WiFi connected"));

  // Start the server
  server.begin();
  Serial.println(F("Server started"));

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.println(F("new client"));

  client.setTimeout(5000); // default is 1000

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(F("request: "));
  Serial.println(req);

  // Match the request
  int val;
  if (req.indexOf(F("/gpio/0")) != -1) {
    val = 0;
  } else if (req.indexOf(F("/gpio/1")) != -1) {
    val = 1;
  } else {
    Serial.println(F("invalid request"));
    val = digitalRead(LED_BUILTIN);
  }

  // Set LED according to the request
  digitalWrite(LED_BUILTIN, val);

  // read/ignore the rest of the request
  // do not client.flush(): it is for output only, see below
  while (client.available()) {
    // byte by byte is not very efficient
    client.read();
  }

  // Send the response to the client
  // it is OK for multiple small client.print/write,
  // because nagle algorithm will group them into one single packet
  client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now "));
  client.print((val) ? F("high") : F("low"));
  client.print(F("<br><br>Click <a href='http://"));
  client.print(WiFi.localIP());
  client.print(F("/gpio/1'>here</a> to switch LED GPIO on, or <a href='http://"));
  client.print(WiFi.localIP());
  client.print(F("/gpio/0'>here</a> to switch LED GPIO off.</html>"));

  // The client will actually be *flushed* then disconnected
  // when the function returns and 'client' object is destroyed (out-of-scope)
  // flush = ensure written data are received by the other side
  Serial.println(F("Disconnecting from client"));
}

 

외부에 LED를 연결합니다.

 

점프선 정리 후

사진 첨부

 

주석문에는 "GPIO2" 용어는 아래 그림의 사각형 단자 입니다 

 

특별히 보드에 이단자에는 LED를 연결 해 두었습니다.

단자의 변수 이름은 "LED_BUILTIN" 입니다.

 

pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 0);

 

외부 "GPIO2" 에 LED 를 다음과 같이 연결합니다.

하이퍼링크 소스입니다. 

 

  client.print(F("/gpio/1'>here</a> to switch LED GPIO on, or <a href='http://"));
  client.print(F("/gpio/0'>here</a> to switch LED GPIO off.</html>"));

 

<a href = " "> :  문서내, 외부문서, 다른 사이트로 이동하는 링크 기능 태그 입니다.

(hypertext reference)

/gpio/1 : ON (외부 LED) ,내부 LED : OFF

/gpio/0 : ON (내부 LED), 외부 LED : OFF 입니다.

 

문제 : 내외부 LED가 똑 같이 동작하도록 회로도만 구성 해봅니다.

 

 

[도전 1]  GPIO5 단자에 LED 연결하고 소스를 수정하여 실행해봅니다.

ㅓㅇ해

'IOT(Arduino+Android)' 카테고리의 다른 글

nodeMCU 1.0 과 DHT11온도  (0) 2022.04.18
LoLin Board  (0) 2022.02.03