Tranzystor.pl – Vortal Elektroniczny

Arduino – DHT11 / DTH22 termometr LAN v2

Tym razem prezentujemy prosty sposób na monitorowanie temeperatuty na dwóch czujnikach po sieci LAN. Wykorzystujemy standardowo Arduino Uno oraz Ethernet Shield. Wszystko jest banalnie proste gdyż oprócz arduino potrzebujemy jedynie czujników temperatury DHT11 lub DHT22. Ja w tym artykule wykorzystuje moduł czujnika DTH22 (AM2302) gdyż tylko ten mam pod ręką. Jeżeli wykorzystywać będziesz sam czujnik, pamiętaj o wpięciu miedzy zasilanie a pin DAT rezystora 10k.

Do wykonania będą nam potrzebne następujące elementy:
– Arduino np. Arduino UNO,
– Moduł sensora temperatury DHT22,
– Arduino Ethernet Shield
– Kable do połączenia wszystkiego.
– Rezystor 10k opcjonalne patrz opis

Schemat połączeniowy.

DTH22 termometr LAN v2

Kod programu:

// Example  DHT humidity/temperature sensors
// Written by swistak
// www.tranzystor.pl
// www.shellmix.pl

#include „DHT.h”

#define DHT1PIN 2
#define DHT2PIN 3

#define DHT1TYPE DHT22   // DHT 22
#define DHT2TYPE DHT22

DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x24, 0x14 };
IPAddress ip(10,10,10,19);
EthernetServer server(80);
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
dht1.begin();
dht2.begin();

Ethernet.begin(mac, ip);
server.begin();
Serial.print(„server is at „);
Serial.println(Ethernet.localIP());
}

void loop() {

float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();

if (isnan(t1) || isnan(h1)) {
Serial.println(„Failed to read from DHT”);
} else {
Serial.print(„Humidity zew: „);
Serial.print(h1);
Serial.print(” %\t”);
Serial.print(„Temperature zew: „);
Serial.print(t1);
Serial.println(” *C”);
}
if (isnan(t2) || isnan(h2)) {
Serial.println(„Failed to read from DHT”);
} else {
Serial.print(„Humidity wew: „);
Serial.print(h2);
Serial.print(” %\t”);
Serial.print(„Temperature wew: „);
Serial.print(t2);
Serial.println(” *C”);
}
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println(„new client”);
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);

if (c == '\n’ && currentLineIsBlank) {
// send a standard http response header
client.println(„HTTP/1.1 200 OK”);
client.println(„Content-Type: text/html”);
client.println(„Connection: close”);
client.println(„Refresh: 5”);
client.println();
client.println(„<!DOCTYPE HTML>”);
client.println(„<html>”);
client.println(„<center><H2>”);
client.print(„The temperature inside and outside”);

// temp sensor 1
client.println(„<H2>”);
client.print(„Humidity out: „);
client.println(„</H2>”);
client.println(„<p />”);
client.println(„<H1>”);
client.print(h1);
client.print(” %\t”);
client.println(„</H1>”);
client.println(„<p />”);
client.println(„<H2>”);
client.print(„Temperature out: „);
client.println(„</H2>”);
client.println(„<H1>”);
//client.print(t*1.8+32); //temp fahrenheit
client.print(t1); //temp Celsjusz
client.println(” &#176;”);
//client.println(„F”); //temp fahrenheit
client.println(„C”); //temp celsjusz
client.println(„</H1>”);
// temp sensor 2
client.println(„<H2>”);
client.print(„Humidity in: „);
client.println(„</H2>”);
client.println(„<p />”);
client.println(„<H1>”);
client.print(h2);
client.print(” %\t”);
client.println(„</H1>”);
client.println(„<p />”);
client.println(„<H2>”);
client.print(„Temperature in: „);
client.println(„</H2>”);
client.println(„<H1>”);
//client.print(t*1.8+32); //temp fahrenheit
client.print(t2); //temp Celsjusz
client.println(” &#176;”);
//client.println(„F”); //temp fahrenheit
client.println(„C”); //temp celsjusz
client.println(„</H1>”);
client.print(„Copyright &copy by <a href=’https://www.tranzystor.pl’>swistak</a>”);
client.println(„</center>”);
client.println(„</html>”);
break;
}
if (c == '\n’) {
// you’re starting a new line
currentLineIsBlank = true;
}
else if (c != '\r’) {

currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
Serial.println(„client disonnected”);
}
}

5/5 - (1 ocena/y)
Exit mobile version