/
esp8266_udpServer_01.ino
243 lines (220 loc) · 6.04 KB
/
esp8266_udpServer_01.ino
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define GPIO5 5
#define GPIO4 4
#define GPIO0 0
#define GPIO2 2
#define GPIO15 15
#define GPIO13 13
#define GPIO12 12
#define GPIO14 14
#define GPIO16 16
extern "C" {
#include "user_interface.h"
}
int status = WL_IDLE_STATUS;
const char* ssid = "IoT-eCat";
const char* pass = "clotClot";
unsigned int udpPortRx = 3334;
unsigned int udpPortTx = 3333;
byte packetBuffer[512]; //buffer to hold incoming and outgoing packets
WiFiUDP Udp;
void vSendUdp(String sz, unsigned int uiLocalPort) {
Udp.beginPacket(Udp.remoteIP(), uiLocalPort);
Udp.print(sz);
Udp.endPacket();
}
bool bManageMsg(String szMsg) {
unsigned int nLdr = analogRead(A0);
bool bUnderstood = false;
if (szMsg == "a" || szMsg == "A") {
Serial.print("LDR level (0..1024): ");
Serial.println(nLdr);
vSendUdp(String(nLdr), udpPortTx);
bUnderstood = true;
}
if (szMsg == "i" || szMsg == "I") {
if (digitalRead(GPIO4)){
Serial.println("Button is not pressed: GPIO4 HIGH");
vSendUdp("H", udpPortTx);
}else{
Serial.println("Button is pressed: GPIO4 LOW");
vSendUdp("L", udpPortTx);
}
bUnderstood = true;
}
if (szMsg == "5h" || szMsg == "5H") {
digitalWrite(GPIO5, HIGH);
Serial.println("GPIO5 HIGH");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "5l" || szMsg == "5L") {
digitalWrite(GPIO5, LOW);
Serial.println("GPIO5 LOW");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "4h" || szMsg == "4H") {
digitalWrite(GPIO4, HIGH);
Serial.println("GPIO4 HIGH");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "4l" || szMsg == "4L") {
digitalWrite(GPIO4, LOW);
Serial.println("GPIO4 LOW");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "0h" || szMsg == "0H") {
digitalWrite(GPIO0, HIGH);
Serial.println("GPIO0 HIGH");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "0l" || szMsg == "0L") {
digitalWrite(GPIO0, LOW);
Serial.println("GPIO0 LOW");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "2h" || szMsg == "2H") {
digitalWrite(GPIO2, HIGH);
Serial.println("GPIO2 HIGH");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "2l" || szMsg == "2L") {
digitalWrite(GPIO2, LOW);
Serial.println("GPIO2 LOW");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "15h" || szMsg == "15H") {
digitalWrite(GPIO15, HIGH);
Serial.println("GPIO15 HIGH -> Red ON");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "15l" || szMsg == "15L") {
digitalWrite(GPIO15, LOW);
Serial.println("GPIO15 LOW -> Red OFF");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "13h" || szMsg == "13H") {
digitalWrite(GPIO13, HIGH);
Serial.println("GPIO13 HIGH -> Blue ON");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "13l" || szMsg == "13L") {
digitalWrite(GPIO13, LOW);
Serial.println("GPIO13 LOW -> Blue OFF");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "12h" || szMsg == "12H") {
digitalWrite(GPIO12, HIGH);
Serial.println("GPIO12 HIGH -> Green ON");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "12l" || szMsg == "12L") {
digitalWrite(GPIO12, LOW);
Serial.println("GPIO12 LOW -> Green OFF");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "14h" || szMsg == "14H") {
digitalWrite(GPIO14, HIGH);
Serial.println("GPIO14 HIGH");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "14l" || szMsg == "14L") {
digitalWrite(GPIO14, LOW);
Serial.println("GPIO14 LOW");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "16h" || szMsg == "16H") {
digitalWrite(GPIO16, HIGH);
Serial.println("GPIO16 HIGH");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
if (szMsg == "16l" || szMsg == "16L") {
digitalWrite(GPIO16, LOW);
Serial.println("GPIO16 LOW");
bUnderstood = true;
vSendUdp("OK", udpPortTx);
}
return bUnderstood;
}
void setup(){
pinMode(GPIO2, OUTPUT); // Internal blue led
pinMode(GPIO15, OUTPUT); // Red of RGB led
pinMode(GPIO13, OUTPUT); // Blue of RGB led
pinMode(GPIO12, OUTPUT); // Green of RGB led
Serial.begin(115200);
WiFi.begin(ssid, pass);
Serial.print("[Connecting]");
Serial.print(ssid);
int tries = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
tries++;
if (tries > 30) {
break;
}
}
Serial.println();
printWifiStatus();
Serial.println("Connected to wifi");
Serial.print("Udp server started at port ");
Serial.println(udpPortRx);
Udp.begin(udpPortRx);
}
void loop(){
int noBytes = Udp.parsePacket();
String received_command = "";
if ( noBytes ) {
Serial.print(millis() / 1000);
Serial.print(":Packet of ");
Serial.print(noBytes);
Serial.print(" received from ");
Serial.print(Udp.remoteIP());
Serial.print(":");
Serial.println(Udp.remotePort());
// We've received a packet, read the data from it
Udp.read(packetBuffer, noBytes); // read the packet into the buffer
// display the packet contents in HEX
for (int i = 1; i <= noBytes; i++) {
Serial.print(packetBuffer[i - 1], HEX);
received_command = received_command + char(packetBuffer[i - 1]);
if (i % 32 == 0) {
Serial.println();
}
else Serial.print(' ');
} // end for
Serial.println();
if(! bManageMsg(received_command)){
vSendUdp("I don't understand this command: ", udpPortTx);
vSendUdp(received_command, udpPortTx);
}
Serial.println(received_command);
Serial.println();
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi ESP8266's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}