So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Chrisko
#82151 Hello,
Im new to all this and trying to have a relay turn on for 10 seconds and then turn off and stay off.

Here is the code that i have acquired with the help from others. But it still does not work and currently when the board is powered up the relay is activated.

Any help would be appreciated.

Thanks,

Code: Select all#include <WebSockets.h>
#include <WebSocketsClient.h>
#include <WebSocketsServer.h>

#define WIFI_NAME "Network"
#define WIFI_PASSWORD "4197548d"
#define DEVICE_ID 2
#define DEVICE_NAME "Relay"
#define TOKEN "~420713_YAn4BqH3tv"

#include <RemoteMe.h>
#include <RemoteMeSocketConnector.h>
#include <RemoteMeDirectWebSocketConnector.h>
#include <ESP8266WiFi.h>

#define Relay_1 D1
#define Relay_2 D2
#define Relay_3 D3
#define Relay_4 D4

// Declaring variables
unsigned long currentTime;
unsigned long previousTime = 0;

RemoteMe& remoteMe = RemoteMe::getInstance(TOKEN, DEVICE_ID);

//*************** CODE FOR CONFORTABLE VARIABLE SET *********************
inline void setRelay_1(boolean b) {remoteMe.getVariables()->setBoolean("Relay_1", b); }
inline void setRelay_2(boolean b) {remoteMe.getVariables()->setBoolean("Relay_2", b); }
inline void setRelay_3(boolean b) {remoteMe.getVariables()->setBoolean("Relay_3", b); }
inline void setRelay_4(boolean b) {remoteMe.getVariables()->setBoolean("Relay_4", b); }

//*************** IMPLEMENT FUNCTIONS BELOW *********************

// Do same for other functions
void onRelay_1Change(boolean b) {
    Serial.printf("onRelay_1Change: b: %d\n",b);
    currentTime = millis();
    if(b){
    if((currentTime - previousTime) >= 15000){
    digitalWrite(Relay_1, LOW);
}
    else{
    digitalWrite(Relay_1, HIGH);
    previousTime = currentTime;
}
    }
    else{
        digitalWrite(Relay_1, LOW);
    }
}

void onRelay_2Change(boolean b) {
    Serial.printf("onRelay_2Change: b: %d\n",b);
    digitalWrite(Relay_2,b ? HIGH : LOW);
    }

void onRelay_3Change(boolean b) {
    Serial.printf("onRelay_3Change: b: %d\n",b);
    digitalWrite(Relay_3,b ? HIGH : LOW);
}

void onRelay_4Change(boolean b) {
    Serial.printf("onRelay_4Change: b: %d\n",b);
    digitalWrite(Relay_4,b ? HIGH : LOW);
}

void setup() {
    Serial.begin(9600);
    WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
    delay(100);
}

remoteMe.getVariables()->observeBoolean("Relay_1" ,onRelay_1Change);
remoteMe.getVariables()->observeBoolean("Relay_2" ,onRelay_2Change);
remoteMe.getVariables()->observeBoolean("Relay_3" ,onRelay_3Change);
remoteMe.getVariables()->observeBoolean("Relay_4" ,onRelay_4Change);
remoteMe.setConnector(new RemoteMeSocketConnector());
remoteMe.setDirectConnector(new RemoteMeDirectWebSocketConnector());
remoteMe.sendRegisterDeviceMessage(DEVICE_NAME);

pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);

digitalWrite(Relay_1, LOW);
digitalWrite(Relay_2, LOW);
digitalWrite(Relay_3, LOW);
digitalWrite(Relay_4, LOW);
}

void loop() {
    remoteMe.loop();
}