-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·345 lines (267 loc) · 8.93 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·345 lines (267 loc) · 8.93 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
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
/**
* ESP32-C6 Zigbee Keypad with MPR121 Touch Sensor
*
* Based on Espressif Arduino-ESP32 Zigbee examples
* Uses proper initialization sequence: Configure endpoints -> Add to Zigbee -> Begin
*/
#include "esp32-hal-gpio.h"
#ifndef ZIGBEE_MODE_ED
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
#endif
#include "Zigbee.h"
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_MPR121.h>
#include <esp_sleep.h>
#include <nvs_flash.h>
#include <nvs.h>
uint8_t button = BOOT_PIN;
// Pin Configuration
const int SDA_PIN = 18;
const int SCL_PIN = 4;
const int IRQ_PIN = 6;
const int BATTERY_PIN = 2;
const int BUZZER_PIN = 15;
const int BOOT_BUTTON_PIN = 9;
// MPR121 object
Adafruit_MPR121 cap = Adafruit_MPR121();
// Last touch state
uint16_t lastTouchState = 0;
// Keypad input string
String inputString = "1";
const int MAX_INPUT_LENGTH = 8;
// Deep sleep configuration
const unsigned long SLEEP_TIMEOUT_MS = 10000; // 20 seconds
unsigned long lastActivityTime = 0;
// Debounce configuration
const unsigned long DEBOUNCE_DELAY_MS = 100;
unsigned long lastKeyPressTime = 0;
// Buzzer control
const unsigned long BEEP_DURATION_MS = 20;
unsigned long beepEndTime = 0;
bool beepActive = false;
// Zero code timeout (send zero 2 seconds after entering a code)
const unsigned long ZERO_CODE_TIMEOUT_MS = 2000;
unsigned long enterPressTime = 0;
bool waitingForZeroSend = false;
// Zigbee endpoints - MUST be created as global variables
#define BATTERY_ENDPOINT 1
#define INPUT_ENDPOINT 2
ZigbeeAnalog zbAnalogDevice = ZigbeeAnalog(BATTERY_ENDPOINT);
ZigbeeAnalog zbAnalogSOC = ZigbeeAnalog(INPUT_ENDPOINT);
// Map channel to keypad character
char getKeypadChar(int channel) {
if (channel >= 0 && channel <= 8) {
return '1' + channel; // '1' to '9'
} else if (channel == 9) {
return '0';
} else if (channel == 10) {
return 'C'; // Clear
} else if (channel == 11) {
return 'E'; // Enter
}
return '\0';
}
int get_battery_SOC()
{
// Sample battery voltage 10 times with 10ms delay and take average
long batteryRawSum = 0;
for (int i = 0; i < 10; i++) {
batteryRawSum += analogRead(BATTERY_PIN);
delay(10);
}
int batteryRaw = batteryRawSum / 10;
float batteryVoltage = (batteryRaw / 4095.0) * 3.3 * 2.0 *1.28;
float batterySOC=(batteryVoltage-3.2);
if (batterySOC<0.0)
batterySOC=0.0;
if (batterySOC>1.0)
batterySOC=1.0;
batterySOC=batterySOC*100.0;
Serial.print("Battery Voltage: ");
Serial.print(batteryVoltage, 2);
Serial.print("Battery %: ");
Serial.println(batterySOC);
zbAnalogDevice.setBatteryPercentage(int(batterySOC));
zbAnalogDevice.setBatteryVoltage(int(batteryVoltage*10.0));
zbAnalogDevice.reportBatteryPercentage();
return(batterySOC);
}
void handleKeypadInput(int channel) {
char key = getKeypadChar(channel);
if (key == '\0') return;
// Trigger beep
beepActive = true;
beepEndTime = millis() + BEEP_DURATION_MS;
digitalWrite(BUZZER_PIN, HIGH);
if (key == 'C') {
inputString = "1";
Serial.println("Input: []");
} else if (key == 'E') {
Serial.print("ENTER - Input: [");
Serial.print(inputString);
Serial.println("]");
long inputValue = inputString.length() > 0 ? inputString.toInt() : 0;
// Report to Zigbee
zbAnalogDevice.setAnalogInput((float)inputValue);
zbAnalogDevice.reportAnalogInput();
zbAnalogSOC.setAnalogInput(get_battery_SOC());
zbAnalogSOC.reportAnalogInput();
// Start timer to send zero code after 2 seconds
enterPressTime = millis();
waitingForZeroSend = true;
inputString = "1";
} else {
if (inputString.length() < MAX_INPUT_LENGTH) {
inputString += key;
Serial.print("Input: [");
Serial.print(inputString);
Serial.println("]");
} else {
Serial.println("Max 8 digits reached!");
}
}
}
void enterDeepSleep() {
Serial.println("Setting MPR121 to slowest update rate for low power...");
cap.writeRegister(MPR121_CONFIG2, 0x3F);
Serial.println("Entering deep sleep...");
Serial.flush();
digitalWrite(BUZZER_PIN, LOW);
//gpio_pullup_en(GPIO_NUM_6);
// Initialize pins
pinMode(IRQ_PIN, INPUT);
pinMode(BUZZER_PIN, INPUT);
pinMode(BOOT_BUTTON_PIN, INPUT);
pinMode(SDA_PIN, INPUT);
pinMode(SCL_PIN, INPUT);
pinMode(IRQ_PIN, INPUT);
esp_deep_sleep_enable_gpio_wakeup(1ULL << IRQ_PIN, ESP_GPIO_WAKEUP_GPIO_LOW);
esp_deep_sleep_start();
}
void setup() {
// WRITE_PERI_REG(CONFIG_ESP_BROWNOUT_DET, 0); // REMOVED - causes crash!
Serial.begin(115200);
//while (!Serial && millis() < 3000);
Serial.println("ESP32-C6 Zigbee Keypad");
//delay(1000);
// Initialize pins
pinMode(IRQ_PIN, INPUT_PULLUP);
pinMode(BATTERY_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
pinMode(BOOT_BUTTON_PIN, INPUT_PULLUP);
digitalWrite(BUZZER_PIN, HIGH);
// Optional: set Zigbee device name and model
zbAnalogDevice.setManufacturerAndModel("DrDoms", "KeyPad2");
zbAnalogDevice.setPowerSource(ZB_POWER_SOURCE_BATTERY, 100, 42);
// Set up analog input
zbAnalogDevice.addAnalogInput();
zbAnalogDevice.setAnalogInputApplication(ESP_ZB_ZCL_AI_COUNT_UNITLESS_OTHER);
zbAnalogDevice.setAnalogInputDescription("Passcode");
zbAnalogDevice.setAnalogInputResolution(1);
// Set up analog input
zbAnalogSOC.addAnalogInput();
zbAnalogSOC.setAnalogInputApplication(ESP_ZB_ZCL_AI_PERCENTAGE_OTHER);
zbAnalogSOC.setAnalogInputDescription("Battery SOC");
zbAnalogSOC.setAnalogInputResolution(1);
// Add endpoints to Zigbee Core
Zigbee.addEndpoint(&zbAnalogDevice);
Zigbee.addEndpoint(&zbAnalogSOC);
// Initialize NVS for Zigbee data persistence
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
Serial.println("Starting Zigbee...");
// When all EPs are registered, start Zigbee in End Device mode
if (!Zigbee.begin(ZIGBEE_END_DEVICE)) {
Serial.println("Zigbee failed to start!");
Serial.println("Rebooting...");
ESP.restart();
} else {
Serial.println("Zigbee started successfully!");
}
Serial.println("Connecting to network");
while (!Zigbee.connected()) {
Serial.print(".");
delay(100);
delay(100);
}
Serial.println("Connected");
// Initialize I2C and MPR121
Serial.println("Initializing MPR121...");
Wire.begin(SDA_PIN, SCL_PIN);
Wire.setClock(100000);
delay(100);
if (!cap.begin(0x5A)) {
Serial.println("MPR121 not found!");
ESP.restart();
}
cap.setAutoconfig(true);
cap.setThresholds(8, 4); // 12 touch, 6 release
Serial.println("MPR121 initialized!");
Serial.println("");
Serial.println("=== Keypad Layout ===");
Serial.println("Channels 0-8: Numbers 1-9");
Serial.println("Channel 9: Number 0");
Serial.println("Channel 10: Clear");
Serial.println("Channel 11: Enter");
Serial.println("Max 8 digits");
Serial.println("Press BOOT for 3s to factory reset");
Serial.println("====================");
lastActivityTime = millis();
digitalWrite(BUZZER_PIN, LOW);
}
void loop() {
uint16_t touchState = cap.touched();
unsigned long currentTime = millis();
// Check for new touches
for (int i = 0; i < 12; i++) {
uint16_t mask = 1 << i;
bool currentlyTouched = touchState & mask;
bool wasTouched = lastTouchState & mask;
if (currentlyTouched && !wasTouched) {
lastActivityTime = currentTime;
if (currentTime - lastKeyPressTime > DEBOUNCE_DELAY_MS) {
handleKeypadInput(i);
lastKeyPressTime = currentTime;
}
}
}
lastTouchState = touchState;
// Handle buzzer
if (beepActive && millis() >= beepEndTime) {
digitalWrite(BUZZER_PIN, LOW);
beepActive = false;
}
// Check if we need to send zero code after timeout
if (waitingForZeroSend && (millis() - enterPressTime >= ZERO_CODE_TIMEOUT_MS)) {
Serial.println("Sending zero code...");
zbAnalogDevice.setAnalogInput(0.0);
zbAnalogDevice.reportAnalogInput();
waitingForZeroSend = false;
}
// Checking button for factory reset and reporting
if (digitalRead(button) == LOW) { // Push button pressed
// Key debounce handling
delay(100);
int startTime = millis();
while (digitalRead(button) == LOW) {
delay(50);
if ((millis() - startTime) > 3000) {
// If key pressed for more than 3secs, factory reset Zigbee and reboot
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
delay(1000);
Zigbee.factoryReset();
}
}
}
// Check for sleep timeout
if (millis() - lastActivityTime > SLEEP_TIMEOUT_MS) {
enterDeepSleep();
}
delay(10);
}