kakopappa / sinric

Amazon Alexa Smart home skill / Google Home Action for ESP8266 / ESP32 / Arduino
https://sinric.com
287 stars 166 forks source link

Alexa doesn't seem to be communicating properly #14

Open lukerichards0n opened 6 years ago

lukerichards0n commented 6 years ago

Hey, want to use this to trigger a relay. I don't see where you define which pins to use. I got as far as connecting to Alexa, but when I tell Alexa to turn on or off the relay, she says "Okay" but then nothing happens. I've tried every single other way to control a relay with Alexa and an ESP8266 d1 mini, but this is the first way that I can get Alexa to discover the device

MacSass commented 6 years ago

Hi @directhacker , at least you should see some output on the serial console of your ESP when Alexa says that everything is OK. If you only use one device you can add your code to trigger the relay in those routines of the example code:

void turnOn() { Serial.println("Turn on ..."); } void turnOff() { Serial.println("Turn off ..."); }

Alternatively if you want to control more than one relay you can take a look at my sample code for multiple devices I posted. By accident I had posted it to a fairly unrelated thread - so I have attached it here again: switch_multi_sinric.txt

It has some more comments - which I hope you find helpful to understand how it works. Of course you will have to add your own code to actually turn on / off the relay ...

Regards - MacSass

kakopappa commented 6 years ago

@directhacker Sinric will send on/off request from Alexa to WeMos D1 module and it's up to you to turn on or off the relay.

If you are using wemos relay shield try this https://wiki.wemos.cc/products:d1_mini_shields:relay_shield and change the on/off functions to turn on / off the relay

anums18 commented 6 years ago

Hi @directhacker I had modified the code on below link with addition of 4 channel relay. https://gist.github.com/anums18/078e0671ba28af2a809cb9dea7a131bb

CEStanley58 commented 6 years ago

Hi @directhacker, Mods to make the WEMOS D1 are to set the GPIO pin(s) to OUTPUT as follows: pinMode(pinX, OUTPUT); where pinX = D0, D1, D2, D3 ... D7, D8 Do this in the setup() function/method. Then write the pin to HIGH or LOW: digitalWrite(pinX, HIGH); or digitalWrite(pinX, LOW); Do this in the tourOn() or turnOff() functions/methods.

Hope this is a direct answer to your question.

pacostello commented 6 years ago

I appear to be having the same issue. Wemos D1 mini. Started with the Switch example code. Put in my SSID, password, API code and device code. I have defined D1 and BUILTIN_LED as outputs in void setup() Used digitalWrite to set the above high and low in void turnOn and turnOff. Nothing happens. I can see on the Alexa app that it believes the device is being switched on and off but it appears to just be talking to the API not the Wemos, as it does the same regardless of whether the Wemos is powered or not!

CEStanley58 commented 6 years ago

Hi pacostello,

My issue is cleared up. My Alexa thinks the WeMOS does not respond but the LEDs do show the WeMOS operating correctly.

Charles Stanley

From: pacostello notifications@github.com Sent: Sunday, March 18, 2018 10:07 AM To: kakopappa/sinric sinric@noreply.github.com Cc: CEStanley58 CEStanley58@gmail.com; Comment comment@noreply.github.com Subject: Re: [kakopappa/sinric] Alexa doesn't seem to be communicating properly (#14)

I appear to be having the same issue. Wemos D1 mini. Started with the Switch example code. Put in my SSID, password, API code and device code. I have defined D1 and BUILTIN_LED as outputs in void setup() Used digitalWrite to set the above high and low in void turnOn and turnOff. Nothing happens. I can see on the Alexa app that it believes the device is being switched on and off but it appears to just be talking to the API not the Wemos, as it does the same regardless of whether the Wemos is powered or not!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kakopappa/sinric/issues/14#issuecomment-374016721 , or mute the thread https://github.com/notifications/unsubscribe-auth/AUSB1Gl-RIgiYIvJgIrX-Z61irAuYzwKks5tfpQfgaJpZM4Rv05c . https://github.com/notifications/beacon/AUSB1BJfRckvQkw52s52ro8FsEXOIu8Lks5tfpQfgaJpZM4Rv05c.gif

kakopappa commented 6 years ago

Hello.

  1. Does Arduino IDE console output says waiting for commands?

  2. Make sure device id is correct

On Mon, Mar 19, 2018 at 6:04 AM CEStanley58 notifications@github.com wrote:

Hi pacostello,

