1 #include <ESP8266WiFi.h>
5 #define WIFI_SSID ("LEDE")
6 #define WIFI_PASSWD ("1qaz2wsx3edcEsp")
8 // the port the UDP will use.
9 #define UDP_PORT (5556)
14 #define NUMBER_OF_OUTPUTS (9)
15 int outputsPins[9] = {13, 12, 14, 16, 15, 2, 0, 4, 5};
16 int outputPinsState[9];
17 int outputPinsDuration[9];
18 unsigned long outputPinsRemainingDuration[9];
20 unsigned long prevMillis = 0;
22 // Function Declarations
23 void parseUdpData(char* _data, int _length);
24 void setPin(int _pin, int _state, int _duration);
29 // Enable the Serial Connection
33 // We start by connecting to a WiFi network
36 Serial.print("Connecting to ");
37 Serial.println(WIFI_SSID);
39 // WiFi.begin(ssid, password);
40 WiFi.begin(WIFI_SSID, WIFI_PASSWD);
43 while (WiFi.status() != WL_CONNECTED)
50 Serial.println("WiFi connected");
51 Serial.println("IP address: ");
52 Serial.println(WiFi.localIP());
54 // start the UDP on the specific port
57 // Setup the pins for output
58 for (int i = 0; i < NUMBER_OF_OUTPUTS; ++i)
60 pinMode(outputsPins[i], OUTPUT);
61 digitalWrite(outputsPins[i], LOW);
62 outputPinsState[i] = 0;
69 // Check if there is a packet available
70 int packetSize = udp.parsePacket();
75 IPAddress remoteIp = udp.remoteIP();
77 //buffer to hold incoming packet
78 char packetBuffer[1024];
79 memset(packetBuffer, 0, 1024);
81 int len = udp.read(packetBuffer, 255);
82 parseUdpData(packetBuffer, len);
87 prevMillis = millis();
91 unsigned long currentMillis = millis();
94 if (prevMillis >= currentMillis)
96 prevMillis = currentMillis;
100 // get the millisecond difference
101 unsigned long difference = (currentMillis - prevMillis);
104 if ( difference > 1000)
106 // update the previous millis to current millis
107 prevMillis = currentMillis;
109 for (int i = 0; i < NUMBER_OF_OUTPUTS; i++)
111 if ((outputPinsState[i] == 1) && (outputPinsDuration[i] != -1))
113 outputPinsRemainingDuration[i] -= difference;
115 if (outputPinsRemainingDuration[i] <= 0)
126 void parseUdpData(char* _data, int _length)
128 // Packet is not large enough for any valid command
134 Serial.println("packetDataArrived");
136 // convert into a String object for easy processing
137 char dataTmp[_length + 1];
138 dataTmp[_length] = 0;
139 memcpy(dataTmp, _data, _length);
140 String data = String(dataTmp);
142 String commandType = data.substring(0, 3);
144 if (commandType.equals("GET"))
146 Serial.println("GET Command");
149 else if (commandType.equals("SET"))
151 Serial.println("SET Command");
153 // count the number of commas present
155 for (int i = 0; i < data.length(); i++)
157 if (data.charAt(i) == ',')
163 // data is incorrectly formated
169 // find the comma positions
171 for (int i = 0; i < 3; i++)
175 commaPos[i] = data.indexOf(',');
179 commaPos[i] = data.indexOf(',', commaPos[i - 1] + 1);
183 // split the string into the value positions based on the commas
184 String a = data.substring(commaPos[0], commaPos[1]);
185 String b = data.substring(commaPos[1], commaPos[2]);
186 String c = data.substring(commaPos[2], data.length());
188 // remove the commas from the string
189 a = a.substring(1, a.length());
190 b = b.substring(1, b.length());
191 c = c.substring(1, c.length());
193 // get rid of the leading and trailing white spaces
198 // Convert to an integer
199 int aInt = a.toInt();
200 int bInt = b.toInt();
201 int cInt = c.toInt();
204 // convert pin number from starting at 1 to starting at 0
205 setPin(aInt - 1, bInt, cInt);
211 void setPin(int _pin, int _state, int _duration)
213 // Serial.print(_pin);
214 // Serial.print(" ,");
215 // Serial.print(outputsPins[_pin]);
216 // Serial.print(" ,");
217 // Serial.print(_state);
218 // Serial.print(" ,");
219 // Serial.print(_duration);
223 // update the pin state info
224 outputPinsState[_pin] = _state;
225 outputPinsDuration[_pin] = _duration;
226 outputPinsRemainingDuration[_pin] = _duration;
231 digitalWrite(outputsPins[_pin], LOW);
235 digitalWrite(outputsPins[_pin], HIGH);
243 String retString = "";
245 for (int i = 0; i < NUMBER_OF_OUTPUTS; i++)
247 retString += (i + 1);
249 retString += outputPinsState[i];
251 retString += outputPinsDuration[i];
255 udp.beginPacket(udp.remoteIP(), udp.remotePort());
256 udp.write(retString.c_str());
259 Serial.println(retString);