Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arduino esp8266 script changed in Alexa #44

Open
TexAcoon opened this issue Nov 4, 2019 · 97 comments
Open

arduino esp8266 script changed in Alexa #44

TexAcoon opened this issue Nov 4, 2019 · 97 comments

Comments

@TexAcoon
Copy link

TexAcoon commented Nov 4, 2019

kakopappa -
I have used this script for over a year and worked so well I was planning on adding more devices to my home automation. Not sure what has gone wrong when asking Alexa to turn on the appliance it "states there is a malfunction" OR "cant find the appliance" (depending on what day it is and how good she feels.

I have not made any chances to the software and still can not get Alexa do discover any of my WeMos ESP8266s ... tried looking in "Serial Monitor" and do not see anything working. Has something changed in the Amazon Alexa for the system not to recognize the WeMos ESP8266 any longer?

@kakopappa
Copy link
Owner

Hi @TexAcoon

Emulation seems to have issues with new Alexa firmware updates. I think a smart home skill is the right way to do this. Do you mind giving a https://sinric.pro a try ?

@TexAcoon
Copy link
Author

TexAcoon commented Nov 4, 2019

Sure I will download it and try it out .. Will let you know what the status is when complete.

@TexAcoon
Copy link
Author

TexAcoon commented Nov 4, 2019

You have to pay for that service - I thought the whole idea of home automation using what we have is to only pay for the components and not the monthly programing. However, there is a free month involved and really interested what is going on with this ..

@TexAcoon
Copy link
Author

TexAcoon commented Nov 4, 2019

Kakopappa -
Do I use the same Arduino script or do I have to change another script .. for the smart devices that are built in like the Phillips light bulbs etc ... I can see how the connection would work .. Not sure if I have to add a new script to the WeMos or use the one you already created ... Thanks

@kakopappa
Copy link
Owner

Pro provides 5 devices for free for life and additional device is $3 per year!

you can find a sample sketch for switch here
https://github.com/sinricpro/esp8266-esp32-sdk/tree/master/examples/switch

Fill in the functions with the code you had already. shouldn't take longer than 5 mins

@TexAcoon
Copy link
Author

TexAcoon commented Nov 4, 2019

I have signed up and tried loading the software - Before there was not any issue uploading your script to "NodeMCU 1.0(ESP-12E)Module .. Now it gives me an error message: "exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module)." with the SicRic
I have tried several difference boards and they say the same thing .. What do you suggest?

@kakopappa
Copy link
Owner

kakopappa commented Nov 4, 2019 via email

@TexAcoon
Copy link
Author

TexAcoon commented Nov 4, 2019

I'm using Arduino never have tried the PlatformIO...
Here is my error message I am getting at this time..
https://www.dropbox.com/home?preview=esp8266+errpr.PNG

@kakopappa
Copy link
Owner

I think you forgot to share the image properly.

It shows me this when i open your link

image

@kakopappa
Copy link
Owner

Maybe you do not have the dependencies installed

image

https://github.com/sinricpro/esp8266-esp32-sdk#dependencies

@TexAcoon
Copy link
Author

TexAcoon commented Nov 4, 2019 via email

@TexAcoon
Copy link
Author

TexAcoon commented Nov 4, 2019 via email

@kakopappa
Copy link
Owner

#define SOCKET_AUTH_TOKEN -> In the Left menu -> Goto Credentails tab -> Copy APP KEY
#define SIGNING_KEY -> In the Left menu -> Goto Credentails tab -> Copy APP SECRET

image

"#define SWITCH_ID --> In the left menu -> Devices -> Device you created has an id like ID: 5d7exx18cxxdabxxca420bxx

image

@TexAcoon
Copy link
Author

TexAcoon commented Nov 5, 2019 via email

@kakopappa
Copy link
Owner

have you enabled the Sinric Pro skill and linked it? it's in the home page https://sinric.pro/

image

After linking the account you should be able to see your device in Alexa app

@TexAcoon
Copy link
Author

TexAcoon commented Nov 5, 2019 via email

