Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
NodeManager_BME280_EasyPCB
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SmartHome
NodeManager_BME280_EasyPCB
Commits
1087ba0c
Commit
1087ba0c
authored
7 years ago
by
Reinhold Kainhofer
Committed by
user2684
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Added support for the MH-Z19 CO2 sensor (#161)
parent
15a199c9
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
NodeManager.cpp
+107
-0
107 additions, 0 deletions
NodeManager.cpp
NodeManager.h
+35
-0
35 additions, 0 deletions
NodeManager.h
README.md
+10
-1
10 additions, 1 deletion
README.md
config.h
+2
-0
2 additions, 0 deletions
config.h
with
154 additions
and
1 deletion
NodeManager.cpp
+
107
−
0
View file @
1087ba0c
...
@@ -2332,6 +2332,108 @@ int SensorMQ::_MQGetPercentage(float rs_ro_ratio, float *pcurve) {
...
@@ -2332,6 +2332,108 @@ int SensorMQ::_MQGetPercentage(float rs_ro_ratio, float *pcurve) {
}
}
#endif
#endif
/*
SensorMHZ19
*/
#if MODULE_MHZ19 == 1
// contructor
SensorMHZ19
::
SensorMHZ19
(
NodeManager
*
node_manager
,
int
child_id
,
int
pin
)
:
Sensor
(
node_manager
,
child_id
,
pin
)
{
// set presentation, type and value type
setPresentation
(
S_AIR_QUALITY
);
setType
(
V_LEVEL
);
setRxTx
(
pin
,
pin
+
1
);
}
void
SensorMHZ19
::
setRxTx
(
int
rxpin
,
int
txpin
)
{
_rx_pin
=
rxpin
;
_tx_pin
=
txpin
;
}
// what to do during before
void
SensorMHZ19
::
onBefore
()
{
}
// what to do during setup
void
SensorMHZ19
::
onSetup
()
{
_ser
=
new
SoftwareSerial
(
_rx_pin
,
_tx_pin
);
_ser
->
begin
(
9600
);
delay
(
2000
);
while
(
_ser
->
read
()
!=-
1
)
{};
// clear CO2 buffer.
}
// what to do during loop
void
SensorMHZ19
::
onLoop
()
{
// Read the ppm value
int
co2ppm
=
readCO2
();
// This is there the function gets called that talks to the Co2 sensor.
#if DEBUG == 1
Serial
.
print
(
F
(
"CO2 I="
));
Serial
.
print
(
_child_id
);
Serial
.
print
(
F
(
" ppm="
));
Serial
.
println
(
co2ppm
);
#endif
// store the value
_value_int
=
co2ppm
;
}
// Read out the CO2 data
int
SensorMHZ19
::
readCO2
()
{
while
(
_ser
->
read
()
!=
-
1
)
{};
//clear serial buffer
unsigned
char
response
[
9
];
// for answer
byte
cmd
[
9
]
=
{
0xFF
,
0x01
,
0x86
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x79
};
// Command to ask for data.
_ser
->
write
(
cmd
,
9
);
//request PPM CO2
// Then for 1 second listen for 9 bytes of data.
_ser
->
readBytes
(
response
,
9
);
#if DEBUG == 1
for
(
int
i
=
0
;
i
<
9
;
i
++
)
{
Serial
.
print
(
response
[
i
],
HEX
);
Serial
.
print
(
F
(
"-"
));
}
Serial
.
println
(
F
(
"END"
));
#endif
if
(
response
[
0
]
!=
0xFF
)
{
Serial
.
println
(
F
(
"Wrong starting byte from co2 sensor! (should be FF)"
));
return
-
1
;
}
if
(
response
[
1
]
!=
0x86
)
{
Serial
.
println
(
F
(
"Wrong command from co2 sensor! (should be 86)"
));
return
-
1
;
}
int
responseHigh
=
(
int
)
response
[
2
];
int
responseLow
=
(
int
)
response
[
3
];
int
ppm
=
(
256
*
responseHigh
)
+
responseLow
;
return
ppm
;
}
// what to do as the main task when receiving a message
void
SensorMHZ19
::
onReceive
(
const
MyMessage
&
message
)
{
if
(
message
.
getCommand
()
==
C_REQ
)
onLoop
();
}
// what to do when receiving a remote message
void
SensorMHZ19
::
onProcess
(
Request
&
request
)
{
int
function
=
request
.
getFunction
();
switch
(
function
)
{
default:
return
;
}
_send
(
_msg_service
.
set
(
function
));
}
#endif
/*******************************************
/*******************************************
NodeManager
NodeManager
*/
*/
...
@@ -2624,6 +2726,11 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) {
...
@@ -2624,6 +2726,11 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) {
return
registerSensor
(
new
SensorMQ
(
this
,
child_id
,
pin
));
return
registerSensor
(
new
SensorMQ
(
this
,
child_id
,
pin
));
}
}
#endif
#endif
#if MODULE_MHZ19 == 1
else
if
(
sensor_type
==
SENSOR_MHZ19
)
{
return
registerSensor
(
new
SensorMHZ19
(
this
,
child_id
,
pin
));
}
#endif
else
{
else
{
#if DEBUG == 1
#if DEBUG == 1
Serial
.
print
(
F
(
"INVALID "
));
Serial
.
print
(
F
(
"INVALID "
));
...
...
This diff is collapsed.
Click to expand it.
NodeManager.h
+
35
−
0
View file @
1087ba0c
...
@@ -171,6 +171,10 @@
...
@@ -171,6 +171,10 @@
#ifndef MODULE_MQ
#ifndef MODULE_MQ
#define MODULE_MQ 0
#define MODULE_MQ 0
#endif
#endif
// Enable this module to use one of the following sensors: SENSOR_MHZ19
#ifndef MODULE_MHZ19
#define MODULE_MHZ19 0
#endif
/***********************************
/***********************************
Supported Sensors
Supported Sensors
...
@@ -260,6 +264,10 @@ enum supported_sensors {
...
@@ -260,6 +264,10 @@ enum supported_sensors {
// MQ2 air quality sensor
// MQ2 air quality sensor
SENSOR_MQ
,
SENSOR_MQ
,
#endif
#endif
#if MODULE_MHZ19 == 1
// MH-Z19 CO2 sensor
SENSOR_MHZ19
,
#endif
};
};
/***********************************
/***********************************
Libraries
Libraries
...
@@ -317,6 +325,9 @@ enum supported_sensors {
...
@@ -317,6 +325,9 @@ enum supported_sensors {
#include
<Wire.h>
#include
<Wire.h>
#include
"Adafruit_MCP9808.h"
#include
"Adafruit_MCP9808.h"
#endif
#endif
#if MODULE_MHZ19 == 1
#include
<SoftwareSerial.h>
#endif
/*******************************************************************
/*******************************************************************
Classes
Classes
...
@@ -1120,6 +1131,30 @@ class SensorMQ: public Sensor {
...
@@ -1120,6 +1131,30 @@ class SensorMQ: public Sensor {
};
};
#endif
#endif
/*
SensorMHZ19
*/
#if MODULE_MHZ19 == 1
class
SensorMHZ19
:
public
Sensor
{
public:
SensorMHZ19
(
NodeManager
*
node_manager
,
int
child_id
,
int
pin
);
// set the pins for RX and TX of the SoftwareSerial (default: Rx=6, Tx=7)
void
setRxTx
(
int
rxpin
,
int
txpin
);
// define what to do at each stage of the sketch
void
onBefore
();
void
onSetup
();
void
onLoop
();
void
onReceive
(
const
MyMessage
&
message
);
void
onProcess
(
Request
&
request
);
int
readCO2
();
protected:
SoftwareSerial
*
_ser
;
int
_tx_pin
=
6
;
int
_rx_pin
=
7
;
};
#endif
/***************************************
/***************************************
NodeManager: manages all the aspects of the node
NodeManager: manages all the aspects of the node
*/
*/
...
...
This diff is collapsed.
Click to expand it.
README.md
+
10
−
1
View file @
1087ba0c
...
@@ -173,6 +173,8 @@ Those NodeManager's directives in the `config.h` file control which module/libra
...
@@ -173,6 +173,8 @@ Those NodeManager's directives in the `config.h` file control which module/libra
#define MODULE_MCP9808 0
#define MODULE_MCP9808 0
// Enable this module to use one of the following sensors: SENSOR_MQ
// Enable this module to use one of the following sensors: SENSOR_MQ
#define MODULE_MQ 0
#define MODULE_MQ 0
// Enable this module to use one of the following sensors: SENSOR_MHZ19
#define MODULE_MHZ19 0
~~~
~~~
### Installing the dependencies
### Installing the dependencies
...
@@ -333,6 +335,7 @@ SENSOR_MCP9808 | MCP9808 sensor, measure the temperature through the attached mo
...
@@ -333,6 +335,7 @@ SENSOR_MCP9808 | MCP9808 sensor, measure the temperature through the attached mo
SENSOR_RAIN_GAUGE | Rain gauge sensor
SENSOR_RAIN_GAUGE | Rain gauge sensor
SENSOR_RAIN | Rain sensor, return the percentage of rain from an attached analog sensor
SENSOR_RAIN | Rain sensor, return the percentage of rain from an attached analog sensor
SENSOR_SOIL_MOISTURE | Soil moisture sensor, return the percentage of moisture from an attached analog sensor
SENSOR_SOIL_MOISTURE | Soil moisture sensor, return the percentage of moisture from an attached analog sensor
SENSOR_MHZ19 | MH-Z19 CO2 sensor via UART (SoftwareSerial, default on pins 6(Rx) and 7(Tx)
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
...
@@ -585,6 +588,12 @@ Each sensor class can expose additional methods.
...
@@ -585,6 +588,12 @@ Each sensor class can expose additional methods.
void
setLedPin
(
int
value
);
void
setLedPin
(
int
value
);
~~~
~~~
*
SensorMHZ19
~~~
c
// set the RX and TX pins for the software serial port to talk to the sensor
void
setRxTx
(
int
rxpin
,
int
txpin
);
~~~
### Upload your sketch
### Upload your sketch
Upload your sketch to your arduino board as you are used to.
Upload your sketch to your arduino board as you are used to.
...
@@ -1229,4 +1238,4 @@ v1.5:
...
@@ -1229,4 +1238,4 @@ v1.5:
*
ESP8266WiFi.h has to be included in the main sketch if MY_GATEWAY_ESP8266 is defined
*
ESP8266WiFi.h has to be included in the main sketch if MY_GATEWAY_ESP8266 is defined
*
Added receiveTime() wrapper in the main sketch
*
Added receiveTime() wrapper in the main sketch
*
Fixed the logic for output sensors
*
Fixed the logic for output sensors
*
Added common gateway settings in config.h
*
Added common gateway settings in config.h
\ No newline at end of file
This diff is collapsed.
Click to expand it.
config.h
+
2
−
0
View file @
1087ba0c
...
@@ -132,5 +132,7 @@
...
@@ -132,5 +132,7 @@
#define MODULE_MCP9808 0
#define MODULE_MCP9808 0
// Enable this module to use one of the following sensors: SENSOR_MQ
// Enable this module to use one of the following sensors: SENSOR_MQ
#define MODULE_MQ 0
#define MODULE_MQ 0
// Enable this module to use one of the following sensors: SENSOR_MHZ19
#define MODULE_MHZ19 0
#endif
#endif
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment