~jojo/jojos-hue

ref: 55988d34443b99b70f84962cd44595e8119f830f jojos-hue/client/client.ino -rw-r--r-- 5.2 KiB
55988d34JoJo web ui w color picker & esp32 works & a month ago
                                                                                
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <FastLED.h>

#define UNO_WS2812B
// #define ESP32_WS2801

// vvvvvvv UNIT vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// ---------------------------------------------------------
#if                               defined(UNO_WS2812B)
#define LED_PINS 8
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 48
#define MAX_MILLIAMPS 2000
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
#define CONN_ETH
// ---------------------------------------------------------
#elif                             defined(ESP32_WS2801)
#define LED_PINS G33, G23
#define LED_TYPE WS2801
#define COLOR_ORDER RGB
#define NUM_LEDS 50
#define MAX_MILLIAMPS 3000
#define CONN_WIFI
// ---------------------------------------------------------
#                                 else
#error No unit defined (e.g. ESP32_WS2801)
#endif
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


// vvvvvvv Connectivity vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// ---------------------------------------------------------
#if                               defined(CONN_ETH)
#include <Ethernet.h>
EthernetClient client;
#define NET
// ---------------------------------------------------------
#elif                             defined(CONN_WIFI)
#include <WiFi.h>
// #include <WiFiClient.h>
// #include <WebServer.h>
// #include <ESPmDNS.h>
#include "secrets.h"
WiFiClient client;
#define NET
// ---------------------------------------------------------
#                                 else
// ...
#endif

#ifdef NET
#define SERVER_IP "192.168.0.41"
#define SERVER_PORT 7017
#endif

#define NUM_BYTES (3*NUM_LEDS)

CRGB leds[NUM_LEDS];

void setAll(CRGB c) {
    for (uint16_t i = 0 ; i < NUM_LEDS; i++) {
        leds[i] = c;
    }
}

void setEach(byte* buf) {
    for (uint16_t i = 0; i < NUM_LEDS; i++) {
        leds[i] = CRGB(buf[3*i+0], buf[3*i+1], buf[3*i+2]);
    }
}

void setup() {
    Serial.begin(115200);
    Serial.println("hello!");
#if defined(CONN_ETH)
    Ethernet.init(SS);
    Ethernet.begin(mymac);
    Serial.println("online");
    Serial.print("IP address: ");
    Serial.println(Ethernet.localIP());
#elif defined(CONN_WIFI)
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(300);
        Serial.print(".");
    }
    Serial.print("\nConnected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    // if (MDNS.begin("esp32")) {
    //     Serial.println("MDNS responder started");
    // }
#endif

    FastLED.addLeds<LED_TYPE, LED_PINS, COLOR_ORDER>(leds, NUM_LEDS)
        .setCorrection(TypicalLEDStrip);
    FastLED.setMaxPowerInVoltsAndMilliamps(5, MAX_MILLIAMPS);
    CRGB initCols[] = {
        CRGB(255, 0, 0),
        CRGB(0, 255, 0),
        CRGB(0, 0, 255),
        CRGB(255, 255, 255),
        CRGB(0, 0, 0)
    };
    for (uint8_t i = 0; i < sizeof(initCols)/sizeof(CRGB); i++) {
        setAll(initCols[i]);
        FastLED.show();
        delay(1000);
    }
    Serial.println("setup finished");
}

#ifdef NET
bool connected() {
    return (bool)client.connected();
}
bool connect() {
    return (bool)client.connect(SERVER_IP, SERVER_PORT);
}
uint16_t readMax(uint16_t n, byte* buf) {
    int r = client.read(buf, n);
    return max(0, r);
}

uint16_t writeMax(uint16_t n, const byte* buf) {
    int r = client.write(buf, n);
    return max(0, r);
}
#endif

void writeExact(uint16_t, byte*);

void ensureConnected() {
    if (!connected()) {
        Serial.println("disconnected. connecting...");
        uint16_t tries = 0;
        const uint16_t max_tries = 200;
        while (!connect()) {
            if (tries == max_tries) {
                Serial.println("server seems to be offline");
                Serial.println("will turn off leds and keep trying");
                setAll(CRGB(0, 0, 0));
                FastLED.show();
            }
            tries = min(max_tries, tries) + 1;
            delay(300);
        }
        uint16_t x = NUM_LEDS;
        // If the write fails, fuck it. The server will just timeout the read and
        // disconnect us again and we'll retry.
        writeMax(2, (byte*)&x);
        Serial.println("connected");
    }
}

void readExact(uint16_t n, byte* buf) {
    while (n > 0) {
        uint16_t m = readMax(n, buf);
        if (m == 0) {
            ensureConnected();
            delay(10);
        }
        n -= m;
        buf += m;
    }
}

byte readSingle() {
    byte x;
    readExact(1, &x);
    return x;
}

#define MAGIC_COOKIE_LEN 5
const byte magicCookie[MAGIC_COOKIE_LEN] = {'s','a','t','a','n'};

bool isMagicCookie(byte* buf) {
    uint16_t i = 0;
    for (uint16_t i = 0; i < MAGIC_COOKIE_LEN; i++) {
        if (magicCookie[i] != buf[i]) {
            return false;
        }
    }
    return true;
}

void sync() {
    byte buf[MAGIC_COOKIE_LEN];
    readExact(MAGIC_COOKIE_LEN, buf);
    if (isMagicCookie(buf)) {
        return;
    }
    Serial.println("out of sync");
    while (!isMagicCookie(buf)) {
        uint16_t i = 0;
        for (; i < MAGIC_COOKIE_LEN; i++) {
            buf[i] = buf[i+1];
        }
        buf[i] = readSingle();
    }
}

byte frame[NUM_BYTES];

void readFrame() {
    sync();
    readExact(NUM_BYTES, frame);
    setEach(frame);
}

void loop() {
    readFrame();
    FastLED.show();
}