@kakopappa
Copy link
Owner

kakopappa commented Nov 6, 2019

np,

Yesterday, there were some problems with the sevice and it's fixed now. If still does not work

Please enable Debug Info, to do that

eg:

`
#include <Arduino.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif

// new debug code starts here

#define DEBUG_ESP_PORT Serial // Add this
#define NODEBUG_WEBSOCKETS // Add this
#define NDEBUG // Add this

// new debug code ends here

#include "SinricPro.h"
#include "SinricProSwitch.h"
`

.......

This enables Debug on Serial port. Please post your debug output here

@TexAcoon
Copy link
Author

TexAcoon commented Nov 6, 2019 via email

@TexAcoon
Copy link
Author

TexAcoon commented Nov 6, 2019 via email

@kakopappa
Copy link
Owner

kakopappa commented Nov 6, 2019 via email

@TexAcoon
Copy link
Author

TexAcoon commented Nov 6, 2019 via email

@kakopappa
Copy link
Owner

kakopappa commented Nov 6, 2019 via email

@kakopappa
Copy link
Owner

kakopappa commented Nov 6, 2019

According to your profile settings, you have not installed the skill.  Please make sure you have linked "Sinric Pro" skill.. not "Sinric" Skill

image

This is the correct icon
https://www.amazon.com/dp/B07ZT5VDT8

@TexAcoon
Copy link
Author

TexAcoon commented Nov 6, 2019 via email

@kakopappa
Copy link
Owner

Yes, Alexa integration seems to be fine now. your problem is there are no devices connected with device id "5dxxxxxx26505c"

Please make sure ESP device id is correct

@TexAcoon
Copy link
Author

TexAcoon commented Nov 6, 2019 via email

@kakopappa
Copy link
Owner

Can I connect to your pc Teamviewer / quick assist and take a look?

@TexAcoon
Copy link
Author

TexAcoon commented Nov 6, 2019 via email

@kakopappa
Copy link
Owner

support@sinric.com - please drop me an email when you have a chance.

@clox-coder
Copy link

clox-coder commented Nov 8, 2019

@kakopappa I set everything up a few weeks ago. I am able to make everything discoverable. But when i say "Turn On" It says this device does not have the capability. But i am able to discover etc . it showing up as a philips hue Bulb. I signed up for Sinric Pro. But why cant i just use the code instead of the Sinric service
SO basically your Sinric Service works because your setting up a skill server with API? and still emulating with the same code?

@TexAcoon
Copy link
Author

TexAcoon commented Nov 13, 2019 via email

@clox-coder
Copy link

@TexAcoon I have run from 5 volt side of 8266 to 5 volt relay. Shows it has power but its not switching. I tried a few variations of the code. Last one i tried to run was this below.

#include <AsyncPrinter.h>
#include <DebugPrintMacros.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncTCPbuffer.h>
#include <SyncClient.h>
#include <async_config.h>
#include <tcp_axtls.h>

// Uncomment the following line to enable serial debug output
//#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif

#include <Arduino.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif

#include "SinricPro.h"
#include "SinricProSwitch.h"

#define WIFI_SSID ""
#define WIFI_PASS ""
#define APP_KEY "" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET "3ea29c9d-1622-429c-9e51-4732eaba5b71-7f1a9ba9-3cf0-40a7-a48f-4f6c7bbe048c" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define SWITCH_ID "" // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE 115200 // Change baudrate to your need

#define BTN_FLASH 0

bool myPowerState = false;
unsigned long lastBtnPress = 0;

/* bool onPowerState(String deviceId, bool &state)
*

  • Callback for setPowerState request
  • parameters
  • String deviceId (r)
  • contains deviceId (useful if this callback used by multiple devices)
  • bool &state (r/w)
  • contains the requested state (true:on / false:off)
  • must return the new state
  • return
  • true if request should be marked as handled correctly / false if not
    */
    bool onPowerState(String deviceId, bool &state) {
    Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off");
    myPowerState = state;
    digitalWrite(LED_BUILTIN, myPowerState?LOW:HIGH);
    return true; // request handled properly
    }

