-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp12-ikea-dioder-fhem.ino
127 lines (102 loc) · 2.93 KB
/
esp12-ikea-dioder-fhem.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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
// GPIO definitions for LEDs
#define red 14
#define green 13
#define blue 12
// Max values for PWM
#define colorOnRed 1024
#define colorOnGreen 1024
#define colorOnBlue 1024
// Hardcode WiFi parameters
const char* ssid = ".....";
const char* password = ".....";
// Define TCP Server on port 5577
WiFiServer server(5577);
WiFiClient client = server.available();
// HTTP Push Update
// curl -F "[email protected]" <esp-ip>:<port>/update
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
void setup() {
// Disable WiFi AP Mode
WiFi.mode(WIFI_STA);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
Serial.begin(115200);
WiFi.begin(ssid,password);
Serial.println("");
//Wait for connection
digitalWrite(red, HIGH);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
httpUpdater.setup(&httpServer);
httpServer.begin();
delay(10);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
delay(3000);
Serial.print("Connected to "); Serial.println(ssid);
Serial.print("IP Address: "); Serial.println(WiFi.localIP());
digitalWrite(green, LOW);
// Start the TCP server
server.begin();
Serial.println("Server started");
}
void loop() {
// For push flashing only
httpServer.handleClient();
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
// Serial.println("new client");
while(!client.available()){
delay(1);
}
int buflen = 8;
byte inputbuf[buflen];
client.readBytes(inputbuf, buflen);
client.flush();
/*
// DEBUG
Serial.print("Byte 0: "); Serial.println(inputbuf[0]);
Serial.print("byte array length: "); Serial.println(sizeof(inputbuf));
Serial.print("last byte: "); Serial.println(inputbuf[sizeof(inputbuf) - 1]);
*/
// Find startbyte (LW12 connection of FHEM's Wifilight module)
int startbyte;
for (int i = 0; i < buflen; i++) {
if (inputbuf[i] == 86 and inputbuf[i + 4] == 170) {
startbyte = i;
}
/*
// DEBUG
Serial.print(i); Serial.print(": "); Serial.println(inputbuf[i]);
*/
}
int redval = map(inputbuf[startbyte + 1], 0, 255, 0, colorOnRed);
int greenval = map(inputbuf[startbyte + 2], 0, 255, 0, colorOnGreen);
int blueval = map(inputbuf[startbyte + 3], 0, 255, 0, colorOnBlue);
/*
// DEBUG
Serial.print("redval: "); Serial.println(redval);
Serial.print("greenval: "); Serial.println(greenval);
Serial.print("blueval: "); Serial.println(blueval);
*/
analogWrite(red, redval);
analogWrite(green, greenval);
analogWrite(blue, blueval);
client.flush();
delay(1);
//Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}