]> git.gir.st - Ledberg-ESP8266.git/blob - Udp/Udp.ino
initial commit
[Ledberg-ESP8266.git] / Udp / Udp.ino
1 /*
2 firmware for ESP-Ledberg mod. Based on UDPSendReceive.pde
3 set STASSID and STAPSK; will listen on port 1337.
4 (c) 2019 Tobias Girstmair, GPLv3
5 https://gir.st/blog/esp8266-ledberg.htm
6 */
7
8
9 #include <ESP8266WiFi.h>
10 #include <WiFiUdp.h>
11 typedef unsigned char uint8;
12
13 #ifndef STASSID
14 #define STASSID "FIXME"
15 #define STAPSK "FIXME"
16 #endif
17
18 unsigned int localPort = 1337; // local port to listen on
19 uint8 rgb[3] = {0xff, 0x6b, 0x55}; // gives nice, slightly warm, white on boot
20 int active = 1;
21 const int red = 14;
22 const int grn = 12;
23 const int blu = 13;
24
25 WiFiUDP Udp;
26
27 #define ACK 0x06
28 #define NAK 0x15
29
30 void setup() {
31 pinMode(red, OUTPUT);
32 pinMode(grn, OUTPUT);
33 pinMode(blu, OUTPUT);
34 Serial.begin(115200);
35 WiFi.mode(WIFI_STA);
36 WiFi.begin(STASSID, STAPSK);
37 while (WiFi.status() != WL_CONNECTED) {
38 Serial.print('.');
39 delay(500);
40 }
41 Serial.print("Connected! IP address: ");
42 Serial.println(WiFi.localIP());
43 Serial.printf("UDP server on port %d\n", localPort);
44 Udp.begin(localPort);
45 }
46
47 void loop() {
48 // if there's data available, read a packet
49 int packetSize = Udp.parsePacket();
50 if (packetSize) {
51 uint8 response = NAK;
52 Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
53
54 // read the packet into packetBufffer
55 uint8 type;
56 Udp.read(&type, 1);
57 switch (type) {
58 case 0: // get current power,r,g,b
59 Udp.write(ACK);
60 Udp.write(active);
61 Udp.write(rgb[0]);
62 Udp.write(rgb[1]);
63 Udp.write(rgb[2]);
64 break;
65 case 1: // set on/off
66 if (packetSize == 2) { //type+power
67 active = Udp.read();
68 Udp.write(ACK);
69 } else {
70 Udp.write(NAK);
71 }
72 break;
73 case 2: // set rgb
74 if (packetSize == 4) { //type+r+g+b
75 uint8 tmp[3];
76 Udp.read(tmp, 3);
77 if (tmp[0]+tmp[1]+tmp[2] > 512) {
78 Udp.write(NAK);
79 break;
80 }
81 for (int i = 0; i < 3; i++) {
82 rgb[i] = tmp[i];
83 }
84 Udp.write(ACK);
85 } else {
86 Udp.write(NAK);
87 }
88 break;
89 case 3: // save color to eeprom
90 Udp.write(NAK); //TODO: not implemented
91 break;
92 case 4: // set ssid and wpa2psk; write to non-volatile memory
93 Udp.write(NAK); //TODO: not implemented
94 break;
95 case 5: //set static ip / dhcp
96 Udp.write(NAK); //TODO: not implemented
97 break;
98 default: //send nak
99 Udp.write(NAK);
100 }
101 Udp.endPacket();
102 if (active) {
103 analogWrite(red, rgb[0]<<2);
104 analogWrite(grn, rgb[1]<<2);
105 analogWrite(blu, rgb[2]<<2);
106 } else {
107 analogWrite(red, 0);
108 analogWrite(grn, 0);
109 analogWrite(blu, 0);
110 }
111 }
112 delay(10);
113 }
Imprint / Impressum