Skip to content

Commit

Permalink
Merge pull request #126 from kike-canaries/fix_issue_125_uart
Browse files Browse the repository at this point in the history
Fix issue 125 uart
  • Loading branch information
hpsaturn authored Jan 28, 2022
2 parents 43e3c1c + 29851c7 commit 3519a01
Show file tree
Hide file tree
Showing 10 changed files with 450 additions and 92 deletions.
2 changes: 1 addition & 1 deletion doxygen.conf
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PROJECT_NAME = "CanAirIO Sensors Library"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = 0.5.0
PROJECT_NUMBER = 0.5.1

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
2 changes: 0 additions & 2 deletions examples/co2_cm1106/co2_cm1106_altitude_comp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ void setup() {

sensors.init(sensors.CM1106); // forced UART sensor. (empty for auto detection)

Serial.println("-->[SETUP] Sensor configured: " + sensors.getMainDeviceSelected());

delay(500);
}

Expand Down
3 changes: 2 additions & 1 deletion examples/m5atom/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ upload_speed = 1500000
monitor_speed = 115200
build_flags =
-D CORE_DEBUG_LEVEL=0
-D M5ATOM
lib_deps =
fastled/FastLED@^3.5.0
m5stack/M5Atom@^0.0.7
hpsaturn/CanAirIO Air Quality Sensors Library @ 0.5.0
hpsaturn/CanAirIO Air Quality Sensors Library @ ^0.5.0
4 changes: 1 addition & 3 deletions examples/m5atom/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,14 @@ void setup() {
delay(200);
Serial.println("\n== Sensor test setup ==\n");
M5.begin(true,true,true); //Init M5Atom(Initialization of external I2C is also included). M5Atom(初始化外部I2C也包含在内)
//Wire.begin(21, 22); //Detect internal I2C, if this sentence is not added, it will detect external I2C. 检测内部I2C,若不加此句为检测外部I2C
Wire.begin(26,32);

Serial.println("-->[SETUP] Detecting sensors..");

sensors.setSampleTime(5); // config sensors sample time interval
sensors.setOnDataCallBack(&onSensorDataOk); // all data read callback
sensors.setOnErrorCallBack(&onSensorDataError); // [optional] error callback
sensors.setDebugMode(true); // [optional] debug mode
sensors.detectI2COnly(true); // disable force to only i2c sensors
sensors.detectI2COnly(true); // force to only i2c sensors (disable for UART sensors)
sensors.init(); // Auto detection to UART and i2c sensors

// Alternatives only for UART sensors (TX/RX):
Expand Down
24 changes: 24 additions & 0 deletions examples/m5coreink/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:m5stack-core-esp32]
platform = espressif32
board = m5stack-core-esp32
monitor_speed = 115200
framework = arduino
monitor_filters = time
build_flags =
-D M5COREINK=1
-D CORE_DEBUG_LEVEL=0

lib_deps =
hpsaturn/CanAirIO Air Quality Sensors Library @ ^0.5.0
https://github.com/ZinggJM/GxEPD2.git
https://github.com/hpsaturn/M5-CoreInk.git#fix_inkEnable_issue
278 changes: 278 additions & 0 deletions examples/m5coreink/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
/***********************************************************************************
* CanAirIO M5CoreInk
* @author @hpsaturn
*
* This project using CanAirIO Sensors Library. You can find the library here:
* https://github.com/kike-canaries/canairio_sensorlib
*
* The source code, documentation and the last version of this project here:
* https://github.com/hpsaturn/co2_m5coreink
*
* Tested with:
*
* - One SCD30 (C02 sensor)
* - One GCJA5 (Particulate Matter sensor)
* - ENVII M5 Hat
*
* But you can use it with any other i2c sensors, for example SPS30 or SCD41
* UART sensors right nos is untested.
*
***********************************************************************************/


#include <Arduino.h>
#include <M5CoreInk.h>
#include <Sensors.hpp>
#include <StreamString.h>
#include <rom/rtc.h>

#define DEEP_SLEEP_MODE 1 // eInk and esp32 hibernate
#define DEEP_SLEEP_TIME 20 // Please change it to 600s (10m) or more
#define SAMPLES_COUNT 8 // samples before suspend (for PM2.5 ~9, 18sec, or more)
#define LOOP_DELAY 2 // seconds
#define BEEP_ENABLE 1 // eneble high level alarm
#define PM25_ALARM_BEEP 50 // PM2.5 level to trigger alarm
#define CO2_ALARM_BEEP 2000 // CO2 ppm level to trigger alarm
#define DISABLE_LED // improve battery

