Guest User

Untitled

a guest
Dec 3rd, 2024
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 5.21 KB | Software | 0 0
  1. /**************************************************************************/
  2. /*!
  3.     @file     DHTxxAs1W.ino
  4.     @version  1.0.1
  5.     @author   Jiri Jasa
  6.     @company  DESO development, s.r.o.
  7.               http://www.deso.cz
  8.  
  9.     This tool is able to read data from DHT11, DHT21 or DHT22 sensors, received values are then send over 1-Wire as two DS18B20 sensors or one DS2438.
  10.     If DS18B20 is used, you will see two devices with same address byte 1-6, 7th varies by 1 (humidity is the higher one) and last byte is completely different (CRC)
  11.     In case of DS2438 the temperature is send as temperature and humidity as VAD value, note that voltages can take values only up to 10 V so you will need
  12.     to multiple value by 10. (5,0 V => 50 %, 6,12 V => 61,2 %).
  13.     During first run Arduino generates it's own random 1-Wire address that is then stored in EEPROM
  14.     Make sure to define either DS18B20 or DS2438, don't use both of them at the same time!
  15.  
  16.     Prerequisites:
  17.       https://github.com/adafruit/Adafruit_Sensor
  18.       https://github.com/adafruit/DHT-sensor-library
  19.       https://github.com/orgua/OneWireHub
  20.       https://github.com/sirleech/TrueRandom
  21. */
  22. /**************************************************************************/
  23.  
  24. #include "DHT.h"
  25. #include <EEPROM.h>
  26. #include "OneWireHub.h"
  27. #include "OneWireItem.h"
  28. #include <TrueRandom.h>
  29.  
  30. #define LED_PIN 13  // LED osazená přímo na Mini Pro
  31. #define SERIAL_SPEED 115200
  32.  
  33. // Define if to use DS2438 or DS18B20, comment out not needed type, default DS2438
  34. //#define USE_DS18B20
  35. #define USE_DS2438
  36.  
  37. // Pin definition
  38. #define PIN_DHT      2  // Pin where DHTXX is connected
  39. #define PIN_ONE_WIRE 4  // 1-Wire pin
  40.  
  41. // Select correct type
  42. #define DHT_TYPE DHT11   // DHT 11
  43. //#define DHT_TYPE DHT21   // DHT 21 (AM2301)
  44. //#define DHT_TYPE DHT22   // DHT 22  (AM2302), AM2321
  45.  
  46. // Reading intervals [s]
  47. #define DHT_READING_INTERVAL 10000
  48.  
  49.  
  50. // Init Hub
  51. OneWireHub hub = OneWireHub(PIN_ONE_WIRE);
  52.  
  53. // Initialize DHT sensor
  54. DHT dht = DHT(PIN_DHT, DHT_TYPE);
  55.  
  56. // App will generate unique 1W address
  57. uint8_t addr[7] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  58.  
  59. #ifdef USE_DS18B20
  60.   #include "DS18B20.h"
  61.   DS18B20 *ds18b20Temp;
  62.   DS18B20 *ds18b20Hum;
  63. #endif
  64.  
  65. #ifdef USE_DS2438
  66.   #include "DS2438New.h"
  67.   DS2438New *ds2438;
  68. #endif
  69.  
  70. void dumpAddress(char *prefix, OneWireItem *item, char *postfix);
  71.  
  72. void setup() {
  73.     Serial.begin(SERIAL_SPEED);
  74.  
  75.     // nastavení LED pinu jako výstup
  76.     pinMode(LED_PIN, OUTPUT);
  77.  
  78.     Serial.println("OneWire-Hub DHT11 temperature / humidity sensor");
  79.     Serial.print("FREQ CPU: ");
  80.     Serial.println(F_CPU);
  81.     Serial.flush();
  82.  
  83.     // 1W address storred in EEPROM
  84.     if (EEPROM.read(0) == '#') {
  85.         Serial.println("Reading 1W address from EEPROM");
  86.         for (int i = 1; i < 7; i++)
  87.             addr[i] = EEPROM.read(i);
  88.     }
  89.     // 1W address not set, generate random value
  90.     else {
  91.         Serial.println("Generating random 1W address");
  92.         for (int i = 1; i < 7; i++) {
  93.             addr[i] = (i != 6) ? TrueRandom.random(256): TrueRandom.random(255);
  94.             EEPROM.write(i, addr[i]);
  95.         }
  96.         EEPROM.write(0, '#');
  97.     }
  98.  
  99.     // Init devices
  100.     #ifdef USE_DS18B20
  101.       ds18b20Temp = new DS18B20(DS18B20::family_code, addr[1], addr[2], addr[3], addr[4], addr[5], addr[6]);
  102.       ds18b20Hum = new DS18B20(DS18B20::family_code, addr[1], addr[2], addr[3], addr[4], addr[5], addr[6]+1);
  103.       hub.attach(*ds18b20Temp);
  104.       hub.attach(*ds18b20Hum);
  105.     #endif
  106.    
  107.     #ifdef USE_DS2438
  108.       ds2438 = new DS2438New(DS2438New::family_code, addr[1], addr[2], addr[3], addr[4], addr[5], addr[6]);
  109.       hub.attach(*ds2438);
  110.     #endif
  111.  
  112.     // Log addresses
  113.     #ifdef USE_DS18B20
  114.       dumpAddress("1-Wire DS18B20 DHT device temperature address: ", ds18b20Temp, "");
  115.       dumpAddress("1-Wire DS18B20 DHT device humidity address: ", ds18b20Hum, "");
  116.     #endif
  117.    
  118.     #ifdef USE_DS2438
  119.       dumpAddress("1-Wire DS2438 DHT device address: ", ds2438, "");
  120.     #endif
  121.  
  122.     // Init DHTXX
  123.     dht.begin();
  124. }
  125.  
  126.  
  127. void loop() {
  128.   digitalWrite(LED_PIN, HIGH);
  129.  
  130.   // Read data from sensor
  131.   float hum = dht.readHumidity();
  132.   float temp = dht.readTemperature();
  133.  
  134.   #ifdef USE_DS18B20
  135.     ds18b20Temp->setTemperature(temp);
  136.     ds18b20Hum->setTemperature(hum);
  137.   #endif
  138.   #ifdef USE_DS2438
  139.     ds2438->setTemperature(temp);
  140.     ds2438->setVADVoltage((int16_t)(hum * 10));
  141.  
  142.     // VDD and current not used at the moment
  143.     ds2438->setVDDVoltage((int16_t)0);
  144.     ds2438->setCurrent(0);
  145.   #endif
  146.  
  147.   Serial.print("Temperature: "); Serial.print(temp, 2); Serial.print(" °C ");
  148.   Serial.print("Humidity: "); Serial.print(hum, 2); Serial.println(" %");
  149.  
  150.   digitalWrite(LED_PIN, LOW);
  151.  
  152.   // Polling one wire data
  153.   unsigned long stopPolling = millis() + DHT_READING_INTERVAL;
  154.   while (stopPolling > millis())
  155.       hub.poll();
  156. }
  157.  
  158.  
  159. // Prints the device address to console
  160. void dumpAddress(char *prefix, OneWireItem *item, char *postfix) {
  161.   Serial.print(prefix);
  162.  
  163.   for (int i = 0; i < 8; i++) {
  164.       Serial.print(item->ID[i] < 16 ? "0":"");
  165.       Serial.print(item->ID[i], HEX);
  166.       Serial.print((i < 7)?".":"");
  167.   }
  168.  
  169.   Serial.println(postfix);
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment