modbus485_dust_co2_node/src/main.cpp

47 lines
2.2 KiB
C++
Raw Normal View History

2019-02-06 23:25:03 +01:00
#include <Arduino.h>
2019-02-14 23:14:37 +01:00
#include "MHZ19.h" // include main library
#include <SoftwareSerial.h> // Remove if using HardwareSerial
2019-02-07 01:13:58 +01:00
2019-02-14 23:14:37 +01:00
#define RX_PIN 9 // Rx pin which the MHZ19 Tx pin is attached to
#define TX_PIN 10 // Tx pin which the MHZ19 Rx pin is attached to
#define BAUDRATE 9600 // Native to the sensor (do not change)
2019-02-07 01:13:58 +01:00
2019-02-14 23:14:37 +01:00
MHZ19 myMHZ19; // Constructor for MH-Z19 class
SoftwareSerial mySerial(RX_PIN, TX_PIN); // Constructor for Stream class *change for HardwareSerial, i.e. ESP32 ***
2019-02-07 01:13:58 +01:00
2019-02-14 23:14:37 +01:00
//HardwareSerial mySerial(1); // ESP32 Example
2019-02-06 23:25:03 +01:00
2019-02-14 23:14:37 +01:00
unsigned long getDataTimer = 0; // Variable to store timer interval
void setup()
{
Serial.begin(57600); // For ESP32 baudarte is 115200 etc.
2019-02-06 23:25:03 +01:00
2019-02-14 23:14:37 +01:00
mySerial.begin(BAUDRATE); // Begin Stream with MHZ19 baudrate
//mySerial.begin(BAUDRATE, SERIAL_8N1, RX_PIN, TX_PIN); // ESP32 Example
2019-02-07 01:13:58 +01:00
2019-02-14 23:14:37 +01:00
myMHZ19.begin(mySerial); // *Important, Pass your Stream reference
myMHZ19.autoCalibration(); // Turn ABC ON. Disable with autoCalibration(false) (also default)
2019-02-07 02:44:13 +01:00
}
2019-02-14 23:14:37 +01:00
void loop()
{
if (millis() - getDataTimer >= 2000) // Check if interval has elapsed (non-blocking delay() equivilant)
{
int CO2; // Buffer for CO2
CO2 = myMHZ19.getCO2(); // Request CO2 (as ppm)
Serial.print("CO2 (ppm): ");
Serial.println(CO2);
float Temp; // Buffer for temperature
Temp = myMHZ19.getTemperature(); // Request Temperature (as Celsius)
Serial.print("Temperature (C): ");
Serial.println(Temp);
getDataTimer = millis(); // Update interval
}
2019-02-06 23:25:03 +01:00
}