void handleButtonPress() {
unsigned long actualMillis = millis(); // get actual millis() and keep it in variable actualMillis
if (digitalRead(BTN_FLASH) == LOW && actualMillis - lastBtnPress > 1000) { // is button pressed (inverted logic! button pressed = LOW) and debounced?
if (myPowerState) { // flip myPowerState: if it was true, set it to false, vice versa
myPowerState = false;
} else {
myPowerState = true;
}
digitalWrite(LED_BUILTIN, myPowerState?LOW:HIGH); // if myPowerState indicates device turned on: turn on led (builtin led uses inverted logic: LOW = LED ON / HIGH = LED OFF)

// get Switch device back
SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];
// send powerstate event
mySwitch.sendPowerStateEvent(myPowerState); // send the new powerState to SinricPro server
Serial.printf("Device %s turned %s (manually via flashbutton)\r\n", mySwitch.getDeviceId(), myPowerState?"on":"off");

lastBtnPress = actualMillis;  // update last button press variable

}
}

// setup function for WiFi connection
void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
WiFi.begin(WIFI_SSID, WIFI_PASS);

while (WiFi.status() != WL_CONNECTED) {
Serial.printf(".");
delay(250);
}
IPAddress localIP = WiFi.localIP();
Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
}

// setup function for SinricPro
void setupSinricPro() {
// add device to SinricPro
SinricProSwitch& mySwitch = SinricPro.add(SWITCH_ID);

// set callback function to device
mySwitch.onPowerState(onPowerState);

// setup SinricPro
SinricPro.begin(APP_KEY, APP_SECRET);
}

// main setup function
void setup() {
pinMode(BTN_FLASH, INPUT_PULLUP); // GPIO 0 as input, pulled high
pinMode(LED_BUILTIN, OUTPUT); // define LED GPIO as output
digitalWrite(LED_BUILTIN, HIGH); // turn off LED on bootup

Serial.begin(BAUD_RATE);
setupWiFi();
setupSinricPro();
}

void loop() {
handleButtonPress();
SinricPro.handle();
}

@TexAcoon
Copy link
Author

TexAcoon commented Nov 13, 2019 via email

@clox-coder
Copy link

@TexAcoon hmm. yea I know its gotta be something simple I am missing. what pin are you activating on relay. are you setting up defining LEDs both as pin 1? I am trying D2 etc to relay and nothing.

@TexAcoon
Copy link
Author

TexAcoon commented Nov 13, 2019 via email

@clox-coder
Copy link

@TexAcoon I am using the usb for power on 8266. i have 5v pin on 8266 connected to dc+ on relay . grnd from 8266 to dc - on relay and pin D2 connected to IN on relay . 5v power showing lit on relay.

@TexAcoon
Copy link
Author

TexAcoon commented Nov 13, 2019 via email

@TexAcoon
Copy link
Author

TexAcoon commented Nov 13, 2019 via email

@TexAcoon
Copy link
Author

TexAcoon commented Nov 13, 2019 via email

@clox-coder
Copy link

@TexAcoon I checked the voltage its putting out 4.87 volts from esp8266 . I didnt want to use the sinric lock feature. I wonder if it is because of the code i am using is stating for Button Press. So it is not correct. I am probably gonna have too look over the code. Define Pins etc without the sinric lock feature @sivar2311 is this what i am missing?