#define ENABLE_GxEPD2_GFX 0

#include <GxEPD2_BW.h>
#include <GxEPD2_3C.h>
#include <GxEPD2_7C.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include <Fonts/FreeMonoBold18pt7b.h>
#include <Fonts/FreeMonoBold12pt7b.h>
#include <Fonts/FreeMonoBold24pt7b.h>
#include "bitmaps/Bitmaps200x200.h" // 1.54" b/w

GxEPD2_154_M09 medp = GxEPD2_154_M09(/*CS=D8*/ 9, /*DC=D3*/ 15, /*RST=D4*/ 0, /*BUSY=D2*/ 4);
GxEPD2_BW<GxEPD2_154_M09, GxEPD2_154_M09::HEIGHT> display(medp); // GDEH0154D67

UNIT mainUnit = UNIT::NUNIT;
UNIT minorUnit = UNIT::NUNIT;
UNIT tempUnit = UNIT::NUNIT;
UNIT humiUnit = UNIT::NUNIT;
UNIT otherUnit = UNIT::NUNIT;

uint16_t count;
bool drawReady;
bool isCharging;

void displayHomeCallback(const void*) {
uint16_t x = 15;
uint16_t y = display.height() / 2 - 30;
display.fillScreen(GxEPD_WHITE);
display.setCursor(x, y);
display.print("0000");
}

void displayHome() {
display.setRotation(0);
display.setFont(&FreeMonoBold18pt7b);
display.setTextSize(2);
display.setTextColor(GxEPD_BLACK);
display.setFullWindow();
display.drawPaged(displayHomeCallback, 0);
}

void displayCO2ValuesCallback(const void*) {
uint16_t x = 15;
uint16_t y = display.height() / 2 - 30;
display.fillScreen(GxEPD_WHITE);
display.setTextSize(2);
display.setCursor(x, y);
display.setFont(&FreeMonoBold18pt7b);
display.printf("%04i", (uint16_t)sensors.getUnitValue(mainUnit));

display.setFont(&FreeMonoBold9pt7b);
String mainUnitSymbol = sensors.getUnitName(mainUnit)+" / "+sensors.getUnitSymbol(mainUnit);
uint16_t lenght = mainUnitSymbol.length();
x = (display.width() / 2) - ((lenght*11)/2);
y = display.height() / 2 - 8;
display.setTextSize(0);
display.setCursor(x, y);
display.print(mainUnitSymbol);

display.setFont(&FreeMonoBold12pt7b);
display.setTextSize(1);

x = 11;

y = display.height() / 2 + 25;
display.setCursor(x, y);
String minorName = sensors.getUnitName(minorUnit);
display.printf("%5s: %04d", minorName.c_str(), (uint16_t)sensors.getUnitValue(minorUnit));

y = display.height() / 2 + 45;
display.setCursor(x, y);
display.printf(" Temp: %0.1fC",sensors.getUnitValue(tempUnit));

y = display.height() / 2 + 65;
display.setCursor(x, y);
display.printf(" Humi: %0.1f%%",sensors.getUnitValue(humiUnit));

y = display.height() / 2 + 85;
display.setCursor(x, y);
String oUnit = sensors.getUnitName(otherUnit);
display.printf("%5s: %04.1f", oUnit.c_str(),sensors.getUnitValue(otherUnit));

delay(100);

drawReady = true;
Serial.println("done");
}

/**
* Display CO2 values in partialMode update.
*
* it is a partial refresh mode can be used to full screen,
* effective if display panel hasFastPartialUpdate
*/
void displayValuesPartialMode() {
Serial.println("-->[eINK] displayValuesPartialMode");
Serial.print("-->[eINK] drawing..");
drawReady = false;
display.setPartialWindow(0, 0, display.width(), display.height());
display.setRotation(0);
display.setFont(&FreeMonoBold24pt7b);
display.setTextSize(1);
display.setTextColor(GxEPD_BLACK);
display.drawPaged(displayCO2ValuesCallback, 0);
}

void resetVariables() {
mainUnit = UNIT::NUNIT;
minorUnit = UNIT::NUNIT;
tempUnit = UNIT::NUNIT;
humiUnit = UNIT::NUNIT;
otherUnit = UNIT::NUNIT;
sensors.resetUnitsRegister();
sensors.resetSensorsRegister();
sensors.resetAllVariables();
}

