|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2021, Sensirion AG |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: BSD 3-Clause License |
| 4 | + |
| 5 | +/* |
| 6 | + * I2C-Generator: 0.3.0 |
| 7 | + * Yaml Version: 2.1.3 |
| 8 | + * Template Version: 0.7.0-112-g190ecaa |
| 9 | + */ |
| 10 | +/* |
| 11 | + * Copyright (c) 2021, Sensirion AG |
| 12 | + * All rights reserved. |
| 13 | + * |
| 14 | + * Redistribution and use in source and binary forms, with or without |
| 15 | + * modification, are permitted provided that the following conditions are met: |
| 16 | + * |
| 17 | + * * Redistributions of source code must retain the above copyright notice, this |
| 18 | + * list of conditions and the following disclaimer. |
| 19 | + * |
| 20 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 21 | + * this list of conditions and the following disclaimer in the documentation |
| 22 | + * and/or other materials provided with the distribution. |
| 23 | + * |
| 24 | + * * Neither the name of Sensirion AG nor the names of its |
| 25 | + * contributors may be used to endorse or promote products derived from |
| 26 | + * this software without specific prior written permission. |
| 27 | + * |
| 28 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 29 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 30 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 31 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 32 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 33 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 34 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 35 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 36 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 37 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 38 | + * POSSIBILITY OF SUCH DAMAGE. |
| 39 | + */ |
| 40 | + |
| 41 | +#include <Arduino.h> |
| 42 | +#include <SensirionI2CSen5x.h> |
| 43 | +#include <Wire.h> |
| 44 | + |
| 45 | +// The used commands use up to 48 bytes. On some Arduino's the default buffer |
| 46 | +// space is not large enough |
| 47 | +#define MAXBUF_REQUIREMENT 48 |
| 48 | + |
| 49 | +#if (defined(I2C_BUFFER_LENGTH) && \ |
| 50 | + (I2C_BUFFER_LENGTH >= MAXBUF_REQUIREMENT)) || \ |
| 51 | + (defined(BUFFER_LENGTH) && BUFFER_LENGTH >= MAXBUF_REQUIREMENT) |
| 52 | +#define USE_PRODUCT_INFO |
| 53 | +#endif |
| 54 | + |
| 55 | +SensirionI2CSen5x sen5x; |
| 56 | + |
| 57 | +void printModuleVersions() { |
| 58 | + uint16_t error; |
| 59 | + char errorMessage[256]; |
| 60 | + |
| 61 | + unsigned char productName[32]; |
| 62 | + uint8_t productNameSize = 32; |
| 63 | + |
| 64 | + error = sen5x.getProductName(productName, productNameSize); |
| 65 | + |
| 66 | + if (error) { |
| 67 | + Serial.print("Error trying to execute getProductName(): "); |
| 68 | + errorToString(error, errorMessage, 256); |
| 69 | + Serial.println(errorMessage); |
| 70 | + } else { |
| 71 | + Serial.print("ProductName:"); |
| 72 | + Serial.println((char*)productName); |
| 73 | + } |
| 74 | + |
| 75 | + uint8_t firmwareMajor; |
| 76 | + uint8_t firmwareMinor; |
| 77 | + bool firmwareDebug; |
| 78 | + uint8_t hardwareMajor; |
| 79 | + uint8_t hardwareMinor; |
| 80 | + uint8_t protocolMajor; |
| 81 | + uint8_t protocolMinor; |
| 82 | + |
| 83 | + error = sen5x.getVersion(firmwareMajor, firmwareMinor, firmwareDebug, |
| 84 | + hardwareMajor, hardwareMinor, protocolMajor, |
| 85 | + protocolMinor); |
| 86 | + if (error) { |
| 87 | + Serial.print("Error trying to execute getVersion(): "); |
| 88 | + errorToString(error, errorMessage, 256); |
| 89 | + Serial.println(errorMessage); |
| 90 | + } else { |
| 91 | + Serial.print("Firmware: "); |
| 92 | + Serial.print(firmwareMajor); |
| 93 | + Serial.print("."); |
| 94 | + Serial.print(firmwareMinor); |
| 95 | + Serial.print(", "); |
| 96 | + |
| 97 | + Serial.print("Hardware: "); |
| 98 | + Serial.print(hardwareMajor); |
| 99 | + Serial.print("."); |
| 100 | + Serial.println(hardwareMinor); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +void printSerialNumber() { |
| 105 | + uint16_t error; |
| 106 | + char errorMessage[256]; |
| 107 | + unsigned char serialNumber[32]; |
| 108 | + uint8_t serialNumberSize = 32; |
| 109 | + |
| 110 | + error = sen5x.getSerialNumber(serialNumber, serialNumberSize); |
| 111 | + if (error) { |
| 112 | + Serial.print("Error trying to execute getSerialNumber(): "); |
| 113 | + errorToString(error, errorMessage, 256); |
| 114 | + Serial.println(errorMessage); |
| 115 | + } else { |
| 116 | + Serial.print("SerialNumber:"); |
| 117 | + Serial.println((char*)serialNumber); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +void setup() { |
| 122 | + |
| 123 | + Serial.begin(115200); |
| 124 | + while (!Serial) { |
| 125 | + delay(100); |
| 126 | + } |
| 127 | + |
| 128 | + Wire.begin(); |
| 129 | + |
| 130 | + sen5x.begin(Wire); |
| 131 | + |
| 132 | + uint16_t error; |
| 133 | + char errorMessage[256]; |
| 134 | + error = sen5x.deviceReset(); |
| 135 | + if (error) { |
| 136 | + Serial.print("Error trying to execute deviceReset(): "); |
| 137 | + errorToString(error, errorMessage, 256); |
| 138 | + Serial.println(errorMessage); |
| 139 | + } |
| 140 | + |
| 141 | +// Print SEN55 module information if i2c buffers are large enough |
| 142 | +#ifdef USE_PRODUCT_INFO |
| 143 | + printSerialNumber(); |
| 144 | + printModuleVersions(); |
| 145 | +#endif |
| 146 | + |
| 147 | + // set a temperature offset in degrees celsius |
| 148 | + // Note: supported by SEN54 and SEN55 sensors |
| 149 | + // By default, the temperature and humidity outputs from the sensor |
| 150 | + // are compensated for the modules self-heating. If the module is |
| 151 | + // designed into a device, the temperature compensation might need |
| 152 | + // to be adapted to incorporate the change in thermal coupling and |
| 153 | + // self-heating of other device components. |
| 154 | + // |
| 155 | + // A guide to achieve optimal performance, including references |
| 156 | + // to mechanical design-in examples can be found in the app note |
| 157 | + // “SEN5x – Temperature Compensation Instruction” at www.sensirion.com. |
| 158 | + // Please refer to those application notes for further information |
| 159 | + // on the advanced compensation settings used |
| 160 | + // in `setTemperatureOffsetParameters`, `setWarmStartParameter` and |
| 161 | + // `setRhtAccelerationMode`. |
| 162 | + // |
| 163 | + // Adjust tempOffset to account for additional temperature offsets |
| 164 | + // exceeding the SEN module's self heating. |
| 165 | + float tempOffset = 0.0; |
| 166 | + error = sen5x.setTemperatureOffsetSimple(tempOffset); |
| 167 | + if (error) { |
| 168 | + Serial.print("Error trying to execute setTemperatureOffsetSimple(): "); |
| 169 | + errorToString(error, errorMessage, 256); |
| 170 | + Serial.println(errorMessage); |
| 171 | + } else { |
| 172 | + Serial.print("Temperature Offset set to "); |
| 173 | + Serial.print(tempOffset); |
| 174 | + Serial.println(" deg. Celsius (SEN54/SEN55 only"); |
| 175 | + } |
| 176 | + |
| 177 | + // Start Measurement |
| 178 | + error = sen5x.startMeasurement(); |
| 179 | + if (error) { |
| 180 | + Serial.print("Error trying to execute startMeasurement(): "); |
| 181 | + errorToString(error, errorMessage, 256); |
| 182 | + Serial.println(errorMessage); |
| 183 | + } |
| 184 | + |
| 185 | +} |
| 186 | + |
| 187 | +void loop() { |
| 188 | + uint16_t error; |
| 189 | + char errorMessage[256]; |
| 190 | + |
| 191 | + delay(1000); |
| 192 | + |
| 193 | + // Read Measurement |
| 194 | + float massConcentrationPm1p0; |
| 195 | + float massConcentrationPm2p5; |
| 196 | + float massConcentrationPm4p0; |
| 197 | + float massConcentrationPm10p0; |
| 198 | + float ambientHumidity; |
| 199 | + float ambientTemperature; |
| 200 | + float vocIndex; |
| 201 | + float noxIndex; |
| 202 | + |
| 203 | + error = sen5x.readMeasuredValues( |
| 204 | + massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, |
| 205 | + massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, |
| 206 | + noxIndex); |
| 207 | + |
| 208 | + if (error) { |
| 209 | + Serial.print("Error trying to execute readMeasuredValues(): "); |
| 210 | + errorToString(error, errorMessage, 256); |
| 211 | + Serial.println(errorMessage); |
| 212 | + } else { |
| 213 | + Serial.print("MassConcentrationPm1p0:"); |
| 214 | + Serial.print(massConcentrationPm1p0); |
| 215 | + Serial.println("\t"); |
| 216 | + Serial.print("MassConcentrationPm2p5:"); |
| 217 | + Serial.print(massConcentrationPm2p5); |
| 218 | + Serial.println("\t"); |
| 219 | + Serial.print("MassConcentrationPm4p0:"); |
| 220 | + Serial.print(massConcentrationPm4p0); |
| 221 | + Serial.println("\t"); |
| 222 | + Serial.print("MassConcentrationPm10p0:"); |
| 223 | + Serial.print(massConcentrationPm10p0); |
| 224 | + Serial.println("\t"); |
| 225 | + Serial.print("AmbientHumidity:"); |
| 226 | + if (isnan(ambientHumidity)) { |
| 227 | + Serial.print("n/a"); |
| 228 | + } else { |
| 229 | + Serial.print(ambientHumidity); |
| 230 | + } |
| 231 | + Serial.println("\t"); |
| 232 | + Serial.print("AmbientTemperature:"); |
| 233 | + if (isnan(ambientTemperature)) { |
| 234 | + Serial.print("n/a"); |
| 235 | + } else { |
| 236 | + Serial.print(ambientTemperature); |
| 237 | + } |
| 238 | + Serial.println("\t"); |
| 239 | + Serial.print("VocIndex:"); |
| 240 | + if (isnan(vocIndex)) { |
| 241 | + Serial.print("n/a"); |
| 242 | + } else { |
| 243 | + Serial.print(vocIndex); |
| 244 | + } |
| 245 | + Serial.println("\t"); |
| 246 | + Serial.print("NoxIndex:"); |
| 247 | + if (isnan(noxIndex)) { |
| 248 | + Serial.println("n/a"); |
| 249 | + } else { |
| 250 | + Serial.println(noxIndex); |
| 251 | + } |
| 252 | + Serial.println(); |
| 253 | + } |
| 254 | +} |
0 commit comments