This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
UdpSendReceive_LAN8720.ino
94 lines (75 loc) · 3.37 KB
/
UdpSendReceive_LAN8720.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
/****************************************************************************************************************************
UDPSendReceive_LAN8720.ino - Simple Arduino web server sample for ESP8266/ESP32 AT-command shield
For STM32 with built-in Ethernet LAN8742A (Nucleo-144, DISCOVERY, etc) or W5x00/ENC28J60 shield/module
EthernetWebServer_STM32 is a library for the STM32 running 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
*****************************************************************************************************************************/
/*
Currently support
1) STM32 boards with built-in LAN8742A Ethernet (to use USE_BUILTIN_ETHERNET = true) such as :
- Nucleo-144 (F429ZI, F767ZI)
- Discovery (STM32F746G-DISCOVERY)
- STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet,
- See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1)
2) STM32 boards with LAN8720 Ethernet (to use USE_BUILTIN_ETHERNET and USING_LAN8720 = true) such as :
- BLACK_F407XX (BLACK_F407VE, BLACK_F407VG, BLACK_F407ZE, BLACK_F407ZG, BLACK_F407VE-mini), NUCLEO_F429ZI, DISCO_F746NG,
NUCLEO_F7x6ZG
3) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false)
4) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 shields
*/
#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);
delay(2000);
Serial.println("\nStart UDPSendReceive_LAN8720 on " + String(BOARD_NAME) + ", using " + String(SHIELD_TYPE));
Serial.println(ETHERNET_WEBSERVER_STM32_VERSION);
// 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();
}
}