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
67129void 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
113167void 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 --
0 commit comments