Skip to content
This repository has been archived by the owner on Oct 1, 2021. It is now read-only.

wemos board control with Alexa #55

Open
4x1ks opened this issue Apr 1, 2018 · 39 comments
Open

wemos board control with Alexa #55

4x1ks opened this issue Apr 1, 2018 · 39 comments

Comments

@4x1ks
Copy link

4x1ks commented Apr 1, 2018

Newbie alert...I have an arduino d1 mini. I added the sinric skill to alexa smart home. I uploaded code to turn the BUILTIN_LED on and off. The mini shows up on my wifi. The alexa app finds my device "bigrelay."

When I ask Alexa to turn on or off the device, she says "ok" but nothing happens. The LED does not change state.

I think my code is ok, it isn't mine..I copied it. I also tried the code to control 5v to a pin to a relay and this also did not work. I'm thinking that it isn't the code, it is the Alexa / Wemos connection.

Any help is appreciated very much.

Mark

@kakopappa
Copy link
Owner

kakopappa commented Apr 1, 2018 via email

@4x1ks
Copy link
Author

4x1ks commented Apr 1, 2018 via email

@4x1ks
Copy link
Author

4x1ks commented Apr 1, 2018 via email

@kakopappa
Copy link
Owner

kakopappa commented Apr 1, 2018 via email

@4x1ks
Copy link
Author

4x1ks commented Apr 1, 2018 via email

@kakopappa
Copy link
Owner

Follow this youtube link to install Arduinojson.
https://www.youtube.com/watch?v=GUTpaY1YaXo

do the same for websocket

it's not that complicated.

@4x1ks
Copy link
Author

4x1ks commented Apr 1, 2018 via email

@4x1ks
Copy link
Author

4x1ks commented Apr 1, 2018 via email

@4x1ks
Copy link
Author

4x1ks commented Apr 1, 2018 via email

@4x1ks
Copy link
Author

4x1ks commented Apr 1, 2018 via email

@kakopappa
Copy link
Owner

kakopappa commented Apr 1, 2018 via email

@4x1ks
Copy link
Author

4x1ks commented Apr 1, 2018 via email

@4x1ks
Copy link
Author

4x1ks commented Apr 1, 2018 via email

@kakopappa
Copy link
Owner

kakopappa commented Apr 1, 2018 via email

@4x1ks
Copy link
Author

4x1ks commented Apr 1, 2018 via email

@4x1ks
Copy link
Author

4x1ks commented Apr 1, 2018 via email

@kakopappa
Copy link
Owner

kakopappa commented Apr 1, 2018 via email

@4x1ks
Copy link
Author

4x1ks commented Apr 2, 2018 via email

@kakopappa
Copy link
Owner

kakopappa commented Apr 2, 2018

Hello

// {"deviceId": xxxx, "action": "setPowerState", value: "ON"}

xxx here is the device id displayed in your dashboard. device id is used to identify which device is this command related to when you have multiple smart home devices.

if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // <--- copy device id from the dashboard and paste here
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
}
else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // <-- another device id if you have created multiple switches
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
}
else {
Serial.print("Turn off for unknown device id: ");
Serial.println(deviceId);
}

@4x1ks
Copy link
Author

4x1ks commented Apr 2, 2018 via email

@kakopappa
Copy link
Owner

kakopappa commented Apr 2, 2018

if (deviceId == "your device id from dashboard")
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
}

  1. In the setup you have to set the pinmode

void setup(){
pinMode(MYPIN, OUTPUT);
// rest of the code below..
}

@4x1ks
Copy link
Author

4x1ks commented Apr 2, 2018 via email

@kakopappa
Copy link
Owner

still waiting ..

@4x1ks
Copy link
Author

4x1ks commented Apr 4, 2018 via email

@kakopappa
Copy link
Owner

kakopappa commented Apr 5, 2018

becareful when you are working with high voltage. don't burn your house down

https://paypal.me/arunat

I am planning to make a bigger's video this weekend

@4x1ks
Copy link
Author

