#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress serverIP(172,25,60,127); // IP Adress to our Server
int serverPort=56787;
String stringOne ;
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to
EthernetClient client;
void setup() {
// start the serial for debugging
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// give the Ethernet shield a second to initialize:
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
//Serial.println("connecting to Server ...");
// if you get a connection to the Server
if (client.connect(serverIP, serverPort)) {
Serial.println("connected");//report it to the Serial
String msg="I,8,67005AC948;";//Message to be sent
Serial.println("sending Message:"+msg);//log to serial
client.println(msg);//send the message
}
if (client.available()) {
char c = client.read();
Serial.print(c);
stringOne=stringOne+c;
if(c==';'){
Serial.println(stringOne.indexOf('CLOSE'));
if(stringOne.indexOf('CLOSE')>0){
Serial.println("shutdown the door");
}
client.stop();
Serial.println("disconnected");
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();//report it to the serial
Serial.println("disconnected");
client.stop();
for(;;)
;
}
}
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress serverIP(172,25,60,127); // IP Adress to our Server
int serverPort=56787;
String stringOne ;
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to
EthernetClient client;
String flag1 ="N";
int ledPin1=8; //设定控制LED的数字IO脚
void setup() {
// start the serial for debugging
Serial.begin(9600);
pinMode(ledPin1,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// give the Ethernet shield a second to initialize:
digitalWrite(ledPin1,HIGH);
delay(1000);
digitalWrite(ledPin1,LOW);
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
// if you get a connection to the Server
if (client.connect(serverIP, serverPort)) {
Serial.println("connected");//report it to the Serial
String msg="I,8,67005AC948;";//Message to be sent
Serial.println("sending Message:"+msg);//log to serial
client.println(msg);//send the message
}
while(true){
if (client.available()) {
char c = client.read();
Serial.print(c);
stringOne=stringOne+c;
if(c==';'){
Serial.println(stringOne.indexOf('CLOSE'));
if(stringOne.endsWith("OPEN;")){
Serial.println("shutdown the door");
digitalWrite(ledPin1,HIGH);
}
client.stop();
Serial.println("disconnected");
break;
}
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();//report it to the serial
Serial.println("disconnected");
client.stop();
for(;;)
;
}
}
#include #include #include // Enter a MAC address for your controller below.// Newer Ethernet shields have a MAC address printed on a sticker on the shieldbyte mac[] = { 0xDE, 0xAD, 0xBE, 0