kakopappa / sinric

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

[WSc] Webservice disconnected from sinric.com! #401

Open KhaledElGayyar opened 4 years ago

KhaledElGayyar commented 4 years ago

I was using nodeMCU 0.9 to control 4 relays and worked just fine, till now trying to connect it again and this message repeated over and over for every action i do .. noted that i'm connecting to my mobile hotspot and successfully connecting but no action .. Help me please

the code is :

/ Version 0.4 - April 26 2019 /

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 (use the correct version)

include

ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;

define MyApiKey "ab4773bd-f053-4ead-987e-c19f7a6716ba" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard

define MySSID "G-S6 Edge Mobile" // TODO: Change to your Wifi network SSID

define MyWifiPassword "comeflywithme" // TODO: Change to your Wifi network password

define HEARTBEAT_INTERVAL 300000 // 5 Minutes

uint64_t heartbeatTimestamp = 0; bool isConnected = false;

int relay1 = 14; int relay2 = 12; int relay3 = 13; int relay4 = 15;

// 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 == "5e6fe6cc28848668072d2024") // Device ID of first device {
Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(relay1,HIGH); } else if (deviceId == "5e6fe6e428848668072d202e") // Device ID of second device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(relay2,HIGH); } else if (deviceId == "5e6fe6ee28848668072d2034") // Device ID of second device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(relay3,HIGH); } else if (deviceId == "5e6fe6f928848668072d203a") // Device ID of second device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(relay4,HIGH); } else { Serial.print("Turn on for unknown device id: "); Serial.println(deviceId);
}
}

void turnOff(String deviceId) { if (deviceId == "5e6fe6cc28848668072d2024") // Device ID of first device {
Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(relay1,LOW); } else if (deviceId == "5e6fe6e428848668072d202e") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(relay2,LOW); } else if (deviceId == "5e6fe6ee28848668072d2034") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(relay3,LOW); } else if (deviceId == "5e6fe6f928848668072d203a") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(relay4,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); pinMode(relay1,OUTPUT); pinMode(relay2,OUTPUT); pinMode(relay3,OUTPUT); pinMode(relay4,OUTPUT); digitalWrite(relay1,LOW); digitalWrite(relay2,LOW); digitalWrite(relay3,LOW); digitalWrite(relay4,LOW);

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 want a push button: https://github.com/kakopappa/sinric/blob/master/arduino_examples/switch_with_push_button.ino