// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 8, en = 7, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}
void loop() {
int time0=0;
int time1=0;
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
while(1){
time0 = millis()/2000;//隔2000ms测一次温度湿度信息
if (time0!=time1){
time1=time0;
lcd.setCursor(0, 0);
dht11.read(&temperature, &humidity, NULL);
lcd.print(temperature); lcd.print("C");
lcd.setCursor(0, 1);
lcd.print(humidity); lcd.print("H");
}
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == 'n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("");
client.println("");
client.print("Temperature:");
client.print(temperature);
client.print("C");
client.println(" ");
client.print("Humidity:");
client.print(humidity);
client.print("%");
client.println(" ");
client.print("Light intensity:");
client.print(analogRead(0));
client.println(" ");
client.println("");
break;
}
if (c == 'n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != 'r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
}
注意:在使用W5500联网时IP地址需要设置与路由器同一个网段的,不然无法进行通信。
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 8, en = 7, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}
void loop() {
int time0=0;
int time1=0;
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
while(1){
time0 = millis()/2000;//隔2000ms测一次温度湿度信息
if (time0!=time1){
time1=time0;
lcd.setCursor(0, 0);
dht11.read(&temperature, &humidity, NULL);
lcd.print(temperature); lcd.print("C");
lcd.setCursor(0, 1);
lcd.print(humidity); lcd.print("H");
}
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == 'n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("");
client.println("");
client.print("Temperature:");
client.print(temperature);
client.print("C");
client.println(" ");
client.print("Humidity:");
client.print(humidity);
client.print("%");
client.println(" ");
client.print("Light intensity:");
client.print(analogRead(0));
client.println(" ");
client.println("");
break;
}
if (c == 'n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != 'r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
}
注意:在使用W5500联网时IP地址需要设置与路由器同一个网段的,不然无法进行通信。