Skip to content

Commit 8280de9

Browse files
author
user2684
committed
Multiple fixes
1 parent 4b3f8b7 commit 8280de9

File tree

5 files changed

+93
-36
lines changed

5 files changed

+93
-36
lines changed

NodeManager.cpp

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,33 @@ float getVcc() {
3434
*/
3535

3636
// set the vcc and ground pin the sensor is connected to
37-
void PowerManager::setPowerPins(int ground_pin, int vcc_pin, long wait) {
37+
void PowerManager::setPowerPins(int ground_pin, int vcc_pin, int wait_time) {
3838
#if DEBUG == 1
3939
Serial.print(F("PWR G="));
4040
Serial.print(ground_pin);
4141
Serial.print(F(" V="));
4242
Serial.println(vcc_pin);
4343
#endif
44-
// configure the vcc pin as output and initialize to low (power off)
44+
// configure the vcc pin as output and initialize to high (power on)
4545
_vcc_pin = vcc_pin;
4646
pinMode(_vcc_pin, OUTPUT);
47-
digitalWrite(_vcc_pin, LOW);
47+
digitalWrite(_vcc_pin, HIGH);
4848
// configure the ground pin as output and initialize to low
4949
_ground_pin = ground_pin;
5050
pinMode(_ground_pin, OUTPUT);
5151
digitalWrite(_ground_pin, LOW);
52-
_wait = wait;
52+
_wait = wait_time;
5353
}
5454

5555
// return true if power pins have been configured
56-
bool PowerManager::_hasPowerManager() {
56+
bool PowerManager::isConfigured() {
5757
if (_vcc_pin != -1 && _ground_pin != -1) return true;
5858
return false;
5959
}
6060

6161
// turn on the sensor by activating its power pins
6262
void PowerManager::powerOn() {
63-
if (! _hasPowerManager()) return;
63+
if (! isConfigured()) return;
6464
#if DEBUG == 1
6565
Serial.print(F("ON P="));
6666
Serial.println(_vcc_pin);
@@ -73,7 +73,7 @@ void PowerManager::powerOn() {
7373

7474
// turn off the sensor
7575
void PowerManager::powerOff() {
76-
if (! _hasPowerManager()) return;
76+
if (! isConfigured()) return;
7777
#if DEBUG == 1
7878
Serial.print(F("OFF P="));
7979
Serial.println(_vcc_pin);
@@ -145,8 +145,8 @@ void Sensor::setFloatPrecision(int value) {
145145
_float_precision = value;
146146
}
147147
#if POWER_MANAGER == 1
148-
void Sensor::setPowerPins(int ground_pin, int vcc_pin, long wait) {
149-
_powerManager.setPowerPins(ground_pin, vcc_pin, wait);
148+
void Sensor::setPowerPins(int ground_pin, int vcc_pin, int wait_time) {
149+
_powerManager.setPowerPins(ground_pin, vcc_pin, wait_time);
150150
}
151151
void Sensor::setAutoPowerPins(bool value) {
152152
_auto_power_pins = value;
@@ -1032,6 +1032,8 @@ SensorDs18b20::SensorDs18b20(int child_id, int pin, DallasTemperature* sensors,
10321032
setValueType(TYPE_FLOAT);
10331033
_index = index;
10341034
_sensors = sensors;
1035+
// retrieve and store the address from the index
1036+
_sensors->getAddress(_device_address, index);
10351037
}
10361038

10371039
// what do to during before
@@ -1045,13 +1047,13 @@ void SensorDs18b20::onSetup() {
10451047
// what do to during loop
10461048
void SensorDs18b20::onLoop() {
10471049
// request the temperature
1048-
_sensors->requestTemperaturesByIndex(_index);
1050+
_sensors->requestTemperatures();
10491051
// read the temperature
10501052
float temperature = _sensors->getTempCByIndex(_index);
10511053
// convert it
10521054
if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32;
10531055
#if DEBUG == 1
1054-
Serial.print(F("DS18 I="));
1056+
Serial.print(F("DS18B20 I="));
10551057
Serial.print(_child_id);
10561058
Serial.print(F(" T="));
10571059
Serial.println(temperature);
@@ -1064,6 +1066,22 @@ void SensorDs18b20::onLoop() {
10641066
void SensorDs18b20::onReceive(const MyMessage & message) {
10651067
onLoop();
10661068
}
1069+
1070+
// function to print a device address
1071+
DeviceAddress* SensorDs18b20::getDeviceAddress() {
1072+
return &_device_address;
1073+
}
1074+
1075+
// returns the sensor's resolution in bits
1076+
int SensorDs18b20::getResolution() {
1077+
return _sensors->getResolution(_device_address);
1078+
}
1079+
1080+
// set the sensor's resolution in bits
1081+
void SensorDs18b20::setResolution(int value) {
1082+
_sensors->setResolution(_device_address, value);
1083+
}
1084+
10671085
#endif
10681086

10691087
/*
@@ -1311,8 +1329,8 @@ void NodeManager::setInterrupt(int pin, int mode, int pull) {
13111329
}
13121330
}
13131331
#if POWER_MANAGER == 1
1314-
void NodeManager::setPowerPins(int ground_pin, int vcc_pin, long wait) {
1315-
_powerManager.setPowerPins(ground_pin, vcc_pin, wait);
1332+
void NodeManager::setPowerPins(int ground_pin, int vcc_pin, int wait_time) {
1333+
_powerManager.setPowerPins(ground_pin, vcc_pin, wait_time);
13161334
}
13171335
void NodeManager::setAutoPowerPins(bool value) {
13181336
_auto_power_pins = value;
@@ -1380,7 +1398,7 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) {
13801398
else if (sensor_type == SENSOR_DOOR) index = registerSensor(new SensorDoor(child_id, pin));
13811399
else if (sensor_type == SENSOR_MOTION) index = registerSensor(new SensorMotion(child_id, pin));
13821400
// set an interrupt on the pin and activate internal pull up
1383-
SensorSwitch* sensor = (SensorSwitch*)get(index);
1401+
SensorSwitch* sensor = (SensorSwitch*)getSensor(index);
13841402
setInterrupt(pin,sensor->getMode(),sensor->getInitial());
13851403
return index;
13861404
}
@@ -1466,6 +1484,22 @@ Sensor* NodeManager::get(int child_id) {
14661484
// return a pointer to the sensor from the given child_id
14671485
return _sensors[child_id];
14681486
}
1487+
Sensor* NodeManager::getSensor(int child_id) {
1488+
return get(child_id);
1489+
}
1490+
1491+
// assign a different child id to a sensor'
1492+
bool NodeManager::renameSensor(int old_child_id, int new_child_id) {
1493+
// ensure the old id exists and the new is available
1494+
if (_sensors[old_child_id] == 0 || _sensors[new_child_id] != 0) return false;
1495+
// assign the sensor to new id
1496+
_sensors[new_child_id] = _sensors[old_child_id];
1497+
// set the new child id
1498+
_sensors[new_child_id]->setChildId(new_child_id);
1499+
// free up the old id
1500+
_sensors[old_child_id] = 0;
1501+
return true;
1502+
}
14691503

14701504
// setup NodeManager
14711505
void NodeManager::before() {
@@ -1525,7 +1559,7 @@ void NodeManager::before() {
15251559
#endif
15261560
}
15271561
#endif
1528-
#if POWER_MANAGER == 1
1562+
#if BATTERY_MANAGER == 1
15291563
// set analogReference to internal if measuring the battery through a pin
15301564
if (! _battery_internal_vcc && _battery_pin > -1) analogReference(INTERNAL);
15311565
#endif
@@ -1731,7 +1765,7 @@ void NodeManager::_process(const char * message) {
17311765
}
17321766
// VERSION: send back the extension's version
17331767
else if (strcmp(message, "VERSION") == 0) {
1734-
_send(_msg.set(VERSION, 1));
1768+
_send(_msg.set(VERSION));
17351769
}
17361770
#if REMOTE_CONFIGURATION == 1
17371771
// IDxxx: change the node id to the provided one. E.g. ID025: change the node id to 25. Requires a reboot/restart

NodeManager.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include <Arduino.h>
99

10+
// define NodeManager version
11+
#define VERSION "1.4-dev1"
1012

1113
/***********************************
1214
Constants
@@ -40,9 +42,6 @@
4042
#define EEPROM_SLEEP_TIME_MINOR 3
4143
#define EEPROM_SLEEP_UNIT 4
4244

43-
// define NodeManager version
44-
#define VERSION 1.4
45-
4645
/************************************
4746
* Include user defined configuration settings
4847
*/
@@ -247,15 +246,15 @@ class PowerManager {
247246
public:
248247
PowerManager() {};
249248
// to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
250-
void setPowerPins(int ground_pin, int vcc_pin, long wait = 50);
249+
void setPowerPins(int ground_pin, int vcc_pin, int wait_time = 50);
251250
void powerOn();
252251
void powerOff();
253252
float getVcc();
253+
bool isConfigured();
254254
private:
255255
int _vcc_pin = -1;
256256
int _ground_pin = -1;
257257
long _wait = 0;
258-
bool _hasPowerManager();
259258
};
260259

261260

@@ -295,7 +294,7 @@ class Sensor {
295294
void setSleepBetweenSend(int value);
296295
#if POWER_MANAGER == 1
297296
// to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
298-
void setPowerPins(int ground_pin, int vcc_pin, long wait = 50);
297+
void setPowerPins(int ground_pin, int vcc_pin, int wait_time = 50);
299298
// if enabled the pins will be automatically powered on while awake and off during sleeping (default: true)
300299
void setAutoPowerPins(bool value);
301300
// manually turn the power on
@@ -637,10 +636,17 @@ class SensorDs18b20: public Sensor {
637636
void onSetup();
638637
void onLoop();
639638
void onReceive(const MyMessage & message);
639+
// return the sensors' device address
640+
DeviceAddress* getDeviceAddress();
641+
// returns the sensor's resolution in bits
642+
int getResolution();
643+
// set the sensor's resolution in bits
644+
void setResolution(int value);
640645
protected:
641646
float _offset = 0;
642647
int _index;
643648
DallasTemperature* _sensors;
649+
DeviceAddress _device_address;
644650
};
645651
#endif
646652

@@ -722,7 +728,7 @@ class NodeManager {
722728
void setBatteryPin(int value);
723729
// if setBatteryInternalVcc() is set to false, the volts per bit ratio used to calculate the battery voltage (default: 0.003363075)
724730
void setBatteryVoltsPerBit(float value);
725-
// If true, wake up by an interrupt counts as a valid cycle for battery reports otherwise only uninterrupted sleep cycles would contribute (default: false)
731+
// If true, wake up by an interrupt counts as a valid cycle for battery reports otherwise only uninterrupted sleep cycles would contribute (default: true)
726732
void setBatteryReportWithInterrupt(bool value);
727733
#endif
728734
#if SLEEP_MANAGER == 1
@@ -746,9 +752,12 @@ class NodeManager {
746752
int registerSensor(Sensor* sensor);
747753
// return a sensor by its index
748754
Sensor* get(int sensor_index);
755+
Sensor* getSensor(int sensor_index);
756+
// assign a different child id to a sensor
757+
bool renameSensor(int old_child_id, int new_child_id);
749758
#if POWER_MANAGER == 1
750759
// to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
751-
void setPowerPins(int ground_pin, int vcc_pin, long wait = 50);
760+
void setPowerPins(int ground_pin, int vcc_pin, int wait_time = 50);
752761
// if enabled the pins will be automatically powered on while awake and off during sleeping (default: true)
753762
void setAutoPowerPins(bool value);
754763
// manually turn the power on
@@ -773,7 +782,7 @@ class NodeManager {
773782
float _battery_min = 2.6;
774783
float _battery_max = 3.3;
775784
int _battery_report_cycles = 10;
776-
bool _battery_report_with_interrupt = false;
785+
bool _battery_report_with_interrupt = true;
777786
bool _battery_internal_vcc = true;
778787
int _battery_pin = -1;
779788
float _battery_volts_per_bit = 0.003363075;

NodeManager.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ void before() {
2929
/*
3030
* Register below your sensors
3131
*/
32-
nodeManager.registerSensor(SENSOR_ML8511,A1);
32+
33+
3334

3435
/*
3536
* Register above your sensors

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Node Manager comes with a reasonable default configuration. If you want/need to
134134
void setBatteryPin(int value);
135135
// if setBatteryInternalVcc() is set to false, the volts per bit ratio used to calculate the battery voltage (default: 0.003363075)
136136
void setBatteryVoltsPerBit(float value);
137-
// If true, wake up by an interrupt counts as a valid cycle for battery reports otherwise only uninterrupted sleep cycles would contribute (default: false)
137+
// If true, wake up by an interrupt counts as a valid cycle for battery reports otherwise only uninterrupted sleep cycles would contribute (default: true)
138138
void setBatteryReportWithInterrupt(bool value);
139139
#endif
140140
#if SLEEP_MANAGER == 1
@@ -158,6 +158,9 @@ Node Manager comes with a reasonable default configuration. If you want/need to
158158
int registerSensor(Sensor* sensor);
159159
// return a sensor by its index
160160
Sensor* get(int sensor_index);
161+
Sensor* getSensor(int sensor_index);
162+
// assign a different child id to a sensor
163+
bool renameSensor(int old_child_id, int new_child_id);
161164
#if POWER_MANAGER == 1
162165
// to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
163166
void setPowerPins(int ground_pin, int vcc_pin, long wait = 0);
@@ -234,10 +237,10 @@ nodeManager.registerSensor(new SensorCustom(child_id, pin));
234237
## Configuring the sensors
235238
Each built-in sensor class comes with reasonable default settings. In case you want/need to customize any of those settings, after having registered the sensor, you can retrieve it back and call set functions common to all the sensors or specific for a given class.
236239

237-
To do so, use `nodeManager.get(child_id)` which will return a pointer to the sensor. Remeber to cast it to the right class before calling their functions. For example:
240+
To do so, use `nodeManager.getSensor(child_id)` which will return a pointer to the sensor. Remeber to cast it to the right class before calling their functions. For example:
238241

239242
~~~c
240-
((SensorLatchingRelay*)nodeManager.get(2))->setPulseWidth(50);
243+
((SensorLatchingRelay*)nodeManager.getSensor(2))->setPulseWidth(50);
241244
~~~
242245
243246
@@ -359,6 +362,16 @@ Each sensor class can expose additional methods.
359362
void setInitial(int value);
360363
~~~
361364
365+
#### SensorDs18b20
366+
~~~c
367+
// return the sensors' device address
368+
DeviceAddress* getDeviceAddress();
369+
// returns the sensor's resolution in bits
370+
int getResolution();
371+
// set the sensor's resolution in bits
372+
void setResolution(int value);
373+
~~~
374+
362375
## Upload your sketch
363376

364377
Upload your sketch to your arduino board as you are used to.
@@ -507,14 +520,14 @@ Register a LDR sensor attached to pin A1 and send to the gateway the average of
507520

508521
~~~c
509522
int sensor_ldr = nodeManager.registerSensor(SENSOR_LDR,A1);
510-
((SensorLDR*)nodeManager.get(sensor_ldr))->setSamples(3);
523+
((SensorLDR*)nodeManager.getSensor(sensor_ldr))->setSamples(3);
511524
~~~
512525
513526
Register a rain sensor connected to A0. This will be powered with via pins 4 (ground) and 5 (vcc) just before reading its value at each cycle, it will be presented as S_RAIN. sending V_RAINRATE messages, the output will be a percentage (calculated between 200 and 1024) and the value will be reversed (so that no rain will be 0%):
514527
515528
~~~c
516529
int rain = nodeManager.registerSensor(SENSOR_ANALOG_INPUT,A0);
517-
SensorAnalogInput* rainSensor = ((SensorAnalogInput*)nodeManager.get(rain));
530+
SensorAnalogInput* rainSensor = ((SensorAnalogInput*)nodeManager.getSensor(rain));
518531
rainSensor->setPowerPins(4,5,300);
519532
rainSensor->setPresentation(S_RAIN);
520533
rainSensor->setType(V_RAINRATE);
@@ -765,7 +778,7 @@ void receive(const MyMessage &message) {
765778
## Rain and Soil Moisture Sensor
766779
767780
The following sketch can be used to report the rain level and the soil moisture based on two sensors connected to the board's analog pins (A1 and A2). In this case we are customizing the out-of-the-box SENSOR_ANALOG_INPUT sensor type since we just need to measure an analog input but we also want to provide the correct type and presentation for each sensor.
768-
We register the sensors first with registerSensor() which returns the child id assigned to the sensor. We then retrieve the sensor's reference by calling get() so we can invoke the sensor-specific functions, like setPresentation() and setType().
781+
We register the sensors first with registerSensor() which returns the child id assigned to the sensor. We then retrieve the sensor's reference by calling getSensor() so we can invoke the sensor-specific functions, like setPresentation() and setType().
769782
In this example, the two sensors are not directly connected to the battery's ground and vcc but, to save additional power, are powered through two arduino's pins. By using e.g. setPowerPins(4,5,300), NodeManger will assume pin 4 is ground and pin 5 is vcc for that specific sensor so it will turn on the power just before reading the analog input (and waiting 300ms for the sensor to initialize) and back off before going to sleep.
770783
For both the sensors we want a percentage output and with setRangeMin() and setRangeMax() we define the boundaries for calculating the percentage (if we read e.g. 200 when the rain sensor is completely into the water, we know for sure it will not go below this value which will represent the new lower boundary).
771784
Finally, since both the sensors reports low when wet and high when dry but we need the opposite, we set setReverse() so to have 0% reported when there is no rain/moisture, 100% on the opposite situation.
@@ -809,8 +822,8 @@ void before() {
809822
int rain = nodeManager.registerSensor(SENSOR_ANALOG_INPUT,A1);
810823
int soil = nodeManager.registerSensor(SENSOR_ANALOG_INPUT,A2);
811824
812-
SensorAnalogInput* rainSensor = ((SensorAnalogInput*)nodeManager.get(rain));
813-
SensorAnalogInput* soilSensor = ((SensorAnalogInput*)nodeManager.get(soil));
825+
SensorAnalogInput* rainSensor = ((SensorAnalogInput*)nodeManager.getSensor(rain));
826+
SensorAnalogInput* soilSensor = ((SensorAnalogInput*)nodeManager.getSensor(soil));
814827
815828
rainSensor->setPresentation(S_RAIN);
816829
rainSensor->setType(V_RAINRATE);

config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* Sketch configuration
66
*/
77

8-
#define SKETCH_NAME "NodeManager"
9-
#define SKETCH_VERSION "1.4"
8+
#define SKETCH_NAME "NodeManagerTemplate"
9+
#define SKETCH_VERSION "1.0"
1010

1111
/**********************************
1212
* MySensors configuration

0 commit comments

Comments
 (0)