-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm5stack_timer_camera_x.ino
More file actions
194 lines (160 loc) · 5.49 KB
/
m5stack_timer_camera_x.ino
File metadata and controls
194 lines (160 loc) · 5.49 KB
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
/**
* @file m5stack_timer_camera_x.ino
* @author Can Celasun
* @brief TimerCamera-X deep sleep & HTTP Post
* @version 0.1
* @date 2026-02-20
*
*
* @Hardwares: TimerCamera-X
* @Platform Version: Arduino M5Stack Board Manager v2.0.9
* @Dependent Library:
* TimerCam-arduino: https://github.com/m5stack/TimerCam-arduino
* ArduinoHttpClient: https://github.com/arduino-libraries/ArduinoHttpClient
* Based on: https://github.com/m5stack/TimerCam-arduino/blob/ca24786e3cecdad060d86d6eb1cbc299685591b9/examples/wakeup/wakeup.ino
* Based on: https://github.com/m5stack/TimerCam-arduino/blob/ca24786e3cecdad060d86d6eb1cbc299685591b9/examples/http_post/http_post.ino
* Based on: https://github.com/m5stack/M5Unit-FlashLight/blob/008853f9e9832db20ba95c8b58ec68f327f61e36/SetFlashTimeAndBrightness/SetFlashTimeAndBrightness.ino
*/
#include "M5TimerCAM.h"
#include <WiFi.h>
#include <ArduinoHttpClient.h>
#define FLASH_EN_PIN 4 // SDA port is G4 on the Timer Camera X
#define _WIFI_SSID "My SSID"
#define _WIFI_PASS "MyPassword"
#define _SERVER_HOST "127.0.0.1"
#define _SERVER_PORT 80
#define SLEEP_INTERVAL_SECS 14400
void unit_flash_set_brightness(uint8_t brightness);
void led_flash(int ms) {
TimerCAM.Power.setLed(127);
vTaskDelay(pdMS_TO_TICKS(ms));
TimerCAM.Power.setLed(0);
}
// See https://github.com/m5stack/TimerCam-arduino/issues/16
void setup() {
TimerCAM.begin(true);
Serial.println("Waking up");
led_flash(1000);
pinMode(FLASH_EN_PIN, OUTPUT);
if (!configureCamera()) {
Serial.println("Camera configuration failed, halting");
TimerCAM.Power.timerSleep(SLEEP_INTERVAL_SECS);
return;
}
WiFiClient wifi = connectToWiFi();
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi connection failed, halting");
TimerCAM.Power.timerSleep(SLEEP_INTERVAL_SECS);
return;
}
if (!sendImage(wifi, batLevel, batVoltage)) {
Serial.println("Image upload failed, halting");
TimerCAM.Power.timerSleep(SLEEP_INTERVAL_SECS);
return;
}
Serial.println("Going to sleep now");
TimerCAM.Power.timerSleep(SLEEP_INTERVAL_SECS);
}
bool configureCamera() {
if (!TimerCAM.Camera.begin()) {
Serial.println("Camera Init Fail");
return false;
}
Serial.println("Camera init success");
TimerCAM.Camera.sensor->set_pixformat(TimerCAM.Camera.sensor, PIXFORMAT_JPEG);
// 2MP Sensor
//TimerCAM.Camera.sensor->set_framesize(TimerCAM.Camera.sensor, FRAMESIZE_UXGA);
// 3MP Sensor
TimerCAM.Camera.sensor->set_framesize(TimerCAM.Camera.sensor, FRAMESIZE_QXGA);
TimerCAM.Camera.sensor->set_vflip(TimerCAM.Camera.sensor, 1);
TimerCAM.Camera.sensor->set_hmirror(TimerCAM.Camera.sensor, 0);
TimerCAM.Camera.free();
return true;
}
WiFiClient connectToWiFi() {
WiFiClient wifi;
WiFi.mode(WIFI_STA);
WiFi.begin(_WIFI_SSID, _WIFI_PASS);
WiFi.setSleep(false);
Serial.println("");
Serial.print("Connecting to ");
Serial.println(_WIFI_SSID);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED) {
if (attempts >= 20) {
Serial.println("\nFailed to connect after 20 attempts");
return WiFiClient();
}
delay(1000);
Serial.print(".");
attempts++;
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(_WIFI_SSID);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
return wifi;
}
bool sendImage(WiFiClient wifi) {
unit_flash_set_brightness(9);
delay(300);
if (!TimerCAM.Camera.get()) {
unit_flash_set_brightness(0);
Serial.println("Could not get camera");
return false;
}
unit_flash_set_brightness(0);
int batVoltage = TimerCAM.Power.getBatteryVoltage();
int batLevel = TimerCAM.Power.getBatteryLevel();
Serial.printf("Bat Voltage: %dmv\r\n", batVoltage);
Serial.printf("Bat Level: %d%%\r\n", batLevel);
HttpClient client = HttpClient(wifi, _SERVER_HOST, _SERVER_PORT);
Serial.println("making POST request");
String path = "/ocr?bat_level=" + String(batLevel) + "&bat_voltage=" + String(batVoltage);
String contentType = "image/jpeg";
client.post(path.c_str(), contentType.c_str(), TimerCAM.Camera.fb->len, TimerCAM.Camera.fb->buf);
int statusCode = client.responseStatusCode();
String response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
TimerCAM.Camera.free();
if (statusCode != 202) {
Serial.println("Upload failed");
return false;
}
return true;
}
// 0: Flashlight off
// 1: 100% brightness + 220ms
// 2: 90% brightness + 220ms
// 3: 80% brightness + 220ms
// 4: 70% brightness + 220ms
// 5: 60% brightness + 220ms
// 6: 50% brightness + 220ms
// 7: 40% brightness + 220ms
// 8: 30% brightness + 220ms
// 9: 100% brightness + 1.3s
// 10: 90% brightness + 1.3s
// 11: 80% brightness + 1.3s
// 12: 70% brightness + 1.3s
// 13: 60% brightness + 1.3s
// 14: 50% brightness + 1.3s
// 15: 40% brightness + 1.3s
// 16: 30% brightness + 1.3s
void unit_flash_set_brightness(uint8_t brightness) {
if ((brightness >= 1) && (brightness <= 16)) {
for (int i = 0; i < brightness; i++) {
digitalWrite(FLASH_EN_PIN, LOW);
delayMicroseconds(4);
digitalWrite(FLASH_EN_PIN, HIGH);
delayMicroseconds(4);
}
} else {
digitalWrite(FLASH_EN_PIN, LOW);
}
}
void loop() {
}