My issue is cleared up. My Alexa thinks the WeMOS does not respond but the LEDs do show the WeMOS operating correctly.

Charles Stanley

From: pacostello notifications@github.com Sent: Sunday, March 18, 2018 10:07 AM To: kakopappa/sinric sinric@noreply.github.com Cc: CEStanley58 CEStanley58@gmail.com; Comment < comment@noreply.github.com> Subject: Re: [kakopappa/sinric] Alexa doesn't seem to be communicating properly (#14)

I appear to be having the same issue. Wemos D1 mini. Started with the Switch example code. Put in my SSID, password, API code and device code. I have defined D1 and BUILTIN_LED as outputs in void setup() Used digitalWrite to set the above high and low in void turnOn and turnOff. Nothing happens. I can see on the Alexa app that it believes the device is being switched on and off but it appears to just be talking to the API not the Wemos, as it does the same regardless of whether the Wemos is powered or not!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub < https://github.com/kakopappa/sinric/issues/14#issuecomment-374016721> , or mute the thread < https://github.com/notifications/unsubscribe-auth/AUSB1Gl-RIgiYIvJgIrX-Z61irAuYzwKks5tfpQfgaJpZM4Rv05c> . < https://github.com/notifications/beacon/AUSB1BJfRckvQkw52s52ro8FsEXOIu8Lks5tfpQfgaJpZM4Rv05c.gif>

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/kakopappa/sinric/issues/14#issuecomment-374063322, or mute the thread https://github.com/notifications/unsubscribe-auth/AHIM5tTelKNytsBx6_1ZwmR_F4hzXG7Iks5tfugHgaJpZM4Rv05c .

CEStanley58 commented 6 years ago

The Arduino IDE console is clear, no text. My device is WEMOS D1 R2 & mini. It programs correctly and runs as commanded when I select the correct board.

Charles Stanley

From: Aruna Tennakoon notifications@github.com Sent: Sunday, March 18, 2018 6:13 PM To: kakopappa/sinric sinric@noreply.github.com Cc: CEStanley58 CEStanley58@gmail.com; Comment comment@noreply.github.com Subject: Re: [kakopappa/sinric] Alexa doesn't seem to be communicating properly (#14)

Hello.

  1. Does Arduino IDE console output says waiting for commands?

  2. Make sure device id is correct

On Mon, Mar 19, 2018 at 6:04 AM CEStanley58 <notifications@github.com mailto:notifications@github.com > wrote:

Hi pacostello,

My issue is cleared up. My Alexa thinks the WeMOS does not respond but the LEDs do show the WeMOS operating correctly.

Charles Stanley

From: pacostello <notifications@github.com mailto:notifications@github.com > Sent: Sunday, March 18, 2018 10:07 AM To: kakopappa/sinric <sinric@noreply.github.com mailto:sinric@noreply.github.com > Cc: CEStanley58 <CEStanley58@gmail.com mailto:CEStanley58@gmail.com >; Comment < comment@noreply.github.com mailto:comment@noreply.github.com > Subject: Re: [kakopappa/sinric] Alexa doesn't seem to be communicating properly (#14)

I appear to be having the same issue. Wemos D1 mini. Started with the Switch example code. Put in my SSID, password, API code and device code. I have defined D1 and BUILTIN_LED as outputs in void setup() Used digitalWrite to set the above high and low in void turnOn and turnOff. Nothing happens. I can see on the Alexa app that it believes the device is being switched on and off but it appears to just be talking to the API not the Wemos, as it does the same regardless of whether the Wemos is powered or not!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub < https://github.com/kakopappa/sinric/issues/14#issuecomment-374016721> , or mute the thread < https://github.com/notifications/unsubscribe-auth/AUSB1Gl-RIgiYIvJgIrX-Z61irAuYzwKks5tfpQfgaJpZM4Rv05c> . < https://github.com/notifications/beacon/AUSB1BJfRckvQkw52s52ro8FsEXOIu8Lks5tfpQfgaJpZM4Rv05c.gif>

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/kakopappa/sinric/issues/14#issuecomment-374063322, or mute the thread https://github.com/notifications/unsubscribe-auth/AHIM5tTelKNytsBx6_1ZwmR_F4hzXG7Iks5tfugHgaJpZM4Rv05c .

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kakopappa/sinric/issues/14#issuecomment-374074596 , or mute the thread https://github.com/notifications/unsubscribe-auth/AUSB1FH3XptGfxz6okD0mtjZVh9PLapMks5tfwYBgaJpZM4Rv05c . https://github.com/notifications/beacon/AUSB1B3Es9mOnwIv21hN_faFtG-HuFMmks5tfwYBgaJpZM4Rv05c.gif

