-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUdp.ino
More file actions
63 lines (61 loc) · 1.78 KB
/
Udp.ino
File metadata and controls
63 lines (61 loc) · 1.78 KB
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
bool GetUdpAdress() {
bool success = false;
#if PIONEER
success = udpSendMessage(udpHost, "!pECNQSTN", port);
#else //Onkyo
success = udpSendMessage(udpHost, "!xECNQSTN", port);
#endif
return success;
}
//******UDP******
bool udpSendMessage(IPAddress ipAddr, String udpMsg, int udpPort) {
WiFiUDP udpClientServer;
// Start UDP client for sending packets
if (udpClientServer.begin(udpPort) == 0) {
return false;
}
if (udpClientServer.beginPacket(ipAddr, udpPort) == 0) { // Problem occured!
udpClientServer.stop();
#if DEBUG
Serial.println("UDP connection failed");
#endif
return false;
}
//Create Message with Header & Footer
SetCommand(udpMsg);
//Bytes to send:
uint8_t bts[GetMessageLength()];
CreateCommand(bts);
int bytesSent = udpClientServer.write(bts,GetMessageLength());
if (bytesSent == GetMessageLength()) {
udpClientServer.endPacket();
udpClientServer.flush();
//Receive Response
int packetSize = udpClientServer.parsePacket();
unsigned long timeout = millis();
while (!packetSize) {
if (millis() - timeout > UdpTimeout) {
#if DEBUG
Serial.println("UdpPackage Timeout");
#endif
break;
}
packetSize = udpClientServer.parsePacket();
}
if (packetSize) {
char packetBuffer[255];
int len = udpClientServer.read(packetBuffer, 255);
host = udpClientServer.remoteIP();
}
udpClientServer.flush();
udpClientServer.stop();
return packetSize > 0;
} else {
#if DEBUG
Serial.println("Failed to send " + udpMsg + ", sent " + String(bytesSent) + " of " + String(udpMsg.length()) + " bytes");
#endif
udpClientServer.endPacket();
udpClientServer.stop();
return false;
}
}