void getSensorsUnits() {
Serial.println("-->[MAIN] Preview sensor values:");
UNIT unit = sensors.getNextUnit();
while(unit != UNIT::NUNIT) {
if ((unit == UNIT::CO2 || unit == UNIT::PM25) && mainUnit == UNIT::NUNIT) {
mainUnit = unit;
} else if (unit == UNIT::CO2 && mainUnit == UNIT::PM25) {
minorUnit = mainUnit; // CO2 in indoors has more priority
mainUnit = unit; // and is shown in main unit field
} else if (unit == UNIT::PM10 && minorUnit == UNIT::NUNIT) {
minorUnit = unit;
}
if (unit == UNIT::TEMP || unit == UNIT::CO2TEMP) {
tempUnit = unit;
if (mainUnit == UNIT::NUNIT) mainUnit = unit;
}
if (unit == UNIT::HUM || unit == UNIT::CO2HUM) {
humiUnit = unit;
}
if (unit == UNIT::PRESS || mainUnit == UNIT::GAS || mainUnit == UNIT::ALT) {
otherUnit = unit;
}
if (minorUnit == UNIT::NUNIT && unit == UNIT::ALT) minorUnit = unit;
if (otherUnit == UNIT::NUNIT && unit == UNIT::CO2TEMP) otherUnit = unit;

String uName = sensors.getUnitName(unit);
float uValue = sensors.getUnitValue(unit);
String uSymb = sensors.getUnitSymbol(unit);
Serial.println("-->[MAIN] " + uName + " \t: " + String(uValue) + " " + uSymb);
unit = sensors.getNextUnit();
}
}

bool sensorsLoop() {
if (sensors.readAllSensors()) {
getSensorsUnits();
return true;
}
return false;
}

void sensorsInit() {
Serial.println("-->[SETUP] Detecting sensors..");
Wire.begin(32, 33); // I2C external port (bottom connector)
Wire1.begin(25, 26); // I2C via Hat (top connector)
sensors.setSampleTime(1); // config sensors sample time interval
sensors.setDebugMode(false); // [optional] debug mode
sensors.detectI2COnly(true); // force to only i2c sensors
sensors.init(); // Auto detection to UART and i2c sensors
}

void beep() {
M5.Speaker.tone(2000, 50);
delay(30);
M5.Speaker.mute();
}

void simulateDeepSleep() {
Serial.println("-->[MAIN] Simulate deep sleep");
sensors.init();
}

void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("-->[SETUP] setup init");

#ifdef DISABLE_LED // turnoff it for improve battery life
pinMode(LED_EXT_PIN, OUTPUT);
digitalWrite(LED_EXT_PIN, HIGH);
#endif

M5.begin(false, false, true);
sensorsInit();
display.init(115200,false);
int reset_reason = rtc_get_reset_reason(0);
// Serial.println("-->[MAIN] Reset reason \t: " + String(reset_reason));
M5.update();
// if (M5.BtnMID.isPressed() || reset_reason == 1) {
if (M5.BtnMID.isPressed()) {
displayHome();
sensorsLoop();
displayValuesPartialMode();
beep();
}

delay(100);
Serial.println("-->[SETUP] setup done");
}

void loop() {
if (sensorsLoop()) {
count++;
if (count >= SAMPLES_COUNT) {
displayValuesPartialMode();
uint16_t alarmValue = 0;
uint16_t mainValue = sensors.getUnitValue(mainUnit);
if (mainUnit == UNIT::PM25) alarmValue = PM25_ALARM_BEEP;
else alarmValue = CO2_ALARM_BEEP;
if(BEEP_ENABLE == 1 && mainValue > alarmValue ) beep();
count = 0;
}
}else{
resetVariables();
displayValuesPartialMode();
}

if (drawReady) {
resetVariables(); // only for demostration connection and reconnection sensors
if (DEEP_SLEEP_MODE == 1) {
Serial.println("-->[LOOP] Deep sleep..");
display.display(isCharging);
display.powerOff();
M5.shutdown(DEEP_SLEEP_TIME);
Serial.println("-->[LOOP] USB is connected..");
isCharging = true; // it only is reached when the USB is connected
simulateDeepSleep();
Serial.println("-->[LOOP] Deep sleep done.");
}
drawReady = false;
}

delay(LOOP_DELAY * 1000);
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "CanAirIO Air Quality Sensors Library",
"version": "0.5.0",
"version": "0.5.1",
"homepage":"https://canair.io",
"keywords":
[
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=CanAirIO Air Quality Sensors Library
version=0.5.0
version=0.5.1
author=@hpsaturn, CanAirIO project <[email protected]>
maintainer=Antonio Vanegas <[email protected]>
url=https://github.com/kike-canaries/canairio_sensorlib
Expand Down
Loading

0 comments on commit 3519a01

Please sign in to comment.