Skip to content

Commit 4f46069

Browse files
committed
+ initial version (not working yet)
1 parent 4571399 commit 4f46069

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
//
2+
// FILE: DHT_simulator.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.00
5+
// PURPOSE:
6+
// DATE: 2014-06-14
7+
// URL:
8+
//
9+
// Released to the public domain
10+
//
11+
12+
// TODO
13+
// - robustness
14+
// - timeout loops
15+
// - get timing accurate
16+
17+
const int dataPin = 5;
18+
byte b[5];
19+
20+
void setup()
21+
{
22+
Serial.begin(115200);
23+
Serial.println("Start ");
24+
25+
pinMode(dataPin, INPUT_PULLUP);
26+
}
27+
28+
void loop()
29+
{
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)
38+
{
39+
H = 1000;
40+
}
41+
// Serial.print(H);
42+
// Serial.print("\t");
43+
// Serial.println(T);
44+
45+
// WAKE UP SIGNAL DETECTED
46+
if (digitalRead(dataPin) == LOW)
47+
{
48+
uint32_t start = micros();
49+
// wait until signal goes high // todo timeout onblocking loop?
50+
while (digitalRead(dataPin) == LOW);
51+
if (micros() - start > 500) // serious request...
52+
{
53+
54+
pinMode(dataPin, OUTPUT);
55+
// send ACK
56+
digitalWrite(dataPin, LOW);
57+
delayMicroseconds(80); // overhead digitalwrite (2..4us)
58+
digitalWrite(dataPin, HIGH);
59+
delayMicroseconds(80);
60+
61+
DHTsend(H, T);
62+
63+
digitalWrite(dataPin, LOW);
64+
delayMicroseconds(50);
65+
pinMode(dataPin, INPUT_PULLUP);
66+
67+
// for (int i = 0; i < 5; i++)
68+
// {
69+
// Serial.print(b[i]);
70+
// Serial.print(" ");
71+
// }
72+
// Serial.println();
73+
74+
Serial.print(H);
75+
Serial.print("\t");
76+
Serial.println(T);
77+
}
78+
}
79+
}
80+
81+
void DHTsend(int H, int T)
82+
{
83+
// prepare data
84+
b[0] = H / 256;
85+
b[1] = H & 255;
86+
87+
uint16_t t;
88+
if (T >= 0)
89+
{
90+
t = T;
91+
b[2] = 0;
92+
}
93+
else
94+
{
95+
t = -T;
96+
b[2] = 0x80;
97+
}
98+
Serial.println(t);
99+
100+
b[2] |= (t / 256);
101+
b[3] = (t & 255);
102+
103+
b[4] = b[0] + b[1] + b[2] + b[3]; // CRC
104+
105+
for (int i = 0; i < 5; i++)
106+
{
107+
DHTsendbyte(b[i]);
108+
}
109+
}
110+
111+
void DHTsendbyte(byte b)
112+
{
113+
byte mask = 128;
114+
for(int i = 0; i < 8; i++)
115+
{
116+
digitalWrite(dataPin, LOW);
117+
delayMicroseconds(45); // overhead digitalwrite (2..4us)
118+
digitalWrite(dataPin, HIGH);
119+
120+
if (b & mask) delayMicroseconds(60); // idem
121+
else delayMicroseconds(24); // idem
122+
123+
mask >>= 1;
124+
}
125+
}
126+
127+
128+
129+
130+
131+
132+
133+
134+
135+
136+
137+
138+
139+
140+
141+

0 commit comments

Comments
 (0)