You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.6 KiB

#include "Arduino.h"
#include <fauxmoESP.h>
#include <ESP8266WiFi.h>
fauxmoESP fauxmo;
// Replace with your network credentials
const char *ssid = "FRITZ!BoxWZ";
const char *password = "40360548708873074408";
IPAddress ip(192, 168, 178, 14);
IPAddress subnet(255, 255, 255, 0);
IPAddress gateway(192, 168, 178, 1);
const char* serverhost = "192.168.178.12";
void httpConnect() {
WiFiClient client;
Serial.printf("\n[Connecting to %s ... ", serverhost);
if (client.connect(serverhost,88))
{
Serial.println("connected]");
client.println("PUT /ledDemoOn");
delay(10000);
client.println("PUT /ledDemoOff");
delay(10000);
Serial.println("[Response:]");
while (client.connected() || client.available())
{
if (client.available())
{
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop();
Serial.println("\n[Disconnected]");
}
else
{
Serial.println("connection failed!]");
client.stop();
}
delay(5000);
}
void setup() {
Serial.begin(115200);
if (connectWifi()) {
fauxmo.addDevice("LED Schrank");
fauxmo.addDevice("LED Fernseher");
fauxmo.addDevice("LED Demo");
fauxmo.enable(true);
//fauxmo.onMessage(fn)
fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {
Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
httpConnect();
// HttpClient client;
//
// if (state == true) {
// client.begin("http://192.168.178.12:88/ledDemoOn"); //Specify request destination
// } else {
// client.begin("http://192.168.178.12:88/ledDemoOff"); //Specify request destination
// }
// int httpCode = client.GET();
// String payload = client.getString();
// Serial.println(payload);
// Serial.println("Test");
// client.end();
});
}
}
// ------------ FINISH -----------------
boolean connectWifi() {
boolean stat = true;
// Connect to Wi-Fi
WiFi.config(ip, gateway, subnet); // uncomment for dynamic IP
WiFi.begin(ssid, password);
int count = 0;
while (WiFi.status() != WL_CONNECTED) {
count++;
digitalWrite(D4, LOW);
delay(1000);
digitalWrite(D4, HIGH);
Serial.println("Connecting to WiFi..");
if (count == 10) {
stat = false;
break;
}
}
if (stat == true) {
digitalWrite(D4, LOW);
// Print ESP Local IP Address
Serial.println(WiFi.localIP());
}
return stat;
}
void loop() {
fauxmo.handle();
}