|
| 1 | +/** |
| 2 | + * @file main.cpp |
| 3 | + * @author Antonio Vanegas @hpsaturn |
| 4 | + * @date June 2018 - 2024 |
| 5 | + * @brief CanAirIO M5AirQ test |
| 6 | + * @license GPL3 |
| 7 | + * |
| 8 | + * Full documentation: |
| 9 | + * https://github.com/kike-canaries/canairio_sensorlib#canairio-air-quality-sensors-library |
| 10 | + * |
| 11 | + * Full implementation for WiFi and Bluetooth Air Quality fixed and mobile station: |
| 12 | + * https://github.com/kike-canaries/canairio_firmware#canairio-firmware |
| 13 | + * |
| 14 | + * CanAirIO project documentation: |
| 15 | + * https://canair.io/docs |
| 16 | + */ |
| 17 | + |
| 18 | +#include <Arduino.h> |
| 19 | + |
| 20 | +#include <Sensors.hpp> |
| 21 | + |
| 22 | +#define POWER_HOLD 46 // M5AirQ main board |
| 23 | +#define SEN55_POWER_EN 10 |
| 24 | + |
| 25 | +#define GROVE_SDA 13 |
| 26 | +#define GROVE_SCL 15 |
| 27 | + |
| 28 | +#define I2C1_SDA_PIN 11 |
| 29 | +#define I2C1_SCL_PIN 12 |
| 30 | + |
| 31 | +void printSensorsDetected() { |
| 32 | + uint16_t sensors_count = sensors.getSensorsRegisteredCount(); |
| 33 | + uint16_t units_count = sensors.getUnitsRegisteredCount(); |
| 34 | + Serial.println("-->[MAIN] Sensors detected \t: " + String(sensors_count)); |
| 35 | + Serial.println("-->[MAIN] Sensors units count\t: " + String(units_count)); |
| 36 | + Serial.print("-->[MAIN] Sensors devices names\t: "); |
| 37 | + int i = 0; |
| 38 | + while (sensors.getSensorsRegistered()[i++] != 0) { |
| 39 | + Serial.print(sensors.getSensorName((SENSORS)sensors.getSensorsRegistered()[i - 1])); |
| 40 | + Serial.print(","); |
| 41 | + } |
| 42 | + Serial.println(); |
| 43 | +} |
| 44 | + |
| 45 | +void printSensorsValues() { |
| 46 | + Serial.println("-->[MAIN] Preview sensor values :"); |
| 47 | + UNIT unit = sensors.getNextUnit(); |
| 48 | + while (unit != UNIT::NUNIT) { |
| 49 | + String uName = sensors.getUnitName(unit); |
| 50 | + float uValue = sensors.getUnitValue(unit); |
| 51 | + String uSymb = sensors.getUnitSymbol(unit); |
| 52 | + Serial.printf("-->[MAIN] %6s:\t%02.1f\t%s\n", uName.c_str(), uValue, uSymb.c_str()); |
| 53 | + unit = sensors.getNextUnit(); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +void onSensorDataOk() { |
| 58 | + Serial.println("======= E X A M P L E T E S T ========="); |
| 59 | + printSensorsDetected(); |
| 60 | + printSensorsValues(); |
| 61 | +} |
| 62 | + |
| 63 | +void onSensorDataError(const char* msg) {} |
| 64 | +/****************************************************************************** |
| 65 | + * M A I N |
| 66 | + ******************************************************************************/ |
| 67 | + |
| 68 | +void powerEnableSensors() { |
| 69 | + Serial.println("-->[POWR] == enable sensors =="); |
| 70 | + pinMode(POWER_HOLD, OUTPUT); |
| 71 | + digitalWrite(POWER_HOLD, HIGH); |
| 72 | + pinMode(SEN55_POWER_EN, OUTPUT); |
| 73 | + digitalWrite(SEN55_POWER_EN, LOW); |
| 74 | +} |
| 75 | + |
| 76 | +void setup() { |
| 77 | + Serial.begin(115200); |
| 78 | + delay(2000); // Only for debugging |
| 79 | + powerEnableSensors(); // M5AirQ enable sensors |
| 80 | + |
| 81 | + delay(100); |
| 82 | + Serial.println("\n== Sensor test setup ==\n"); |
| 83 | + Serial.println("-->[SETUP] Detecting sensors.."); |
| 84 | + |
| 85 | + sensors.setSampleTime(10); // config sensors sample time interval |
| 86 | + sensors.setOnDataCallBack(&onSensorDataOk); // all data read callback |
| 87 | + sensors.setDebugMode(false); // [optional] debug mode |
| 88 | + sensors.detectI2COnly(true); // not force to only i2c sensors |
| 89 | + sensors.setTemperatureUnit(TEMPUNIT::CELSIUS); // comment for Celsius or set Fahrenheit |
| 90 | + sensors.init(); // Auto detection (UART and i2c sensors) |
| 91 | + delay(1000); |
| 92 | +} |
| 93 | + |
| 94 | +void loop() { |
| 95 | + sensors.loop(); // read sensor data and showed it |
| 96 | +} |
0 commit comments