|
| 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 -- |
0 commit comments