Skip to content

Commit d36fddb

Browse files
committed
fix ESP compile failure
added flags & error messages added MIT license refactor
1 parent 9478203 commit d36fddb

File tree

3 files changed

+161
-61
lines changed

3 files changed

+161
-61
lines changed
Lines changed: 98 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,48 @@
11
//
22
// FILE: DHT_simulator.ino
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.01
5-
// PURPOSE:
4+
// VERSION: 0.2.0
5+
// PURPOSE: Simulation of the DHT protocol
66
// DATE: 2014-06-14
7-
// URL:
8-
//
9-
// Released to the public domain
10-
//
7+
// URL: https://github.com/RobTillaart/DHT_Simulator
118

129
// TODO
13-
// - robustness
14-
// - timeout loops
10+
// - simulate CRC errors
11+
// - simulate timeout error
12+
// - simulate pulselength error (single bit)
13+
// - split loop into some functions?
14+
15+
16+
// SET ACTUAL PINS PER PLATFORM
17+
const int dataPin = 5; // connect to MCU ( !! also connect GND !! )
18+
19+
#if defined(__AVR__)
20+
const int humPin = A0; // analog pins for potmeters.
21+
const int tempPin = A2;
22+
#elif defined(ESP32)
23+
const int humPin = 14;
24+
const int tempPin = 15;
25+
#elif defined(ESP8266)
26+
const int humPin = 2;
27+
const int tempPin = 3;
28+
#endif
1529

16-
const int dataPin = 5;
17-
byte b[5];
1830

19-
void setup()
31+
// DATA TO SEND
32+
byte b[5]; // actual bytes sent
33+
int humidity; // humidity * 10 - prevent float operations
34+
int temperature; // temperature * 10
35+
36+
37+
// CONFIGURE
38+
const bool randomize = true; // use random generator
39+
const bool debug = false; // test data generation
40+
uint32_t count = 0; // count values per second generated
41+
uint32_t lastTime = 0; // keep track of timing
42+
43+
44+
/////////////////////////////////////////
45+
void setup()
2046
{
2147
Serial.begin(115200);
2248
Serial.print("Start ");
@@ -25,48 +51,84 @@ void setup()
2551
pinMode(dataPin, INPUT_PULLUP);
2652
}
2753

28-
void loop()
54+
void loop()
2955
{
30-
// T = -200 .. 1800
31-
analogRead(A2);
32-
int T = analogRead(A2) * 2 - 200;
33-
34-
// H = 0 .. 1000
35-
analogRead(A0);
36-
int H = analogRead(A0);
37-
if (H > 1000)
56+
yield(); // keep ESP happy
57+
58+
count++;
59+
uint32_t now = millis();
60+
if (now - lastTime >= 1000)
3861
{
39-
H = 1000;
62+
uint32_t nps = round((1000.0 * count) / (now - lastTime));
63+
Serial.print("DATA PER SECOND: ");
64+
Serial.println(nps);
65+
lastTime = now;
66+
count = 0;
67+
}
68+
69+
if (randomize)
70+
{
71+
humidity = random(20, 1000);
72+
temperature = random(-200, 1800);
73+
}
74+
else
75+
{
76+
analogRead(humPin);
77+
humidity = analogRead(humPin);
78+
analogRead(tempPin);
79+
temperature = analogRead(tempPin) * 2 - 200;
80+
}
81+
humidity = constrain(humidity, 0, 1000);
82+
temperature = constrain(temperature, -200, 1800);
83+
84+
if (debug)
85+
{
86+
Serial.print(humidity);
87+
Serial.print("\t");
88+
Serial.print(temperature);
89+
Serial.println();
4090
}
41-
// Serial.print(H);
42-
// Serial.print("\t");
43-
// Serial.println(T);
4491

4592
// WAKE UP SIGNAL DETECTED
46-
if (digitalRead(dataPin) == LOW)
93+
if (digitalRead(dataPin) == LOW)
4794
{
4895
uint32_t start = micros();
49-
// wait until signal goes high
50-
// todo timeout on blocking loop
96+
// wait max 1500 us until signal goes high
5197
while (digitalRead(dataPin) == LOW)
5298
{
53-
if (micros() - start > 1500) return;
99+
if (micros() - start > 1500)
100+
{
101+
// Serial.println("ERROR: low puise too long");
102+
return;
103+
}
54104
}
55-
if (micros() - start > 500) // serious request...
105+
if (micros() - start > 500) // serious request...
56106
{
57-
DHTsend(H, T);
107+
DHTsend(humidity, temperature);
58108

59-
Serial.print(H);
109+
Serial.print(humidity);
60110
Serial.print("\t");
61-
Serial.println(T);
111+
Serial.print(temperature);
112+
Serial.print("\t");
113+
for (int i = 0; i < 5; i++)
114+
{
115+
if (b[i] < 0x10) Serial.print('0');
116+
Serial.print(b[i], HEX);
117+
Serial.print(' ');
118+
}
119+
Serial.println();
120+
}
121+
else
122+
{
123+
Serial.println("ERROR: low puise too short");
62124
}
63125
}
64126
}
65127

