-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnomzknop.ino
192 lines (174 loc) · 5.35 KB
/
nomzknop.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
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
#include <Adafruit_NeoPixel.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include "config.h"
#define PIN_LEDS D5
#define PIN_KNOP D6
#define DELAY_DEBOUNCE 20
#define DELAY_FLASH_FAST 25
#define DELAY_FLASH_SLOW 500
#define WIFI_SSID "revspace-pub-2.4ghz"
#define WIFI_PASS ""
#define MQTT_HOST "mosquitto.space.revspace.nl"
#define MQTT_TOPIC "revspace/button/nomz"
Adafruit_NeoPixel strip = Adafruit_NeoPixel(14, PIN_LEDS, NEO_GRBW + NEO_KHZ800);
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
Serial.println("Setup...");
strip.setBrightness(255);
strip.begin();
strip.setPixelColor(0, 255, 0, 0);
strip.setPixelColor(1, 255, 0, 0);
strip.setPixelColor(2, 255, 0, 0);
strip.setPixelColor(3, 255, 0, 0);
strip.setPixelColor(4, 0, 0, 0);
strip.show();
pinMode(PIN_KNOP, INPUT_PULLUP);
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(MQTT_HOST, 1883);
reconnect();
}
void reconnect() {
int connect_counter = 0;
strip.setPixelColor(0, 255, 0, 0);
strip.setPixelColor(1, 255, 0, 0);
strip.setPixelColor(2, 255, 0, 0);
strip.setPixelColor(3, 255, 0, 0);
strip.setPixelColor(4, 0, 0, 0);
strip.show();
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
// Attempt to connect
if (client.connect("nomzknop")) {
Serial.println("Connected");
} else {
connect_counter++;
if(connect_counter > 3) {
Serial.println("Failed >3 times, restarting");
ESP.restart();
}
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
void loop() {
uint32_t wheelVal;
for(int j=0; j<256; j++) { // 5 cycles of all colors on wheel
for(int i=0; i< strip.numPixels(); i++) {
wheelVal = Wheel(((i * 256 / strip.numPixels()) + j) & 255);
strip.setPixelColor(i, strip.Color(red(wheelVal), green(wheelVal), blue(wheelVal)));
}
strip.show();
delay(6);
if (!client.connected()) {
Serial.println("Connection lost, reconnecting...");
reconnect();
}
client.loop();
if(digitalRead(PIN_KNOP) == LOW) {
Serial.println("NOMZ! Posting on mqtt...");
String mqtt_text = "lekker! ";
mqtt_text += millis();
client.publish(MQTT_TOPIC, mqtt_text.c_str());
for(int i=15; i>0; i--) {
for(int n=5; n>0; n--) {
strip.setPixelColor(0, 0, 0, 255);
strip.setPixelColor(1, 0, 0, 255);
strip.setPixelColor(2, 0, 0, 0);
strip.setPixelColor(3, 0, 0, 0);
strip.show();
delay(DELAY_FLASH_FAST);
strip.setPixelColor(0, 0, 0, 0);
strip.setPixelColor(1, 0, 0, 0);
strip.setPixelColor(2, 0, 0, 0);
strip.setPixelColor(3, 0, 0, 0);
strip.show();
delay(DELAY_FLASH_FAST);
strip.setPixelColor(0, 0, 0, 0);
strip.setPixelColor(1, 0, 0, 0);
strip.setPixelColor(2, 0, 0, 255);
strip.setPixelColor(3, 0, 0, 255);
strip.show();
delay(DELAY_FLASH_FAST);
strip.setPixelColor(0, 0, 0, 0);
strip.setPixelColor(1, 0, 0, 0);
strip.setPixelColor(2, 0, 0, 0);
strip.setPixelColor(3, 0, 0, 0);
strip.show();
delay(DELAY_FLASH_FAST);
}
delay(DELAY_FLASH_SLOW);
for(int n=5; n>0; n--) {
strip.setPixelColor(0, 255, 128, 0);
strip.setPixelColor(1, 255, 128, 0);
strip.setPixelColor(2, 0, 0, 0);
strip.setPixelColor(3, 0, 0, 0);
strip.show();
delay(DELAY_FLASH_FAST);
strip.setPixelColor(0, 0, 0, 0);
strip.setPixelColor(1, 0, 0, 0);
strip.setPixelColor(2, 0, 0, 0);
strip.setPixelColor(3, 0, 0, 0);
strip.show();
delay(DELAY_FLASH_FAST);
strip.setPixelColor(0, 0, 0, 0);
strip.setPixelColor(1, 0, 0, 0);
strip.setPixelColor(2, 255, 128, 0);
strip.setPixelColor(3, 255, 128, 0);
strip.show();
delay(DELAY_FLASH_FAST);
strip.setPixelColor(0, 0, 0, 0);
strip.setPixelColor(1, 0, 0, 0);
strip.setPixelColor(2, 0, 0, 0);
strip.setPixelColor(3, 0, 0, 0);
strip.show();
delay(DELAY_FLASH_FAST);
}
delay(DELAY_FLASH_SLOW);
}
Serial.println("Restarting");
ESP.restart();
}
}
Serial.print("Alive for ");
Serial.print(millis()/1000);
Serial.println(" seconds");
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3,0);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3,0);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0,0);
}
uint8_t red(uint32_t c) {
return (c >> 16);
}
uint8_t green(uint32_t c) {
return (c >> 8);
}
uint8_t blue(uint32_t c) {
return (c);
}