jigneshk5 / home-automation-with-alexa

This repo contains arduino code for the ALEXA BASED Home Automation project using ESP8266, Adafruit.IO, SINRIC and IFTTT
https://youtu.be/P-Fa-005bd4
2 stars 6 forks source link

Need Help ---- To make it work for ESP32 Board #1

Open joykumarsen opened 3 years ago

joykumarsen commented 3 years ago

Hi,

First of all thank you for this tutorial.

can you please help me or guide me to make it work for ESP32 borad. I taken the reference of your code.

issues --- I am not able to turn ON and OFF the lights. lights are getting on only while setting the brightness using Alexa command or Brightness slider in the Alexa app. Once the lights are ON, not able to make the lights OFF.

Your quick reply will be highly appreciable.

Below is the code what i am using for ESP32 board.

include

include

include

include // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries

include // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries

include

WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;

define MyApiKey "XXXX // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard

define MySSID "XXX" // TODO: Change to your Wifi network SSID

define MyWifiPassword "XXX" // 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;

const int Dev1 = 17; const int Dev2 = 16;

const int freq = 5000; const int ledChannel_0 = 0; const int ledChannel_1 = 1; const int resolution = 8;

void turnOn(String deviceId) { if (deviceId == "60430057c26766757ec0ccd4") // Device ID of first device {
Serial.print("Living Room light is turned on"); ledcWrite(ledChannel_0,HIGH); } else if (deviceId == "60437794c26766757ec0e2c9") // Device ID of second device { Serial.print("Drawing Room Light turned on"); ledcWrite(ledChannel_1,HIGH); }

}

void turnOff(String deviceId) { if (deviceId == "60430057c26766757ec0ccd4") // Device ID of first device {
Serial.print("Living Room Light is turned off"); ledcWrite(ledChannel_0,LOW); } else if (deviceId == "60437794c26766757ec0e2c9") // Device ID of second device { Serial.print("drawing Room Light is turned off"); ledcWrite(ledChannel_1,LOW); }

} void setIntensity(String deviceId,int intensity) { if (deviceId == "60430057c26766757ec0ccd4") // Device ID of redLight {
int mappedIntensity = map(intensity, 0, 100, 0, 255); ledcWrite(ledChannel_0,mappedIntensity); } else if (deviceId == "60437794c26766757ec0e2c9") // Device ID of greenLight { int mappedIntensity = map(intensity, 0, 100, 0, 255); ledcWrite(ledChannel_1,mappedIntensity); } }

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

    // For Light device type
    // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html
    // {"deviceId": xxxx, "action": "AdjustBrightness", value: 3} // https://developer.amazon.com/docs/device-apis/alexa-brightnesscontroller.html
    // {"deviceId": xxxx, "action": "setBrightness", value: 42} // https://developer.amazon.com/docs/device-apis/alexa-brightnesscontroller.html
    // {"deviceId": xxxx, "action": "SetColor", value: {"hue": 350.5,  "saturation": 0.7138, "brightness": 0.6501}} // https://developer.amazon.com/docs/device-apis/alexa-colorcontroller.html
    // {"deviceId": xxxx, "action": "DecreaseColorTemperature"} // https://developer.amazon.com/docs/device-apis/alexa-colortemperaturecontroller.html
    // {"deviceId": xxxx, "action": "IncreaseColorTemperature"} // https://developer.amazon.com/docs/device-apis/alexa-colortemperaturecontroller.html
    // {"deviceId": xxxx, "action": "SetColorTemperature", value: 2200} // https://developer.amazon.com/docs/device-apis/alexa-colortemperaturecontroller.html

if ARDUINOJSON_VERSION_MAJOR == 5

    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.parseObject((char*)payload);

endif

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 == "setBrightness") {
      String brightness = json ["value"];
      Serial.println("Brightness set to: " + brightness);
      int intensity= brightness.toInt();
      setIntensity(deviceId,intensity);    
    }
    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(redlight,OUTPUT); //pinMode(greenlight,OUTPUT);

ledcSetup(ledChannel_0, freq, resolution); ledcSetup(ledChannel_1, freq, resolution); ledcAttachPin(Dev1, ledChannel_0); ledcAttachPin(Dev2, ledChannel_1);

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, "/");

// 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();

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");          
  }

}
}

jigneshk5 commented 3 years ago

I'm not able to see any bug in the above code since it looks very similar to my alexa_sinric.ino code.

Please test each section of code separately.

if(action == "setPowerState") { // Switch or Light
        String value = json ["value"];
        if(value == "ON") {
            Serial.println("checking on");
            turnOn(deviceId);
        } else {
              Serial.println("checking off");
            turnOff(deviceId);
        }
    }

Add Serial.println("checking on"); and Serial.println("checking off"); and check if this line gets printed on serial monitor. If this is working, you have to check turnOn and turnOff functions. If this isn't working, Try to rewrite your code with sinric.pro (new version of Sinric) as you might face issue with older version (sinric.com )

joykumarsen commented 3 years ago

Thank you for your quick reply and guidance.

After made the below changes, ON OFF part is working fine. But setBrightness part is stop working (Earlier the scenario was just reverse). you can check the attached Serial monitor screen shot as well for just reference.

As you told there might be issue with older version. Even in the sinric web site I have noticed that the service has been discontinued. let's hope it should work with Sinric Pro!

Changes_to_1024

ONOFF