วันพุธที่ 12 มิถุนายน พ.ศ. 2556

ใช้ Arduino + W5100 เปิดปิด Relay

/*
Control 2 relays over Ethernet (Ethernet shield)
Supports DHCP
Relays are connected to pins 6 (relay 1) and 7 (relay 2). More relays can be added by putting their pin numbers to the relayPins array
Listening on TCP/IP port 22000
Control command syntax: R([aa],[bb])<CR><LF> where [aa]= relay number 00 for both, 01 or 02. [bb] is 00 (off) or 01 (on)
Response: OK<CR><LF> or ERROR<CR><LF> if a bad relay number or command is given
Multiple commands may be given. The server closes the client socket after 30s inactivity timeout
*/

#include <Ethernet.h>
#include <SPI.h>
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte ip[] = { 192, 168, 0, 199 };   //Manual setup only
byte gateway[] = { 192, 168, 0, 1 }; //Manual setup only
byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only
// if need to change the MAC address (Very Rare)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server = EthernetServer(22000); //port
////////////////////////////////////////////////////////////////////////
int relayPins[] = {6, 7};
int numRelays = sizeof(relayPins) / sizeof(int);
const bool useDhcp = true;
void setup()
{
// configure relay pins to output, OFF
for (int i = 0; i < numRelays; i++){
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], HIGH);
}
Serial.begin(9600);
//Pins 10,11,12 & 13 are used by the ethernet shield
Serial.println("Start");
Serial.print("Number of relays: " );
Serial.println(numRelays);
if (useDhcp) {
Ethernet.begin(mac); // DHCP
} else {
Ethernet.begin(mac, ip, gateway, subnet); //for manual setup
}
server.begin();
Serial.println(Ethernet.localIP());
}
void loop()
{
if (useDhcp) {
Ethernet.maintain();
}
// listen for incoming clients, and process request.
checkForClient();
}
int state = 0;
int relayNumber = 0;
int action = 0;
// reset command state machine
void Reset()
{
state = 0;
relayNumber = 0;
action = 0;
}
void checkForClient()
{
EthernetClient client = server.available();
if (client) {
int timeout = 1000;
Serial.println("Connected");
while (client.connected()) {
if (client.available()) {
char c = client.read();
// command parser is implemented as a simple state machine
switch (state){
case 0:
if (c == 'R') state++; break;
case 1:
if (c == '(') state++; else Reset(); break;
case 2:
case 3:
state++;
if ((c <= '9') && (c >= '0')){
relayNumber = relayNumber * 10 + (c - '0');
} else {
Reset();
}
break;
case 4:
if ((c == ':') || (c == ',') || (c == '.') || (c == ';')) state++; else Reset(); break;
case 5:
case 6:
state++;
if ((c <= '9') && (c >= '0')){
action = action * 10 + (c - '0');
} else {
Reset();
}
break;
case 7:
if (c == ')') state++; else Reset(); break;
case 8:
if (c == '\r') state++; else Reset(); break;
case 9:
if (c == '\n') {
if ((relayNumber > numRelays) || (action > 1)){
client.print("ERROR\r\n");
} else {
setRelay(relayNumber, action);
client.print("OK\r\n");
}
client.flush();
delay(100);
}
Reset();
break;
}
if (state > 0){
timeout = 1000;
}
} else {
timeout--;
if (timeout == 0){
Serial.println("Disconnecting - timeout");
client.stop(); // close the connection:
return;
} else {
delay(30); // 30ms * 1000 = 30s disconnect on idle timeout
}
}
}
Serial.println("Client disconnect");
}
}
void setRelay(int relayNumber, int action)
{
if (relayNumber == 0) { // 0 - all relays
for (int i = 0; i < numRelays; i++){
digitalWrite(relayPins[i], (action)?LOW:HIGH);
}
return;
}
digitalWrite(relayPins[relayNumber-1], (action)?LOW:HIGH);
}
ที่มา

ไม่มีความคิดเห็น:

แสดงความคิดเห็น