Adding config file for sharing.
[iot2.git] / benchmarks / other / MicrocontrollerCode / esp8266_sprinkler / esp_sprinkler / esp_sprinkler.ino
1 #include <ESP8266WiFi.h>
2 #include <WiFiUdp.h>
3
4 // wifi credentials
5 #define WIFI_SSID   ("LEDE")
6 #define WIFI_PASSWD ("1qaz2wsx3edcEsp")
7
8 // the port the UDP will use.
9 #define UDP_PORT (5556)
10
11 // UDP connection
12 WiFiUDP udp;
13
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];
19
20 unsigned long prevMillis = 0;
21
22 // Function Declarations
23 void parseUdpData(char* _data, int _length);
24 void setPin(int _pin, int _state, int _duration);
25 void sendPinInfo();
26
27 void setup()
28 {
29     // Enable the Serial Connection
30     Serial.begin(115200);
31     delay(100);
32
33     // We start by connecting to a WiFi network
34     Serial.println();
35     Serial.println();
36     Serial.print("Connecting to ");
37     Serial.println(WIFI_SSID);
38
39     // WiFi.begin(ssid, password);
40     WiFi.begin(WIFI_SSID, WIFI_PASSWD);
41
42
43     while (WiFi.status() != WL_CONNECTED)
44     {
45         delay(500);
46         Serial.print(".");
47     }
48
49     Serial.println("");
50     Serial.println("WiFi connected");
51     Serial.println("IP address: ");
52     Serial.println(WiFi.localIP());
53
54     // start the UDP on the specific port
55     udp.begin(UDP_PORT);
56
57     // Setup the pins for output
58     for (int i = 0; i < NUMBER_OF_OUTPUTS; ++i)
59     {
60         pinMode(outputsPins[i], OUTPUT);
61         digitalWrite(outputsPins[i], LOW);
62         outputPinsState[i] = 0;
63     }
64 }
65
66
67 void loop()
68 {
69     // Check if there is a packet available
70     int packetSize = udp.parsePacket();
71
72     // there is data
73     if (packetSize > 0)
74     {
75         IPAddress remoteIp = udp.remoteIP();
76
77         //buffer to hold incoming packet
78         char packetBuffer[1024];
79         memset(packetBuffer, 0, 1024);
80
81         int len = udp.read(packetBuffer, 255);
82         parseUdpData(packetBuffer, len);
83     }
84
85     if (prevMillis == 0)
86     {
87         prevMillis = millis();
88         return;
89     }
90
91     unsigned long currentMillis = millis();
92
93     // Handle rollover
94     if (prevMillis >= currentMillis)
95     {
96         prevMillis = currentMillis;
97         return;
98     }
99
100     // get the millisecond difference
101     unsigned long difference = (currentMillis - prevMillis);
102
103     // wait 1 second
104     if ( difference > 1000)
105     {
106         // update the previous millis to current millis
107         prevMillis = currentMillis;
108
109         for (int i = 0; i < NUMBER_OF_OUTPUTS; i++)
110         {
111             if ((outputPinsState[i] == 1) && (outputPinsDuration[i] != -1))
112             {
113                 outputPinsRemainingDuration[i] -= difference;
114
115                 if (outputPinsRemainingDuration[i] <= 0)
116                 {
117                     setPin(i, 0, -1);
118                 }
119             }
120         }
121     }
122 }
123
124
125
126 void parseUdpData(char* _data, int _length)
127 {
128     // Packet is not large enough for any valid command
129     if (_length < 3)
130     {
131         return;
132     }
133
134     Serial.println("packetDataArrived");
135
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);
141
142     String commandType = data.substring(0, 3);
143
144     if (commandType.equals("GET"))
145     {
146         Serial.println("GET Command");
147         sendPinInfo();
148     }
149     else if (commandType.equals("SET"))
150     {
151         Serial.println("SET Command");
152
153         // count the number of commas present
154         int commaCount = 0;
155         for (int i = 0; i < data.length(); i++)
156         {
157             if (data.charAt(i) == ',')
158             {
159                 commaCount++;
160             }
161         }
162
163         // data is incorrectly formated
164         if (commaCount != 3)
165         {
166             return;
167         }
168
169         // find the comma positions
170         int commaPos[3];
171         for (int i = 0; i < 3; i++)
172         {
173             if (i == 0)
174             {
175                 commaPos[i] = data.indexOf(',');
176             }
177             else
178             {
179                 commaPos[i] = data.indexOf(',', commaPos[i - 1] + 1);
180             }
181         }
182
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());
187
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());
192
193         // get rid of the leading and trailing white spaces
194         a.trim();
195         b.trim();
196         c.trim();
197
198         // Convert to an integer
199         int aInt = a.toInt();
200         int bInt = b.toInt();
201         int cInt = c.toInt();
202
203         // set the pins
204         // convert pin number from starting at 1 to starting at 0
205         setPin(aInt - 1, bInt, cInt);
206     }
207 }
208
209
210
211 void setPin(int _pin, int _state, int _duration)
212 {
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);
220     // Serial.println();
221
222
223     // update the pin state info
224     outputPinsState[_pin] = _state;
225     outputPinsDuration[_pin] = _duration;
226     outputPinsRemainingDuration[_pin] = _duration;
227
228     // set the pin mode
229     if (_state == 0)
230     {
231         digitalWrite(outputsPins[_pin], LOW);
232     }
233     else
234     {
235         digitalWrite(outputsPins[_pin], HIGH);
236     }
237 }
238
239
240
241 void sendPinInfo()
242 {
243     String retString = "";
244
245     for (int i = 0; i < NUMBER_OF_OUTPUTS; i++)
246     {
247         retString += (i + 1);
248         retString += ", ";
249         retString += outputPinsState[i];
250         retString += ", ";
251         retString += outputPinsDuration[i];
252         retString += "\n";
253     }
254
255     udp.beginPacket(udp.remoteIP(), udp.remotePort());
256     udp.write(retString.c_str());
257     udp.endPacket();
258
259     Serial.println(retString);
260 }
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280