임베디드 보드/아두이노

아두이노 메가 + WIFI모듈 ESP8266 + 팬모터 송풍기 제어

ZEROWIN.TECH 2020. 7. 13. 17:41
728x90

아두이노 메가

https://store.arduino.cc/usa/mega-2560-r3

 

Arduino Mega 2560 Rev3 | Arduino Official Store

The Arduino Mega 2560 is a microcontroller board based on the ATmega2560. It has 54 digital input/output pins (of which 15 can be used as PWM outputs), 16 analog inputs, 4 UARTs (hardware serial ports), a 16 MHz crystal oscillator, a USB connection, a powe

store.arduino.cc

Arduino Mega 2560은 ATmega2560을 기반으로하는 마이크로 컨트롤러 보드입니다. 54 개의 디지털 입력 / 출력 핀 (15 개는 PWM 출력으로 사용 가능), 16 개의 아날로그 입력, 4 개의 UART (하드웨어 직렬 포트), 16MHz 수정 발진기, USB 연결, 전원 잭, ICSP 헤더, 및 리셋 버튼. 마이크로 컨트롤러를 지원하는 데 필요한 모든 것이 포함되어 있습니다. USB 케이블로 컴퓨터에 연결하거나 AC-DC 어댑터 또는 배터리로 전원을 켜서 시작하십시오. Mega 2560 보드는 Uno 및 이전 보드 Duemilanove 또는 Diecimila를 위해 설계된 대부분의 쉴드와 호환됩니다.

 

하드웨어 스펙

https://en.wikipedia.org/wiki/ESP8266

 

 

ESP8266 - Wikipedia

ESP8266ESP-01 module by Ai-ThinkerManufacturerEspressif SystemsType32-bit microcontrollerCPU@ 80 MHz (default) or 160 MHzMemory32 KiB instruction, 80 KiB user dataInput16 GPIO pinsPower3.3 V DCSuccessorESP32 The ESP8266 is a low-cost Wi-Fi microchip, with

en.wikipedia.org

ESP8266은 중국 상하이의 Espressif Systems [1]가 생산 한 전체 TCP / IP 스택 및 마이크로 컨트롤러 기능을 갖춘 저렴한 Wi-Fi 마이크로 칩입니다.

 

아두이노 소스코드

#include "WiFiEsp.h"
int status = WL_IDLE_STATUS;     // the Wifi radio's status
WiFiEspServer server(800);

char ssid[] = "ZEROWIN";         // your network SSID (name)
char pass[] = "zzzzzzzz";        // your network password

int FAN_MOTOR_PIN = 3;

void printWifiStatus()
{
#if 1 // #if DEBUG
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
#endif

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
#if 1 // #if DEBUG
  Serial.print("IP Address: ");
  Serial.println(ip);
#endif

  // print the received signal strength
  long rssi = WiFi.RSSI();
  
#if 1 // #if DEBUG
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
#endif
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  // initialize serial for ESP module
  Serial3.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial3);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    #if DEBUG
    Serial.println("WiFi shield not present");
  #endif
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    #if DEBUG
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
  #endif
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }
  #if DEBUG
  Serial.println("You're connected to the network");
  #endif
  printWifiStatus(); // display IP address on LCD
  delay(2000);
  
  server.begin(); 

  pinMode(FAN_MOTOR_PIN, OUTPUT);
}

#define R_MAXNUM 32 // 64
char rData[R_MAXNUM] = { 0x00, };
int rPos = 0;

void loop() {

  WiFiEspClient c = server.available();
  if(c) {
    #if DEBUG
    Serial.println("RECV: ");
    #endif
    boolean bDataRead = false;
    
    while (0 < c.available()) {
        char ch = c.read();
      #if DEBUG
        Serial.write(ch);
      #endif
      bDataRead = true;

      rData[rPos] = ch; rPos += 1;
    }
    
    if(bDataRead == true) {
      #if DEBUG
      Serial.println();
      #endif
      // c.print(rData);

      if(5 <= rPos)
      { 
        Serial.println(rData);
        
        if(memcmp(rData, "C_M-", 4) == 0)
        {
        
          int power = atoi(rData+4);
          analogWrite(FAN_MOTOR_PIN, power);
          Serial.print("FAN MOTOR=");
          Serial.println(power);

          c.print("OK");
        }

        rPos = 0;
        memset(rData, 0x00, R_MAXNUM);

        
      }
    }
    delay(10);

    // close the connection:
    c.stop();
    // Serial.println("Client disconnected");
  }
}

 

테스트영상

아두이노 메가 보드에 WIFI Tcp 서버를 구축하고 피시에서 팬모터 제어 데이터 신호를 송신합니다.

정상 동작하면 피시에서 OK 신호를 수신합니다.

 

Node-RED Slider 를 이용하여 팬모터를 제어합니다.