Search code examples
arduinoautohotkeytelnetarduino-ide

Single command relay on/off


Hey I have a home automation project I've been working on recently, in which I have an Arduino Mega with an Ethernet shield.

The Mega is waiting for Telnet commands. When a command is received, it turns a relay on. I then have an auto-hotkey script that sends Telnet commands when I press specific keys on my Windows PC.

My problem is that I plan to use 4 relays and right now I have to assign two keys per relay(one for on and for off).

I researched and found about impulse relays, but due to the lockdown, I can't buy any. I tried to find/write code that implemented the same idea in a simple relay but failed.

So, my question is, How do you trigger a relay on/off with a single command?

The code I am using:

#include <SPI.h>
#include <Ethernet.h>

int backlight = 7;
int fan = 6;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,21,108);
IPAddress gateway(192,168,21,21);
IPAddress subnet(255, 255, 255, 0);

// telnet defaults to port 23
EthernetServer server(23);
boolean alreadyConnected = false; // whether or not the client was connected previously

String commandString;

void setup() {

    pinMode(fan, OUTPUT);
    pinMode(backlight, OUTPUT);  

    Ethernet.begin(mac, ip, gateway, subnet);
    // start listening for clients
    server.begin();
    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {
        ; // wait for serial port to connect. Needed for Leonardo only
    }

    Serial.print("Chat server address:");
    Serial.println(Ethernet.localIP());
}

void loop() {

    // wait for a new client:
    EthernetClient client = server.available();

    // when the client sends the first byte, say hello:
    if (client) {

        if (!alreadyConnected) {
            // clear out the input buffer:
            client.flush();    
            commandString = ""; //clear the commandString variable

            server.println("--> Please type your command and hit Return...");
            alreadyConnected = true;
        } 

        while (client.available()) {      
            // read the bytes incoming from the client:
            char newChar = client.read();

            if (newChar == 0x0D) {  //If a 0x0D is received, a Carriage Return, then evaluate the command
                server.print("Received this command: ");
                server.println(commandString);   
                processCommand(commandString);
            } else {
                Serial.println(newChar);
                commandString += newChar;
            }
        }
    }
}

void processCommand(String command)
{
    server.print("Processing command ");
    server.println(command);

    if (command.indexOf("backlight1") > -1){
        server.println("Backlight On command received"); 
        digitalWrite(backlight, HIGH);   // sets the LED on
        server.println("Backlight was turned on");
        commandString = "";
        return;
    }
    if (command.indexOf("backlight0") > -1){
        Serial.println("Backlight Off command received"); 
        digitalWrite(backlight, LOW);   // sets the LED off
        server.println("Backlight was turned off");
        commandString = "";
        return;;
    }
    if (command.indexOf("fan1") > -1){
        server.println("fan On command received"); 
        digitalWrite(fan, HIGH);   // sets the LED on
        server.println("Fan was turned on");
        commandString = "";
        return;
    } 
    if (command.indexOf("fan0") > -1 ){
        Serial.println("fan Off command received"); 
        digitalWrite(fan, LOW);   // sets the LED off
        server.println("Fan was turned off");
        commandString = "";
        return;
    } 

    commandString = "";
    instructions();
}

void instructions()
{
    server.println("Please use one of these commands:");
    server.println("* backlight1, to turn backlight on");
    server.println("* backlight0, to turn off the backligt");
    server.println("* fan1, to turn on the fan");
    server.println("* fan0, to turn off the fan");
}

Solution

  • If you want to have a single command (= toggle) you need a global bool var for each relay:

    bool fanIsOn = false;
    
    // The toggle command is fan
    if (command.indexOf("fan") > -1 && fanIsOn == false){
        server.println("fan On command received"); 
        digitalWrite(fan, HIGH);   // sets the LED on
        server.println("Fan was turned on");
        fanIsOn = true; 
        commandString = "";
        return;
      } 
    
      if (command.indexOf("fan") > -1 && fanIsOn == true){
        Serial.println("fan Off command received"); 
        digitalWrite(fan, LOW);   // sets the LED off
        server.println("Fan was turned off");
        fanIsOn = false;
        commandString = "";
        return;
      } 
    

    Hope this was what you meant