[智能硬件][WIFISocket]基於ESP8266的智能插座開發(2)_WIFISocketLite

在本文中,使用WeMos D1 mini開發板實現通過硬件按鈕控制繼電器開關的功能。

Arduino IDE的配置

在這一小節中,需要將IDE配置成功。請參考

ESP8266的GPIO

本文的所有GPIO引腳都尊從WeMos D1 mini的定義。
請參考

電路原理圖

ESP12N

繼電器模塊

按鍵指示燈
GPIO配置如下:

  • 繼電器IO:GPIO5
  • 按鍵IO:GPIO4
  • LEDIO:GPIO2(模塊內置LED)

實物圖

實物圖

代碼

WIFISocketLED.h

#ifndef __WIFISOCKETLED_H__
#define __WIFISOCKETLED_H__
/*JinZhu 17/9/22 Review */
#include<Arduino.h>
#include <Ticker.h>
#define LED_PIN         LED_BUILTIN
#define BLINK_LED_DELAY 1000

class WIFISocketLEDClass
{
  public:
    WIFISocketLEDClass();
    void begin(int pin = LED_PIN);
    void Blink(int time = BLINK_LED_DELAY);
    void Blink(int time, int count);
    void BlinkDelay(int time = BLINK_LED_DELAY);
    void BlinkDelay(int time, int count);
    void BlinkHandle();
    void Switch(bool state);
    bool getState();
  private:
    Ticker      ticker;
    int         blinkCount;
  private:
    int     pin;
    bool    state;
};
extern WIFISocketLEDClass    WIFISocketLED;
#endif

WIFISocketLED.cpp

#include "WIFISocketLED.h"
WIFISocketLEDClass    WIFISocketLED;

static void WIFISocketTickerHandle(WIFISocketLEDClass* WIFISocketLED)
{
  WIFISocketLED->BlinkHandle();
}

WIFISocketLEDClass::WIFISocketLEDClass()
{
  this->pin = LED_PIN;
  this->state = false;
  this->blinkCount = 0;
}
void WIFISocketLEDClass::BlinkHandle()
{
  if (this->blinkCount == 0)
  {
    ticker.detach();
    return;
  }
  this->blinkCount--;
  this->Switch(!this->getState());
}
void WIFISocketLEDClass::Blink(int time)
{
  this->ticker.detach();
  this->blinkCount = (1 * 2) - 1;
  this->ticker.attach_ms(time, WIFISocketTickerHandle, this);
  this->BlinkHandle();
}
void WIFISocketLEDClass::Blink(int time, int count)
{
  this->ticker.detach();
  this->blinkCount = (count * 2) - 1;
  this->ticker.attach_ms(time, WIFISocketTickerHandle, this);
  this->BlinkHandle();
}
void WIFISocketLEDClass::BlinkDelay(int time)
{
  this->Switch(!this->getState());
  delay(time);
  this->Switch(!this->getState());
  delay(time);
}
void WIFISocketLEDClass::BlinkDelay(int time, int count)
{
  for (int i = 0; i < count; i++)
  {
    this->Switch(!this->getState());
    delay(time);
    this->Switch(!this->getState());
    delay(time);
  }
}
void WIFISocketLEDClass::begin(int pin)
{
  this->pin = pin;
  pinMode(this->pin, OUTPUT);
  this->Switch(false);
}
void WIFISocketLEDClass::Switch(bool state)
{
  this->state = state;
  if (state)
  {
    digitalWrite(this->pin, LOW);
  } else
  {
    digitalWrite(this->pin, HIGH);
  }
}
bool WIFISocketLEDClass::getState()
{
  return this->state;
}

WIFISocketButton.h

#ifndef __WIFISOCKETBUTTON_H__
#define __WIFISOCKETBUTTON_H__
/*JinZhu 17/9/22 Review */
#include <Arduino.h>
#define BUTTON_PIN  4
typedef enum BUTTON_STATE
{
  NONE_PRESS = 0,
  SHORT_PRESS = 1,
  LONG_PRESS = 2,
} BUTTON_STATE;
class WIFISocketButtonClass
{
  public:
    WIFISocketButtonClass();
    void begin(int pin = BUTTON_PIN);
    BUTTON_STATE  Scan();
  private:
    int     pin;
};
extern WIFISocketButtonClass WIFISocketButton;
#endif

WIFISocketButton.cpp

#include "WIFISocketButton.h"
WIFISocketButtonClass WIFISocketButton;
WIFISocketButtonClass::WIFISocketButtonClass()
{
  this->pin = BUTTON_PIN;
}
void WIFISocketButtonClass::begin(int pin)
{
  this->pin = pin;
  pinMode(this->pin, INPUT);
}
BUTTON_STATE WIFISocketButtonClass::Scan()
{
  const int LONG_PRESS_TIME = 2000;
  const int LONG_PRESS_SETP = 100;
  int       TimeOutCount = 0;
  if (digitalRead(this->pin) == HIGH)
  {
    return NONE_PRESS;
  } else
  {
    delay(10);
    if (digitalRead(this->pin) == HIGH)
    {
      return SHORT_PRESS;
    } else
    {
      for (TimeOutCount = 0; TimeOutCount < LONG_PRESS_TIME; TimeOutCount += LONG_PRESS_SETP)
      {
        if (digitalRead(this->pin) == HIGH)
        {
          break;
        }
        delay(LONG_PRESS_SETP);
      }
      if (TimeOutCount < LONG_PRESS_TIME)
      {
        return SHORT_PRESS;
      } else
      {
        return LONG_PRESS;
      }
    }
  }
}

WIFISocketSwitch.h

#ifndef __WIFISOCKETSWITCH_H__
#define __WIFISOCKETSWITCH_H__
/*JinZhu 17/9/22 Review */
#include <Arduino.h>
#define SWITCH_PIN  5
class WIFISocketSwitchClass
{
  public:
    WIFISocketSwitchClass();
    void begin(int pin = SWITCH_PIN);
    void Switch(bool state);
    bool getState();
  private:
    int         pin;
    bool        state;
};
extern WIFISocketSwitchClass WIFISocketSwitch;
#endif

WIFISocketSwitch.cpp

#include "WIFISocketSwitch.h"
#include "WIFISocketLED.h"
WIFISocketSwitchClass WIFISocketSwitch;
WIFISocketSwitchClass::WIFISocketSwitchClass()
{
    this->pin = SWITCH_PIN;
}
void WIFISocketSwitchClass::begin(int pin)
{
    this->pin = pin;
    pinMode(this->pin, OUTPUT);
    this->Switch(state);
    WIFISocketLED.Switch(state);
}
void WIFISocketSwitchClass::Switch(bool state)
{
    this->state = state;
    WIFISocketLED.Switch(state);
    if (state)
    {
        digitalWrite(this->pin, HIGH);
    } else
    {
        digitalWrite(this->pin, LOW);
    }
}
bool WIFISocketSwitchClass::getState()
{
    return state;
}

WIFISocketLite.ino

#include <Arduino.h>
#include "WIFISocketLED.h"
#include "WIFISocketButton.h"
#include "WIFISocketSwitch.h"
void setup()
{
  WIFISocketLED.begin();
  WIFISocketButton.begin();
  WIFISocketSwitch.begin();
}

void loop()
{
  BUTTON_STATE ButtonState = WIFISocketButton.Scan();
  if (ButtonState == SHORT_PRESS)
  {
    boolean switchstate = WIFISocketSwitch.getState();
    WIFISocketSwitch.Switch(!switchstate);
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章