kakopappa / sinric

Amazon Alexa Smart home skill / Google Home Action for ESP8266 / ESP32 / Arduino

Home Page:https://sinric.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Esp8266 does not connect when router restarts

Mahesh1385 opened this issue · comments

Esp8266 does not connect to router when router restarts.
The board needs to power off and then power on to connect to the router. What may be the problem. Please help

Inside the loop, do you check if the card is still connected to the network?

It is connected. If i restart the power to the board its connecting again.

I think WiFi does not reconnect automaticly.
If you restart the board, WiFi.begin(ssid, pass) gets called which will connect to your router.
Maybe you should try WiFi.setAutoReconnect(true) see here

Ok i will check the same and notify

This is the code once check please
int device1 = 5;
int device2 = 4;
int device3 = 0;
int device4 = 2;
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h> // get it from https://github.com/Links2004/arduinoWebSockets/releases
#include <ArduinoJson.h> // get it from https://arduinojson.org/ or install via Arduino library manager
#include <StreamString.h>

ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
WiFiClient client;

#define MyApiKey "8c3cc952-6169-4c49-910d-75876501b112" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
#define MySSID " " // TODO: Change to your Wifi network SSID
#define MyWifiPassword "srinika123" // TODO: Change to your Wifi network password

#define HEARTBEAT_INTERVAL 300000 // 5 Minutes

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 == "5f6d91b7d1e9084a7085f0ee") // Device ID of device 1
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(device1, LOW);
}
else if (deviceId == "5f6d91ead1e9084a7085f0f2") // Device ID of device 2
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(device2, LOW);
}
else if (deviceId == "5f6d91f7d1e9084a7085f0ff") // Device ID of device 3
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(device3, LOW);
}
else if (deviceId == "5f6d9202d1e9084a7085f101") // Device ID of device 4
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(device4, LOW);
}
else
{
Serial.print("Turn on for unknown device id: ");
Serial.println(deviceId);
}
}

void turnOff(String deviceId) {
if (deviceId == "5f6d91b7d1e9084a7085f0ee") // Device ID of device 1
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(device1, HIGH);
}
else if (deviceId == "5f6d91ead1e9084a7085f0f2") // Device ID of device 2
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(device2, HIGH);
}
else if (deviceId == "5f6d91f7d1e9084a7085f0ff") // Device ID of device 3
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(device3, HIGH);
}
else if (deviceId == "5f6d9202d1e9084a7085f101") // Device ID of device 4
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(device4, HIGH);
}
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

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") {
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()
{
pinMode (device1, OUTPUT);
pinMode (device2, OUTPUT);
pinMode (device3, OUTPUT);
pinMode (device4, OUTPUT);

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

// 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")
void setPowerStateOnServer(String deviceId, String value) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["deviceId"] = deviceId;
root["action"] = "setPowerState";
root["value"] = value;
StreamString databuf;
root.printTo(databuf);

webSocket.sendTXT(databuf);
}

//eg: setPowerStateOnServer("deviceid", "CELSIUS", "25.0")
void setTargetTemperatureOnServer(String deviceId, String value, String scale) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["action"] = "SetTargetTemperature";
root["deviceId"] = deviceId;

JsonObject& valueObj = root.createNestedObject("value");
JsonObject& targetSetpoint = valueObj.createNestedObject("targetSetpoint");
targetSetpoint["value"] = value;
targetSetpoint["scale"] = scale;

StreamString databuf;
root.printTo(databuf);

webSocket.sendTXT(databuf);
}

Sorry, i dont have the time to analyze your code. But you can change to SinricPro. Our sdk reconnects websocket connection automaticly (except wifi, but for this setAutoReconnect)

is sinric pro free to use?

ok thank you for the fast response

This is the code once check please
int device1 = 5;
int device2 = 4;
int device3 = 0;
int device4 = 2;
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h> // get it from https://github.com/Links2004/arduinoWebSockets/releases
#include <ArduinoJson.h> // get it from https://arduinojson.org/ or install via Arduino library manager
#include <StreamString.h>

ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
WiFiClient client;

#define MyApiKey "8c3cc952-6169-4c49-910d-75876501b112" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
#define MySSID " " // TODO: Change to your Wifi network SSID
#define MyWifiPassword "srinika123" // TODO: Change to your Wifi network password

#define HEARTBEAT_INTERVAL 300000 // 5 Minutes

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 == "5f6d91b7d1e9084a7085f0ee") // Device ID of device 1
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(device1, LOW);
}
else if (deviceId == "5f6d91ead1e9084a7085f0f2") // Device ID of device 2
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(device2, LOW);
}
else if (deviceId == "5f6d91f7d1e9084a7085f0ff") // Device ID of device 3
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(device3, LOW);
}
else if (deviceId == "5f6d9202d1e9084a7085f101") // Device ID of device 4
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(device4, LOW);
}
else
{
Serial.print("Turn on for unknown device id: ");
Serial.println(deviceId);
}
}

void turnOff(String deviceId) {
if (deviceId == "5f6d91b7d1e9084a7085f0ee") // Device ID of device 1
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(device1, HIGH);
}
else if (deviceId == "5f6d91ead1e9084a7085f0f2") // Device ID of device 2
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(device2, HIGH);
}
else if (deviceId == "5f6d91f7d1e9084a7085f0ff") // Device ID of device 3
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(device3, HIGH);
}
else if (deviceId == "5f6d9202d1e9084a7085f101") // Device ID of device 4
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(device4, HIGH);
}
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

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") {
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()
{
pinMode (device1, OUTPUT);
pinMode (device2, OUTPUT);
pinMode (device3, OUTPUT);
pinMode (device4, OUTPUT);

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

// 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")
void setPowerStateOnServer(String deviceId, String value) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["deviceId"] = deviceId;
root["action"] = "setPowerState";
root["value"] = value;
StreamString databuf;
root.printTo(databuf);

webSocket.sendTXT(databuf);
}

//eg: setPowerStateOnServer("deviceid", "CELSIUS", "25.0")
void setTargetTemperatureOnServer(String deviceId, String value, String scale) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["action"] = "SetTargetTemperature";
root["deviceId"] = deviceId;

JsonObject& valueObj = root.createNestedObject("value");
JsonObject& targetSetpoint = valueObj.createNestedObject("targetSetpoint");
targetSetpoint["value"] = value;
targetSetpoint["scale"] = scale;

StreamString databuf;
root.printTo(databuf);

webSocket.sendTXT(databuf);
}

I was facing the same issue, the websocket was not able to know when the connection was lost and websocket did not establish a connection again unless the board was reset. The heartbeat checking is the one which is supposed to ensure that the connection loss is detected and reconnect websocket. I discovered that websocket library has an inbuilt function which can be used to sort out this issue. You can just avoid using the manual heartbeat checking and use the following line of code within setup(), the problem should be resolved. It was resolved for me.

// can be put just after the " webSocket.setReconnectInterval(5000); " line in the code and heartbeattimestamp part can be //deleted as it servers no purpose anymore.
webSocket.enableHeartbeat(15000, 3000, 2);

I've spent quite some time to find an issue for the same problem and i found a solution a couple of days back n did a few tests to verify that the problem was resolved. Hope this helps. Do let me know if the issue is resolved for you as well.

Thank you

Thanks for the response. I can not understand what you said because i don't have basic knowledge in this. Please could you say where to replace and where to delete in detail