Skip to content

Commit d5b8597

Browse files
committed
first commit
0 parents  commit d5b8597

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MIT

README

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
Ledje:
3+
- eventjes aan: boot
4+
- uit + pulserend aan: idle
5+
- aan + pulserend uit: portal actief (access point)
6+
- lang aan: deurbel ingedrukt
7+
8+
Knopje:
9+
- indrukken = ESP-WiFiSettings configuratieportal (access point)
10+
11+
12+
NB: de 5mm led op het bordje staat antiparallel aan de led in de opto.

platformio.ini

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[platforms]
2+
src_dir = .
3+
default_envs = serial
4+
5+
[env]
6+
platform = espressif32
7+
board = lolin_c3_mini
8+
framework = arduino
9+
monitor_speed = 115200
10+
upload_speed = 921600
11+
upload_port = /dev/ttyACM*
12+
monitor_dtr = 0
13+
monitor_rts = 0
14+
board_build.partitions = default.csv
15+
build_unflags =
16+
-DARDUINO_USB_CDC_ON_BOOT=1
17+
-DARDUINO_USB_MODE=1
18+
-DPIO_FRAMEWORK_ARDUINO_ENABLE_CDC=1
19+
-DUSBCON=1
20+
21+
lib_deps =
22+
ESP-WiFiSettings
23+
MQTT
24+
25+
[env:serial]
26+
upload_protocol = esptool

src/main.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <Arduino.h>
2+
#include <WiFi.h>
3+
#include <SPIFFS.h>
4+
#include <WiFiSettings.h>
5+
#include <MQTT.h>
6+
7+
String topic;
8+
String value;
9+
MQTTClient mqtt;
10+
WiFiClient wificlient;
11+
12+
int buttonpin = 9;
13+
int ledpin = 8; // LOW = aan
14+
int bellpin = 4;
15+
16+
int failures = 0;
17+
int max_failures = 10;
18+
19+
void setup() {
20+
Serial.begin(115200); // Serial werkt niet. Geen idee waarom. De debugmeuk van esp-idf komt wel aan.
21+
Serial.println("setup");
22+
SPIFFS.begin(true);
23+
24+
pinMode(buttonpin, INPUT);
25+
pinMode(bellpin, INPUT);
26+
WiFiSettings.hostname = "deurbel2mqtt-";
27+
28+
String server = WiFiSettings.string("mqtt_server", 64, "10.42.42.10", "MQTT Server hostname");
29+
int port = WiFiSettings.integer("mqtt_port", 0, 65535, 1883, "MQTT Server port");
30+
topic = WiFiSettings.string("mqtt_topic", "revspace/button/doorbell", "MQTT topic");
31+
value = WiFiSettings.string("mqtt_value", "triiiiing", "MQTT topic");
32+
33+
pinMode(ledpin, OUTPUT);
34+
35+
36+
WiFiSettings.onPortalWaitLoop = [] {
37+
digitalWrite(ledpin, millis() % 1000 < 100);
38+
};
39+
40+
for (int i = 0; i < 2000; i++) {
41+
if (!digitalRead(buttonpin)) WiFiSettings.portal();
42+
delay(1);
43+
}
44+
45+
WiFiSettings.connect(false);
46+
mqtt.begin(server.c_str(), port, wificlient);
47+
}
48+
49+
void loop() {
50+
digitalWrite(ledpin, millis() % 1000 < 900);
51+
52+
if (!mqtt.connected()) {
53+
if (mqtt.connect(WiFiSettings.hostname.c_str())) {
54+
failures = 0;
55+
} else {
56+
failures++;
57+
if (failures >= max_failures) { delay(1000); ESP.restart(); }
58+
}
59+
}
60+
61+
if (!digitalRead(buttonpin)) {
62+
delay(25);
63+
if (!digitalRead(buttonpin)) { // lompe debounce
64+
WiFiSettings.portal();
65+
}
66+
}
67+
68+
if (!digitalRead(bellpin)) {
69+
delay(25);
70+
if (!digitalRead(bellpin)) { // lompe debounce
71+
digitalWrite(ledpin, LOW);
72+
mqtt.publish(topic, value);
73+
mqtt.loop();
74+
delay(5000); // nog lompere debounce
75+
digitalWrite(ledpin, HIGH);
76+
}
77+
}
78+
79+
mqtt.loop();
80+
81+
delay(1);
82+
}

0 commit comments

Comments
 (0)