-
Notifications
You must be signed in to change notification settings - Fork 2
/
DEV_Sensors.h
211 lines (163 loc) · 6.65 KB
/
DEV_Sensors.h
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
/*
* Adafruit Temperature and Humidity Sensor DHT22
*/
#include "DHT.h"
#define DHTPIN 4 // DHT Pin
#define DHTTYPE DHT22 // DHT Type, DHT22 in this project
DHT dht(DHTPIN, DHTTYPE);
/*
* Adafruit Monochrome 0.91" 128x32 I2C OLED Display
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library
// On an esp32S: D21(SDA), D22(SCL)
#define OLED_RESET 35 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // < See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int tempDisplay = 0; // Integer to store initial temperature value for display
int humidDisplay = 0; // Integer to store initial humidity value for display
char totalLine[20] = {' '}; // String to store temperature and humidity data for drawing to display
int lineLength = 10; // Length of line printed on OLED display
/* Function: displaySetup
* Input: None
* Output: None
* Description: Setup the SSD1306 OLED display according to example from Adafruit
* Modified 2022/12/14 from Adafruit Example Code SSD1306 I2C
*/
void displaySetup(void) {
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("Error: SSD1306 allocation failed!"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen
display.display();
}
/* Function: sensorSetup
* Input: None
* Output: None
* Description: Setup the Temperature and Humidity Sensor DHT22 according to example from Adafruit
* Modified 2022/12/14 from Adafruit Example Code DHT Sensor Library
*/
void sensorSetup(void) {
dht.begin();
}
/* Function: drawLine
* Input: char[]
* Output: None
* Description: Prints a line of characters to 0.91" OLED Display
* Modified 2022/12/14 from Adafruit Example Code SSD1306 I2C
*/
void drawLine(char line[]) {
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
delay(1000);
display.clearDisplay();
display.setTextSize(3); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
// Note: Library will draw what it can and the rest will be clipped
for (int16_t i = 0; i < lineLength; i++) {
if (line[i] == '\n') {
display.write(' ');
}
else {
display.write(line[i]);
}
}
display.display();
delay(1000);
Serial.println(F("Display"));
}
/* Function: readTemperature
* Input: None
* Output: Temperature in float
* Description: Returns the current temperature in celcius from DHT22 sensor and rounds the value for OLED printing
*/
float readTemperature(void) {
float temperature = dht.readTemperature();
if (isnan(temperature)) {
Serial.println(F("Error: Failed to read Temperature!"));
temperature = 23;
tempDisplay = 23;
return temperature;
}
tempDisplay = round(temperature);
return temperature;
}
/* Function: readHumidity
* Input: None
* Output: Humidity in float
* Description: Returns the current humidity in percentage from DHT22 sensor and rounds the value for OLED printing
*/
float readHumidity(void) {
float humidity = dht.readHumidity();
if (isnan(humidity)) {
Serial.println(F("Error: Failed to read Humidity!"));
humidity = 23;
humidDisplay = 23;
return humidity;
}
humidDisplay = round(humidity);
return humidity;
}
/* Struct: DEV_TempSensor
* Description: Service route for DHT22 temperature readings, DHT22 and SSD1306 OLED display must be initialized
* first before executing
* Modified 2022/12/14 from HomeSpan example 12: Service Loops
* https://github.com/HomeSpan/HomeSpan/tree/master/examples/12-ServiceLoops
*/
struct DEV_TempSensor : Service::TemperatureSensor {
SpanCharacteristic *temp;
DEV_TempSensor() : Service::TemperatureSensor() {
float temperature = readTemperature();
sprintf(totalLine, "%iC %i%%\n", tempDisplay, humidDisplay);
drawLine(totalLine);
temp = new Characteristic::CurrentTemperature(temperature);
temp -> setRange(-40, 80);
Serial.print("Temperature Sensor Initialization Completed!");
Serial.print("\n");
}
void loop() {
if (temp->timeVal() > 5000) {
float temperature = readTemperature();
sprintf(totalLine, "%iC %i%%\n", tempDisplay, humidDisplay);
drawLine(totalLine);
temp->setVal(temperature);
LOG1("Temperature Update: ");
LOG1(temperature);
LOG1("\n");
}
}
};
/* Struct: DEV_HumiditySensor
* Description: Service route for DHT22 humidity readings, DHT22 and SSD1306 OLED display must be initialized
* first before executing
* Modified 2022/12/14 from HomeSpan example 12: Service Loops
* https://github.com/HomeSpan/HomeSpan/tree/master/examples/12-ServiceLoops
*/
struct DEV_HumiditySensor : Service::HumiditySensor {
SpanCharacteristic *relativeHumidity;
DEV_HumiditySensor() : Service::HumiditySensor() {
float humidity = readHumidity();
sprintf(totalLine, "%iC %i%%\n", tempDisplay, humidDisplay);
drawLine(totalLine);
relativeHumidity = new Characteristic::CurrentRelativeHumidity(humidity);
Serial.print("Humidity Sensor Initialization Completed!");
Serial.print("\n");
}
void loop() {
if (relativeHumidity->timeVal() > 5000) {
float humidity = readHumidity();
sprintf(totalLine, "%iC %i%%\n", tempDisplay, humidDisplay);
drawLine(totalLine);
relativeHumidity->setVal(humidity);
}
}
};