原创 Arduino远程控制车库门开关

2013-6-19 16:47 1655 26 26 分类: 消费电子

“我用一个以太网插板负载到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.

 

更多信息与我们交流:

WIZnet邮箱:wiznetbj@wiznettechnology.com

WIZnet中文主页:http://www.iwiznet.cn

WIZnet中文博客:http://blog.iwiznet.cn

WIZnet企业微博:http://e.weibo.com/wiznet2012

文章评论0条评论)

登录后参与讨论
我要评论
0
26
关闭 站长推荐上一条 /2 下一条