From 13cd353acf43fa4e278813c46b8e72108f311055 Mon Sep 17 00:00:00 2001 From: aattww <52109748+aattww@users.noreply.github.com> Date: Sun, 29 Dec 2019 23:45:16 +0200 Subject: [PATCH] Fix NTCSensor enable pin bug (#1) * Fix NTC enable pin bug Do not change any pinmodes or values if enablePin is not in use when reading temperature. * Add version history * Update README.md --- README.md | 11 +++++++++ SensorsGateway/SensorsGateway.ino | 4 ++-- SensorsPulse/SensorsPulse.ino | 4 ++-- libraries/NTCSensor/NTCSensor.cpp | 39 +++++++++++++++++++++---------- libraries/NTCSensor/NTCSensor.h | 27 +++++++++++++++------ 5 files changed, 62 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index d6b08e4..6d20ee9 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ Skip directly to [Instructions](#instructions), although I strongly recommend re * [Battery](#battery-3) * [Pulse / Pulse with Kamstrup Multical](#pulse--pulse-with-kamstrup-multical-1) * [Instructions](#instructions) +* [Version history](#version-history) # External requirements @@ -417,3 +418,13 @@ Place your gateway to a central location and connect it to a Modbus capable netw Distribute other nodes as needed, selecting first their addresses with jumper headers and then connecting external power or batteries. Use the button on the nodes to force a transmit with full power - normally nodes adjust their transmit power automatically to the lowest possible level. One blink of the onboard LED indicates a successful transmit, two blinks a failed one. LED blinks only when forcing a transmit with the button. You can use the provided Python script [*read_modbus.py*](read_modbus.py) to read data from the gateway for debugging purposes. Start logging measurements to a MySQL database ([*save_modbus_to_db.py*](save_modbus_to_db.py) provides a starting point for this), for example, and graph it with [Grafana](https://grafana.com/), or connect the gateway to a home automation hub and monitor measurements that way. + +# Version history + +## v1.0.1 (2019-12-29) + +* Fixed a bug in NTCSensor library where enable pin was controlled even though it was not in actual use, possibly interfering with serial communication. + +## v1.0.0 (2019-12-26) + +Initial public release. diff --git a/SensorsGateway/SensorsGateway.ino b/SensorsGateway/SensorsGateway.ino index 76e7b55..c3bfc1d 100644 --- a/SensorsGateway/SensorsGateway.ino +++ b/SensorsGateway/SensorsGateway.ino @@ -61,7 +61,7 @@ #define MAJOR_VERSION 1 -#define MINOR_VERSION 1 +#define MINOR_VERSION 2 #include #include @@ -120,7 +120,7 @@ RHReliableDatagram radioManager(rf95Driver); SimpleModbusAsync modbus; // NTC instance -NTCSensor sensorNTC(0, P3_PIN); +NTCSensor sensorNTC(NTC_NO_ENABLE_PIN, P3_PIN); // Memory handler instance SensorsMemoryHandler memoryHandler(SRAM_NSS); diff --git a/SensorsPulse/SensorsPulse.ino b/SensorsPulse/SensorsPulse.ino index 615ff4f..6af2d7a 100644 --- a/SensorsPulse/SensorsPulse.ino +++ b/SensorsPulse/SensorsPulse.ino @@ -85,7 +85,7 @@ */ -#define VERSION 1 +#define VERSION 2 #if defined NODE_TYPE_MULTICAL #include @@ -144,7 +144,7 @@ RHReliableDatagram radioManager(rf95Driver); SimpleModbusAsync modbus; #endif -NTCSensor sensorNTC(0, P3_PIN); +NTCSensor sensorNTC(NTC_NO_ENABLE_PIN, P3_PIN); // Payload buffer uint8_t payloadBuffer[PAYLOAD_LEN]; diff --git a/libraries/NTCSensor/NTCSensor.cpp b/libraries/NTCSensor/NTCSensor.cpp index c21b2f0..2065183 100644 --- a/libraries/NTCSensor/NTCSensor.cpp +++ b/libraries/NTCSensor/NTCSensor.cpp @@ -33,27 +33,38 @@ * * Parameters: * enablePin - Enable voltage divider and power on thermistor - * 255 = Not in use, thermistor is always pulled up by external resistor to Vcc - * !=255 = Pin connected to high side of divider + * NTC_NO_ENABLE_PIN = Not in use, thermistor is always pulled up by external resistor to Vcc + * ARDUINO PIN = Pin connected to high side of divider * sensorPin - Thermistor * - * Schematic (enablePin != 255): + * Schematic (enablePin != NTC_NO_ENABLE_PIN): * [GND]---[NTC]---|---[SERIES_RESISTOR]---[enablePin] * | * [sensorPin] * - * Schematic (enablePin == 255): + * Schematic (enablePin == NTC_NO_ENABLE_PIN): * [GND]---[NTC]---|---[SERIES_RESISTOR]---[Vcc] * | * [sensorPin] */ +/* + * Version history + * --------------- + * + * 1.1 2019-12-29 (CURRENT) + * - Fixed a bug when enablePin was not in use but placeholder pin was still controlled. + * + * 1.0 2019-12-26 + * Initial public release + */ + #include "NTCSensor.h" /* * Creates a new instance. * - * enablePin: voltage divider enable pin (set to 255 if not in use) + * enablePin: voltage divider enable pin (set to NTC_NO_ENABLE_PIN if not in use) * sensorPin: thermistor pin * * returns: no @@ -83,7 +94,7 @@ bool NTCSensor::init() { // Go through different scenarios to determine if thermistor is connected properly // If thermistor does not have a separate enable pin (thermistor should always be powered) - if (_enablePin == 255) { + if (_enablePin == NTC_NO_ENABLE_PIN) { // Enable internal pullup resistor so that pin is not floating if there is no thermistor pinMode(_sensorPin, INPUT_PULLUP); delay(50); @@ -136,7 +147,7 @@ bool NTCSensor::init() { } /* - * Reads current temperature. + * Reads current temperature in Celsius. * * parameters: no * @@ -153,9 +164,11 @@ int16_t NTCSensor::readTemperature() { analogReference(DEFAULT); analogRead(_sensorPin); - // Turn on analog voltage divider - digitalWrite(_enablePin, HIGH); - delay(50); + // Turn on analog voltage divider if it is in use + if (_enablePin != NTC_NO_ENABLE_PIN) { + digitalWrite(_enablePin, HIGH); + delay(50); + } float average = 0.0; @@ -165,8 +178,10 @@ int16_t NTCSensor::readTemperature() { delay(10); } - // Turn off analog voltage - digitalWrite(_enablePin, LOW); + // Turn off analog voltage if in use + if (_enablePin != NTC_NO_ENABLE_PIN) { + digitalWrite(_enablePin, LOW); + } // Calculate average average /= 5.0; diff --git a/libraries/NTCSensor/NTCSensor.h b/libraries/NTCSensor/NTCSensor.h index ca275ad..fc79405 100644 --- a/libraries/NTCSensor/NTCSensor.h +++ b/libraries/NTCSensor/NTCSensor.h @@ -33,26 +33,39 @@ * * Parameters: * enablePin - Enable voltage divider and power on thermistor - * 255 = Not in use, thermistor is always pulled up by external resistor to Vcc - * !=255 = Pin connected to high side of divider + * NTC_NO_ENABLE_PIN = Not in use, thermistor is always pulled up by external resistor to Vcc + * ARDUINO PIN = Pin connected to high side of divider * sensorPin - Thermistor * - * Schematic (enablePin != 255): + * Schematic (enablePin != NTC_NO_ENABLE_PIN): * [GND]---[NTC]---|---[SERIES_RESISTOR]---[enablePin] * | * [sensorPin] * - * Schematic (enablePin == 255): + * Schematic (enablePin == NTC_NO_ENABLE_PIN): * [GND]---[NTC]---|---[SERIES_RESISTOR]---[Vcc] * | * [sensorPin] */ +/* + * Version history + * --------------- + * + * 1.1 2019-12-29 (CURRENT) + * - Fixed a bug when enablePin was not in use but placeholder pin was still controlled. + * + * 1.0 2019-12-26 + * Initial public release + */ + #ifndef NTCSENSOR_H #define NTCSENSOR_H #include "Arduino.h" +#define NTC_NO_ENABLE_PIN 255 + // Resistance at 25 C #define NOMINAL_RESISTANCE 10000.0 @@ -69,7 +82,7 @@ class NTCSensor { private: - uint8_t _enablePin; // Voltage divider enable pin (255 if not in use) + uint8_t _enablePin; // Voltage divider enable pin (NTC_NO_ENABLE_PIN if not in use) uint8_t _sensorPin; // Thermistor pin bool _initialised; // Sensor has been initialized @@ -78,7 +91,7 @@ class NTCSensor { /* * Creates a new instance. * - * enablePin: voltage divider enable pin (set to 255 if not in use) + * enablePin: voltage divider enable pin (set to NTC_NO_ENABLE_PIN if not in use) * sensorPin: thermistor pin * * returns: no @@ -99,7 +112,7 @@ class NTCSensor { bool init(); /* - * Reads current temperature. + * Reads current temperature in Celsius. * * parameters: no *