From 38fd51c99e47b6ec90f4885ff1cb0cfe33857ab4 Mon Sep 17 00:00:00 2001 From: user2684 <you@example.com> Date: Sat, 13 May 2017 13:11:47 +0200 Subject: [PATCH] v1.5-dev4. Add support for MCP9808 temperature sensor #87 --- NodeManager.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ NodeManager.h | 34 +++++++++++++++++++++++++++++-- NodeManager.ino | 4 ++-- README.md | 4 ++++ config.h | 4 +++- 5 files changed, 95 insertions(+), 5 deletions(-) diff --git a/NodeManager.cpp b/NodeManager.cpp index b2bb509..c159e84 100644 --- a/NodeManager.cpp +++ b/NodeManager.cpp @@ -1691,6 +1691,47 @@ void SensorHCSR04::onReceive(const MyMessage & message) { } #endif +/* + SensorMCP9808 +*/ +#if MODULE_MCP9808 == 1 +// contructor +SensorMCP9808::SensorMCP9808(int child_id, Adafruit_MCP9808* mcp): Sensor(child_id,A2) { + _mcp = mcp; + setPresentation(S_TEMP); + setType(V_TEMP); + setValueType(TYPE_FLOAT); +} + +// what do to during before +void SensorMCP9808::onBefore() { +} + +// what do to during setup +void SensorMCP9808::onSetup() { +} + +// what do to during loop +void SensorMCP9808::onLoop() { + float temperature = _mcp->readTempC(); + // convert it + if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32; + #if DEBUG == 1 + Serial.print(F("MCP I=")); + Serial.print(_child_id); + Serial.print(F(" T=")); + Serial.println(temperature); + #endif + // store the value + if (! isnan(temperature)) _value_float = temperature; +} + +// what do to as the main task when receiving a message +void SensorMCP9808::onReceive(const MyMessage & message) { + if (message.getCommand() == C_REQ) onLoop(); +} +#endif + /******************************************* NodeManager */ @@ -1929,6 +1970,19 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) { return registerSensor(new SensorHCSR04(child_id, pin)); } #endif + #if MODULE_MCP9808 == 1 + else if (sensor_type == SENSOR_MCP9808) { + Adafruit_MCP9808 * mcp = new Adafruit_MCP9808(); + if (! mcp->begin()) { + #if DEBUG == 1 + Serial.println(F("NO MCP")); + #endif + return -1; + } + // register temperature sensor + registerSensor(new SensorMCP9808(child_id,mcp)); + } + #endif else { #if DEBUG == 1 Serial.print(F("INVALID ")); diff --git a/NodeManager.h b/NodeManager.h index af4ce81..32b5e14 100644 --- a/NodeManager.h +++ b/NodeManager.h @@ -8,7 +8,7 @@ #include <Arduino.h> // define NodeManager version -#define VERSION "1.5-dev3" +#define VERSION "1.5-dev4" /*********************************** Constants @@ -145,6 +145,11 @@ #ifndef MODULE_HCSR04 #define MODULE_HCSR04 0 #endif +// Enable this module to use one of the following sensors: SENSOR_MCP9808 +#ifndef MODULE_MCP9808 + #define MODULE_MCP9808 0 +#endif + /*********************************** Sensors types */ @@ -220,7 +225,11 @@ // HC-SR04 sensor, return the distance between the sensor and an object #define SENSOR_HCSR04 23 #endif -// last Id: 24 +#if MODULE_MCP9808 == 1 + // MCP9808 sensor, precision temperature sensor + #define SENSOR_MCP9808 25 +#endif +// last Id: 25 /*********************************** Libraries */ @@ -276,6 +285,10 @@ #if MODULE_HCSR04 == 1 #include <NewPing.h> #endif +#if MODULE_MCP9808 == 1 + #include <Wire.h> + #include "Adafruit_MCP9808.h" +#endif /************************************** Classes @@ -898,6 +911,23 @@ class SensorSonoff: public Sensor { }; #endif +/* + SensorMCP9808 +*/ +#if MODULE_MCP9808 == 1 +class SensorMCP9808: public Sensor { + public: + SensorMCP9808(int child_id, Adafruit_MCP9808* mcp); + // define what to do at each stage of the sketch + void onBefore(); + void onSetup(); + void onLoop(); + void onReceive(const MyMessage & message); + protected: + Adafruit_MCP9808* _mcp; +}; +#endif + /*************************************** NodeManager: manages all the aspects of the node diff --git a/NodeManager.ino b/NodeManager.ino index b3f2fb3..6422f43 100644 --- a/NodeManager.ino +++ b/NodeManager.ino @@ -33,8 +33,8 @@ void before() { /* * Register below your sensors */ - - + + /* * Register above your sensors diff --git a/README.md b/README.md index 3f535e6..d97f921 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,8 @@ Those NodeManager's directives in the `config.h` file control which module/libra #define MODULE_BMP085 0 // Enable this module to use one of the following sensors: SENSOR_HCSR04 #define MODULE_HCSR04 0 +// Enable this module to use one of the following sensors: SENSOR_MCP9808 +#define MODULE_MCP9808 0 ~~~ ## Installing the dependencies @@ -188,6 +190,7 @@ MODULE_BME280 | https://github.com/adafruit/Adafruit_BME280_Library MODULE_SONOFF | https://github.com/thomasfredericks/Bounce2 MODULE_BMP085 | https://github.com/adafruit/Adafruit-BMP085-Library MODULE_HCSR04 | https://github.com/mysensors/MySensorsArduinoExamples/tree/master/libraries/NewPing +MODULE_MCP9808 | https://github.com/adafruit/Adafruit_MCP9808_Library ## Configure NodeManager @@ -294,6 +297,7 @@ SENSOR_SONOFF | Sonoff wireless smart switch SENSOR_BMP085 | BMP085/BMP180 sensor, return temperature and pressure SENSOR_HCSR04 | HC-SR04 sensor, return the distance between the sensor and an object SENSOR_ACS712 | ACS712 sensor, measure the current going through the attached module +SENSOR_MCP9808 | MCP9808 sensor, measure the temperature through the attached module To register a sensor simply call the NodeManager instance with the sensory type and the pin the sensor is conncted to. For example: ~~~c diff --git a/config.h b/config.h index 2946c0d..acd2aae 100644 --- a/config.h +++ b/config.h @@ -99,7 +99,7 @@ #define PERSIST 0 // if enabled, send a SLEEPING and AWAKE service messages just before entering and just after leaving a sleep cycle and STARTED when starting/rebooting -#define SERVICE_MESSAGES 1 +#define SERVICE_MESSAGES 0 // if enabled, a battery sensor will be created at BATTERY_CHILD_ID and will report vcc voltage together with the battery level percentage #define BATTERY_SENSOR 1 @@ -129,5 +129,7 @@ #define MODULE_BMP085 0 // Enable this module to use one of the following sensors: SENSOR_HCSR04 #define MODULE_HCSR04 0 +// Enable this module to use one of the following sensors: SENSOR_MCP9808 +#define MODULE_MCP9808 0 #endif -- GitLab