//https://youtu.be/48RW4JHMXUA #include #include "Base64.h" #include #include #include "fauxmoESP.h" #define WIFI_SSID "xxxxxxxxxxxxxx" #define WIFI_PASS "xxxxxxxxxxxxxx" fauxmoESP alexawifi; RCSwitch mySwitch = RCSwitch(); WiFiServer server(80); // EasyIoT server definitions #define EIOT_USERNAME "xxxxxxxxxxxxx" #define EIOT_PASSWORD "xxxxxxxxxxxxx" #define EIOT_IP_ADDRESS "192.168.0.1" #define EIOT_PORT 80 #define USER_PWD_LEN 40 char unameenc[USER_PWD_LEN]; boolean Status; boolean Gesamtstatus[16]; boolean InitialisedByAlexa; int StatusChangedInt=10; String req; int val; void wifiSetup() { // ----------------------------------------------------------------------------- // WLAN SETUP // ----------------------------------------------------------------------------- WiFi.mode(WIFI_STA); Serial.printf("Verbindungs zu %s wird aufgebaut ", WIFI_SSID); WiFi.begin(WIFI_SSID, WIFI_PASS); while (WiFi.status() != WL_CONNECTED) { Serial.print(">"); delay(100); } Serial.println(""); Serial.printf("Verbunden! SSID: %s, IP Adresse: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str()); } void sendIOTBoolean(boolean inputState, String EIOT_NODE) { WiFiClient client; if (!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {Serial.println("connection failed");} String url = ""; String command = ""; if (inputState) command = "ControlOn"; else command = "ControlOff"; url = "/Api/EasyIoT/Control/Module/Virtual/"+ String(EIOT_NODE) + "/"+command; // generate EasIoT server node URL // url += "/Api/EasyIoT/Control/Module/Virtual/"+ String(EIOT_NODE) + "/ControlLevel/"+String(Wert); // generate EasIoT server node URL Serial.print("POST data to URL: "); Serial.println(url); client.print(String("POST ") + url + " HTTP/1.1\r\n" + "Host: " + String(EIOT_IP_ADDRESS) + "\r\n" + "Connection: close\r\n" + "Authorization: Basic " + unameenc + " \r\n" + "Content-Length: 0\r\n" + "\r\n"); /* delay(50); while(client.available()){ String line = client.readStringUntil('\r'); Serial.print(line); } Serial.println(); Serial.println("Connection closed"); */ } void anfrage(uint8_t device_id, const char * device_name, bool state) { Serial.print("Gerät: "); Serial.println(device_name); Serial.print("Status: "); InitialisedByAlexa=1; StatusChangedInt=device_id; Status=state; Gesamtstatus[device_id]=state; if (state) {Serial.println("AN");} else {Serial.println("AN");} } void setup() { Serial.begin(115200); Serial.println("Nach dem Verbinden, sage 'Alexa, schalte an' oder 'aus'"); mySwitch.enableTransmit(5); //Setup für IOT char uname[USER_PWD_LEN]; String str = String(EIOT_USERNAME)+":"+String(EIOT_PASSWORD); str.toCharArray(uname, USER_PWD_LEN); memset(unameenc,0,sizeof(unameenc)); base64_encode(unameenc, uname, strlen(uname)); wifiSetup(); geraete(); // Start the server server.begin(); Serial.println("Server started"); } void geraete(){ alexawifi.addDevice("Test Eins"); //ID 0 alexawifi.addDevice("Test Zwei");//ID 1 alexawifi.onMessage(anfrage); alexawifi.onGetState([](unsigned char device_id, const char * device_name) { // delay(100); Serial.print( device_id); Serial.print(", Status:"); Serial.println(Gesamtstatus[device_id]); return Gesamtstatus[device_id] == HIGH; }); } void loop() { alexawifi.handle(); switch (StatusChangedInt) { case 0: if (InitialisedByAlexa==1) {sendIOTBoolean(Status,"N20S0"); InitialisedByAlexa=0; } break; case 1: if (InitialisedByAlexa==1) {sendIOTBoolean(Status,"N21S0"); InitialisedByAlexa=0; } break; case 2: if (InitialisedByAlexa==1) {sendIOTBoolean(Status,"N22S0"); InitialisedByAlexa=0; } break; } StatusChangedInt=10; // This Part is for Receving Requests from IOT Server to control the WeMo Switch. WiFiClient client = server.available(); if (!client) { return; } // Wait until the client sends some data Serial.println("new client"); delay(50); /* while(!client.available()){ Serial.print("+"); delay(100); } */ // Read the first line of the request req = client.readStringUntil('\r'); Serial.print("Request::"); Serial.println(req); client.flush(); // Matchrequest if (req.indexOf("/gpio/N20S0/0") != -1) {Gesamtstatus[0]= 0; StatusChangedInt=0; } else if (req.indexOf("/gpio/N20S0/1") != -1) {Gesamtstatus[0] = 1; StatusChangedInt=0; } else if (req.indexOf("/gpio/N21S0/0") != -1) {Gesamtstatus[1] = 0; StatusChangedInt=1; } else if (req.indexOf("/gpio/N21S0/1") != -1) {Gesamtstatus[1] = 1; StatusChangedInt=1; } else if (req.indexOf("/gpio/N22S0/0") != -1) {Gesamtstatus[2] = 0; StatusChangedInt=2; } else if (req.indexOf("/gpio/N22S0/1") != -1) {Gesamtstatus[2] = 1; StatusChangedInt=2; } else { Serial.println("invalid request"); client.stop(); return; } if (req.indexOf("S0/0") != -1) val = 0; else if (req.indexOf("S0/1") != -1) val = 1; client.flush(); // Prepare the response String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nGPIO is now "; s += (val)?"high":"low"; s += "\n"; // Send the response to the client client.print(s); Serial.println(s); delay(1); Serial.println("Client disonnected"); // The client will actually be disconnected // when the function returns and 'client' object is detroyed }