Skip to content

Commit

Permalink
fix ESP compile failure
Browse files Browse the repository at this point in the history
added flags & error messages
added MIT license
refactor
  • Loading branch information
RobTillaart committed Jul 17, 2020
1 parent 9478203 commit d36fddb
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 61 deletions.
159 changes: 98 additions & 61 deletions sketches/DHT_Simulator/DHT_Simulator.ino
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
//
// FILE: DHT_simulator.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// PURPOSE:
// VERSION: 0.2.0
// PURPOSE: Simulation of the DHT protocol
// DATE: 2014-06-14
// URL:
//
// Released to the public domain
//
// URL: https://github.com/RobTillaart/DHT_Simulator

// TODO
// - robustness
// - timeout loops
// - simulate CRC errors
// - simulate timeout error
// - simulate pulselength error (single bit)
// - split loop into some functions?


// SET ACTUAL PINS PER PLATFORM
const int dataPin = 5; // connect to MCU ( !! also connect GND !! )

#if defined(__AVR__)
const int humPin = A0; // analog pins for potmeters.
const int tempPin = A2;
#elif defined(ESP32)
const int humPin = 14;
const int tempPin = 15;
#elif defined(ESP8266)
const int humPin = 2;
const int tempPin = 3;
#endif

const int dataPin = 5;
byte b[5];

void setup()
// DATA TO SEND
byte b[5]; // actual bytes sent
int humidity; // humidity * 10 - prevent float operations
int temperature; // temperature * 10


// CONFIGURE
const bool randomize = true; // use random generator
const bool debug = false; // test data generation
uint32_t count = 0; // count values per second generated
uint32_t lastTime = 0; // keep track of timing


/////////////////////////////////////////
void setup()
{
Serial.begin(115200);
Serial.print("Start ");
Expand All @@ -25,48 +51,84 @@ void setup()
pinMode(dataPin, INPUT_PULLUP);
}

void loop()
void loop()
{
// T = -200 .. 1800
analogRead(A2);
int T = analogRead(A2) * 2 - 200;

// H = 0 .. 1000
analogRead(A0);
int H = analogRead(A0);
if (H > 1000)
yield(); // keep ESP happy

count++;
uint32_t now = millis();
if (now - lastTime >= 1000)
{
H = 1000;
uint32_t nps = round((1000.0 * count) / (now - lastTime));
Serial.print("DATA PER SECOND: ");
Serial.println(nps);
lastTime = now;
count = 0;
}

if (randomize)
{
humidity = random(20, 1000);
temperature = random(-200, 1800);
}
else
{
analogRead(humPin);
humidity = analogRead(humPin);
analogRead(tempPin);
temperature = analogRead(tempPin) * 2 - 200;
}
humidity = constrain(humidity, 0, 1000);
temperature = constrain(temperature, -200, 1800);

if (debug)
{
Serial.print(humidity);
Serial.print("\t");
Serial.print(temperature);
Serial.println();
}
// Serial.print(H);
// Serial.print("\t");
// Serial.println(T);

// WAKE UP SIGNAL DETECTED
if (digitalRead(dataPin) == LOW)
if (digitalRead(dataPin) == LOW)
{
uint32_t start = micros();
// wait until signal goes high
// todo timeout on blocking loop
// wait max 1500 us until signal goes high
while (digitalRead(dataPin) == LOW)
{
if (micros() - start > 1500) return;
if (micros() - start > 1500)
{
// Serial.println("ERROR: low puise too long");
return;
}
}
if (micros() - start > 500) // serious request...
if (micros() - start > 500) // serious request...
{
DHTsend(H, T);
DHTsend(humidity, temperature);

Serial.print(H);
Serial.print(humidity);
Serial.print("\t");
Serial.println(T);
Serial.print(temperature);
Serial.print("\t");
for (int i = 0; i < 5; i++)
{
if (b[i] < 0x10) Serial.print('0');
Serial.print(b[i], HEX);
Serial.print(' ');
}
Serial.println();
}
else
{
Serial.println("ERROR: low puise too short");
}
}
}


void DHTsend(int H, int T)
{
pinMode(dataPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// SEND ACK
digitalWrite(dataPin, LOW);
delayMicroseconds(80); // 80 us
Expand All @@ -78,7 +140,7 @@ void DHTsend(int H, int T)
b[1] = H & 255;

b[2] = 0;
if (T < 0)
if (T < 0)
{
T = -T;
b[2] = 0x80;
Expand All @@ -99,21 +161,13 @@ void DHTsend(int H, int T)
digitalWrite(dataPin, LOW);
delayMicroseconds(50); // 50 us
pinMode(dataPin, INPUT_PULLUP);

// DEBUG
// for (int i = 0; i < 5; i++)
// {
// Serial.print(b[i]);
// Serial.print(" ");
// }
// Serial.println();
}

// timing manual tuned
void DHTsendbyte(byte b)
{
byte mask = 128;
for(int i = 0; i < 8; i++)
for (int i = 0; i < 8; i++)
{
digitalWrite(dataPin, LOW);
delayMicroseconds(45); // 50 us
Expand All @@ -124,21 +178,4 @@ void DHTsendbyte(byte b)
}
}



















// -- END OF FILE --
21 changes: 21 additions & 0 deletions sketches/DHT_Simulator/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2014-2020 Rob Tillaart

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
42 changes: 42 additions & 0 deletions sketches/DHT_Simulator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# DHT_Simulator

Arduino sketch to simulate a DHT22

## Description

The DHT22 is an often used sensor and many libraries are written for it
including my own DHTNEW library. To better understand the protocol and
to be able to debug my library I wrote a simulator for the DHT sensors
in 2014.

The simulator can be used to test applications to that use a DHT sensor,
e.g. to get high alarm temp or whatever.

Currently the code uses two analog ports to get some value for temperature
and humidity. Just connect two potmeters to play and simulate increase and
decrease of the temperature and humidity.

## 0.2.0 version

- added flag for random generation of data
- added flag for debug
- explicit support ESP32 & ESP8266
- added counter # samples generated per second ==> so one sees it still runs
- added error and debug messages // some commented

The simulator is not tested extensively so please report bugs.

## Future

Idea is to use the code of the simulator in combination with a
Senserion or two separate sensors (DS18B20 + ? ) to provide an
accurate temperature and humidity. These could then be readable
with any DHT library with the performance of a DHT (~ 5ms).
(when time permits)

## Operation

- adjust the pins in the head of the program to meet your setup
- compile, connect, run

And have fun.

0 comments on commit d36fddb

Please sign in to comment.