之後就搞coding 果part
一樣係用nextjs 做backend
今次用既係tuya api
用google home 唔同
tuya 開放哂所有api方便唔同development
reg完acc會有一組api key 同secret
另外拎返device 個id
之後就簡單啦
用返tuya provide 既nodejs library
我自己就改左少少 寫左個function俾佢
個function食兩個parameter, 一個係device id 另一個係個status
status可以係"switch_1" 或者"switch_2"
depends on 返個device 係咩
寫埋個api route
淨低既就係deploy上vercel
寫完backend 就可以搞esp32 果part
用arduino ide
只要esp32 call 到個api 就可以控制到屋企既device
先搞wifi 果part
#include <WiFi.h>
#include <HTTPClient.h>
const char ssid[] = "WIFI SSID NAME";
const char pwd[] = "MY_PASSWORD here";
void setup 入面
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pwd);
Serial.print("WiFi connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
之後搞input pin
int pins[] = [27, 26, 25, 33, 32];
for (int i = 0; i < 5; i++)
pinMode(pins[i], INPUT);
讀到其中一個pin 係HIGH既時候就會自動去call條api
最後upload上esp32 就用得