CEStanley58 commented 6 years ago

Here is my code. Please ensure you have “Generic ESP8266 Module” or “WEMOS D1R2 & mini” selected.

Port must be set in Tools,

Load speed on my system is good at 921600 but also works at lower speeds.

Change the TODO’s to meet your setup.

Cheers,

/*

Version 0.1 - Feb 10 2018

https://sinric.com/login

*/

include

include

include

include // get it from https://github.com/Links2004/arduinoWebSockets/releases

include // get it from https://arduinojson.org/ or install via Arduino library manager

ESP8266WiFiMulti WiFiMulti;

WebSocketsClient webSocket;

WiFiClient client;

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

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

define MyWifiPassword "YOUR WiFi PASSWORD" // TODO: Change to your Wifi network password

define API_ENDPOINT "http://sinric.com"

define HEARTBEAT_INTERVAL 300000 // 5 Minutes

define MASTERBEDROOM D5

define DEN D6

String Device1 = "YOUR SINRIC DEVICE1" // TODO: Change to your www.simric.com device number 1

String Device2 = "YOUR SINRIC DEVICE2" // TODO: Change to your www.simric.com device number 2

uint64_t heartbeatTimestamp = 0;

bool isConnected = false;

void turnOn(String deviceId)

{

if (deviceId == Device1 ) // Device ID of first device masterBedroom

{

Serial.print("Turn on device id: ");

Serial.println(deviceId);

digitalWrite(MASTERBEDROOM, HIGH);

}

else if (deviceId == Device2) // Device ID of second device den

{

Serial.print("Turn on command for device id: ");

Serial.println(deviceId);

digitalWrite(DEN, HIGH);

}

else

{

Serial.print("Turn on for unknown device id: ");

Serial.println(deviceId);    

}

}

void turnOff(String deviceId)

{

if (deviceId == Device1 ) // Device ID of first device bedRoom

{

 Serial.print("Turn off Device ID: ");

 Serial.println(deviceId);  

 digitalWrite(MASTERBEDROOM, LOW);   

}

else if (deviceId == Device2 ) // Device ID of second device

{

 Serial.print("Turn off Device ID: ");

 Serial.println(deviceId);

 digitalWrite(DEN, LOW);

}

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

  // 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

  DynamicJsonBuffer jsonBuffer;

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

  String deviceId = json ["deviceId"];     

  String action = json ["action"];

  if(action == "setPowerState") // Switch or Light

  {

    String value = json ["value"];

    if(value == "ON") 

    {

      //Serial.print("Calling turnOn device: ");

      Serial.println(deviceId);

      turnOn(deviceId);

    } 

    else 

    {

      //Serial.print("Calling turnOff device: ");

      Serial.println(deviceId);

      turnOff(deviceId);

    }

  }

  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;

}

}

void setup()

{

Serial.begin(115200);

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

}

// Set GPIO to output

pinMode(MASTERBEDROOM, OUTPUT);

pinMode(DEN, OUTPUT);

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

  }

}

}

Charles Stanley

From: pacostello notifications@github.com Sent: Sunday, March 18, 2018 10:07 AM To: kakopappa/sinric sinric@noreply.github.com Cc: CEStanley58 CEStanley58@gmail.com; Comment comment@noreply.github.com Subject: Re: [kakopappa/sinric] Alexa doesn't seem to be communicating properly (#14)

I appear to be having the same issue. Wemos D1 mini. Started with the Switch example code. Put in my SSID, password, API code and device code. I have defined D1 and BUILTIN_LED as outputs in void setup() Used digitalWrite to set the above high and low in void turnOn and turnOff. Nothing happens. I can see on the Alexa app that it believes the device is being switched on and off but it appears to just be talking to the API not the Wemos, as it does the same regardless of whether the Wemos is powered or not!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kakopappa/sinric/issues/14#issuecomment-374016721 , or mute the thread https://github.com/notifications/unsubscribe-auth/AUSB1Gl-RIgiYIvJgIrX-Z61irAuYzwKks5tfpQfgaJpZM4Rv05c . https://github.com/notifications/beacon/AUSB1BJfRckvQkw52s52ro8FsEXOIu8Lks5tfpQfgaJpZM4Rv05c.gif