forked from jeecrypt/JeeUI2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpecho.cpp
49 lines (41 loc) · 1.14 KB
/
udpecho.cpp
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
#include "JeeUI2.h"
#include <WiFiUdp.h>
WiFiUDP Udp;
bool udpApply = false;
void jeeui2::udp(){
getAPmac();
udpMessage = mc;
}
void jeeui2::udp(String message){
udpMessage = message;
}
void jeeui2::udpBegin(){
Udp.begin(localUdpPort);
if(udpMessage != "") udpApply = true;
}
void jeeui2::udpLoop(){
static bool st = false;
if(!st){
st = true;
udpBegin();
}
if(!udpApply) return;
int packetSize = Udp.parsePacket();
if (packetSize)
{
udpRemoteIP = Udp.remoteIP().toString();
if(dbg)Serial.printf("Received %d bytes from %s, port %d\n", packetSize, udpRemoteIP.c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 64);
if (len > 0)
{
incomingPacket[len] = 0;
}
if(dbg)Serial.printf("UDP packet contents: %s\n", incomingPacket);
// send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
//Udp.write(ip.c_str());
if(dbg)Serial.println("Send UDP: " + udpMessage);
Udp.print(udpMessage);
Udp.endPacket();
}
}