Server updating.

After receiving so much help from members of this forum I have working code that does just what I need it to.

The last thing I need help with is how to use the last function in the Sinric code to update the server to show the relays are turned off when they time out.

At present the relays and motors work perfectly but when i turn them on via the app and then they turn of due to the timer set in the code, i need the server to be notified so it up dates the app.

I’m fairly certain everything I is all ready in the code it’s just how to make it happen.

[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 "840e2b02-a079-4f9e-b17b-0f6586e8a95e"
#define MySSID "BTHub6-CFR5"
#define MyWifiPassword "RP3wpV9DcNMH"
#define INTERVAL_MESSAGE1 5000
#define HEARTBEAT_INTERVAL 300000 // 5 Minutes 

int relayPin1 = 4;
int relayPin2 = 5;
int CWLimitSwitchPin = 9;
int ACWLimitSwitchPin = 10;
int mySwitchPin = 4;
int mySwitchPin1 = 5;
int motorPin1 = 14;
int motorPin2 = 12;
int motorPin3 = 13;
int motorPin4 = 15;
int delayTime = 2;
float c ;

class TimedRelay
{
  private:
    uint8_t pinNumber;
    bool isOn;
    bool checkTimeToTurnOff;
    unsigned long turnOnTime;
    unsigned long turnOnDuration;
  public:
    void begin(uint8_t pin)
    {
      pinNumber = pin;
      turnOnTime = 0;
      checkTimeToTurnOff = false;

      pinMode(pin, OUTPUT);
      turnOff();
    }
    void turnOn()
    {
      isOn = true;
      digitalWrite(pinNumber, LOW);
    }
    void turnOff()
    {
      digitalWrite(pinNumber, HIGH);
      isOn = false;
      checkTimeToTurnOff = false;
    }
    void turnOnForMs(unsigned long ms)
    {
      turnOnDuration = ms;
      checkTimeToTurnOff = true;
      turnOn();
      turnOnTime = millis();
    }
    void spin()
    {
      if (!isOn)
        return;
      if (!checkTimeToTurnOff)
        return;
      if (millis() - turnOnTime >= turnOnDuration)
      {
        turnOff();
      }
    }
    bool isRelayOn()
    {
      return isOn;
    }
};

TimedRelay gRelay1;
TimedRelay gRelay2;

uint64_t heartbeatTimestamp = 0;
bool isConnected = false;

#define DEVICE1 "5c4cf127dfd0ba6f5c3351f4"
#define DEVICE2 "5c4cf17cdfd0ba6f5c3351f6"


void setPowerStateOnServer(String deviceId, String value);

// 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 == DEVICE1)
  {
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);

    digitalWrite(relayPin1, LOW); // turn on relay with voltage LOW
    gRelay1.turnOnForMs(2000);
  }
  else  if (deviceId == DEVICE2)
  {
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);

    digitalWrite(relayPin2, LOW); // turn on relay with voltage LOW
    gRelay2.turnOnForMs(2000);
  }
  else {
    Serial.print("Turn on for unknown device id: ");
    Serial.println(deviceId);
  }
}
void turnOff(String deviceId) {
  if (deviceId == DEVICE1)
  {
    Serial.print("Turn off Device ID: ");
    Serial.println(deviceId);

    gRelay1.turnOff();
    digitalWrite(relayPin1, HIGH);  // turn off relay with voltage HIGH
  }
  else if (deviceId == DEVICE2)
  {
    Serial.print("Turn off Device ID: ");
    Serial.println(relayPin2);

    gRelay2.turnOff();
    digitalWrite(relayPin2, HIGH);  // turn off relay with voltage 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);

        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 == "test") {
          Serial.println("[WSc] received test command from sinric.com");
        }
      }
      break;
    case WStype_BIN:
      Serial.printf("[WSc] get binary length: %u\n", length);
      break;
  }
}

I will spilt the code across 2 posts due to it’s size.

As always thanks in advance for any help.

void setup() {

  gRelay1.begin(4);
  gRelay2.begin(5);

  Serial.begin(115200);

  pinMode(CWLimitSwitchPin, INPUT_PULLUP);
  pinMode(ACWLimitSwitchPin, INPUT_PULLUP);
  pinMode(mySwitchPin, INPUT_PULLUP);
  pinMode(mySwitchPin1, INPUT_PULLUP);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);


  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);


  digitalWrite(relayPin1, HIGH);
  digitalWrite(relayPin2, HIGH);
  digitalWrite(CWLimitSwitchPin, HIGH);
  digitalWrite(ACWLimitSwitchPin, HIGH);

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

  gRelay1.spin();
  gRelay2.spin();


  webSocket.loop();

  if (digitalRead(mySwitchPin) == LOW)
    clockwise();

  if (digitalRead(mySwitchPin1) == LOW)
    anticlockwise();

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


void clockwise()
{
  for (int i = 0; i < 510; i++)
  {
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, HIGH);
    delay(delayTime);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, HIGH);
    digitalWrite(motorPin4, LOW);
    delay(delayTime);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    delay(delayTime);
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    delay(delayTime);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    delay(delayTime);
    // Exit from the loop if the limit switch has been pressed
    if (digitalRead(CWLimitSwitchPin) == LOW)
      break;
  }
}

void anticlockwise()
{
  for (int i = 0; i < 510; i++)
  {
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    delay(delayTime);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    delay(delayTime);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, HIGH);
    digitalWrite(motorPin4, LOW);
    delay(delayTime);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, HIGH);
    delay(delayTime);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    delay(delayTime);
    // Exit from the loop if the limit switch has been pressed
    if (digitalRead(ACWLimitSwitchPin) == LOW)
      break;
  }
}

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

  webSocket.sendTXT(databuf);
}

Thanks again