Skip to content

Commit 2aa605a

Browse files
committed
DHT_Simulator
1 parent da486d9 commit 2aa605a

File tree

10 files changed

+362
-6
lines changed

10 files changed

+362
-6
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
platforms:
2+
rpipico:
3+
board: rp2040:rp2040:rpipico
4+
package: rp2040:rp2040
5+
gcc:
6+
features:
7+
defines:
8+
- ARDUINO_ARCH_RP2040
9+
warnings:
10+
flags:
11+
12+
packages:
13+
rp2040:rp2040:
14+
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
15+
16+
compile:
17+
# Choosing to run compilation tests on 2 different Arduino platforms
18+
platforms:
19+
- uno
20+
# - due
21+
# - zero
22+
# - leonardo
23+
- m4
24+
- esp32
25+
- esp8266
26+
# - mega2560
27+
- rpipico
28+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# These are supported funding model platforms
2+
3+
github: RobTillaart
4+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
name: Arduino-lint
3+
4+
on: [push, pull_request]
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: arduino/arduino-lint-action@v1
11+
with:
12+
library-manager: update
13+
compliance: strict
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
name: Arduino CI
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
runTest:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: ruby/setup-ruby@v1
13+
with:
14+
ruby-version: 2.6
15+
- run: |
16+
gem install arduino_ci
17+
arduino_ci.rb
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: JSON check
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.json'
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: json-syntax-check
15+
uses: limitusus/json-syntax-check@v1
16+
with:
17+
pattern: "\\.json$"
18+

sketches/DHT_Simulator/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2014-2021 Rob Tillaart
3+
Copyright (c) 2014-2022 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

sketches/DHT_Simulator/README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11

