This repository was archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproto2.ino
162 lines (140 loc) · 5.45 KB
/
proto2.ino
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
/*
air-quality-monitor (aka Ossigenio)
@file proto2.ino
@author Antonio Solida
@license GNU GPLv3
A monitor for air quality, made for IoT and 3D Intelligent Systems @ UniMORE AA 2022-2023.
Please do not divulgate.
v0.2: first version based on ESP32
*/
// start general include section
#include <Arduino.h>
#include <SPI.h>
#include <stdint.h>
#include <Wire.h>
// end general include section
// start BLE include section
#include "BleSerial.h"
BleSerial ble;
#define ledPin 32
// end BLE section
// start HS3001 include section (temperature and humidity sensor)
#include "HS300x.h"
using namespace HS300x;
cHS300x gHs300x {Wire};
// end HS3001 section
// start FEEDBACK include section
volatile uint8_t feedback = 0; //volatile because this variable is read by interrupt fuctions
volatile bool feed = false; //used to avoid too feedback consecutively
// end FEEDBACK include section
// start CCS811 include section (co2 sensor)
#include "SparkFunCCS811.h"
#define CCS811_ADDR 0x5A //I2C Address (is not hardcoded into library for compatibility purpose)
CCS811 mySensor(CCS811_ADDR);
// end CCS811 include section
// start ens210 include section (temp sensor on board of ccs811 chip; used to monitorate last one warming-up)
#include "ens210.h"
ENS210 ens210;
// end ens210 include section
// start serial parsing section
#include "serialProtocol.h"
// end serial parsing section
// start global variables section
#define campTime 1000
int autoSend = 10000; // autosends env data every 10 seconds
unsigned long lastExecutedMillis = 0; // to campionate environment datas every campTime msec
unsigned long lastExecutedMillisCount = 0; // to send automatically environment datas every 10 secs
float temperature;
float humidity;
float co2;
int raw; //seems not possible to read raw data from ccs811; used for ens210 temperature reading (ccs811 warming-up)
// end global variables section
boolean debug = false; //to enable debug mode
// funzione per il setup del ble
void ble_setup(){
//ble.begin("AirQualityMonitorEBV", true, ledPin); //put ble adv name here //DISABLE IN PRODUCTION
ble.begin("Ossigenio", true, ledPin); //ENABLE IN PRODUCTION ONLY
}
void setup() {
Wire.begin(17,16); //initialize i2c comm
mySensor.begin(); //initialize ccs811 sensor
gHs300x.begin(); //initialize gHs3001 sensor
ble_setup(); //initialize ble
touchAttachInterrupt(T0, positive, 40); // PIN 4 (on right side)
touchAttachInterrupt(T2, neutral, 40); // PIN 2 (on right side)
touchAttachInterrupt(T4, negative, 40); // PIN 13 (on left side)
// why i'm not using T1? Touch1 >> Not available on Devkit 30 pin version but available on Devkit 36 pin version
}
void loop() {
unsigned long currentMillis = millis();
cHS300x::Measurements m;
int t_data, t_status, h_data, h_status; //necessary for ens210
// it reads enviroment data from sensors every CampTime seconds
if (currentMillis - lastExecutedMillis >= campTime) {
lastExecutedMillis = currentMillis; // save the last executed time
// temperature and humidity section
if (gHs300x.getTemperatureHumidity(m)){
m.extract(temperature, humidity); // read temperature and humidity values
}
// co2 section (read also co2 sensor temperature for reliability purpose)
if (mySensor.dataAvailable()) {
mySensor.readAlgorithmResults();
co2 = (float) mySensor.getCO2(); // read co2 value
ens210.measure(&t_data, &t_status, &h_data, &h_status ); //see after comment
raw = (int) ens210.toCelsius(t_data,10)/10.0; //temperature of co2 sensor
}
feed = false; //re-enabling feedback disabled into isr
}
// feedback interrupt result management
if ((feedback == 1 || feedback == 2 || feedback == 3) && feed == false) {
getMsg4((int) temperature,(int) humidity,(int) co2, feedback);
feedback = 0;
}
// ble read section
uint8_t buf[4] = {0x00,0x00,0x00,0x00};
while (ble.available() > 0){
int rlen = ble.readBytes(buf, 4); //read 4 bytes from seruak
}
unsigned long currentMillisCount = millis();
//sends a message with sensor data over ble every autoSend seconds
if (currentMillisCount - lastExecutedMillisCount >= autoSend){
lastExecutedMillisCount = currentMillisCount;
getMsg1((int) temperature,(int) humidity,(int) co2);
}
/*
For other types of messages, proto2 will wait for external input and sends they
according to it.
*/
if(buf[0] == 0xAA && buf[2] == 0xFF){
switch(buf[1]){
case 0x1F: //AA1FFF MESSAGE
//environment temperature, humidity and co2 sensor temperature
getMsg0((int) temperature, (int) humidity, raw);
break;
case 0x1E: //AA1EFF MESSAGE
//environment temperature, humidity and co2
getMsg1((int) temperature,(int) humidity,(int) co2);
break;
case 0x1C: //AA1CFF MESSAGE
//model, version, serial number and battery value
getMsg3();
break;
case 0x1B: //AA1BFF MESSAGE (trigger debug mode activation)
feedback = 123;
//environment temperature, humidity, co2 and feedback placeholder value
getMsg4((int) temperature,(int) humidity,(int) co2, feedback);
feedback = 0;
debug = !debug; // enable/disable debug mode
if (debug == true) {
ble.print("Debug mode enabled.");
//enabling debug mode, it sends automatically data every seconds instead of 10 seconds
autoSend = 1000;
}
else {
ble.print("Debug mode disabled.");
autoSend = 10000;
}
break;
}
}
}