4x1ks commented Apr 5, 2018 via email

@GaryLittlemore
Copy link

4x1ks I'm also a Noob trying to get this working with a relay shield. Any chance you could post your working .ino code please?

I'm also confused about this device id, the TODO says "MyApiKey", "MySSID" & "MyWifiPassword", it don't mention I have to get a deviceId?

@kakopappa
Copy link
Owner

kakopappa commented Apr 5, 2018 via email

@GaryLittlemore
Copy link

Thanks for that, I've found it. maybe the code needs to have the "// TODO" on it.

Just need the relay code now ;-)

@GaryLittlemore
Copy link

Where in the .ino code should the relay code be placed? Line number?

void setup(){
pinMode(MYPIN, OUTPUT);
// rest of the code below..
}

@kakopappa
Copy link
Owner

in setup function you have to set the pinMode.

then change to high or low to turn on / off

https://arduinobasics.blogspot.com/2014/09/relay-module.html

@GaryLittlemore
Copy link

GaryLittlemore commented Apr 5, 2018

Thanks, I don't really understand that. I'm not a coder. I've been trying to follow along watching videos and replicating. Hopefully 4x1ks will be able to post their code. Don't worry I'm not doing the wiring with high voltage, a friend will be helping me there.

@4x1ks
Copy link
Author

4x1ks commented Apr 5, 2018 via email

@GaryLittlemore
Copy link

gary (dot) littlemore (at) btinternet (dot) com

Thanks Mark

@nizamaj
Copy link

nizamaj commented Nov 5, 2018

