-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathring_reader.ino
131 lines (103 loc) · 2.73 KB
/
ring_reader.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
/*
* Board type: Wemos D1 mini
*/
// Libraries
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "SSD1306.h"
// Config
#include "credentials.h"
#include "iot_config_home.h" // MQTT broker URL + connection config
// Import fonts and images for the display
#include "font.h"
#include "images.h"
//Device info
#define DEVICE_TYPE "door-reader"
#define DEVICE_FIRMWARE_VERSION "0.2.1"
// IO
#define RX_PIN D5
#define TX_PIN D6
#define SDA_PIN D2
#define SCL_PIN D1
#define BUZZER_PIN D8
// Display parameters
#define DISPLAY_WIDTH 128
#define DISPLAY_HEIGHT 64
#define DISPLAY_INVERSION_PERIOD 60000
#define DISPLAY_LOCK_STATE_TIME 5000
// reader parameters
#define COOLDOWN_DURATION 3000
#define CODE_ADDRESS 0x0b
// MQTT parameters
#define MQTT_LOCK_COMMAND_TOPIC "lock/command"
#define MQTT_LOCK_STATUS_TOPIC "lock/status"
#define MQTT_EVENTS_TOPIC "/ring-reader/events"
#define MQTT_RETAIN true
#define MQTT_RECONNECT_PERIOD 1000
// Web server settings
#define WEB_SERVER_PORT 80
// Misc
#define BUZZER_VOLUME 25
typedef struct Response {
byte status = 0xff;
byte length = 0;
char data[0xff]; // This is a buffer
} Response;
// Declaring objects
SSD1306 display(0x3c, SDA_PIN, SCL_PIN);
AsyncWebServer web_server(WEB_SERVER_PORT);
WiFiClient wifi_client;
PubSubClient MQTT_client(wifi_client);
SoftwareSerial swSer;
// Global variables
long cooldown_start_time = -COOLDOWN_DURATION;
char code[5] = { 0x69, 0x67, 0x91, 0x74, 0x76 };
boolean locked = false;
boolean displaying_lock_state = false;
long displaying_lock_state_start_time;
void setup() {
delay(100);
Serial.begin(115200);
Serial.println("");
Serial.println("Start");
swSer.begin(9600, SWSERIAL_8N1, RX_PIN, TX_PIN, false, 256);
buzzer_init();
wifi_setup();
display_setup();
display_image(logo);
MQTT_setup();
web_server_setup();
}
void loop() {
wifi_connection_manager();
MQTT_connection_manager();
MQTT_client.loop();
invert_display_periodically();
// While wifi connecting, show wifi connecting
if(!wifi_connected()) display_wifi_connecting();
manage_display_lock_state();
if(wifi_connected() && mqtt_connected()) {
int result = compare_em4100(code);
if(result != -1){
if(millis() - cooldown_start_time > COOLDOWN_DURATION){
cooldown_start_time = millis();
if(result == 0) {
display_check();
MQTT_publish_event();
MQTT_publish_toggle();
buzzer_play_success();
}
else if(result == 1) {
display_cross();
buzzer_play_error();
delay(1000);
display_lock_state();
}
}
}
}
}