スマートホームに向けたもう一息
実際、スイッチボットや赤外線は、ハードルが高かったのは確か
しかし、ここまでくるとソース改造だけで事足りる
結局、オムロン製が、到着が遅くなっているので
こちらで作成。
ソースは非常に簡単。
単数スイッチとした。
例によって技適
ac dc モジュール
USBプラグは、使わないほうが良いと思う。
USBプラグは充電用だと不安定だし、必要以上に電力を消費するようなので
自分は、ESPがようやく動作する500mA以上なので700-800mAを利用している。
その分汎用的に利用できる。
ソースはこれ
#include
#include
#include “fauxmoESP.h”
int flag = 0;
#define WIFI_SSID “WIFIのSSD”
#define WIFI_PASS “WIFIのSパスワード”
fauxmoESP fauxmo;
// —————————————————————————–
#define SERIAL_BAUDRATE 115200
#define ID “ロス1”
// —————————————————————————–
// —————————————————————————–
// 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\n”, WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
void setup() {
pinMode(4, OUTPUT);
// Init serial port and clean garbage
Serial.begin(SERIAL_BAUDRATE);
Serial.println();
Serial.println();
// Wifi
wifiSetup();
// By default, fauxmoESP creates it’s own webserver on the defined port
// The TCP port must be 80 for gen3 devices (default is 1901)
// This has to be done before the call to enable()
fauxmo.createServer(true); // not needed, this is the default value
fauxmo.setPort(80); // This is required for gen3 devices
// You have to call enable(true) once you have a WiFi connection
// You can enable or disable the library at any moment
// Disabling it will prevent the devices from being discovered and switched
fauxmo.enable(true);
// Add virtual devices
fauxmo.addDevice(ID);
fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
Serial.printf(“[MAIN] Device #%d (%s) state: %s value: %d\n”, device_id, device_name, state ? “ON” : “OFF”, value);
// Checking for device_id is simpler if you are certain about the order they are loaded and it does not change.
// Otherwise comparing the device_name is safer.
if (strcmp(device_name, ID)==0) {
if(state){
flag = 1;
}else{
flag = 2;
}
}
});
}
void loop() {
// fauxmoESP uses an async TCP server but a sync UDP server
// Therefore, we have to manually poll for UDP packets
fauxmo.handle();
if(flag == 1){
liveOn();
flag = 0;
}else if(flag == 2){
liveOf();
flag = 0;
}
}
void liveOn() {
digitalWrite(4, HIGH); // Schaltet ein
delay(1000);
Serial.print(“Switch on …”);
}
void liveOf() {
digitalWrite(4, LOW); // Schaltet ein
delay(1000);
Serial.print(“Switch off …”);
}
こんな感じ
正直、ケース無かったら張りぼて感満載!
