Skip to content

Commit a266029

Browse files
pilotakuser2684
authored andcommitted
Fix MCP9808 + new features (#484)
1 parent 59e2895 commit a266029

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,16 @@ Each sensor class may expose additional methods.
10061006
void sendSMS(const char* text);
10071007
~~~
10081008

1009+
* SensorMCP9808
1010+
~~~c
1011+
// set I2C address (default: 0x18)
1012+
void setI2CAddress(uint8_t i2caddress);
1013+
// set temperature resolution (default: 3)
1014+
void setResolution(uint8_t resolution);
1015+
// sleep sensor after measurment (default: true)
1016+
void setSleep(bool value);
1017+
~~~
1018+
10091019
### OTA Configuration
10101020
10111021
When `NODEMANAGER_OTA_CONFIGURATION` is set to ON the API presented above can be also called remotely through `SensorConfiguration`, which is automatically added to NodeManager. SensorConfiguration exposes by default child id 200 that can be used to interact with the service by sending `V_CUSTOM` type of messages and commands within the payload. For each `REQ` message, the node will respond with a `SET` message if successful.

sensors/SensorMCP9808.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,44 @@ SensorMCP9808
2929
class SensorMCP9808: public Sensor {
3030
protected:
3131
Adafruit_MCP9808* _mcp;
32-
32+
uint8_t _i2caddress = MCP9808_I2CADDR_DEFAULT;
33+
uint8_t _resolution = 3;
34+
bool _sleep = true;
35+
3336
public:
3437
SensorMCP9808(uint8_t child_id = 0): Sensor(-1) {
3538
_name = "MCP9808";
3639
children.allocateBlocks(1);
3740
new Child(this,FLOAT,nodeManager.getAvailableChildId(child_id),S_TEMP,V_TEMP,_name);
3841
};
39-
42+
// set I2C address (default: 0x18)
43+
void setI2CAddress(uint8_t i2caddress) {
44+
_i2caddress = i2caddress;
45+
};
46+
// set temperature resolution (default: 3)
47+
void setResolution(uint8_t resolution) {
48+
_resolution = resolution;
49+
};
50+
// sleep sensor after measurment (default: true)
51+
void setSleep(bool value){
52+
_sleep = value;
53+
}
4054
// define what to do during setup
4155
void onSetup() {
4256
_mcp = new Adafruit_MCP9808();
57+
_mcp->begin(_i2caddress);
58+
_mcp->setResolution(_resolution);
4359
};
44-
4560
// define what to do during loop
4661
void onLoop(Child* child) {
62+
// wake up
63+
_mcp->shutdown_wake(false);
64+
// sleep based on sample time
65+
if(_sleep) nodeManager.sleepOrWait(33 << _resolution);
66+
// get data
4767
float temperature = _mcp->readTempC();
68+
// sleep
69+
if(_sleep) _mcp->shutdown_wake(true);
4870
// convert it
4971
temperature = nodeManager.celsiusToFahrenheit(temperature);
5072
// store the value

0 commit comments

Comments
 (0)