-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.ino
277 lines (238 loc) · 6.84 KB
/
driver.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
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
#include "time.h"
#include <Wire.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <EEPROM.h>
#include "logo.h"
// Contains the WiFi credentials and the Supabase URL and key
#include "envs.h"
#define I2C_SDA 21
#define I2C_SCL 22
#define WAKEUP_PIN 4
#define i2c_Address 0x3c
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 19
// Time between uploads to the database
#define DATABASE_INTERVAL 20000
#define SEALEVELPRESSURE_HPA (1013.25)
#define DEBUG_LED true // Set to true to enable the debug LED
#define DEBUG_LED_PIN 2 // Onboard LED of the ESP32 dev board
#define INTEGER_LIMIT 2147483647
const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
TaskHandle_t UploadTask;
TwoWire I2CWire = TwoWire(0);
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &I2CWire, OLED_RESET);
Adafruit_BME280 bme;
String uploadError = "";
int screenTime = 0;
void setup()
{
pinMode(WAKEUP_PIN, INPUT);
pinMode(DEBUG_LED_PIN, OUTPUT);
Serial.begin(115200);
I2CWire.begin(I2C_SDA, I2C_SCL, 100000);
delay(250);
display.begin(i2c_Address, true);
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
display.clearDisplay();
display.drawBitmap((SCREEN_WIDTH - 32) / 2, (SCREEN_HEIGHT - 32) / 2, logo, 32, 32, SH110X_WHITE);
display.setCursor((SCREEN_WIDTH - 32) / 2 - 16, (SCREEN_HEIGHT - 32) / 2 + 38);
display.println("pkozak.org");
display.display();
delay(350);
bool status = bme.begin(0x76, &I2CWire);
if (!status)
{
Serial.println("Could not find the BME280 sensor!");
#ifdef DEBUG_LED
digitalWrite(DEBUG_LED_PIN, HIGH);
#endif
while (1)
{
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
display.invertDisplay(false);
display.println("No sensor!");
display.setTextSize(1);
display.println("No BME280 sensor.");
display.println("Please check the wiring");
display.display();
delay(1500);
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
display.invertDisplay(true);
display.println("No sensor!");
display.setTextSize(1);
display.println("No BME280 sensor.");
display.println("Please check the wiring");
display.display();
delay(1500);
}
}
EEPROM.writeInt(0x00, 0);
EEPROM.commit();
WiFi.begin(ssid, password);
Serial.print("Connecting...");
display.clearDisplay();
display.setCursor(0, 0);
display.println("Connecting to WiFi");
display.display();
while (WiFi.status() != WL_CONNECTED && WiFi.status() != WL_CONNECT_FAILED)
{
delay(500);
Serial.print(".");
display.print(".");
display.display();
}
display.clearDisplay();
display.setCursor(0, 0);
if (WiFi.status() != WL_CONNECT_FAILED)
{
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
Serial.println("");
Serial.print("Connected to WiFi\n");
display.println("Connected to WiFi");
display.display();
xTaskCreatePinnedToCore(
UploadData, /* Task function. */
"uploadData", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&UploadTask, /* Task handle to keep track of created task */
1); /* pin task to core 1 */
}
else
{
Serial.println("");
Serial.println("Connection failed\n");
Serial.println("Working in offline mode");
display.println("Connection failed");
display.println("Working in offline mode");
display.display();
}
}
void loop()
{
// After 10s of inactivity, display the screen saver
if (screenTime >= 10000)
{
display.clearDisplay();
while (digitalRead(WAKEUP_PIN) == LOW)
{
// Clear the screen and wait for the button to be pressed
}
screenTime = 0;
}
struct tm timeinfo;
getLocalTime(&timeinfo);
int temp = bme.readTemperature();
int pressure = bme.readPressure() / 100.0F;
int humidity = bme.readHumidity();
display.clearDisplay();
display.setCursor(0, 0);
if (temp == INTEGER_LIMIT || pressure < 0) // Avoid displaying invalid data
{
display.setTextSize(2);
display.println("Error while reading data");
display.setTextSize(1);
display.println("Please restart");
#ifdef DEBUG_LED
digitalWrite(DEBUG_LED_PIN, HIGH);
#endif
}
else
{
display.println("Temp: " + String(temp) + " C");
display.println("Pressure: " + String(pressure) + " hPa");
display.println("Humidity: " + String(humidity) + "%");
screenTime += 500;
}
if (WiFi.status() == WL_CONNECTED)
{
display.println("");
display.println(&timeinfo, "%H:%M:%S");
}
else
{
display.setCursor(0, SCREEN_HEIGHT - 10);
display.println("Offline");
}
if (uploadError != "")
{
display.setCursor(0, SCREEN_HEIGHT - 10);
display.println(uploadError);
}
display.drawBitmap(SCREEN_WIDTH - 32, SCREEN_HEIGHT - 32, logo, 32, 32, SH110X_WHITE);
display.display();
delay(500);
}
void UploadData(void *pvParameters)
{
while (1)
{
int time = millis();
float temp = bme.readTemperature();
int pressure = bme.readPressure() / 100.0F;
if (temp == INTEGER_LIMIT || pressure < 0) // Avoid uploading invalid data
{
Serial.println("Error while reading data, skipping upload");
#ifdef DEBUG_LED
digitalWrite(DEBUG_LED_PIN, HIGH);
#endif
delay(DATABASE_INTERVAL);
return;
// Read restart count from eeprom
int restarts = EEPROM.read(0x00);
if (restarts == 3)
{
return;
}
EEPROM.writeInt(0x00, restarts + 1);
EEPROM.commit();
ESP.restart();
}
HTTPClient http;
http.begin(SUPABASE_URL);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", String("Bearer ") + String(SUPABASE_KEY));
http.addHeader("apikey", String(SUPABASE_KEY));
JSONVar weather;
weather["temperature"] = bme.readTemperature();
weather["pressure"] = bme.readPressure() / 100.0F;
weather["humidity"] = bme.readHumidity();
int resp = http.POST(JSON.stringify(weather));
if (resp != 201)
{
Serial.println("Error uploading data");
Serial.println(http.errorToString(resp));
Serial.println(http.getString());
uploadError = "Error code " + String(resp);
#ifdef DEBUG_LED
digitalWrite(DEBUG_LED_PIN, HIGH);
#endif
}
else
{
uploadError = "";
#ifdef DEBUG_LED
digitalWrite(DEBUG_LED_PIN, LOW);
#endif
}
int diff = millis() - time;
delay(DATABASE_INTERVAL - diff);
}
}