Hardware Reference
In-Depth Information
Listing 10-1 continued
61 if (val < MinHumidity)
62 {
63 // Below minimum humidity. Warn!
64 Serial.println("Plant is thirsty!");
65 sendEmail();
66 firstEmail = false;
67 }
68 else
69 {
70 // All OK
71 Serial.println("Humidity OK");
72 firstEmail = true;
73 }
74
75 // Wait for half an hour
76 delay(1800000);
77 }
78
79
80 int getDht11Humidity()
81 {
82 byte data[6] = {0};
83
84 // Set up variables
85 byte mask = 128;
86 byte idx = 0;
87
88 // Request a sample from the DHT11
89 pinMode(DHTPin, OUTPUT);
90 digitalWrite(DHTPin, LOW);
91 delay(20);
92 digitalWrite(DHTPin, HIGH);
93 delayMicroseconds(40);
94 pinMode(DHTPin, INPUT);
95
96 // Will we get an ACK?
97 unsigned int loopCnt = 255;
98 while(digitalRead(DHTPin) == LOW)
99 {
100 if (--loopCnt == 0) return NAN;
101 }
102
103 loopCnt = 255;
104 while(digitalRead(DHTPin) == HIGH)
105 {
106 if (--loopCnt == 0) return NAN;
107 }
108
109 // Acknowledged, read in 40 bits
110 for (unsigned int i = 0; i < 40; i++)
Search WWH ::




Custom Search