#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClient.h>
#include "fauxmoESP.h"
#define RETRY_COUNT 3 //Httpリクエストに失敗したときのリトライ回数
HTTPClient http;
WiFiClient wifiClient;
fauxmoESP fauxmo;
const char *living_light = "居間照明";
const char *url_living_light_on = "http://192.168.10.40:81/light?act=on";
const char *url_living_light_off = "http://192.168.10.40:81/light?act=off";
const char *living_weak_light = "居間照明弱";
const char *url_living_light_lamp = "http://192.168.10.40:81/light?act=lamp";
const char *washitu_light = "和室照明";
const char *url_washitu_light_on = "http://192.168.10.43/light2?act=power";
const char *kitchen_light = "キッチン照明";
const char *url_kitchen_light_on = "http://192.168.10.40:81/light3?act=on";
const char *url_kitchen_light_off = "http://192.168.10.40:81/light3?act=off";
const char *study_light = "勉強照明";
const char *url_study_light_on = "http://192.168.10.40:81/light4?act=on";
const char *url_study_light_off = "http://192.168.10.40:81/light4?act=off";
const char *father_light = "父部屋照明";
const char *url_father_light_on = "http://192.168.10.41:81/light?act=on";
const char *url_father_light_jyoya= "http://192.168.10.41:81/light?act=night";
const char *url_father_light_off = "http://192.168.10.41:81/light?act=off";
//ネットワーク環境
const char* ssid = "*********";
const char* password = "*********";
// 静的IPアドレス
IPAddress ip(192, 168, 10, 55);
IPAddress gateway(192,168, 10, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress DNS(192, 168, 10, 1);
//httpリクエスト
bool reqestData(String url){
http.setConnectTimeout(200);
http.setTimeout(300);
http.begin(wifiClient,url);
bool ret = false;
int httpCode = http.GET();
if(httpCode > 0) {
if(httpCode == HTTP_CODE_OK) {
ret = true;
}
}
http.end();
return ret;
}
//wifi接続
void connectWifi(){
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.config(ip, gateway, subnet, DNS);
delay(100);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(50);
Serial.print(".");
}
Serial.println("");
Serial.println(WiFi.localIP());
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//Wifi接続
connectWifi();
//fauxmo初期化
fauxmo.createServer(true);
fauxmo.setPort(80); //※3rd GEN Alexa
fauxmo.enable(true);
//仮想スイッチデバイスの追加
fauxmo.addDevice(living_light);
fauxmo.addDevice(washitu_light);
fauxmo.addDevice(living_weak_light);
fauxmo.addDevice(kitchen_light);
fauxmo.addDevice(study_light);
fauxmo.addDevice(father_light);
//アレクサからのトリガー受信処理
fauxmo.onSetState([](unsigned char device_id, const char *device_name, bool state, unsigned char value) {
Serial.printf("Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
//寝室ライト
if (strcmp(device_name, washitu_light) == 0) {
for(int i = 0; i < RETRY_COUNT; i++) {
bool ret = reqestData(url_washitu_light_on);
if(ret) break;
delay(500);
}
Serial.println("和室オンOK");
}
//リビングライト
if (strcmp(device_name, living_light) == 0) {
String url = state ? url_living_light_on : url_living_light_off;
for(int i = 0; i < RETRY_COUNT; i++) {
bool ret = reqestData(url);
if(ret) break;
delay(500);
}
Serial.println("リビングOK");
}
//リビングライト弱調光
if (strcmp(device_name, living_weak_light) == 0) {
for(int i = 0; i < RETRY_COUNT; i++) {
bool ret = reqestData(url_living_light_lamp);
if(ret) break;
delay(500);
}
Serial.println("リビングライト弱調光OK");
}
//キッチンライト
if (strcmp(device_name, kitchen_light) == 0) {
String url = state ? url_kitchen_light_on : url_kitchen_light_off;
for(int i = 0; i < RETRY_COUNT; i++) {
bool ret = reqestData(url);
if(ret) break;
delay(500);
}
Serial.println("キッチンOK");
}
//勉強ライト
if (strcmp(device_name, study_light) == 0) {
String url = state ? url_study_light_on : url_study_light_off;
for(int i = 0; i < RETRY_COUNT; i++) {
bool ret = reqestData(url);
if(ret) break;
delay(500);
}
Serial.println("勉強ライトOK");
}
//父部屋ライト
if (strcmp(device_name, father_light) == 0) {
if(state){
for(int i = 0; i < RETRY_COUNT; i++) {
bool ret = reqestData(url_father_light_on);
if(ret) break;
delay(500);
}
Serial.println("父部屋ライトオンOK");
}else{
for(int i = 0; i < RETRY_COUNT; i++) {
bool ret = reqestData(url_father_light_jyoya);
if(ret) break;
delay(500);
}
delay(1000);
for(int i = 0; i < RETRY_COUNT; i++) {
bool ret = reqestData(url_father_light_off);
if(ret) break;
delay(500);
}
Serial.println("父部屋ライトオフOK");
}
}
});
}
void loop() {
// put your main code here, to run repeatedly:
fauxmo.handle();
}