22
[![Arduino CI](https://github.com/RobTillaart/DHT_Simulator/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
3+
[![Arduino-lint](https://github.com/RobTillaart/DHT_Simulator/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/DHT_Simulator/actions/workflows/arduino-lint.yml)
4+
[![JSON check](https://github.com/RobTillaart/DHT_Simulator/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/DHT_Simulator/actions/workflows/jsoncheck.yml)
35
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/DHT_Simulator/blob/master/LICENSE)
46
[![GitHub release](https://img.shields.io/github/release/RobTillaart/DHT_Simulator.svg?maxAge=3600)](https://github.com/RobTillaart/DHT_Simulator/releases)
57

8+
69
# DHT_Simulator
710

811
Arduino sketch to simulate a DHT22 temperature and humidity sensor.
912

13+
1014
## Description
1115

1216
The DHT22 is an often used sensor and many libraries are written for it
@@ -17,15 +21,20 @@ I wrote a simulator for the DHT sensors in 2014.
1721
The simulator can be used to test applications to that use a DHT sensor,
1822
e.g. to get high alarm temp or whatever.
1923

20-
Currently the code uses two analog ports to get a value for temperature
21-
and humidity. Just connect two potmeters to play and simulate increase and
24+
Currently the code uses two analogue ports to get a value for temperature
25+
and humidity. Just connect two potentiometers to play and simulate increase and
2226
decrease of the temperature and humidity.
2327

28+
29+
## 2022-11-01
30+
- add RP2040 to build-CI
31+
- update license
32+
2433
## 0.2.1
2534

2635
- added pin to trigger CRC errors
2736
- added temp and hum pin for input for CI
28-
- added pins for CRC error, pulselength error and timeout error
37+
- added pins for CRC error, pulse length error and timeout error
2938
(to be tested in detail)
3039

3140

@@ -39,17 +48,19 @@ decrease of the temperature and humidity.
3948

4049
The simulator is not tested extensively so please report bugs.
4150

51+
4252
## Future
4353

4454
1) use the simulator-core as a bridge to other sensors.
4555
Idea is to use the code of the simulator in combination with a
46-
Senserion or two separate sensors (DS18B20 + ? ) to provide an
56+
Sensirion or two separate sensors (DS18B20 + ? ) to provide an
4757
accurate temperature and humidity. These could then be readable
4858
with any DHT library with the performance of a DHT (~ 5ms).
4959
(when time permits)
5060

5161
2) implement a recorder mode, to be able to replay a certain
52-
behavior time after time.
62+
behaviour time after time.
63+
5364

5465
## Operation
5566

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
//
2+
// FILE: DHT_simulator.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.2.1
5+
// PURPOSE: Simulation of the DHT protocol
6+
// DATE: 2014-06-14
7+
// URL: https://github.com/RobTillaart/DHT_Simulator
8+
9+
// TODO
10+
// - split loop into some functions?
11+
12+
13+
// SET ACTUAL PINS PER PLATFORM
14+
const int dataPin = 5; // connect to MCU ( !! also connect GND !! )
15+
const int CRCPin = 6; // connect switch to simulate CRC errors.
16+
const int TimeOutPin = 7; // connect switch to simulate timeout errors.
17+
const int PulseLenPin = 8; // connect switch to simulate pulselength errors.
18+
19+
20+
#if defined(__AVR__)
21+
const int humPin = A0; // analog pins for potmeters.
22+
const int tempPin = A2;
23+
#elif defined(ESP32)
24+
const int humPin = 14;
25+
const int tempPin = 15;
26+
#elif defined(ESP8266)
27+
const int humPin = 2;
28+
const int tempPin = 3;
29+
#else // CI
30+
const int humPin = A0;
31+
const int tempPin = A2;
32+
#endif
33+
34+
35+
// DATA TO SEND
36+
byte b[5]; // actual bytes sent
37+
int humidity; // humidity * 10 - prevent float operations
38+
int temperature; // temperature * 10
39+
40+
41+
// CONFIGURE
42+
const bool randomize = true; // use random generator
43+
const bool debug = false; // test data generation
44+
45+
bool CRCerror = false; // inject CRC error
46+
bool TimeOutError = false; //
47+
bool PulseLenError = false; //.
48+
uint32_t count = 0; // count values per second generated
49+
uint32_t lastTime = 0; // keep track of timing
50+
51+
52+
/////////////////////////////////////////
53+
void setup()
54+
{
55+
Serial.begin(115200);
56+
Serial.print("Start ");
57+
Serial.println(__FILE__);
58+
59+
pinMode(dataPin, INPUT_PULLUP);
60+
pinMode(CRCPin, INPUT_PULLUP);
61+
pinMode(TimeOutPin, INPUT_PULLUP);
62+
pinMode(PulseLenPin, INPUT_PULLUP);
63+
}
64+
65+
void loop()
66+
{
67+
yield(); // keep ESP happy
68+
69+
count++;
70+
uint32_t now = millis();
71+
if (now - lastTime >= 1000)
72+
{
73+
uint32_t nps = round((1000.0 * count) / (now - lastTime));
74+
Serial.print("DATA PER SECOND: ");
75+
Serial.println(nps);
76+
lastTime = now;
77+
count = 0;
78+
}
79+
80+
if (randomize)
81+
{
82+
humidity = random(20, 1000);
83+
temperature = random(-200, 1800);
84+
}
85+
else
86+
{
87+
analogRead(humPin);
88+
humidity = analogRead(humPin);
89+
analogRead(tempPin);
90+
temperature = analogRead(tempPin) * 2 - 200;
91+
}
92+
humidity = constrain(humidity, 0, 1000);
93+
temperature = constrain(temperature, -200, 1800);
94+
95+
if (debug)
96+
{
97+
Serial.print(humidity);
98+
Serial.print("\t");
99+
Serial.print(temperature);
100+
Serial.println();
101+
}
102+
103+
104+
// READ "ERROR" PINS
105+
CRCerror = digitalRead(CRCPin) == LOW;
106+
TimeOutError = digitalRead(TimeOutPin) == LOW;
107+
PulseLenError = digitalRead(PulseLenPin) == LOW;
108+
109+
110+
// WAKE UP SIGNAL DETECTED
111+
if (digitalRead(dataPin) == LOW)
112+
{
113+
uint32_t start = micros();
114+
// wait max 1500 us until signal goes high
115+
while (digitalRead(dataPin) == LOW)
116+
{
117+
if (micros() - start > 1500)
118+
{
119+
// Serial.println("ERROR: low puise too long");
120+
return;
121+
}
122+
}
123+
if (micros() - start > 500) // serious request...
124+
{
125+
DHTsend(humidity, temperature);
126+
127+
Serial.print(humidity);
128+
Serial.print("\t");
129+
Serial.print(temperature);
130+
Serial.print("\t");
131+
for (int i = 0; i < 5; i++)
132+
{
133+
if (b[i] < 0x10) Serial.print('0');
134+
Serial.print(b[i], HEX);
135+
Serial.print(' ');
136+
}
137+
Serial.println();
138+
}
139+
else
140+
{
141+
Serial.println("ERROR: low puise too short");
142+
}
143+
}
144+
}
145+
146+
147+
void DHTsend(int H, int T)
148+
{
149+
pinMode(dataPin, OUTPUT);
150+
// SEND ACK
151+
digitalWrite(dataPin, LOW);
152+
delayMicroseconds(80); // 80 us
153+
digitalWrite(dataPin, HIGH);
154+
delayMicroseconds(80); // 80 us
155+
156+
if (TimeOutError)
157+
{
158+
delayMicroseconds(100); // inject extra 100 microseconds
159+
}
160+
161+
// PREPARE DATA
162+
b[0] = H / 256;
163+
b[1] = H & 255;
164+
165+
b[2] = 0;
166+
if (T < 0)
167+
{
168+
T = -T;
169+
b[2] = 0x80;
170+
}
171+
172+
b[2] |= T / 256;
173+
b[3] = T & 255;
174+
175+
// CRC
176+
b[4] = b[0] + b[1] + b[2] + b[3];
177+
if (CRCerror) b[4]++; // inject CRC error
178+
179+
// SEND DATA
180+
for (int i = 0; i < 5; i++)
181+
{
182+
DHTsendbyte(b[i]);
183+
}
184+
185+
// END OF TRANSMISSION SIGNAL
186+
digitalWrite(dataPin, LOW);
187+
delayMicroseconds(50); // 50 us
188+
pinMode(dataPin, INPUT_PULLUP);
189+
}
190+
191+
// timing manual tuned
192+
void DHTsendbyte(byte b)
193+
{
194+
byte mask = 128;
195+
for (int i = 0; i < 8; i++)
196+
{
197+
digitalWrite(dataPin, LOW);
198+
delayMicroseconds(45); // 50 us
199+
if (PulseLenError)
200+
{
201+
delayMicroseconds(10); // inject extra pulselength // TWEAK amount
202+
}
203+
digitalWrite(dataPin, HIGH);
204+
if (b & mask) delayMicroseconds(60); // 70 us
205+
else delayMicroseconds(24); // 26 us
206+
mask >>= 1;
207+
}
208+
}
209+
210+
// -- END OF FILE --
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
2020-12-19
3+
4+
This is a copy of the .ino file from the root folder.
5+
It is placed here so Arduino-CI will compile it.
6+
7+
TODO - check if github can work with symbolic links.
8+

0 commit comments

Comments
 (0)