Commit 38fd51c9 authored by user2684's avatar user2684
Browse files

v1.5-dev4. Add support for MCP9808 temperature sensor #87

parent 39fbc856
...@@ -1691,6 +1691,47 @@ void SensorHCSR04::onReceive(const MyMessage & message) { ...@@ -1691,6 +1691,47 @@ void SensorHCSR04::onReceive(const MyMessage & message) {
} }
#endif #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 NodeManager
*/ */
...@@ -1929,6 +1970,19 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) { ...@@ -1929,6 +1970,19 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) {
return registerSensor(new SensorHCSR04(child_id, pin)); return registerSensor(new SensorHCSR04(child_id, pin));
} }
#endif #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 { else {
#if DEBUG == 1 #if DEBUG == 1
Serial.print(F("INVALID ")); Serial.print(F("INVALID "));
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include <Arduino.h> #include <Arduino.h>
// define NodeManager version // define NodeManager version
#define VERSION "1.5-dev3" #define VERSION "1.5-dev4"
/*********************************** /***********************************
Constants Constants
...@@ -145,6 +145,11 @@ ...@@ -145,6 +145,11 @@
#ifndef MODULE_HCSR04 #ifndef MODULE_HCSR04
#define MODULE_HCSR04 0 #define MODULE_HCSR04 0
#endif #endif
// Enable this module to use one of the following sensors: SENSOR_MCP9808
#ifndef MODULE_MCP9808
#define MODULE_MCP9808 0
#endif
/*********************************** /***********************************
Sensors types Sensors types
*/ */
...@@ -220,7 +225,11 @@ ...@@ -220,7 +225,11 @@
// HC-SR04 sensor, return the distance between the sensor and an object // HC-SR04 sensor, return the distance between the sensor and an object
#define SENSOR_HCSR04 23 #define SENSOR_HCSR04 23
#endif #endif
// last Id: 24 #if MODULE_MCP9808 == 1
// MCP9808 sensor, precision temperature sensor
#define SENSOR_MCP9808 25
#endif
// last Id: 25
/*********************************** /***********************************
Libraries Libraries
*/ */
...@@ -276,6 +285,10 @@ ...@@ -276,6 +285,10 @@
#if MODULE_HCSR04 == 1 #if MODULE_HCSR04 == 1
#include <NewPing.h> #include <NewPing.h>
#endif #endif
#if MODULE_MCP9808 == 1
#include <Wire.h>
#include "Adafruit_MCP9808.h"
#endif
/************************************** /**************************************
Classes Classes
...@@ -898,6 +911,23 @@ class SensorSonoff: public Sensor { ...@@ -898,6 +911,23 @@ class SensorSonoff: public Sensor {
}; };
#endif #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 NodeManager: manages all the aspects of the node
......
...@@ -171,6 +171,8 @@ Those NodeManager's directives in the `config.h` file control which module/libra ...@@ -171,6 +171,8 @@ Those NodeManager's directives in the `config.h` file control which module/libra
#define MODULE_BMP085 0 #define MODULE_BMP085 0
// Enable this module to use one of the following sensors: SENSOR_HCSR04 // Enable this module to use one of the following sensors: SENSOR_HCSR04
#define MODULE_HCSR04 0 #define MODULE_HCSR04 0
// Enable this module to use one of the following sensors: SENSOR_MCP9808
#define MODULE_MCP9808 0
~~~ ~~~
## Installing the dependencies ## Installing the dependencies
...@@ -188,6 +190,7 @@ MODULE_BME280 | https://github.com/adafruit/Adafruit_BME280_Library ...@@ -188,6 +190,7 @@ MODULE_BME280 | https://github.com/adafruit/Adafruit_BME280_Library
MODULE_SONOFF | https://github.com/thomasfredericks/Bounce2 MODULE_SONOFF | https://github.com/thomasfredericks/Bounce2
MODULE_BMP085 | https://github.com/adafruit/Adafruit-BMP085-Library MODULE_BMP085 | https://github.com/adafruit/Adafruit-BMP085-Library
MODULE_HCSR04 | https://github.com/mysensors/MySensorsArduinoExamples/tree/master/libraries/NewPing MODULE_HCSR04 | https://github.com/mysensors/MySensorsArduinoExamples/tree/master/libraries/NewPing
MODULE_MCP9808 | https://github.com/adafruit/Adafruit_MCP9808_Library
## Configure NodeManager ## Configure NodeManager
...@@ -294,6 +297,7 @@ SENSOR_SONOFF | Sonoff wireless smart switch ...@@ -294,6 +297,7 @@ SENSOR_SONOFF | Sonoff wireless smart switch
SENSOR_BMP085 | BMP085/BMP180 sensor, return temperature and pressure SENSOR_BMP085 | BMP085/BMP180 sensor, return temperature and pressure
SENSOR_HCSR04 | HC-SR04 sensor, return the distance between the sensor and an object 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_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: To register a sensor simply call the NodeManager instance with the sensory type and the pin the sensor is conncted to. For example:
~~~c ~~~c
......
...@@ -99,7 +99,7 @@ ...@@ -99,7 +99,7 @@
#define PERSIST 0 #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 // 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 // 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 #define BATTERY_SENSOR 1
...@@ -129,5 +129,7 @@ ...@@ -129,5 +129,7 @@
#define MODULE_BMP085 0 #define MODULE_BMP085 0
// Enable this module to use one of the following sensors: SENSOR_HCSR04 // Enable this module to use one of the following sensors: SENSOR_HCSR04
#define MODULE_HCSR04 0 #define MODULE_HCSR04 0
// Enable this module to use one of the following sensors: SENSOR_MCP9808
#define MODULE_MCP9808 0
#endif #endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment