diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 7d056e9e..1fe33040 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -115,10 +115,6 @@ #define DALLAS_PIN 13 #endif -#ifndef DALLAS_PULLUP -#define DALLAS_PULLUP 1 -#endif - #define DALLAS_RESOLUTION 9 // Not used atm //-------------------------------------------------------------------------------- diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index ed9c8825..2f7c7d20 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -220,7 +220,6 @@ void _sensorInit() { { DallasSensor * sensor = new DallasSensor(); sensor->setGPIO(DALLAS_PIN); - sensor->setPullUp(DALLAS_PULLUP); _sensorRegister(sensor); } #endif @@ -320,46 +319,14 @@ void _sensorInit() { } -// ----------------------------------------------------------------------------- -// Values -// ----------------------------------------------------------------------------- - -unsigned char sensorCount() { - return _sensors.size(); -} - -unsigned char magnitudeCount() { - return _magnitudes.size(); -} - -String magnitudeName(unsigned char index) { - if (index < _magnitudes.size()) { - sensor_magnitude_t magnitude = _magnitudes[index]; - return magnitude.sensor->slot(magnitude.local); - } - return String(); -} +void _magnitudesInit() { -unsigned char magnitudeType(unsigned char index) { - if (index < _magnitudes.size()) { - return int(_magnitudes[index].type); - } - return MAGNITUDE_NONE; -} - -// ----------------------------------------------------------------------------- - -void sensorSetup() { - - // Load sensors - _sensorInit(); - - // Load magnitudes for (unsigned char i=0; i<_sensors.size(); i++) { BaseSensor * sensor = _sensors[i]; + DEBUG_MSG("[SENSOR] %s\n", sensor->name().c_str()); - if (sensor->count() == 0) DEBUG_MSG("[SENSOR] -> NOTHING FOUND\n"); + if (sensor->error() != 0) DEBUG_MSG("[SENSOR] -> ERROR %d\n", sensor->error()); for (unsigned char k=0; kcount(); k++) { @@ -391,6 +358,45 @@ void sensorSetup() { } +} + +// ----------------------------------------------------------------------------- +// Public +// ----------------------------------------------------------------------------- + +unsigned char sensorCount() { + return _sensors.size(); +} + +unsigned char magnitudeCount() { + return _magnitudes.size(); +} + +String magnitudeName(unsigned char index) { + if (index < _magnitudes.size()) { + sensor_magnitude_t magnitude = _magnitudes[index]; + return magnitude.sensor->slot(magnitude.local); + } + return String(); +} + +unsigned char magnitudeType(unsigned char index) { + if (index < _magnitudes.size()) { + return int(_magnitudes[index].type); + } + return MAGNITUDE_NONE; +} + +// ----------------------------------------------------------------------------- + +void sensorSetup() { + + // Load sensors + _sensorInit(); + + // Load magnitudes + _magnitudesInit(); + #if WEB_SUPPORT // Websockets diff --git a/code/espurna/sensors/DallasSensor.h b/code/espurna/sensors/DallasSensor.h index 8d75df3f..d3559cce 100644 --- a/code/espurna/sensors/DallasSensor.h +++ b/code/espurna/sensors/DallasSensor.h @@ -39,22 +39,12 @@ class DallasSensor : public BaseSensor { _dirty = true; } - void setPullUp(bool pullup) { - if (_pullup == pullup) return; - _pullup = pullup; - _dirty = true; - } - // --------------------------------------------------------------------- unsigned char getGPIO() { return _gpio; } - bool getPullUp() { - return _pullup; - } - // --------------------------------------------------------------------- // Sensor API // --------------------------------------------------------------------- @@ -70,12 +60,15 @@ class DallasSensor : public BaseSensor { if (_wire) delete _wire; _wire = new OneWire(_gpio); - // Must be done after the OneWire initialization - if (_pullup) pinMode(_gpio, INPUT_PULLUP); - // Search devices loadDevices(); + // If no devices found check again pulling up the line + if (_count == 0) { + pinMode(_gpio, INPUT_PULLUP); + loadDevices(); + } + } // Loop-like method, call it in your main loop @@ -230,6 +223,10 @@ class DallasSensor : public BaseSensor { // Protected // --------------------------------------------------------------------- + bool validateID(unsigned char id) { + return (id == DS_CHIP_DS18S20) || (id == DS_CHIP_DS18B20) || (id == DS_CHIP_DS1822) || (id == DS_CHIP_DS1825); + } + unsigned char chip(unsigned char index) { if (index < _count) return _devices[index].address[0]; return 0; @@ -247,14 +244,20 @@ class DallasSensor : public BaseSensor { void loadDevices() { uint8_t address[8]; + _wire->reset(); _wire->reset_search(); while (_wire->search(address)) { // Check CRC if (_wire->crc8(address, 7) == address[7]) { - ds_device_t device; - memcpy(device.address, address, 8); - _devices.push_back(device); + + // Check ID + if (validateID(address[0])) { + ds_device_t device; + memcpy(device.address, address, 8); + _devices.push_back(device); + } + } } @@ -270,7 +273,6 @@ class DallasSensor : public BaseSensor { unsigned char _gpio; unsigned long _interval; - bool _pullup; OneWire * _wire; };