66128

67129
void DHTsend(int H, int T)
68130
{
69-
pinMode(dataPin, OUTPUT);
131+
pinMode(dataPin, OUTPUT);
70132
// SEND ACK
71133
digitalWrite(dataPin, LOW);
72134
delayMicroseconds(80); // 80 us
@@ -78,7 +140,7 @@ void DHTsend(int H, int T)
78140
b[1] = H & 255;
79141

80142
b[2] = 0;
81-
if (T < 0)
143+
if (T < 0)
82144
{
83145
T = -T;
84146
b[2] = 0x80;
@@ -99,21 +161,13 @@ void DHTsend(int H, int T)
99161
digitalWrite(dataPin, LOW);
100162
delayMicroseconds(50); // 50 us
101163
pinMode(dataPin, INPUT_PULLUP);
102-
103-
// DEBUG
104-
// for (int i = 0; i < 5; i++)
105-
// {
106-
// Serial.print(b[i]);
107-
// Serial.print(" ");
108-
// }
109-
// Serial.println();
110164
}
111165

112166
// timing manual tuned
113167
void DHTsendbyte(byte b)
114168
{
115169
byte mask = 128;
116-
for(int i = 0; i < 8; i++)
170+
for (int i = 0; i < 8; i++)
117171
{
118172
digitalWrite(dataPin, LOW);
119173
delayMicroseconds(45); // 50 us
@@ -124,21 +178,4 @@ void DHTsendbyte(byte b)
124178
}
125179
}
126180

127-
128-
129-
130-
131-
132-
133-
134-
135-
136-
137-
138-
139-
140-
141-
142-
143-
144-
181+
// -- END OF FILE --

sketches/DHT_Simulator/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2014-2020 Rob Tillaart
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

sketches/DHT_Simulator/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# DHT_Simulator
2+
3+
Arduino sketch to simulate a DHT22
4+
5+
## Description
6+
7+
The DHT22 is an often used sensor and many libraries are written for it
8+
including my own DHTNEW library. To better understand the protocol and
9+
to be able to debug my library I wrote a simulator for the DHT sensors
10+
in 2014.
11+
12+
The simulator can be used to test applications to that use a DHT sensor,
13+
e.g. to get high alarm temp or whatever.
14+
15+
Currently the code uses two analog ports to get some value for temperature
16+
and humidity. Just connect two potmeters to play and simulate increase and
17+
decrease of the temperature and humidity.
18+
19+
## 0.2.0 version
20+
21+
- added flag for random generation of data
22+
- added flag for debug
23+
- explicit support ESP32 & ESP8266
24+
- added counter # samples generated per second ==> so one sees it still runs
25+
- added error and debug messages // some commented
26+
27+
The simulator is not tested extensively so please report bugs.
28+
29+
## Future
30+
31+
Idea is to use the code of the simulator in combination with a
32+
Senserion or two separate sensors (DS18B20 + ? ) to provide an
33+
accurate temperature and humidity. These could then be readable
34+
with any DHT library with the performance of a DHT (~ 5ms).
35+
(when time permits)
36+
37+
## Operation
38+
39+
- adjust the pins in the head of the program to meet your setup
40+
- compile, connect, run
41+
42+
And have fun.

0 commit comments

Comments
 (0)