diff --git a/.DS_Store b/.DS_Store index 62d177e..85c0f67 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/lib/.DS_Store b/lib/.DS_Store index c699966..729f2a1 100644 Binary files a/lib/.DS_Store and b/lib/.DS_Store differ diff --git a/lib/SimpleModbusSlave/.DS_Store b/lib/SimpleModbusSlave/.DS_Store new file mode 100644 index 0000000..6f49882 Binary files /dev/null and b/lib/SimpleModbusSlave/.DS_Store differ diff --git a/lib/SimpleModbusSlave/examples/.DS_Store b/lib/SimpleModbusSlave/examples/.DS_Store new file mode 100644 index 0000000..21938b9 Binary files /dev/null and b/lib/SimpleModbusSlave/examples/.DS_Store differ diff --git a/platformio.ini b/platformio.ini index e5924d7..cc68b51 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,5 +12,5 @@ platform = atmelavr board = pro16MHzatmega328 framework = arduino -lib_deps = PMS Library, TaskScheduler, MH-Z19 -monitor_speed = 57600 \ No newline at end of file +lib_deps = PMS Library, TaskScheduler +monitor_speed = 9600 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5fe9e1f..597c037 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,47 +1,63 @@ -#include -#include "MHZ19.h" // include main library -#include // Remove if using HardwareSerial +#include +#include +#include +#include -#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) +SoftwareSerial serial1(2, 3); // RX, TX +SoftwareSerial serial2(4, 5); // RX, TX +PMS pms(serial1); +PMS::DATA data; +Scheduler runner; -MHZ19 myMHZ19; // Constructor for MH-Z19 class -SoftwareSerial mySerial(RX_PIN, TX_PIN); // Constructor for Stream class *change for HardwareSerial, i.e. ESP32 *** +void sensor_co2(); +Task t1(5000, TASK_FOREVER, &sensor_co2); -//HardwareSerial mySerial(1); // ESP32 Example +//////////////// MODBUS Registers /////////////////// +enum +{ + R00_Dust_10, + R01_Dust_25, + R02_Dust_100, + R03_CO2_PPM, + TOTAL_ERRORS, + TOTAL_REGS_SIZE +}; +unsigned int holdingRegs[TOTAL_REGS_SIZE]; // function 3 and 16 register array +////////////////////////////////////////////////////// +int PM10 = 0; +int PM25 = 0; +int PM100 = 0; +int CO2 = 0; +byte return1[9]; +byte return2[9]; +byte com[]={0xff,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79}; -unsigned long getDataTimer = 0; // Variable to store timer interval - -void setup() -{ - Serial.begin(57600); // For ESP32 baudarte is 115200 etc. - - mySerial.begin(BAUDRATE); // Begin Stream with MHZ19 baudrate - - //mySerial.begin(BAUDRATE, SERIAL_8N1, RX_PIN, TX_PIN); // ESP32 Example - - myMHZ19.begin(mySerial); // *Important, Pass your Stream reference - - myMHZ19.autoCalibration(); // Turn ABC ON. Disable with autoCalibration(false) (also default) +void setup() { + serial1.begin(9600); + serial2.begin(9600); + modbus_configure(9600, 6, 6, TOTAL_REGS_SIZE, 0); //Modbus via MAX485 + runner.init(); + runner.addTask(t1); + t1.enable(); } -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) +void loop() { + runner.execute(); + holdingRegs[R00_Dust_10] = PM10; + holdingRegs[R01_Dust_25] = PM25; + holdingRegs[R02_Dust_100] = PM100; + holdingRegs[R03_CO2_PPM] = CO2; + holdingRegs[TOTAL_ERRORS] = modbus_update(holdingRegs); - Serial.print("CO2 (ppm): "); - Serial.println(CO2); + if (pms.read(data)) { + PM10 = (int)data.PM_AE_UG_1_0; + PM25 = (int)data.PM_AE_UG_2_5; + PM100 = (int)data.PM_AE_UG_10_0; + } +} - float Temp; // Buffer for temperature - Temp = myMHZ19.getTemperature(); // Request Temperature (as Celsius) - - Serial.print("Temperature (C): "); - Serial.println(Temp); - - getDataTimer = millis(); // Update interval - } +void sensor_co2() { + serial2.write(com,9); + serial2.readBytes(return1,9); + CO2 = return1[2]*256+return1[3]; } \ No newline at end of file diff --git a/test/main2.cpp b/test/main2.cpp deleted file mode 100644 index 31d176a..0000000 --- a/test/main2.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include -#include -#include -#include - -SoftwareSerial pms7003(11, 12); // RX, TX -SoftwareSerial mhz19(9, 10); // RX, TX -PMS pms(pms7003); -PMS::DATA data; -Scheduler runner; - -void sensor_pms(); -Task t1(5000, TASK_FOREVER, &sensor_pms); - -//////////////// MODBUS Registers /////////////////// -enum -{ - R00_Dust_10, - R01_Dust_25, - R02_Dust_100, - TOTAL_ERRORS, - TOTAL_REGS_SIZE -}; -unsigned int holdingRegs[TOTAL_REGS_SIZE]; // function 3 and 16 register array -int PM10; -int PM25; -int PM100; -////////////////////////////////////////////////////// - -void setup() { - //Serial.begin(57600); - //Serial.print("Starting up..."); - mhz19.begin(9600); - pms7003.begin(9600); - pinMode(8, OUTPUT); //MAX485 Tranmit Enable - modbus_configure(9600, 6, 8, TOTAL_REGS_SIZE, 0); //Modbus via MAX485 - runner.init(); - //runner.addTask(t1); - //t1.enable(); -} - -void loop() { - runner.execute(); - holdingRegs[R00_Dust_10] = PM10; - holdingRegs[R01_Dust_25] = PM25; - holdingRegs[R02_Dust_100] = PM100; - holdingRegs[TOTAL_ERRORS] = modbus_update(holdingRegs); - - if (pms.read(data)) { - /*Serial.print("PM 1.0 (ug/m3): "); - Serial.println(data.PM_AE_UG_1_0); - Serial.print("PM 2.5 (ug/m3): "); - Serial.println(data.PM_AE_UG_2_5); - Serial.print("PM 10.0 (ug/m3): "); - Serial.println(data.PM_AE_UG_10_0); - Serial.println(); - */ - PM10 = (int)data.PM_AE_UG_1_0; - PM25 = (int)data.PM_AE_UG_2_5; - PM100 = (int)data.PM_AE_UG_10_0; - } -} - -void sensor_pms() { - Serial.print("PM 1.0 (ug/m3): "); - Serial.println(PM10); - Serial.print("PM 2.5 (ug/m3): "); - Serial.println(PM25); - Serial.print("PM 10.0 (ug/m3): "); - Serial.println(PM100); - Serial.println(); -} \ No newline at end of file diff --git a/test/test_mhz19.cpp b/test/test_mhz19.cpp new file mode 100644 index 0000000..fb8557b --- /dev/null +++ b/test/test_mhz19.cpp @@ -0,0 +1,47 @@ +#include +#include "MHZ19.h" // include main library +#include // Remove if using HardwareSerial + +#define RX_PIN 4 // Rx pin which the MHZ19 Tx pin is attached to +#define TX_PIN 5 // Tx pin which the MHZ19 Rx pin is attached to +#define BAUDRATE 9600 // Native to the sensor (do not change) + +MHZ19 myMHZ19; // Constructor for MH-Z19 class +SoftwareSerial mySerial(RX_PIN, TX_PIN); // Constructor for Stream class *change for HardwareSerial, i.e. ESP32 *** + +//HardwareSerial mySerial(1); // ESP32 Example + +unsigned long getDataTimer = 0; // Variable to store timer interval + +void setup() +{ + Serial.begin(57600); // For ESP32 baudarte is 115200 etc. + + mySerial.begin(BAUDRATE); // Begin Stream with MHZ19 baudrate + + //mySerial.begin(BAUDRATE, SERIAL_8N1, RX_PIN, TX_PIN); // ESP32 Example + + myMHZ19.begin(mySerial); // *Important, Pass your Stream reference + + myMHZ19.autoCalibration(); // Turn ABC ON. Disable with autoCalibration(false) (also default) +} + +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 + } +} \ No newline at end of file