This repository has been archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
holzklotz.ino
151 lines (117 loc) · 3.67 KB
/
holzklotz.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
#include <Adafruit_NeoPixel.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define PIN_BUTTON D3
#define PIN_RGBLED D2
#define STASSID "ortenau.freifunk.net"
#define STAPSK ""
// RGB LED
Adafruit_NeoPixel statusLed = Adafruit_NeoPixel(1, PIN_RGBLED, NEO_GRB + NEO_KHZ800);
int brightness = 4;
bool brighter = true;
unsigned long lastBrightnessChange = 0;
unsigned long brightnessDelay = 64;
// Button
int buttonState = LOW;
int thisButtonState = LOW;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
// Status
bool roomOpen = false;
unsigned long lastStatusUpdateTime = 0;
unsigned long statusUpdateDelay = 1 * 60 * 1000;
void setup() {
pinMode(PIN_BUTTON, INPUT);
thisButtonState = digitalRead(PIN_BUTTON);
buttonState = thisButtonState;
lastButtonState = thisButtonState;
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, LOW);
statusLed.begin();
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println();
WiFi.begin(STASSID, STAPSK);
digitalWrite(BUILTIN_LED, HIGH);
}
void loop() {
updateStatusFromApi();
handleStatusLed();
handleButton();
}
void handleStatusLed() {
if ((millis() - lastBrightnessChange) > brightnessDelay) {
brightness = brighter ? brightness + 1 : brightness - 1;
if (brightness == 64) {
brighter = false;
} else if (brightness == 4) {
brighter = true;
}
lastBrightnessChange = millis();
}
if (WiFi.status() == WL_CONNECTED && lastStatusUpdateTime > 0) {
if (roomOpen) {
statusLed.setPixelColor(0, 0, brightness, 0);
} else {
statusLed.setPixelColor(0, brightness, 0, 0);
}
} else {
statusLed.setPixelColor(0, 0, 0, brightness);
}
statusLed.show();
}
void handleButton() {
thisButtonState = digitalRead(PIN_BUTTON);
if (thisButtonState != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (thisButtonState != buttonState) {
buttonState = thisButtonState;
if (buttonState == HIGH && WiFi.status() == WL_CONNECTED && lastStatusUpdateTime > 0) {
roomOpen = !roomOpen;
sendStatusToApi();
}
}
}
lastButtonState = thisButtonState;
}
void sendStatusToApi() {
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
http.begin(client, "http://api.section77.de/sensors/people_now_present/");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
Serial.print("[HTTP] PUT...\n");
int httpCode = http.PUT(roomOpen ? "value=1" : "value=0");
if (httpCode > 0) {
Serial.printf("[HTTP] PUT... code: %d\n", httpCode);
} else {
Serial.printf("[HTTP] PUT... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
void updateStatusFromApi() {
if (WiFi.status() == WL_CONNECTED && ((millis() - lastStatusUpdateTime) > statusUpdateDelay || lastStatusUpdateTime == 0)) {
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
http.begin(client, "http://api.section77.de/");
Serial.print("[HTTP] GET...\n");
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
String payload = http.getString();
if (payload.indexOf("\"state\":{\"open\":") > 0) {
roomOpen = payload.indexOf("\"state\":{\"open\":true") > 0;
Serial.printf("[HTTP] GET... newState: %s\n", roomOpen ? "true" : "false");
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
lastStatusUpdateTime = millis();
}
}