Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**************************************************************************/
- /*!
- @file DHTxxAs1W.ino
- @version 1.0.1
- @author Jiri Jasa
- @company DESO development, s.r.o.
- http://www.deso.cz
- 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.
- 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)
- 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
- to multiple value by 10. (5,0 V => 50 %, 6,12 V => 61,2 %).
- During first run Arduino generates it's own random 1-Wire address that is then stored in EEPROM
- Make sure to define either DS18B20 or DS2438, don't use both of them at the same time!
- Prerequisites:
- https://github.com/adafruit/Adafruit_Sensor
- https://github.com/adafruit/DHT-sensor-library
- https://github.com/orgua/OneWireHub
- https://github.com/sirleech/TrueRandom
- */
- /**************************************************************************/
- #include "DHT.h"
- #include <EEPROM.h>
- #include "OneWireHub.h"
- #include "OneWireItem.h"
- #include <TrueRandom.h>
- #define LED_PIN 13 // LED osazená přímo na Mini Pro
- #define SERIAL_SPEED 115200
- // Define if to use DS2438 or DS18B20, comment out not needed type, default DS2438
- //#define USE_DS18B20
- #define USE_DS2438
- // Pin definition
- #define PIN_DHT 2 // Pin where DHTXX is connected
- #define PIN_ONE_WIRE 4 // 1-Wire pin
- // Select correct type
- #define DHT_TYPE DHT11 // DHT 11
- //#define DHT_TYPE DHT21 // DHT 21 (AM2301)
- //#define DHT_TYPE DHT22 // DHT 22 (AM2302), AM2321
- // Reading intervals [s]
- #define DHT_READING_INTERVAL 10000
- // Init Hub
- OneWireHub hub = OneWireHub(PIN_ONE_WIRE);
- // Initialize DHT sensor
- DHT dht = DHT(PIN_DHT, DHT_TYPE);
- // App will generate unique 1W address
- uint8_t addr[7] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
- #ifdef USE_DS18B20
- #include "DS18B20.h"
- DS18B20 *ds18b20Temp;
- DS18B20 *ds18b20Hum;
- #endif
- #ifdef USE_DS2438
- #include "DS2438New.h"
- DS2438New *ds2438;
- #endif
- void dumpAddress(char *prefix, OneWireItem *item, char *postfix);
- void setup() {
- Serial.begin(SERIAL_SPEED);
- // nastavení LED pinu jako výstup
- pinMode(LED_PIN, OUTPUT);
- Serial.println("OneWire-Hub DHT11 temperature / humidity sensor");
- Serial.print("FREQ CPU: ");
- Serial.println(F_CPU);
- Serial.flush();
- // 1W address storred in EEPROM
- if (EEPROM.read(0) == '#') {
- Serial.println("Reading 1W address from EEPROM");
- for (int i = 1; i < 7; i++)
- addr[i] = EEPROM.read(i);
- }
- // 1W address not set, generate random value
- else {
- Serial.println("Generating random 1W address");
- for (int i = 1; i < 7; i++) {
- addr[i] = (i != 6) ? TrueRandom.random(256): TrueRandom.random(255);
- EEPROM.write(i, addr[i]);
- }
- EEPROM.write(0, '#');
- }
- // Init devices
- #ifdef USE_DS18B20
- ds18b20Temp = new DS18B20(DS18B20::family_code, addr[1], addr[2], addr[3], addr[4], addr[5], addr[6]);
- ds18b20Hum = new DS18B20(DS18B20::family_code, addr[1], addr[2], addr[3], addr[4], addr[5], addr[6]+1);
- hub.attach(*ds18b20Temp);
- hub.attach(*ds18b20Hum);
- #endif
- #ifdef USE_DS2438
- ds2438 = new DS2438New(DS2438New::family_code, addr[1], addr[2], addr[3], addr[4], addr[5], addr[6]);
- hub.attach(*ds2438);
- #endif
- // Log addresses
- #ifdef USE_DS18B20
- dumpAddress("1-Wire DS18B20 DHT device temperature address: ", ds18b20Temp, "");
- dumpAddress("1-Wire DS18B20 DHT device humidity address: ", ds18b20Hum, "");
- #endif
- #ifdef USE_DS2438
- dumpAddress("1-Wire DS2438 DHT device address: ", ds2438, "");
- #endif
- // Init DHTXX
- dht.begin();
- }
- void loop() {
- digitalWrite(LED_PIN, HIGH);
- // Read data from sensor
- float hum = dht.readHumidity();
- float temp = dht.readTemperature();
- #ifdef USE_DS18B20
- ds18b20Temp->setTemperature(temp);
- ds18b20Hum->setTemperature(hum);
- #endif
- #ifdef USE_DS2438
- ds2438->setTemperature(temp);
- ds2438->setVADVoltage((int16_t)(hum * 10));
- // VDD and current not used at the moment
- ds2438->setVDDVoltage((int16_t)0);
- ds2438->setCurrent(0);
- #endif
- Serial.print("Temperature: "); Serial.print(temp, 2); Serial.print(" °C ");
- Serial.print("Humidity: "); Serial.print(hum, 2); Serial.println(" %");
- digitalWrite(LED_PIN, LOW);
- // Polling one wire data
- unsigned long stopPolling = millis() + DHT_READING_INTERVAL;
- while (stopPolling > millis())
- hub.poll();
- }
- // Prints the device address to console
- void dumpAddress(char *prefix, OneWireItem *item, char *postfix) {
- Serial.print(prefix);
- for (int i = 0; i < 8; i++) {
- Serial.print(item->ID[i] < 16 ? "0":"");
- Serial.print(item->ID[i], HEX);
- Serial.print((i < 7)?".":"");
- }
- Serial.println(postfix);
- }
Advertisement
Add Comment
Please, Sign In to add comment