用Arduino远程控制车库门开关

“我用一个以太网插板负载到Arduino上。如此我便实现了打开我的车库门……就是这样! 我也安装了一个磁传感器,门是开着的或者封闭的都可以让我知道。所有都通过Telnet实现。 我修改了来自聊天服务器代码。现在贴出代码给爱好者使用。“

 控制车库门1

图一  安装在车库里的Arduino装置

控制车库门2

图二 正在打开车库门

控制车库门3

图三 用手机远程控制车库门开关

代码:

#include <SPI.h>
#include <Ethernet.h>
String readString;

// 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,1, 177);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
// telnet defaults to port 23
EthernetServer server(23);
boolean alreadyConnected = false; // whether or not the client was connected previously

const int analogInPin = A4;
int relay = 3;
int sensorValue = 0;

void setup() {
 
  pinMode(2, INPUT_PULLUP);
  pinMode(relay, OUTPUT);
 
  digitalWrite(relay, HIGH);
  // initialize the ethernet device
  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) {
      // clead out the input buffer:
      client.flush();   
      Serial.println(“We have a new client”);
      client.println(“Hello, client!”);
      alreadyConnected = true;
       
    }

  while (client.available()) {
  
 
 
    if (client.available() > 0) {
     
    
      // read the bytes incoming from the client:
    
      char thisChar = client.read();
       readString += thisChar;
      // echo the bytes back to the client:
      //server.write(thisChar);
 //     server.write(thisChar);
      // echo the bytes to the server as well:
     Serial.write(thisChar);
 
    }
  }
  //check here for stuff
 
  //Code to open
if (readString.toInt() == 1111) {
  server.println(“Running Opener”);
if (checkOpen() == 0) { server.println(“Closing”); }else { server.println (“Opening”);}

  digitalWrite(relay, LOW);
  delay(1000);
    digitalWrite(relay, HIGH);
  }
 
  //code to check if open/closed
  if (readString.toInt() == 5555) {if (checkOpen() == 0) {
      server.println(“Garage is OPEN”);
    } else { server.println(“Garage is CLOSED”); }}
    readString = “”;
//end check
  }
}

//checkopen
int checkOpen() {sensorValue = digitalRead(2);
if (sensorValue == 0) {return 1; }else {return 0;}}
// 0 = OPEN so we close.
//1 = CLOSE so we open.

 

欢迎与我们交流更多交流!

 

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值