void handleButtonPress() {
unsigned long actualMillis = millis(); // get actual millis() and keep it in variable actualMillis
if (digitalRead(BTN_FLASH) == LOW && actualMillis - lastBtnPress > 1000) { // is button pressed (inverted logic! button pressed = LOW) and debounced?
if (myPowerState) { // flip myPowerState: if it was true, set it to false, vice versa
myPowerState = false;
} else {
myPowerState = true;

@sivar2311
Copy link

sivar2311 commented Nov 13, 2019

Take a look at this code I found on instructables: https://www.instructables.com/id/Garage-Door-Opener-With-Feedback-Using-Esp8266/ ...
By itself it worked fine but have to log into another site iot site like Sinric Pro.

The project from instructables doesn't require another external service. There is a webserver running on ESP8266 wich lets you open and close the door.

Afaik if you're using a relay and a magnetic switch to read the door state, you have 2 pins to define: 1x to turn on the coil and 1x feedback from magnetic switch. I think you're using two of them: one relay to close the door and another one to open the door? If it is so, you have to define 4 pins! 2 as output to control the relays and 2 as inputs to read the states of the magnetic switches.

Please take out the defines i made (LED_GREEN / LED_RED) and make your own defines matching your wiring! Assuming you're using RELAY_ONE to open the door, and RELAY_TWO to close the door

#define RELAY_ONE D1 // used to open the door
#define MAGNET_ONE D2 // high=door is open
#define RELAY_TWO D3 // used to close the door
#define MAGNET_TWO D4 // high = door is closed

in setup() you have to setup the pins correctly

  pinMode(RELAY_ONE, OUTPUT);
  pinMode(RELAY_TWO, OUTPUT);
  pinMode(MAGNET_ONE, INPUT);
  pinMode(MAGNET_TWO, INPUT);

To activate the relays

  digitalWrite(RELAY_ONE, HIGH); // will turn on relay 1 and open the door
  digitalWrite(RELAY_TWO, HIGH); // will turn on relay 2 and close the door

To read the states of the magnetic switches:

  bool state_one = digitalRead(MAGNET_ONE);
  bool state_two = digitalRead(MAGNET_TWO);
state_one state_two state
HIGH LOW Door is in open position
LOW HIGH Door is in closed position
LOW LOW Door is opening or closing
HIGH HIGH You have a problem, because door can't be open and closed at the same time ;)

So you have to write your own functions to open and close the door. Call these functions from onLockState callback like this:

#define RELAY_ONE  D1      // used to open the door
#define MAGNET_ONE D2      // high=door is open | low = door is NOT open
#define RELAY_TWO  D3      // used to close the door
#define MAGNET_TWO D4      // high = door is closed | low = door is NOT closed
#define MOVE_TIMEOUT 10000 // timout 60 seconds for door movement -> please use a value which fits to your open/close time

/* function: turn on relay connected to pin RELAY and waits until MAGNET gets closed or timeout happened
 * input:    RELAY  = pin which is RELAY connected to
 *           MAGNET = pin which is MAGNET connected to
 * return:   true if MAGNET is closed 
 *           false if MAGNET is still open -> timout happened!
*/
bool turnOnRelayAndWait(int RELAY, int MAGNET, unsigned long timeout) {
  unsigned long startMillis = millis();
  digitalWrite(RELAY, HIGH); // turn on relay

  bool success = false;
  while ((!success) && (millis()-startMillis<timeout)) { // wait until MAGNET is HIGH or timeout is reached
    success = digitalRead(MAGNET);
    yield(); 
  }
  digitalWrite(RELAY, LOW); // turn off relay
  return success; 
}

bool onLockState(String deviceId, bool &lockState) {
  if (lockState==true) { // command: close GarageDoor
    Serial.printf("Closing garage door...");
    bool success = turnOnRelayAndWait(RELAY_TWO, MAGNET_TWO, MOVE_TIMEOUT);
    switch(success) {
      case true:   Serial.printf("closed\r\n"); 
                   lockState = true; // tell sinric door is closed           
                   break; 
      case false:  Serial.printf("timeout, door is not closed!!\r\n");
                   lockState = false; // tell sinric door is not closed
                   break;
    }
  } else { // command: open GarageDoor
    Serial.printf("Opening garage door...");
    bool success = turnOnRelayAndWait(RELAY_ONE, MAGNET_ONE, MOVE_TIMEOUT);
    switch(success) {
      case true:   Serial.printf("open\r\n");
                   lockState = false;  // tell sinric door is open
                   break;
      case false:  Serial.printf("timeout, door is not open!!\r\n");
                   lockState = true; // tell sinric door is not open
                   break;
    }
  }
  return true; // callback handled successfully
}

I hope this will help you.

@sivar2311
Copy link

@clox-coder

@sivar2311 is this what i am missing?

Sorry i can't follow you. What's your project like?
The code for handleButtonPress is for manually switching the device (on/off) and send this as an event back to SinricPro Server. So alexa get's informed about the new device state.

@sivar2311
Copy link

Because it was early in the morning when i wrote the code above...it turned out that it was not working correctly. That's why i edited the post. The code shown above is now correct.

@sivar2311
Copy link

You'll find my complete demo and wiring here: GarageDoorExample

Because i don't have any relays or reed-switches i used two LED's and two pushbuttons.

@TexAcoon
Copy link
Author

TexAcoon commented Nov 13, 2019 via email

@sivar2311
Copy link

Hi Doug,

you can't send photos to a @reply.github.com address.
There are two possibilities: a) upload your photos to a sharing site and post the link or b) send me your photos via e-mail to my address (sivar2311@gmail.com). Alternatively, if you're using google photos you can share your photos from there.

@clox-coder
Copy link

@sivar2311 that's what I was wondering. I wanted to have my relay switch on automatically at D2 when I ask Alexa to turn on device Instead of using push button method.

@sivar2311
Copy link

@clox-coder But the difference between onPowerState and sendPowerStateEvent is clear to you now?

@clox-coder
Copy link

@sivar2311 I am looking over both of these now. My old code was completely different. I was using the below code to turn my relay on . Would i define both pins at the beginning and then use the onPowerState and sendPowerStateEvent under main setup function?

void callback(uint8_t device_id, const char * device_name, bool state) {
Serial.print("Device "); Serial.print(device_name);
Serial.print(" state: ");
if (state) {
Serial.println("ON");
digitalWrite(D2,HIGH);
} else {
Serial.println("OFF");
digitalWrite(D2,LOW);
}
}

@sivar2311
Copy link

sivar2311 commented Nov 13, 2019

Yes because we are talking about SinricPro now. Topic changed during discussion. Have a look at https://sinric.pro and corresponding git repository: https://github.com/sinricpro/esp8266-esp32-sdk

@TexAcoon
Copy link
Author

TexAcoon commented Nov 13, 2019 via email

@sivar2311
Copy link

Oh okay... i thought you want to use the reed switches as endstop, to identify the position of the door. And no, i don't have automatic garage door opener....i don't have a garage :(
So, scenario is following:
You need to activate your existing garage door opener by a high signal for about 2 to 5 seconds.
Problem is: you (your ESP) doesn't know door's position! It's just sending the same signal (2-5 seconds high) to open or close the door.

@TexAcoon
Copy link
Author

TexAcoon commented Nov 13, 2019 via email

@TexAcoon
Copy link
Author

TexAcoon commented Nov 13, 2019 via email

@sivar2311
Copy link

Okay, i think i've got it.
so scenario is
1x relay to send 2-5 seconds high signal to turn the door (in whatever direction)
2x reed switches to identify door position. 1 for closed position and 1 for open position, right?

@sivar2311
Copy link

sivar2311 commented Nov 14, 2019

@TexAcoon I changed the example in my git repo to your needs. please have a look: https://github.com/sivar2311/GarageDoorExample

Note: I changed the used pins:
D5 = relay
D6 = reedswitch for open door state
D7 = reedswitch for closed door state

@TexAcoon
Copy link
Author

TexAcoon commented Nov 14, 2019 via email

@sivar2311
Copy link

Sounds good. I am going to improve the code a bit...so that manually actions (open / close) will be sent to server. But let us close this issue here, because we're discussing in wrong place. This issue is related to alexa-wemo-switch. But we're working on a concrete implementation about a garage door using SinricPro service. Let us switch over to https://github.com/sivar2311/GarageDoorExample.

@TexAcoon
Copy link
Author

TexAcoon commented Nov 14, 2019 via email

@sivar2311
Copy link

I created a discussion "issue" -> https://github.com/sivar2311/GarageDoorExample/issues/1. So we can switch over now.
@kakopappa I think we can close this issue here now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants