주니봉
  • [아두이노] 온도측정 센서 만들기(3) - ESP8266(WIFI)
    2023년 02월 16일 10시 58분 52초에 업로드 된 글입니다.
    작성자: 봉주니

    앞서 구매한 WIFI 모듈을 사용해보려 한다.

     

    ESP8266 을 연결하기 위해서는 저항이 필요했고, 간단히 아래와 같이 설계가 가능했다.

     

    현재는 thinkercad에서 ESP8266 모듈을 지원하지 않아, 구글링하면 사람들이 만들어놓은 회로를 사용가능하다.

     

     

    위와 같이 구성하기 위해서 문제점이 생겼다.

    저항을 구매하지 않았고, 점퍼 케이블을 M/F 만 구매해서, 브레브보드와 아두이노를 연결할 수가 없었다...

    2차 구매를 한다...

     

    1) M/M 점퍼 케이블을 구매

    2) ESP8266을 저항없이 쓰기위해 ESP-01 어댑터 모듈을 구매!!

     

    ESP8266 에 ESP-01 어댑터를 연결하여, 와이파이 연결을 해본다.

     

    ESP-01 어댑터의 그림은 아래와 같고, 어댑터 자세히 핀별 구성이 적혀있어 그대로 꽂아주기만 하면된다.

     

    주의사항 ) 어댑터와 아두이노의 TX, RX는 반대방향이다.

    어댑터 TX <-> 아두이노 RX

    어댑터 RX <-> 아두이노 TX

    어댑터 VCC <-> 아두이노 5V

    어댑터 GND <-> 아두이노 GND

     

    그리고 위에 어댑터 없이 연결하는 경우, 저항을 연결하고 3.3V 를 연결해야 모듈에 무리가 가지 않는다고 한다.

    하지만 어댑터를 사용하는 경우 5V에 꽂아도 사용가능하다! 어댑터가 편리한듯!

     

    아래와 같이 코드를 작성해줍니다.

    baudrate 가 일반적으로 아두이노(hardware)인 경우 115200로 테스트를 했고, esp8266 software는 모듈마다 다르고 다양한 글에서 59600까지만 지원 가능하다고 하는데, 어떤게 정확한 정보를 의미하는지는 잘모르겠다.

     

    제가 구매한 ESP-8266는 기본 통신속도가 115200 으로 되어있었다.

    https://www.devicemart.co.kr/goods/view?no=1279338 

     

    ESP8266 시리얼 와이파이 모듈 ESP-01 (DIP) [SZH-EK051]

    Espressif Systems사에서 내놓은 [ESP8266]은 가장 가격대 성능비가 뛰어난 것으로 알려진 대중적이고 활용도가 높은 시리얼 와이파이 칩셋입니다. [802.11 b/g/n 프로토콜]과 [WiFi Direct], [Soft-AP]기능도 지

    www.devicemart.co.kr

    그리고, 아두이노 시리얼 통신의 경우 9600으로 해야지만 통신이 가능했습니다. 이유는...찾지 못했습니다.

     

     

    /*
     WiFiEsp example: ConnectWPA
     
     This example connects to an encrypted WiFi network using an ESP8266 module.
     Then it prints the  MAC address of the WiFi shield, the IP address obtained
     and other network details.
    
     For more details see: http://yaab-arduino.blogspot.com/p/wifiesp-example-connect.html
    */
    
    #include "WiFiEsp.h"
    
    // Emulate Serial1 on pins 6/7 if not present
    #ifndef HAVE_HWSERIAL1
    #include "SoftwareSerial.h"
    SoftwareSerial Serial1(6, 7); // RX, TX
    #endif
    
    char ssid[] = "juni";            // your network SSID (name)
    char pass[] = "12345678";        // your network password
    int status = WL_IDLE_STATUS;     // the Wifi radio's status
    
    void setup()
    {
      // initialize serial for debugging
      Serial.begin(9600);
      // initialize serial for ESP module
      Serial1.begin(115200);
      // initialize ESP module
      WiFi.init(&Serial1);
    
      // check for the presence of the shield
      if (WiFi.status() == WL_NO_SHIELD) {
        Serial.println("WiFi shield not present");
        // don't continue
        while (true);
      }
    
      // attempt to connect to WiFi network
      while ( status != WL_CONNECTED) {
        Serial.print("Attempting to connect to WPA SSID: ");
        Serial.println(ssid);
        // Connect to WPA/WPA2 network
        status = WiFi.begin(ssid, pass);
      }
    
      Serial.println("You're connected to the network");
    }
    
    void loop()
    {
      // print the network connection information every 10 seconds
      Serial.println();
      printCurrentNet();
      printWifiData();
      
      delay(10000);
    }
    
    void printWifiData()
    {
      // print your WiFi shield's IP address
      IPAddress ip = WiFi.localIP();
      Serial.print("IP Address: ");
      Serial.println(ip);
    
      // print your MAC address
      byte mac[6];
      WiFi.macAddress(mac);
      char buf[20];
      sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
      Serial.print("MAC address: ");
      Serial.println(buf);
    }
    
    void printCurrentNet()
    {
      // print the SSID of the network you're attached to
      Serial.print("SSID: ");
      Serial.println(WiFi.SSID());
    
      // print the MAC address of the router you're attached to
      byte bssid[6];
      WiFi.BSSID(bssid);
      char buf[20];
      sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
      Serial.print("BSSID: ");
      Serial.println(buf);
    
      // print the received signal strength
      long rssi = WiFi.RSSI();
      Serial.print("Signal strength (RSSI): ");
      Serial.println(rssi);
    }

     

    어김없이 Wifiesp.h 오류가 난다. 라이브러리 설치를 해줘야하는데, .zip 파일로 처리해준다.

    WiFiEsp.zip
    0.06MB

     

    다운로드 받은 파일을 Arduino IDE 에 라이브러리 등록시켜준다.

     

     

    Upload 결과!!

    연결은 가능하지만 안정적인 연결상태를 계속 유지하지는 못하는 것 같습니다.

    위에 값은 연결안되고, 아래에서 정상 연결되면 정확한 값이 표기됩니다!!!

     

    완전한 연결상태를 유지하지는 못하지만, 다음에는 온도측정 후 DB에 저장시켜 보도록 하겠습니다.

    반응형
    댓글