-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ps1
More file actions
627 lines (566 loc) · 20.1 KB
/
setup.ps1
File metadata and controls
627 lines (566 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# ===== AIRVYN ESP32 Setup Script =====
New-Item -ItemType Directory -Force -Path "src\sensors","src\ble","src\alert" | Out-Null
# .gitignore
@'
.pio/
.vscode/
*.swp
*.swo
'@ | Set-Content -Encoding UTF8 ".gitignore"
# platformio.ini
@'
[platformio]
default_envs = premium
[env:base]
platform = espressif32
framework = arduino
board = esp32dev
monitor_speed = 115200
build_flags =
-DARDUINO_ARCH_ESP32
lib_deps =
h2zero/NimBLE-Arduino@^1.4.2
sensirion/Sensirion I2C SCD4x@^0.4.0
sensirion/Sensirion I2C SGP40@^0.1.0
sensirion/Sensirion Gas Index Algorithm@^3.2.1
[env:budget]
extends = env:base
build_flags =
${env:base.build_flags}
-DPRODUCT_TIER=1
[env:standard]
extends = env:base
build_flags =
${env:base.build_flags}
-DPRODUCT_TIER=2
[env:premium]
extends = env:base
build_flags =
${env:base.build_flags}
-DPRODUCT_TIER=3
'@ | Set-Content -Encoding UTF8 "platformio.ini"
# src/config.h
@'
#pragma once
#define TIER_BUDGET 1
#define TIER_STANDARD 2
#define TIER_PREMIUM 3
#ifndef PRODUCT_TIER
#define PRODUCT_TIER TIER_PREMIUM
#endif
#define SENSOR_CO2_ENABLED 1
#if PRODUCT_TIER >= TIER_STANDARD
#define SENSOR_PM_ENABLED 1
#else
#define SENSOR_PM_ENABLED 0
#endif
#if PRODUCT_TIER >= TIER_PREMIUM
#define SENSOR_VOC_ENABLED 1
#else
#define SENSOR_VOC_ENABLED 0
#endif
#define PIN_I2C_SDA 21
#define PIN_I2C_SCL 22
#define PIN_PM_RX 16
#define PIN_PM_TX 17
#define PM_UART_BAUD 9600
#define PIN_STATUS_LED 2
#define BLE_DEVICE_NAME "AIRVYN"
#define SENSOR_READ_INTERVAL_MS 5000
#define LED_BLINK_NORMAL_MS 3000
#define LED_BLINK_WARNING_MS 1000
#define LED_BLINK_CRITICAL_MS 300
#define CO2_WARNING_PPM 1000
#define CO2_CRITICAL_PPM 2000
#define PM25_WARNING_UGM3 15
#define PM25_CRITICAL_UGM3 35
#define PM10_WARNING_UGM3 45
#define PM10_CRITICAL_UGM3 75
#define VOC_INDEX_WARNING 150
#define VOC_INDEX_CRITICAL 300
'@ | Set-Content -Encoding UTF8 "src\config.h"
# src/AirQualityData.h
@'
#pragma once
#include <cstdint>
enum class AlertLevel : uint8_t { NORMAL=0, WARNING=1, CRITICAL=2 };
enum AlertSource : uint8_t { ALERT_SRC_NONE=0x00, ALERT_SRC_CO2=0x01, ALERT_SRC_PM=0x02, ALERT_SRC_VOC=0x04 };
enum SensorValid : uint8_t { VALID_NONE=0x00, VALID_CO2=0x01, VALID_PM=0x02, VALID_VOC=0x04 };
struct AirQualityData {
uint16_t co2_ppm = 0;
float temperature = 0.0f;
float humidity = 0.0f;
uint16_t pm1_0 = 0;
uint16_t pm2_5 = 0;
uint16_t pm10 = 0;
int32_t voc_index = 0;
uint8_t valid_sensors = VALID_NONE;
AlertLevel alert_level = AlertLevel::NORMAL;
uint8_t alert_sources = ALERT_SRC_NONE;
bool isCO2Valid() const { return valid_sensors & VALID_CO2; }
bool isPMValid() const { return valid_sensors & VALID_PM; }
bool isVOCValid() const { return valid_sensors & VALID_VOC; }
static constexpr size_t PACKET_SIZE = 19;
void toPacket(uint8_t* buf) const {
buf[0] = co2_ppm & 0xFF; buf[1] = (co2_ppm>>8) & 0xFF;
buf[2] = pm1_0 & 0xFF; buf[3] = (pm1_0>>8) & 0xFF;
buf[4] = pm2_5 & 0xFF; buf[5] = (pm2_5>>8) & 0xFF;
buf[6] = pm10 & 0xFF; buf[7] = (pm10>>8) & 0xFF;
auto vi = static_cast<int32_t>(voc_index);
buf[8]=(vi)&0xFF; buf[9]=(vi>>8)&0xFF; buf[10]=(vi>>16)&0xFF; buf[11]=(vi>>24)&0xFF;
int16_t tx = static_cast<int16_t>(temperature*100.0f);
buf[12]=tx&0xFF; buf[13]=(tx>>8)&0xFF;
uint16_t hx = static_cast<uint16_t>(humidity*100.0f);
buf[14]=hx&0xFF; buf[15]=(hx>>8)&0xFF;
buf[16]=static_cast<uint8_t>(alert_level);
buf[17]=alert_sources; buf[18]=valid_sensors;
}
};
'@ | Set-Content -Encoding UTF8 "src\AirQualityData.h"
# src/sensors/SensorBase.h
@'
#pragma once
enum class SensorStatus { OK, NOT_READY, ERROR };
class SensorBase {
public:
virtual ~SensorBase() = default;
virtual bool begin() = 0;
virtual SensorStatus read() = 0;
bool isReady() const { return _ready; }
protected:
bool _ready = false;
};
'@ | Set-Content -Encoding UTF8 "src\sensors\SensorBase.h"
# src/sensors/CO2Sensor.h
@'
#pragma once
#include "SensorBase.h"
#include <SensirionI2CScd4x.h>
struct CO2Data { uint16_t co2_ppm; float temperature; float humidity; };
class CO2Sensor : public SensorBase {
public:
bool begin() override;
SensorStatus read() override;
const CO2Data& getData() const { return _data; }
private:
SensirionI2CScd4x _scd4x;
CO2Data _data = {};
};
'@ | Set-Content -Encoding UTF8 "src\sensors\CO2Sensor.h"
# src/sensors/CO2Sensor.cpp
@'
#include "CO2Sensor.h"
#include <Arduino.h>
#include <Wire.h>
bool CO2Sensor::begin() {
_scd4x.begin(Wire);
_scd4x.stopPeriodicMeasurement();
delay(500);
uint16_t error = _scd4x.startPeriodicMeasurement();
if (error) { Serial.printf("[CO2] Start failed (err=%d)\n", error); return false; }
_ready = true;
Serial.println("[CO2] SCD40 ready");
return true;
}
SensorStatus CO2Sensor::read() {
if (!_ready) return SensorStatus::ERROR;
bool dataReady = false;
uint16_t error = _scd4x.getDataReadyFlag(dataReady);
if (error || !dataReady) return SensorStatus::NOT_READY;
uint16_t co2; float temp, hum;
error = _scd4x.readMeasurement(co2, temp, hum);
if (error) { Serial.printf("[CO2] Read error (err=%d)\n", error); return SensorStatus::ERROR; }
_data.co2_ppm = co2; _data.temperature = temp; _data.humidity = hum;
Serial.printf("[CO2] %d ppm | %.1f C | %.1f%%RH\n", co2, temp, hum);
return SensorStatus::OK;
}
'@ | Set-Content -Encoding UTF8 "src\sensors\CO2Sensor.cpp"
# src/sensors/PMSensor.h
@'
#pragma once
#include "SensorBase.h"
#include <HardwareSerial.h>
struct PMData { uint16_t pm1_0; uint16_t pm2_5; uint16_t pm10; };
class PMSensor : public SensorBase {
public:
PMSensor(HardwareSerial& serial, int rxPin, int txPin);
bool begin() override;
SensorStatus read() override;
void poll();
const PMData& getData() const { return _data; }
private:
HardwareSerial& _serial;
int _rxPin, _txPin;
PMData _data = {};
static constexpr size_t FRAME_LEN = 32;
uint8_t _buf[FRAME_LEN];
bool parseFrame();
};
'@ | Set-Content -Encoding UTF8 "src\sensors\PMSensor.h"
# src/sensors/PMSensor.cpp
@'
#include "PMSensor.h"
#include <Arduino.h>
#include "../config.h"
PMSensor::PMSensor(HardwareSerial& serial, int rxPin, int txPin)
: _serial(serial), _rxPin(rxPin), _txPin(txPin) {}
bool PMSensor::begin() {
_serial.begin(PM_UART_BAUD, SERIAL_8N1, _rxPin, _txPin);
delay(100); _ready = true;
Serial.println("[PM] PMS5003 ready");
return true;
}
void PMSensor::poll() {
if (!_ready) return;
while (_serial.available() >= static_cast<int>(FRAME_LEN)) {
if (_serial.peek() != 0x42) { _serial.read(); continue; }
_serial.readBytes(_buf, FRAME_LEN);
if (_buf[1] == 0x4D) parseFrame();
}
}
SensorStatus PMSensor::read() {
if (!_ready) return SensorStatus::ERROR;
return (_data.pm2_5 > 0 || _data.pm10 > 0) ? SensorStatus::OK : SensorStatus::NOT_READY;
}
bool PMSensor::parseFrame() {
uint16_t checksum = 0;
for (size_t i = 0; i < FRAME_LEN - 2; i++) checksum += _buf[i];
uint16_t frame_crc = (static_cast<uint16_t>(_buf[30]) << 8) | _buf[31];
if (checksum != frame_crc) { Serial.println("[PM] Checksum error"); return false; }
_data.pm1_0 = (static_cast<uint16_t>(_buf[10]) << 8) | _buf[11];
_data.pm2_5 = (static_cast<uint16_t>(_buf[12]) << 8) | _buf[13];
_data.pm10 = (static_cast<uint16_t>(_buf[14]) << 8) | _buf[15];
Serial.printf("[PM] PM1.0=%d | PM2.5=%d | PM10=%d\n", _data.pm1_0, _data.pm2_5, _data.pm10);
return true;
}
'@ | Set-Content -Encoding UTF8 "src\sensors\PMSensor.cpp"
# src/sensors/VOCSensor.h
@'
#pragma once
#include "SensorBase.h"
#include <SensirionI2CSgp40.h>
#include <VOCGasIndexAlgorithm.h>
struct VOCData { int32_t voc_index; uint16_t raw_signal; };
class VOCSensor : public SensorBase {
public:
bool begin() override;
SensorStatus read() override;
void setCompensation(float humidity_rh, float temperature_c);
const VOCData& getData() const { return _data; }
private:
SensirionI2CSgp40 _sgp40;
VOCGasIndexAlgorithm _vocAlgo;
VOCData _data = {};
uint16_t _comp_rh = 0x8000;
uint16_t _comp_t = 0x6666;
};
'@ | Set-Content -Encoding UTF8 "src\sensors\VOCSensor.h"
# src/sensors/VOCSensor.cpp
@'
#include "VOCSensor.h"
#include <Arduino.h>
#include <Wire.h>
bool VOCSensor::begin() {
_sgp40.begin(Wire);
uint16_t testResult;
uint16_t error = _sgp40.executeSelfTest(testResult);
if (error) { Serial.printf("[VOC] Self-test error (err=%d)\n", error); return false; }
if (testResult != 0xD400) { Serial.printf("[VOC] Self-test failed (0x%04X)\n", testResult); return false; }
_ready = true;
Serial.println("[VOC] SGP40 ready");
return true;
}
void VOCSensor::setCompensation(float humidity_rh, float temperature_c) {
_comp_rh = static_cast<uint16_t>((humidity_rh * 65535.0f) / 100.0f);
_comp_t = static_cast<uint16_t>(((temperature_c + 45.0f) * 65535.0f) / 175.0f);
}
SensorStatus VOCSensor::read() {
if (!_ready) return SensorStatus::ERROR;
uint16_t sraw;
uint16_t error = _sgp40.measureRawSignal(_comp_rh, _comp_t, sraw);
if (error) { Serial.printf("[VOC] Read error (err=%d)\n", error); return SensorStatus::ERROR; }
_data.raw_signal = sraw;
_data.voc_index = static_cast<int32_t>(_vocAlgo.process(sraw));
Serial.printf("[VOC] Index=%d (raw=%d)\n", _data.voc_index, sraw);
return SensorStatus::OK;
}
'@ | Set-Content -Encoding UTF8 "src\sensors\VOCSensor.cpp"
# src/ble/BLEManager.h
@'
#pragma once
#include <NimBLEDevice.h>
#include "../AirQualityData.h"
#define BLE_SVC_UUID "4ACED100-C801-4C71-8B0E-79C4D7B37B79"
#define BLE_CHAR_PACKET "4ACED101-C801-4C71-8B0E-79C4D7B37B79"
#define BLE_CHAR_ALERT "4ACED102-C801-4C71-8B0E-79C4D7B37B79"
#define BLE_CHAR_CO2 "4ACED103-C801-4C71-8B0E-79C4D7B37B79"
#define BLE_CHAR_PM25 "4ACED104-C801-4C71-8B0E-79C4D7B37B79"
#define BLE_CHAR_VOC "4ACED105-C801-4C71-8B0E-79C4D7B37B79"
#define BLE_CHAR_TEMP "4ACED106-C801-4C71-8B0E-79C4D7B37B79"
#define BLE_CHAR_HUMIDITY "4ACED107-C801-4C71-8B0E-79C4D7B37B79"
class BLEManager : public NimBLEServerCallbacks {
public:
bool begin(const char* deviceName);
void update(const AirQualityData& data);
bool isConnected() const { return _connected; }
void onConnect(NimBLEServer* server) override;
void onDisconnect(NimBLEServer* server) override;
private:
NimBLEServer* _server = nullptr;
NimBLECharacteristic* _charPacket = nullptr;
NimBLECharacteristic* _charAlert = nullptr;
NimBLECharacteristic* _charCO2 = nullptr;
NimBLECharacteristic* _charPM25 = nullptr;
NimBLECharacteristic* _charVOC = nullptr;
NimBLECharacteristic* _charTemp = nullptr;
NimBLECharacteristic* _charHumidity = nullptr;
bool _connected = false;
void setupCharacteristics(NimBLEService* svc);
};
'@ | Set-Content -Encoding UTF8 "src\ble\BLEManager.h"
# src/ble/BLEManager.cpp
@'
#include "BLEManager.h"
#include <Arduino.h>
#include "esp_mac.h"
bool BLEManager::begin(const char* deviceName) {
NimBLEDevice::init(deviceName);
NimBLEDevice::setPower(ESP_PWR_LVL_P9);
_server = NimBLEDevice::createServer();
_server->setCallbacks(this);
_server->advertiseOnDisconnect(false);
NimBLEService* svc = _server->createService(BLE_SVC_UUID);
setupCharacteristics(svc);
svc->start();
NimBLEAdvertising* adv = NimBLEDevice::getAdvertising();
adv->addServiceUUID(BLE_SVC_UUID);
adv->setScanResponse(true);
adv->setMinPreferred(0x06);
adv->start();
Serial.printf("[BLE] Advertising as \"%s\"\n", deviceName);
return true;
}
void BLEManager::setupCharacteristics(NimBLEService* svc) {
constexpr uint32_t RN = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY;
_charPacket = svc->createCharacteristic(BLE_CHAR_PACKET, RN);
_charAlert = svc->createCharacteristic(BLE_CHAR_ALERT, RN);
_charCO2 = svc->createCharacteristic(BLE_CHAR_CO2, RN);
_charPM25 = svc->createCharacteristic(BLE_CHAR_PM25, RN);
_charVOC = svc->createCharacteristic(BLE_CHAR_VOC, RN);
_charTemp = svc->createCharacteristic(BLE_CHAR_TEMP, RN);
_charHumidity = svc->createCharacteristic(BLE_CHAR_HUMIDITY, RN);
}
void BLEManager::update(const AirQualityData& data) {
uint8_t packet[AirQualityData::PACKET_SIZE];
data.toPacket(packet);
_charPacket->setValue(packet, AirQualityData::PACKET_SIZE);
_charPacket->notify();
uint8_t alertByte = static_cast<uint8_t>(data.alert_level);
_charAlert->setValue(&alertByte, 1);
_charAlert->notify();
if (data.isCO2Valid()) {
_charCO2->setValue(data.co2_ppm); _charCO2->notify();
int16_t tx = static_cast<int16_t>(data.temperature * 100.0f);
_charTemp->setValue(reinterpret_cast<uint8_t*>(&tx), 2); _charTemp->notify();
uint16_t hx = static_cast<uint16_t>(data.humidity * 100.0f);
_charHumidity->setValue(reinterpret_cast<uint8_t*>(&hx), 2); _charHumidity->notify();
}
if (data.isPMValid()) { _charPM25->setValue(data.pm2_5); _charPM25->notify(); }
if (data.isVOCValid()) {
int32_t vi = data.voc_index;
_charVOC->setValue(reinterpret_cast<uint8_t*>(&vi), 4); _charVOC->notify();
}
}
void BLEManager::onConnect(NimBLEServer* server) { _connected = true; Serial.println("[BLE] Client connected"); }
void BLEManager::onDisconnect(NimBLEServer* server) {
_connected = false;
Serial.println("[BLE] Disconnected - restarting advertising");
NimBLEDevice::startAdvertising();
}
'@ | Set-Content -Encoding UTF8 "src\ble\BLEManager.cpp"
# src/alert/AlertManager.h
@'
#pragma once
#include "../AirQualityData.h"
class AlertManager {
public:
void evaluate(AirQualityData& data);
private:
AlertLevel evalCO2(uint16_t ppm);
AlertLevel evalPM25(uint16_t pm25);
AlertLevel evalVOC(int32_t voc_index);
void logAlert(const AirQualityData& data);
};
'@ | Set-Content -Encoding UTF8 "src\alert\AlertManager.h"
# src/alert/AlertManager.cpp
@'
#include "AlertManager.h"
#include "../config.h"
#include <Arduino.h>
void AlertManager::evaluate(AirQualityData& data) {
AlertLevel maxLevel = AlertLevel::NORMAL;
uint8_t sources = ALERT_SRC_NONE;
if (data.isCO2Valid()) {
AlertLevel lvl = evalCO2(data.co2_ppm);
if (lvl > maxLevel) maxLevel = lvl;
if (lvl != AlertLevel::NORMAL) sources |= ALERT_SRC_CO2;
}
#if SENSOR_PM_ENABLED
if (data.isPMValid()) {
AlertLevel lvl = evalPM25(data.pm2_5);
if (lvl > maxLevel) maxLevel = lvl;
if (lvl != AlertLevel::NORMAL) sources |= ALERT_SRC_PM;
}
#endif
#if SENSOR_VOC_ENABLED
if (data.isVOCValid()) {
AlertLevel lvl = evalVOC(data.voc_index);
if (lvl > maxLevel) maxLevel = lvl;
if (lvl != AlertLevel::NORMAL) sources |= ALERT_SRC_VOC;
}
#endif
data.alert_level = maxLevel;
data.alert_sources = sources;
if (maxLevel != AlertLevel::NORMAL) logAlert(data);
}
AlertLevel AlertManager::evalCO2(uint16_t ppm) {
if (ppm >= CO2_CRITICAL_PPM) return AlertLevel::CRITICAL;
if (ppm >= CO2_WARNING_PPM) return AlertLevel::WARNING;
return AlertLevel::NORMAL;
}
AlertLevel AlertManager::evalPM25(uint16_t pm25) {
if (pm25 >= PM25_CRITICAL_UGM3) return AlertLevel::CRITICAL;
if (pm25 >= PM25_WARNING_UGM3) return AlertLevel::WARNING;
return AlertLevel::NORMAL;
}
AlertLevel AlertManager::evalVOC(int32_t voc_index) {
if (voc_index >= VOC_INDEX_CRITICAL) return AlertLevel::CRITICAL;
if (voc_index >= VOC_INDEX_WARNING) return AlertLevel::WARNING;
return AlertLevel::NORMAL;
}
void AlertManager::logAlert(const AirQualityData& data) {
const char* lvl = (data.alert_level == AlertLevel::CRITICAL) ? "CRITICAL" : "WARNING";
Serial.printf("[ALERT] %s | sources=0x%02X\n", lvl, data.alert_sources);
if (data.alert_sources & ALERT_SRC_CO2) {
if (data.alert_level == AlertLevel::CRITICAL)
Serial.printf(" >> CO2 %d ppm -- 졸음운전 위험! 창문을 여세요.\n", data.co2_ppm);
else
Serial.printf(" >> CO2 %d ppm -- 환기 필요\n", data.co2_ppm);
}
if (data.alert_sources & ALERT_SRC_PM)
Serial.printf(" >> PM2.5 %d ug/m3 -- 미세먼지 주의\n", data.pm2_5);
if (data.alert_sources & ALERT_SRC_VOC)
Serial.printf(" >> VOC Index %d -- 유해가스 주의\n", data.voc_index);
}
'@ | Set-Content -Encoding UTF8 "src\alert\AlertManager.cpp"
# src/main.cpp
@'
#include <Arduino.h>
#include <Wire.h>
#include "esp_mac.h"
#include "config.h"
#include "AirQualityData.h"
#include "sensors/CO2Sensor.h"
#include "ble/BLEManager.h"
#include "alert/AlertManager.h"
#if SENSOR_PM_ENABLED
#include "sensors/PMSensor.h"
#endif
#if SENSOR_VOC_ENABLED
#include "sensors/VOCSensor.h"
#endif
static AirQualityData gData;
static CO2Sensor co2Sensor;
static BLEManager bleManager;
static AlertManager alertManager;
#if SENSOR_PM_ENABLED
static PMSensor pmSensor(Serial2, PIN_PM_RX, PIN_PM_TX);
#endif
#if SENSOR_VOC_ENABLED
static VOCSensor vocSensor;
#endif
static uint32_t lastSensorReadMs = 0;
static uint32_t lastLedToggleMs = 0;
static bool ledState = false;
static void initLED() { pinMode(PIN_STATUS_LED, OUTPUT); digitalWrite(PIN_STATUS_LED, LOW); }
static void updateLED() {
uint32_t now = millis();
uint32_t period = LED_BLINK_NORMAL_MS;
if (gData.alert_level == AlertLevel::WARNING) period = LED_BLINK_WARNING_MS;
if (gData.alert_level == AlertLevel::CRITICAL) period = LED_BLINK_CRITICAL_MS;
if (now - lastLedToggleMs >= period) {
lastLedToggleMs = now;
ledState = !ledState;
digitalWrite(PIN_STATUS_LED, ledState ? HIGH : LOW);
}
}
static String buildDeviceName() {
uint8_t mac[6];
esp_read_mac(mac, ESP_MAC_BT);
char suffix[8];
snprintf(suffix, sizeof(suffix), "-%02X%02X", mac[4], mac[5]);
return String(BLE_DEVICE_NAME) + suffix;
}
static void readSensors() {
if (co2Sensor.read() == SensorStatus::OK) {
const CO2Data& d = co2Sensor.getData();
gData.co2_ppm = d.co2_ppm; gData.temperature = d.temperature; gData.humidity = d.humidity;
gData.valid_sensors |= VALID_CO2;
#if SENSOR_VOC_ENABLED
vocSensor.setCompensation(d.humidity, d.temperature);
#endif
}
#if SENSOR_PM_ENABLED
if (pmSensor.read() == SensorStatus::OK) {
const PMData& d = pmSensor.getData();
gData.pm1_0 = d.pm1_0; gData.pm2_5 = d.pm2_5; gData.pm10 = d.pm10;
gData.valid_sensors |= VALID_PM;
}
#endif
#if SENSOR_VOC_ENABLED
if (vocSensor.read() == SensorStatus::OK) {
gData.voc_index = vocSensor.getData().voc_index;
gData.valid_sensors |= VALID_VOC;
}
#endif
}
void setup() {
Serial.begin(115200); delay(200);
Serial.println("========================================");
Serial.println(" AIRVYN Car Air Quality Monitor");
Serial.printf (" Tier: %d (%s)\n", PRODUCT_TIER,
PRODUCT_TIER==1?"Budget":PRODUCT_TIER==2?"Standard":"Premium");
Serial.println("========================================");
initLED();
Wire.begin(PIN_I2C_SDA, PIN_I2C_SCL);
if (!co2Sensor.begin()) Serial.println("[WARN] CO2 sensor not found");
#if SENSOR_PM_ENABLED
pmSensor.begin();
#endif
#if SENSOR_VOC_ENABLED
if (!vocSensor.begin()) Serial.println("[WARN] VOC sensor not found");
#endif
String name = buildDeviceName();
bleManager.begin(name.c_str());
Serial.println("[MAIN] Setup complete\n");
}
void loop() {
uint32_t now = millis();
#if SENSOR_PM_ENABLED
pmSensor.poll();
#endif
if (now - lastSensorReadMs >= SENSOR_READ_INTERVAL_MS) {
lastSensorReadMs = now;
readSensors();
alertManager.evaluate(gData);
if (bleManager.isConnected()) bleManager.update(gData);
}
updateLED();
}
'@ | Set-Content -Encoding UTF8 "src\main.cpp"
Write-Host "✓ 모든 파일 생성 완료"
Write-Host ""
Write-Host "이제 다음 명령어를 실행하세요:"
Write-Host " git add ."
Write-Host " git commit -m 'feat: initial AIRVYN ESP32 car air quality monitor'"
Write-Host " git push -u origin claude/car-air-quality-monitor-Lvu5d"