单命令继电器on/off
Single command relay on/off
嘿,我最近有一个家庭自动化项目,我有一个带有以太网屏蔽的 Arduino Mega。
Mega 正在等待 Telnet 命令。收到命令后,它会打开继电器 on
。然后我有一个自动热键脚本,当我在 Windows PC 上按下特定键时,它会发送 Telnet 命令。
我的问题是我计划使用 4 个中继,现在我必须为每个中继分配两个密钥(一个用于 on
和一个用于 off
)。
我研究并发现了脉冲继电器,但由于封锁,我买不到。我尝试 find/write 代码在一个简单的中继中实现相同的想法但失败了。
所以,我的问题是,如何使用单个命令触发继电器 on
/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");
}
如果你想要一个单一的命令(=切换),你需要为每个继电器一个全局布尔变量:
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;
}
希望这就是你的意思
嘿,我最近有一个家庭自动化项目,我有一个带有以太网屏蔽的 Arduino Mega。
Mega 正在等待 Telnet 命令。收到命令后,它会打开继电器 on
。然后我有一个自动热键脚本,当我在 Windows PC 上按下特定键时,它会发送 Telnet 命令。
我的问题是我计划使用 4 个中继,现在我必须为每个中继分配两个密钥(一个用于 on
和一个用于 off
)。
我研究并发现了脉冲继电器,但由于封锁,我买不到。我尝试 find/write 代码在一个简单的中继中实现相同的想法但失败了。
所以,我的问题是,如何使用单个命令触发继电器 on
/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");
}
如果你想要一个单一的命令(=切换),你需要为每个继电器一个全局布尔变量:
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;
}
希望这就是你的意思