/** * This example does NOT use a VOLTAGE DIVIDER. * Please use only if Vin < 5v!!! * To test, at 7/7/17 */ /** * The MySensors Arduino library handles the wireless radio link and protocol * between your home built sensors/actuators and HA controller of choice. * The sensors forms a self healing radio network with optional repeaters. Each * repeater and gateway builds a routing tables in EEPROM which keeps track of the * network topology allowing messages to be routed to nodes. * * Created by Henrik Ekblad * Copyright (C) 2013-2015 Sensnology AB * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors * * Documentation: http://www.mysensors.org * Support Forum: http://forum.mysensors.org * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * version 2 as published by the Free Software Foundation. * ******************************* * * REVISION HISTORY * Version 1.0: Henrik EKblad * Version 1.1 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz) * * DESCRIPTION * This sketch provides an example of how to implement a humidity/temperature * sensor using a DHT11/DHT-22. * * For more information, please visit: * http://www.mysensors.org/build/humidity * */ // Enable debug prints #define MY_DEBUG #define MY_DEBUG_VERBOSE_SIGNING //!< Enable signing related debug prints to serial monitor // Enable and select radio type attached #define MY_RADIO_NRF24 // Enable security! #define MY_SIGNING_SOFT #define MY_SIGNING_SOFT_RANDOMSEED_PIN 7 #define MY_SIGNING_REQUEST_SIGNATURES #include #include #include // Set this to the pin you connected the DHT's data pin to #define DHT_DATA_PIN 2 // Set this offset if the sensor has a permanent small offset to the real temperatures #define SENSOR_TEMP_OFFSET 0 #define ANALOG_PIN 0 // Sleep time between sensor updates (in milliseconds) // Must be >1000ms for DHT22 and >2000ms for DHT11 static const uint64_t UPDATE_INTERVAL = 600000; // seconds * 1000 // Force sending an update of the temperature after n sensor reads, so a controller showing the // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that // the value didn't change since; // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms] static const uint8_t FORCE_UPDATE_N_READS = 10; #define CHILD_ID_HUM 0 #define CHILD_ID_TEMP 1 #define CHILD_ID_VOLTAGE 2 float lastTemp; float lastHum; uint8_t nNoUpdatesTemp; uint8_t nNoUpdatesHum; bool metric = true; int oldBatteryPcnt = 0; int old_battery_voltage = 0; float battVolts; // made global for wider avaliblity throughout a sketch if needed, example a low voltage alarm, etc float known_battery_start_voltage = 2.73; // the start of battery, to get the percentage MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgVoltage(CHILD_ID_VOLTAGE, V_VOLTAGE); DHT dht; void presentation() { // Send the sketch version information to the gateway sendSketchInfo("DHT22Battery", "2.0"); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_HUM, S_HUM); present(CHILD_ID_TEMP, S_TEMP); present(CHILD_ID_VOLTAGE, S_MULTIMETER); metric = getControllerConfig().isMetric; } void setup() { Serial.println("Starting sketch!"); setupDHT22(); } void setupDHT22() { dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) { Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!"); } // Sleep for the time of the minimum sampling period to give the sensor time to power up // (otherwise, timeout errors might occure for the first reading) sleep(dht.getMinimumSamplingPeriod()); } void getAndSendBatteryData() { //battVolts=getBandgap(); //Determins what actual Vcc is, (X 100), based on known bandgap voltage int int_battery = getBandgap(); battVolts = float(int_battery); battVolts = battVolts/100; //int batteryPcnt = known_battery_start_voltage / 1; // to do //int batteryPcnt = 100; // battery % === known voltage sta a 100 COME attuale voltaggio sta a X int batteryPcnt = (battVolts * 100) / known_battery_start_voltage; Serial.print("Battery Vcc volts = "); Serial.println(battVolts); Serial.print("Battery % = "); Serial.println(batteryPcnt); Serial.println(); if (oldBatteryPcnt != batteryPcnt) { send(msgVoltage.set(battVolts, 2)); sendBatteryLevel(batteryPcnt); oldBatteryPcnt = batteryPcnt; } } // Returns actual value of Vcc (x 100) int getBandgap(void) { #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) // For mega boards const long InternalReferenceVoltage = 1115L; // Adjust this value to your boards specific internal BG voltage x1000 // REFS1 REFS0 --> 0 1, AVcc internal ref. -Selects AVcc reference // MUX4 MUX3 MUX2 MUX1 MUX0 --> 11110 1.1V (VBG) -Selects channel 30, bandgap voltage, to measure ADMUX = (0< 0 1, AVcc internal ref. -Selects AVcc external reference // MUX3 MUX2 MUX1 MUX0 --> 1110 1.1V (VBG) -Selects channel 14, bandgap voltage, to measure ADMUX = (0<