forked from khoih-prog/EthernetWebServer_STM32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UdpSendReceive.ino
121 lines (96 loc) · 4.47 KB
/
UdpSendReceive.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
/****************************************************************************************************************************
UDPSendReceive.ino - Simple Arduino web server sample for ESP8266/ESP32 AT-command shield
For STM32 with built-in Ethernet (Nucleo-144, DISCOVERY, etc) or W5x00/ENC28J60 Ethernet
EthernetWebServer_STM32 is a library for the STM32 run built-in Ethernet WebServer
Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
Built by Khoi Hoang https://github.com/khoih-prog/EthernetWebServer_STM32
Licensed under MIT license
Version: 1.0.5
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 26/02/2020 Initial coding for STM32 with built-in Ethernet (Nucleo-144, DISCOVERY, etc) and ENC28J60
1.0.1 K Hoang 28/02/2020 Add W5x00 Ethernet shields using Ethernet library
1.0.2 K Hoang 05/03/2020 Remove dependency on functional-vlpp
1.0.3 K Hoang 22/07/2020 Fix bug not closing client and releasing socket. Add features.
1.0.4 K Hoang 23/07/2020 Add support to all STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash.
1.0.5 K Hoang 16/09/2020 Add support to Ethernet2, Ethernet3, Ethernet Large for W5x00
Add support to new EthernetENC library for ENC28J60. Add debug feature.
*****************************************************************************************************************************/
#include "defines.h"
unsigned int localPort = 1883; //10002; // local port to listen on
char packetBuffer[255]; // buffer to hold incoming packet
char ReplyBuffer[] = "ACK"; // a string to send back
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("\nStart UDPSendReceive on " + String(BOARD_NAME) + ", using " + String(SHIELD_TYPE));
ET_LOGWARN3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN);
ET_LOGWARN(F("Default SPI pinout:"));
ET_LOGWARN1(F("MOSI:"), MOSI);
ET_LOGWARN1(F("MISO:"), MISO);
ET_LOGWARN1(F("SCK:"), SCK);
ET_LOGWARN1(F("SS:"), SS);
ET_LOGWARN(F("========================="));
#if !(USE_BUILTIN_ETHERNET || USE_UIP_ETHERNET)
// For other boards, to change if necessary
#if ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC )
// Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries
Ethernet.init (USE_THIS_SS_PIN);
#elif USE_ETHERNET3
// Use MAX_SOCK_NUM = 4 for 4K, 2 for 8K, 1 for 16K RX/TX buffer
#ifndef ETHERNET3_MAX_SOCK_NUM
#define ETHERNET3_MAX_SOCK_NUM 4
#endif
Ethernet.setCsPin (USE_THIS_SS_PIN);
Ethernet.init (ETHERNET3_MAX_SOCK_NUM);
#elif USE_CUSTOM_ETHERNET
// You have to add initialization for your Custom Ethernet here
// This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough
//Ethernet.init(USE_THIS_SS_PIN);
#endif //( ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC )
#endif
// start the ethernet connection and the server:
// Use DHCP dynamic IP and random mac
uint16_t index = millis() % NUMBER_OF_MAC;
// Use Static IP
//Ethernet.begin(mac[index], ip);
Ethernet.begin(mac[index]);
// you're connected now, so print out the data
Serial.print(F("You're connected to the network, IP = "));
Serial.println(Ethernet.localIP());
Serial.println(F("\nStarting connection to server..."));
// if you get a connection, report back via serial:
Udp.begin(localPort);
Serial.print(F("Listening on port "));
Serial.println(localPort);
}
void loop()
{
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize)
{
Serial.print(F("Received packet of size "));
Serial.println(packetSize);
Serial.print(F("From "));
IPAddress remoteIp = Udp.remoteIP();
Serial.print(remoteIp);
Serial.print(F(", port "));
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
int len = Udp.read(packetBuffer, 255);
if (len > 0)
{
packetBuffer[len] = 0;
}
Serial.println(F("Contents:"));
Serial.println(packetBuffer);
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
}