Please check at the end of code where is says relay turned on by Alexa
/*
Version 0.1 - Feb 10 2018
*/
#include <ESP8266HTTPClient.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
#include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
WiFiClient client;
const int relay1 = 05; // GPIO05
const int relay2 = 04; // GPIO04
const int relay3 = 00; // GPIO00
const int relay4 = 02; // GPIO02
const int relay5 = 14; // GPIO14
const int relay6 = 12; // GPIO12
const int relay7 = 13; // GPIO13
const int relay8 = 15; // GPIO15
int relayState = digitalRead(relay1);
#define MyApiKey "API" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
#define MySSID "SSID" // TODO: Change to your Wifi network SSID
#define MyWifiPassword "PASSWORD" // TODO: Change to your Wifi network password
#define API_ENDPOINT "http://sinric.com"
#define HEARTBEAT_INTERVAL 300000 // 5 Minutes
uint64_t heartbeatTimestamp = 0;
bool isConnected = false;
void turnOn(String deviceId) {
if (deviceId == "DEVICEID") // Device ID of first device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(relay1,LOW);
}
else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of second device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
}
else {
Serial.print("Turn on for unknown device id: ");
Serial.println(deviceId);
}
}
void turnOff(String deviceId) {
if (deviceId == "DEVICEID") // Device ID of first device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(relay1,HIGH);
}
else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of second device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
}
else {
Serial.print("Turn off for unknown device id: ");
Serial.println(deviceId);
}
}
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
isConnected = false;
Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
break;
case WStype_CONNECTED: {
isConnected = true;
Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
Serial.printf("Waiting for commands from sinric.com ...\n");
}
break;
case WStype_TEXT: {
Serial.printf("[WSc] get text: %s\n", payload);
// Example payloads
#if ARDUINOJSON_VERSION_MAJOR == 6
DynamicJsonDocument json(1024);
deserializeJson(json, (char*) payload);
#endif
String deviceId = json ["deviceId"];
String action = json ["action"];
if(action == "setPowerState") { // Switch or Light
String value = json ["value"];
if(value == "ON") {
turnOn(deviceId);
} else {
turnOff(deviceId);
}
}
else if(action == "SetColor") {
// Alexa, set the device name to red
// get text: {"deviceId":"xxxx","action":"SetColor","value":{"hue":0,"saturation":1,"brightness":1}}
String hue = json ["value"]["hue"];
String saturation = json ["value"]["saturation"];
String brightness = json ["value"]["brightness"];
Serial.println("[WSc] hue: " + hue);
Serial.println("[WSc] saturation: " + saturation);
Serial.println("[WSc] brightness: " + brightness);
}
else if(action == "SetBrightness") {
}
else if(action == "AdjustBrightness") {
}
else if (action == "test") {
Serial.println("[WSc] received test command from sinric.com");
}
}
break;
case WStype_BIN:
Serial.printf("[WSc] get binary length: %u\n", length);
break;
default: break;
}
}
void setup() {
Serial.begin(115200);
pinMode (relay1, OUTPUT);
pinMode (relay2, OUTPUT);
pinMode (relay3, OUTPUT);
pinMode (relay4, OUTPUT);
pinMode (relay5, OUTPUT);
pinMode (relay6, OUTPUT);
pinMode (relay7, OUTPUT);
pinMode (relay8, OUTPUT);
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(relay5,HIGH);
digitalWrite(relay6,HIGH);
digitalWrite(relay7,HIGH);
digitalWrite(relay8,HIGH);
WiFiMulti.addAP(MySSID, MyWifiPassword);
Serial.println();
Serial.print("Connecting to Wifi: ");
Serial.println(MySSID);
// Waiting for Wifi connect
while(WiFiMulti.run() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if(WiFiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.print("WiFi connected. ");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
// server address, port and URL
webSocket.begin("iot.sinric.com", 80, "/");
// webSocket.begin("http://35.237.179.138//home/get_relay_group_status", 80,"/");
// event handler
webSocket.onEvent(webSocketEvent);
webSocket.setAuthorization("apikey", MyApiKey);
// try again every 5000ms if connection has failed
webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
}
void loop() {
webSocket.loop();
delay(1000);
HTTPClient http;
http.begin("http://35.237.179.138//home/get_relay_group_status/"); //Specify the URL
int httpCode = http.GET(); //Make the request
if (httpCode > 0) { //Check for the returning code
String payload = http.getString();
const size_t capacity = JSON_ARRAY_SIZE(8) + 8 * JSON_OBJECT_SIZE(4) + 620;
DynamicJsonDocument doc(capacity);
char charBuf[capacity];
payload.toCharArray(charBuf, capacity);
deserializeJson(doc, charBuf);
JsonObject root_0 = doc[0];
bool root_0_r_status = root_0["r_status"];
JsonObject root_1 = doc[1];
bool root_1_r_status = root_1["r_status"];
JsonObject root_2 = doc[2];
bool root_2_r_status = root_2["r_status"];
JsonObject root_3 = doc[3];
bool root_3_r_status = root_3["r_status"];
JsonObject root_4 = doc[4];
bool root_4_r_status = root_4["r_status"];
JsonObject root_5 = doc[5];
bool root_5_r_status = root_5["r_status"];
JsonObject root_6 = doc[6];
bool root_6_r_status = root_6["r_status"];
JsonObject root_7 = doc[7];
bool root_7_r_status = root_7["r_status"];
if (root_0_r_status == true) {
Serial.println("Relay 1 is on ");
digitalWrite(relay1, LOW);
} else if (root_0_r_status == false){
Serial.println("Relay 1 is off ");
digitalWrite(relay1, HIGH);
} if ((root_0_r_status) == false && relayState == HIGH){
Serial.println("Relay 1 is turned on by Alexa!");
digitalWrite(relay1, LOW);
}
if (root_1_r_status) {
Serial.println("Relay 2 is on ");
digitalWrite(relay2, LOW);
} else {
Serial.println("Relay 2 is off ");
digitalWrite(relay2, HIGH);
}
if(isConnected) {
uint64_t now = millis();
// Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
heartbeatTimestamp = now;
webSocket.sendTXT("H");
}
}
// http.end(); //Free the resources
}