单指令继电器开/关

问题描述 投票:0回答:1

嘿,我最近正在从事一个家庭自动化项目,其中有一个带以太网屏蔽的Arduino mega。巨型正在等待telnet命令。收到命令后,它将打开继电器。然后,我有一个自动热键脚本,当我按Windows PC上的特定键时会发送telnet命令。我的问题是我计划使用4个继电器,现在我必须为每个继电器分配两个键(一个键用于ond或off)。我研究并发现了有关脉冲继电器的信息,但由于锁定原因,我无法购买任何继电器。我试图通过简单的中继查找/编写实现相同想法的代码,但失败了。因此,我的问题是,如何使用单个命令触发继电器开/关。

我正在使用的代码:

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

}
arduino autohotkey telnet arduino-ide
1个回答
1
投票

如果要使用单个命令(=切换),则每个继电器都需要一个全局布尔变量:

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

希望这就是您的意思

© www.soinside.com 2019 - 2024. All rights reserved.