-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32-motorpi2-slack.ino
392 lines (359 loc) · 10.9 KB
/
esp32-motorpi2-slack.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
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
/*
* Connect to Wifi and set time
*
* display time and rssi on SSD1306 I2C display
*
* Read temperature from DS18B20 via w1
* Read temperature from MAX6675 via SPI
*
* Write data back when wlan available
*
* Scanning...
* I2C device found at address 0x3C - display
* I2C device found at address 0x68 - gyrometer MPU6050
* I2C device found at address 0x76 - bme280
* I2C device found at address 0x48 - ADS1115 with grounded ADDR
*
*
*
*
* ToDo:
* Break out sensor reading in own thread
* Write multiple/batched
* bidirectional communication / slack?
*/
#define DEVICE "ESP32"
#include <WiFi.h>
#include "TimeLib.h"
#include <Adafruit_ADS1015.h>
#include "SSD1306Wire.h"
#include <TFT_eSPI.h> // Hardware-specific library
#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>
#include <SPI.h>
#include <LinkedList.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_MPU6050.h>
#include "credentials.h"
#include <WebSocketsClient.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <MD_MAX72xx.h>
// should be adjustable with a potentiometer
const int loopWait = 1000;
// MAX6675 max SPI Clock is 4.3 MHz but does not seem to work, ST7735 runs by default on 27 MHz
// static const int spiClk = 1000000; // 1 MHz
static const int spiClk = 2000000;
struct Measurement {
int rssi;
unsigned int freeheap;
float w1_tempC;
float max6675_1_tempC;
float max6675_2_tempC;
float max6675_3_tempC;
float rpmfreq;
float revfreq;
unsigned int rpm;
unsigned int revolutions;
float bme280_airpressure;
float bme280_humidity;
float bme280_temperature;
float bme280_altitude;
float mpu6050_accel_x;
float mpu6050_accel_y;
float mpu6050_accel_z;
float mpu6050_gyro_x;
float mpu6050_gyro_y;
float mpu6050_gyro_z;
float mpu6050_temperature;
unsigned int analog0;
unsigned int analog1;
unsigned int analog2;
unsigned int analog3;
// We assume 0-5 volt
float afrvoltage0;
float voltage1;
float voltage2;
float voltage3;
float calculatedafr;
unsigned int sensorreadtime;
wl_status_t wlanstatus;
time_t timestamp;
// debugging
unsigned int readtime_w1_temp;
unsigned int readtime_bme280;
unsigned int readtime_mpu6050;
unsigned int readtime_max6675_1;
unsigned int readtime_max6675_2;
unsigned int readtime_max6675_3;
unsigned int readtime_mcp3008;
unsigned int readtime_ads1115;
unsigned int readtime_esp32;
unsigned int readtime_interrupts;
unsigned int writetime_display;
unsigned int lastwritetime;
};
unsigned int roundedrpm=0;
char roundedafr[5];
unsigned long rpmpulses=0;
unsigned long revolutionpulses=0;
unsigned long oldrpmpulses=0;
unsigned long oldrevolutionpulses=0;
Adafruit_BME280 bme; // I2C
unsigned bme280status;
// MPU6050
Adafruit_MPU6050 mpu;
bool mpu6050failed=false;
//uninitalised pointers to SPI objects
SPIClass * vspi = NULL;
#ifdef HAVE_W1
// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
#endif
char timestring[50];
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 0;
#ifdef HAVE_SSD1306
// Initialize the OLED display using Arduino Wire:
SSD1306Wire display(0x3c, SDA, SCL); // ADDRESS, SDA, SCL - SDA and SCL usually populate automatically based on your board's pins_arduino.h
#endif
#ifdef HAVE_ST7735
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
#endif
#ifdef HAVE_ADS1115
//ADDR Pin - GND = I2C Address 0x48
Adafruit_ADS1115 ads1115(0x48); /* Use this for the 16-bit version */
int16_t adc0;
int16_t adc1;
int16_t adc2;
int16_t adc3;
#endif
#ifdef HAVE_MAX7219
MD_MAX72XX max7219 = MD_MAX72XX(MAX7219_HARDWARE_TYPE, MAX7219_CS, MAX7219_DEVICES); // SPI hardware interface
uint8_t arrow[COL_SIZE] =
{
0b00001000,
0b00011100,
0b00111110,
0b01111111,
0b00011100,
0b00011100,
0b00011100,
0b00000000
};
#endif
enum max7219command {
none,
rightarrow,
leftarrow,
uparrow,
downarrow,
alert,
text
};
max7219command max7219_current_command = none;
enum st7735_layout {
original,
driver,
technical
};
st7735_layout st7735_current_layout = ST7735_START_LAYOUT;
// Create the influxdb object
//InfluxDBClient influxclient(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
InfluxDBClient influxclient(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);
// This is the only place we save data, buffer is in the influx class
Measurement curM;
// amateurish way to figure out if we have an ntp sync
# define FEBRUARY2020 1581627570
unsigned int timeiteration = 0;
time_t setuptime;
// telemetry
unsigned long startMillis=millis();
Point row(INFLUXDB_ROWNAME);
void IRAM_ATTR rpminterruptserviceroutine() {
rpmpulses++;
}
void IRAM_ATTR revolutioninterruptserviceroutine() {
revolutionpulses++;
}
unsigned long int rpmmillis;
unsigned long int revolutionmillis;
unsigned long int lastwritetime=0;
unsigned long int startwrite;
int startloop;
int waittime;
unsigned int last_writetime=0;
bool slack_connected = false;
unsigned long slack_lastPing = 0;
unsigned long slack_startconnect = 0;
int slack_attempts;
long slack_nextCmdId = 1;
WebSocketsClient slack_webSocket;
bool clearedscreen_for_slacktext=false;
bool max7219_blink = false;
int slack_textcounter=0;
int max7219_alertcounter=0;
int max7219_curcol=0;
char slack_textmessage[50];
char egpdisplaystring[30];
void setup() {
Serial.begin(115200);
delay(1);
time(&setuptime);
// Initialising the UI will init the display too.
Serial.print(String(setuptime));
Serial.println("Initializing display");
#ifdef HAVE_SSD1306
display.init();
display.flipScreenVertically();
#endif
#ifdef HAVE_ST7735
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(1);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(0, 0, 1);
tft.setTextWrap(false,false);
#endif
printStringOnDisplay("Connecting to Wifi");
// connect to the wifi network
Serial.println();
time(&setuptime);
Serial.print(String(setuptime));
Serial.print(" Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
printStringOnDisplay("Wifi connected");
Serial.println("");
time(&setuptime);
Serial.print(String(setuptime));
Serial.println(" WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("ESP Mac Address: ");
Serial.println(WiFi.macAddress());
// there is no point in writing data unless we have a proper time.
// halt until good time
printStringOnDisplay("Setting time ");
configTime(gmtOffset_sec, daylightOffset_sec, NTPSERVER1, NTPSERVER2, NTPSERVER3);
time(&setuptime);
while (setuptime < FEBRUARY2020) {
displayWhileWaitingForTime();
timeiteration++;
Serial.println("Time attempt " + String(timeiteration) + " " + String(setuptime));
configTime(gmtOffset_sec, daylightOffset_sec, NTPSERVER1, NTPSERVER2, NTPSERVER3);
time(&setuptime);
printStringOnDisplay("Waiting for time");
delay(1000);
}
#ifdef HAVE_MAX7219
pinMode(MAX7219_CS, OUTPUT); //VSPI SS for first SPI device (MAX6675)
max7219.begin();
max7219.control(MD_MAX72XX::INTENSITY, MAX_INTENSITY/4);
max7219.control(MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
max7219.clear();
#endif
//configure the influxdb connection, seconds resolution and write 30 datapoints each time
printStringOnDisplay("connecting to influx");
influxclient.setWriteOptions(INFLUXDB_WRITE_PRECISION,INFLUXDB_BATCHSIZE,INFLUXDB_BUFFER);
#ifdef HAVE_W1
// initialize w1 bus
printStringOnDisplay("init w1 bus, ");
sensors.begin();
#endif
#ifdef HAVE_BME280
printStringOnDisplay("init BME280 i2c, 0x76, ");
// default address for the BME280 is in this library 0x60
bme280status = bme.begin(0x76);
if (!bme280status) {
printStringOnDisplay("BME280 failed, ");
}
#endif
#ifdef HAVE_ADS1115
printStringOnDisplay("init ADS1115 i2c, 0x48, ");
ads1115.begin();
#endif
#ifdef HAVE_MPU6050
// initialize MPU6050
printStringOnDisplay("init MPU6050 i2c, 0x68");
if (!mpu.begin()) {
printStringOnDisplay("MPU6050 failed");
mpu6050failed=true;
}
MPU6050calibration();
#endif
//initialise instance of the SPIClass attached to VSPI (perhaps we need HSPI later)
printStringOnDisplay("init SPI bus ");
vspi = new SPIClass(VSPI);
//initialise vspi with default pins
//SCLK = 18, MISO = 19, MOSI = 23, SS = 5
vspi->begin();
//set up slave select pins as outputs as the Arduino API
//doesn't handle automatically pulling SS low
pinMode(MAX6675_1_CS, OUTPUT); //VSPI SS for first SPI device (MAX6675)
pinMode(MAX6675_2_CS, OUTPUT); //VSPI SS for first SPI device (MAX6675)
pinMode(MAX6675_3_CS, OUTPUT); //VSPI SS for first SPI device (MAX6675)
// pinMode(2, OUTPUT); //VSPI SS for second SPI device
pinMode(RPMPIN, INPUT);
pinMode(REVPIN, INPUT);
#ifdef HAVE_INTERRUPTS
// create interrupt routine for RPMs
attachInterrupt(RPMPIN, rpminterruptserviceroutine, RISING);
// create interrupt routine for Revolutions
attachInterrupt(REVPIN, revolutioninterruptserviceroutine, RISING);
#endif
#ifdef HAVE_SLACK
slack_startconnect=millis();
printStringOnDisplay("connecting to slack");
slack_connected = connectToSlack();
slack_attempts = 0;
while (!slack_connected && slack_attempts<SLACK_MAXCONNECTATTEMPTS){
printStringOnDisplay("waiting for slack");
Serial.println("waiting for slack:" + String(int(millis()-slack_startconnect)));
slack_webSocket.loop();
delay(500);
slack_attempts++;
}
#endif
printStringOnDisplay("setup finished");
rpmmillis=millis();
oldrpmpulses=rpmpulses;
revolutionmillis=millis();
oldrevolutionpulses=revolutionpulses;
}
void loop() {
startloop=millis();
readAllData(&curM, last_writetime);
printOnDisplay(&curM);
last_writetime=writeAllData(&curM);
if (curM.wlanstatus != WL_CONNECTED) {
Serial.print("Wifi lost, WiFi.status = ");
Serial.println(wl_status_to_string(curM.wlanstatus));
printStringOnDisplay("Wifi Lost, reconnecting");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
else
{
#ifdef HAVE_SLACK
slack_webSocket.loop();
#endif
// We have connectivity, sync time and flush the buffer
// configTime(gmtOffset_sec, daylightOffset_sec, NTPSERVER);
// pop all measurements and write them to the db, verify that we have connectivity while we do it
// since we pop first we lose a measurement if the wifi is lost while emptying the list
// printAllInfo(&curM);
}
waittime=loopWait - (millis()-startloop);
if (waittime > 0) {
delay(waittime);
}
}