-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathEsp32ThermalCam.ino
243 lines (218 loc) · 5.92 KB
/
Esp32ThermalCam.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
// Import required libraries
#include <WiFi.h>
#include <Adafruit_MLX90640.h>
#include <esp_camera.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebSrv.h>
#include <ESPmDNS.h>
#include "html.h"
// Bolometer - Replace with your own pinout
#define I2C_SCL 13
#define I2C_SDA 12
#define I2C_PUP 2 // this is where to connect the 2x 4k7 pull up resistors
// Camera - Currently setup according to AI-Thinker board, aka ESP32-CAM
// Replace with your own setup if needed.
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
#define FLASH_LED 4
#define SMALL_LED 33
#define LED_CONTROL 15
#include "wifi.h"
// Bolometer stuff
Adafruit_MLX90640 mlx;
const size_t thermSize = (32 * 24) * sizeof(float);
const size_t frameSize = thermSize + 30000 * sizeof(char);
size_t imageSize = 0;
char frame[frameSize]; // buffer for full frame of temperatures and image
// Websocket stuff
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
// Sends log messages to websockets if true
bool wsdebug = false;
void log(String text)
{
Serial.println(text);
if (wsdebug)
{
ws.textAll("SERIAL:" + text);
}
}
void sendStatus()
{
log("Sending status");
String status = "STATUS:Total heap:" + String(ESP.getHeapSize()) += " | Free heap:" + String(ESP.getFreeHeap()) += " | WiFi RSSI:" + String(WiFi.RSSI()) += " | WiFi Status:" + String(WiFi.status());
ws.textAll(status);
}
void handleWebSocketMessage(void *arg, uint8_t *data, size_t len)
{
AwsFrameInfo *info = (AwsFrameInfo *)arg;
if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT)
{
data[len] = 0;
String command = String((char *)data);
if (command == "status")
{
sendStatus();
}
if (command == "debug")
{
wsdebug = !wsdebug;
}
}
}
void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
void *arg, uint8_t *data, size_t len)
{
switch (type)
{
case WS_EVT_CONNECT:
break;
case WS_EVT_DISCONNECT:
break;
case WS_EVT_DATA:
handleWebSocketMessage(arg, data, len);
break;
case WS_EVT_PONG:
case WS_EVT_ERROR:
break;
}
}
void connectWifi(const char *ssidname, const char *pass)
{
log("Connecting to " + String(ssidname));
WiFi.begin(ssidname, pass);
delay(5000);
}
void setupWifiAp(const char *ssidname, const char *pass)
{
WiFi.mode(WIFI_AP);
WiFi.setTxPower(WIFI_POWER_MINUS_1dBm);
WiFi.softAP(ssidname, pass);
delay(5000);
}
void setup()
{
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println();
log("Setting up bolometer");
pinMode(I2C_PUP, OUTPUT);
digitalWrite(I2C_PUP, HIGH);
Wire.begin(I2C_SDA, I2C_SCL);
mlx.begin(MLX90640_I2CADDR_DEFAULT, &Wire);
mlx.setMode(MLX90640_CHESS);
mlx.setResolution(MLX90640_ADC_16BIT);
mlx90640_resolution_t res = mlx.getResolution();
mlx.setRefreshRate(MLX90640_16_HZ);
mlx90640_refreshrate_t rate = mlx.getRefreshRate();
Wire.setClock(1000000); // max 1 MHz
log("Bolometer setup");
log("Setting up camera");
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 10000000;
config.pixel_format = PIXFORMAT_JPEG;
config.frame_size = FRAMESIZE_QVGA;
config.jpeg_quality = 10;
config.fb_count = 2;
esp_err_t err = esp_camera_init(&config);
sensor_t *s = esp_camera_sensor_get();
log("Camera setup");
log("Setting up WiFi");
WiFi.mode(WIFI_STA);
WiFi.setTxPower(WIFI_POWER_MINUS_1dBm);
if (WiFi.status() != WL_CONNECTED)
{
connectWifi(ssid, password);
if (WiFi.status() != WL_CONNECTED)
{
setupWifiAp(apssid, password);
}
}
log("WiFi setup");
log("Setting up MDNS as thermal");
MDNS.begin("thermal");
log("MDNS setup");
log("Setting web server");
ws.onEvent(onEvent);
server.addHandler(&ws);
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send_P(200, "text/html", index_html); });
server.begin();
log("Webserver setup");
pinMode(FLASH_LED, OUTPUT);
digitalWrite(FLASH_LED, LOW);
pinMode(LED_CONTROL, INPUT_PULLUP);
pinMode(SMALL_LED, OUTPUT);
digitalWrite(SMALL_LED, HIGH);
}
void take_snapshot()
{
camera_fb_t *fb = NULL;
fb = esp_camera_fb_get();
if (!fb)
{
log("Camera capture failed. Restarting");
ESP.restart();
}
log("Moving image to frame buffer " + String(fb->len) + " (max 30000)");
memcpy(&frame[thermSize], fb->buf, fb->len);
imageSize = fb->len;
esp_camera_fb_return(fb);
fb = NULL;
}
void take_thermal()
{
Serial.printf("ThermalgetFrame=%d ",mlx.getFrame((float *)frame));
}
unsigned long messageTimestamp = 0;
int messageCounter = 0;
void loop() {
ws.cleanupClients();
uint64_t now = millis();
if (now - messageTimestamp > 200) {
digitalWrite(SMALL_LED, ws.count()?LOW:HIGH);
digitalWrite(FLASH_LED, digitalRead(LED_CONTROL)?LOW:HIGH);
memset(frame, 0, frameSize);
take_thermal();
take_snapshot();
ws.binaryAll(frame, thermSize + imageSize);
messageTimestamp = now;
messageCounter++;
if (messageCounter > 20) {
sendStatus();
messageCounter = 0;
}
}
}