-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoxoneCafeEff.ino
199 lines (174 loc) · 4.46 KB
/
LoxoneCafeEff.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
192
193
194
195
196
197
198
199
// Entwickler: Willi Dueck
// Email: willi_dueck@gmx.de
//
// Programm zur Schaltung von Licht mithilfe eines Arduino MEGA2560
// und einem Ethernet Shield
// Bibliotheken laden
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoHttpClient.h>
#include "Config.h"
#include "Var.h"
#include "Light.h"
#include "Scene.h"
// Konfiguration in "Config.h" hinterlegen
// Lichtkreise und Szenen in "Var.h" hinterlegen
// globale Variablen
// zaehlt die Anzahl der Input Scans
int scanCount = 0;
// Array zum Speichern der Objekte
Light light[LIGHT_COUNT];
Scene scene[SCENE_COUNT];
// Setup Connection
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x22, 0x11 };
byte ip[] = { FALLBACK_IP };
byte gateway[] = { GATEWAY };
byte mask[] = { MASK };
char url[] = LOXONE_URL;
EthernetClient client;
HttpClient http = HttpClient(client, url, LOXONE_PORT);
// Start des Programms
void setup()
{
Serial.begin(9600);
Serial.println("setup...");
//Serial.println(ip);
//if (Ethernet.begin(mac) == 0) {
//Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, gateway, mask);
//}
Serial.print("IP Address: ");
Serial.println(Ethernet.localIP());
Serial.println("Connected!");
Serial.println("setup lights...");
int cPin = 0;
Serial.println("Lights:");
for (int i = 0; i < LIGHT_COUNT; i++) {
int led = pin[cPin++];
int button = pin[cPin++];
light[i].setup(lightNames[i], led, button);
}
Serial.println("Scenes:");
for (int i = 0; i < SCENE_COUNT; i++) {
int led = pin[cPin++];
int button = pin[cPin++];
scene[i].setup(scenes[i], led, button);
}
startSequenz();
}
void loop()
{
//Serial.println("");
//Serial.print("scanButtons");
scanButtons();
// nach 10000 button scans update led status
// scanCount++;
// if (scanCount >= 100000) {
// Serial.println("updateLeds...");
// updateLeds(http);
// scanCount = 0;
// }
}
// globale Methoden
// Methode zum Updaten der LEDs per Webrequest
void updateLeds(HttpClient http) {
for (int i = 0; i < LIGHT_COUNT; i++) {
light[i].updateLed(http);
scanButtons();
}
scanCount = 0;
}
// Methode zum Scan der Input-Buttons
void scanButtons() {
for (int i = 0; i < LIGHT_COUNT; i++) {
//Serial.print(".");
if (!digitalRead(light[i].getButtonPin())) {
Serial.print("Light Button pin: ");
Serial.println(light[i].getButtonPin());
if (light[i].isOn())
{
light[i].turnOff(http);
}
else
{
light[i].turnOn(http);
}
//light[i].updateLed(http);
}
}
for (int i = 0; i < SCENE_COUNT; i++) {
//Serial.print(".");
if (!digitalRead(scene[i].getButtonPin())) {
Serial.print("Scene Button pin: ");
Serial.println(scene[i].getButtonPin());
int* sc = scene[i].get();
if (sceneCount[i] == 0) { // alles aus
scene[i].turnLedOn();
allOff(false);
scene[i].turnLedOff();
szeneLedsOff();
} else {
if (scene[i].isOn()) {
scene[i].turnLedOff();
for (int j = 0; j < sceneCount[i]; j++) {
light[sc[j]].turnOff(http);
}
} else {
scene[i].turnLedOn();
// allOff(true);
for (int j = 0; j < sceneCount[i]; j++) {
light[sc[j]].turnOn(http);
}
}
}
while(!digitalRead(scene[i].getButtonPin())) {
// warte bis losgelassen
}
}
}
}
void allOff(bool exclude) {
Serial.println("AllOff");
for (int i = 0; i < LIGHT_COUNT; i++) {
if (exclude) {
for (int j = 0; j < (sizeof(excluded)/sizeof(int)); j++) {
if (i == excluded[j]) {
return;
}
}
}
light[i].turnOff(http);
}
}
void szeneLedsOff() {
for (int i = 0; i < SCENE_COUNT; i++) {
scene[i].turnLedOff();
}
}
void startSequenz() { // nur zur show
int ledCount = (LIGHT_COUNT + SCENE_COUNT) * 2;
int leds[ledCount];
int j = 0;
for (int i = 0; i < ledCount; i += 2) {
leds[j++] = pin[i];
digitalWrite(pin[i], HIGH);
delay(30);
}
delay(100);
for (j; j >= 0; j--) {
digitalWrite(leds[j], LOW);
delay(30);
}
for (int i = 0; i < 5; i++){
for (int i = 0; i < ledCount; i += 2) {
digitalWrite(pin[i], HIGH);
}
delay(100);
for (int i = 0; i < ledCount; i += 2) {
digitalWrite(pin[i], LOW);
}
delay(100);
}
}