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.