summaryrefslogtreecommitdiff
path: root/luminate.ino
blob: 2f69b3136d8c03564294efaffc90156a99b845b1 (plain)
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
#include <ESP8266WiFi.h>

const char *ssid = "SSID";
const char *password = "PASSWORD";

#define MAX_CLIENTS 5
WiFiServer server(23);
WiFiClient clients[MAX_CLIENTS];

void setup (void) {
  int32_t channel = 0;
  uint8_t bssid[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};

  pinMode(2, OUTPUT);
  pinMode(0, OUTPUT);
  
  // channel, bssid and IPAddress are optional but speed up the boot process
  WiFi.begin(ssid, password, channel, bssid);
  WiFi.config(IPAddress(0, 0, 0, 0), IPAddress(0, 0, 0, 0), IPAddress(255, 255, 255, 0));
  
  while(WiFi.status() != WL_CONNECTED)
    delay(500);
   
  server.begin();
  server.setNoDelay(true);
}

void loop(void) {
  uint8_t i;
  
  if (server.hasClient()) {
    for (i=0; i<MAX_CLIENTS; i++) {
      if (!clients[i] || !clients[i].connected()) {
        if (clients[i]) clients[i].stop();
        clients[i] = server.available();
        continue;
      }
    }
    server.available().stop();  // reject, no slots free
  }

  for (i=0; i<MAX_CLIENTS; i++) {
    if (clients[i] && clients[i].connected()) {
      uint8_t r;
      while (clients[i].available()) {
        r = clients[i].read();
        switch (r) {
          case 65:  // 'A'
            digitalWrite(2, HIGH); break;
          case 66:  // 'B'
            digitalWrite(0, HIGH); break;
          case 97:  // 'a'
            digitalWrite(2, LOW); break;
          case 98:  // 'b'
            digitalWrite(0, LOW); break;
          default:
            clients[i].write(r);  // echo back
        }
        clients[i].write(10);  // line feed
      }
    }
  }

  delay(500);
}