軟件要求
對于此項目,您將需要以下程序:
NodeMCU
除ESP8266板支持(NodeMCU)外,還應安裝以下Arduino庫(在庫管理器中搜索或手動將文件夾放在Arduino/庫中):
fauxmoESP
ESPAsyncTCP
ESPAsyncWebServer
連接和原理圖
ESP8266
D1→LED/繼電器
D2→按鈕
編程Arduino
此項目的Arduino代碼使用fauxmoESP庫的示例草圖,該庫模擬Belkin WeMo設備。因此,配置家庭自動化開關遵循與商業設備完全相同的過程,這在Alexa應用程序中是輕而易舉的。為了發現這個設備,我將我的設備命名為“光”。
請注意高電壓:在確定繼電器接線之前拔下所有電源插頭。為了控制電路的交流部分,我使用的是5V繼電器 - 只需中斷220V電線,然后將剝開的端子插入常開和常開螺絲端子。 *請記住,如果您沒有太多使用高壓的經驗,請找一個監督的人。
Arduino IDE配置
單擊文件 - 》首選項
添加這個鏈接到附加URL板:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
接下來,安裝電路板文件并按照提及步驟進行操作。
工具→電路板→電路板管理器
搜索ESP8266和安裝模塊包。
選擇您的電路板,如下圖所示。
《二v》
然后,選擇電路板端口。
選擇端口后,編輯源代碼并更改Wi-Fi名稱和密碼,如圖所示:
Arduino代碼
#include
#include
#include “fauxmoESP.h”
#include “ESPAsyncWebServer.h”
#include
#include
#define WIFI_SSID “” // Please Enter you Wifi name here
#define WIFI_PASS “” // Enter password here
#define SERIAL_BAUDRATE 115200
fauxmoESP fauxmo;
#define RELAY_PIN 5
const int buttonPin = 4; // the pin that the pushbutton is attached to
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
// -----------------------------------------------------------------------------
// Wifi
// -----------------------------------------------------------------------------
void wifiSetup() {
// Set WIFI module to STA mode
WiFi.mode(WIFI_STA);
// Connect
Serial.printf(“[WIFI] Connecting to %s ”, WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
// Wait
while (WiFi.status() != WL_CONNECTED) {
Serial.print(“。”);
delay(100);
}
Serial.println();
// Connected!
Serial.printf(“[WIFI] STATION Mode, SSID: %s, IP address: %s ”, WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
void callback(uint8_t device_id, const char * device_name, bool state) {
Serial.print(“Device ”); Serial.print(device_name);
Serial.print(“ state: ”);
if (state) {
Serial.println(“ON”);
digitalWrite(RELAY_PIN, HIGH);
} else {
Serial.println(“OFF”);
digitalWrite(RELAY_PIN, LOW);
}
}
void setup() {
pinMode(RELAY_PIN, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(RELAY_PIN, LOW);
// Init serial port and clean garbage
Serial.begin(SERIAL_BAUDRATE);
Serial.println(“FauxMo demo sketch”);
Serial.println(“After connection, ask Alexa/Echo to ‘turn on’ or ‘off’”);
// Wifi
wifiSetup();
// Fauxmo
fauxmo.addDevice(“the light”);
fauxmo.onMessage(callback);
}
void loop() {
fauxmo.handle();
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
Serial.println(“on”);
digitalWrite(RELAY_PIN, HIGH);
}
else {
// if the current state is LOW then the button
// went from on to off:
Serial.println(“off”);
digitalWrite(RELAY_PIN, LOW);
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
現在是時候玩了!
上傳代碼后讓您的Alexa發現新設備,它會檢測到您的智能家居設備,即ESP8266。通過說“Alexa打開/關閉燈”來控制它。在這種情況下,Alexa可以是您的計算機或Amazon Echo。
-
繼電器
+關注
關注
132文章
5363瀏覽量
149633 -
led燈
+關注
關注
22文章
1592瀏覽量
108466 -
ESP8266
+關注
關注
50文章
962瀏覽量
45374 -
Alexa
+關注
關注
2文章
196瀏覽量
23385
發布評論請先 登錄
相關推薦
把esp8266加入到c51單片機單通道程序怎么寫
arduino(1)--ESP8266配置
esp32和esp8266代碼共用嗎
esp8266和esp32區別是什么
esp8266不燒錄可以使用嗎
esp8266wifi模塊怎么連接手機
請問ESP8266如何在UDP中設置本地端口?
5V電磁繼電器的基本結構和工作原理
國產低成本Wi-Fi SoC解決方案芯片ESP8266與ESP8285對比差異
![國產低成本Wi-Fi SoC解決方案芯片<b class='flag-5'>ESP8266</b>與<b class='flag-5'>ESP</b>8285對比差異](https://file1.elecfans.com/web2/M00/E6/5C/wKgZomZG0KiASP_yAAELgTM0cRs433.png)
使用Wi-Fi ESP8266方案模組接入云平臺
![使用Wi-Fi <b class='flag-5'>ESP8266</b>方案模組接入云平臺](https://file.elecfans.com/web2/M00/3E/6A/pYYBAGJhBGGAGyDYAACBPQuBZQI711.png)
評論