Hi Machan kakopappa , Thanks for the wonderful work .. i am planing to flash my LinkNode R4 to work with alexa (https://www.amazon.com/LinkNode-R4-controller-4-Channel-Module/dp/B01FUC9U58/ref=cm_cr_arp_d_product_top?ie=UTF8) and found out your old code .. (https://github.com/CharlesJGantt/LinkNode-R4-Controlled-By-Amazon-Alexa/tree/master) .. My question is .. is there a way i can include sinric to this code ?? or do i need to change entire libraries and codes ?

@kakopappa
Copy link
Owner

I think old method still can be used but unrealiable. It used to emulate a Belkin WeMos switch. Sometimes it work sometimes not.

If you do not have access to Sinric Skill you have to use the old method.

@nizamaj
Copy link

nizamaj commented Nov 5, 2018

Exactly the old method sometimes it working sometime don't .. planing to flash my LinkNode with new code .. is there any link to know how to add Sinric to the old code ??

@Dr-Bundolo
Copy link

First of all Thanks for creating the library and all the time you've spent trying to help all of us newbies. I've spent the better part of two days trying to get my D1 mini to work with Alexa. I'm very close. As you can see from the screen shot of the serial monitor the sketch works. If I press test on Sinric, or power on with Alexa it registers on the Com. Great! The problem is that it does NOT turn on the relay. I've tried the relay on numerous pins (changing the pin assignment in the program) with no change in the result. I've tried numerous boards (right now I'm using WeMos D1 R1, but I've tried the generic board as well. Still won't trigger the relay. I've tried different program with the same result. I've tested the D1 with a blink program - works. Tried different relays with the same result. Any advice
COM3_SCREEN
Here's the Code
` #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
#include <StreamString.h>

ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
WiFiClient client;

#define MyApiKey "f0e87a67-bcca-469e-b593-3a2882e60ca3" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
#define MySSID "kuggle's_condo_2" // TODO: Change to your Wifi network SSID
#define MyWifiPassword "jakesnak" // TODO: Change to your Wifi network password

#define HEARTBEAT_INTERVAL 300000 // 5 Minutes

const int relayPin = 5;

uint64_t heartbeatTimestamp = 0;
bool isConnected = false;

void setPowerStateOnServer(String deviceId, String value);
void setTargetTemperatureOnServer(String deviceId, String value, String scale);

// deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it here

void turnOn(String deviceId) {
if (deviceId == "600dce7798845d1692f01e0e") // Device ID of first device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(relayPin, HIGH);
digitalWrite(relayPin, HIGH); // turn on relay with voltage HIGH
}
else {
Serial.print("Turn on for unknown device id: ");
Serial.println(deviceId);
digitalWrite(relayPin, LOW);
}
}

void turnOff(String deviceId) {
if (deviceId == "5600dce7798845d1692f01e0e") // Device ID of first device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);

 digitalWrite(relayPin, LOW);  // turn off relay with voltage 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 Switch or Light device types
    // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html

    // For Light device type
    // Look at the light example in github

#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 == "SetTargetTemperature") {
        String deviceId = json ["deviceId"];     
        String action = json ["action"];
        String value = json ["value"];
    }
    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);

// Relay PIN eg: https://github.com/wemos/D1_mini_Examples/blob/master/examples/04.Shields/Relay_Shield/Blink/Blink.ino
pinMode(relayPin, OUTPUT);

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

}
}

// If you are going to use a push button to on/off the switch manually, use this function to update the status on the server
// so it will reflect on Alexa app.
// eg: setPowerStateOnServer("deviceid", "ON")

// Call ONLY If status changed. DO NOT CALL THIS IN loop() and overload the server.
void setPowerStateOnServer(String deviceId, String value) {
#if ARDUINOJSON_VERSION_MAJOR == 5
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
#endif
#if ARDUINOJSON_VERSION_MAJOR == 6
DynamicJsonDocument root(1024);
#endif
root["deviceId"] = deviceId;
root["action"] = "setPowerState";
root["value"] = value;
StreamString databuf;
#if ARDUINOJSON_VERSION_MAJOR == 5
root.printTo(databuf);
#endif
#if ARDUINOJSON_VERSION_MAJOR == 6
serializeJson(root, databuf);
#endif

webSocket.sendTXT(databuf);
}

//eg: setPowerStateOnServer("deviceid", "CELSIUS", "25.0")

// Call ONLY If status changed. DO NOT CALL THIS IN loop() and overload the server.
void setTargetTemperatureOnServer(String deviceId, String value, String scale) {
#if ARDUINOJSON_VERSION_MAJOR == 5
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
#endif
#if ARDUINOJSON_VERSION_MAJOR == 6
DynamicJsonDocument root(1024);
#endif
root["action"] = "SetTargetTemperature";
root["deviceId"] = deviceId;

#if ARDUINOJSON_VERSION_MAJOR == 5
JsonObject& valueObj = root.createNestedObject("value");
JsonObject& targetSetpoint = valueObj.createNestedObject("targetSetpoint");
#endif
#if ARDUINOJSON_VERSION_MAJOR == 6
JsonObject valueObj = root.createNestedObject("value");
JsonObject targetSetpoint = valueObj.createNestedObject("targetSetpoint");
#endif
targetSetpoint["value"] = value;
targetSetpoint["scale"] = scale;

StreamString databuf;
#if ARDUINOJSON_VERSION_MAJOR == 5
root.printTo(databuf);
#endif
#if ARDUINOJSON_VERSION_MAJOR == 6
serializeJson(root, databuf);
#endif

webSocket.sendTXT(databuf);
} `

@Dr-Bundolo
Copy link

Okay, A day later I have solved my problem. As always it was a series of problems. The first was making sure I had the proper drivers downloaded. Then it was testing all my USB cables until I found one that worked and the computer recognized the port. That was a biggie. But still couldn't get the program to work. Alexa would turn on and off the device. You could see the BLUE LED come on when Alexa turned it on, and turn off when Alexa turned it off.... so close. then after searching more on the internet the final piece fell in place. As is the usual case I had the relay powered from the VCC. I checked it with a volt meter and it was getting 4.75. But the relay would not trigger. Finally I read someone suggested trying using the 3.3V output on the board. I was not convinced as my experience is that voltage is too low to trigger the relay, but low and behold it worked! So If your getting everything but the relay working, change to 3.3v. It will work. Hope this helps.

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

No branches or pull requests

5 participants