|
| 1 | +/**************************************************************************************************************************** |
| 2 | + Async_UdpClient.ino |
| 3 | +
|
| 4 | + AsyncUDP_ESP32_ENC is a Async UDP library for the the ESP32 with Ethernet ENC28J60 |
| 5 | +
|
| 6 | + Based on and modified from ESPAsyncUDP Library (https://github.com/me-no-dev/ESPAsyncUDP) |
| 7 | + Built by Khoi Hoang https://github.com/khoih-prog/AsyncUDP_ESP32_ENC |
| 8 | + Licensed under GPLv3 license |
| 9 | + *****************************************************************************************************************************/ |
| 10 | + |
| 11 | +#if !( defined(ESP32) ) |
| 12 | + #error This code is designed for (ESP32 + ENC28J60) to run on ESP32 platform! Please check your Tools->Board setting. |
| 13 | +#endif |
| 14 | + |
| 15 | +#include <Arduino.h> |
| 16 | + |
| 17 | +#define ASYNC_UDP_ESP32_ENC_DEBUG_PORT Serial |
| 18 | + |
| 19 | +// Use from 0 to 4. Higher number, more debugging messages and memory usage. |
| 20 | +#define _ASYNC_UDP_ESP32_ENC_LOGLEVEL_ 1 |
| 21 | + |
| 22 | +////////////////////////////////////////////////////////// |
| 23 | + |
| 24 | +// Optional values to override default settings |
| 25 | +//#define SPI_HOST 1 |
| 26 | +//#define SPI_CLOCK_MHZ 8 |
| 27 | + |
| 28 | +// Must connect INT to GPIOxx or not working |
| 29 | +//#define INT_GPIO 4 |
| 30 | + |
| 31 | +//#define MISO_GPIO 19 |
| 32 | +//#define MOSI_GPIO 23 |
| 33 | +//#define SCK_GPIO 18 |
| 34 | +//#define CS_GPIO 5 |
| 35 | + |
| 36 | +////////////////////////////////////////////////////////// |
| 37 | + |
| 38 | +#include <AsyncUDP_ESP32_ENC.h> |
| 39 | + |
| 40 | +IPAddress remoteIPAddress = IPAddress(192, 168, 2, 112); |
| 41 | + |
| 42 | +#define UDP_REMOTE_PORT 5698 |
| 43 | + |
| 44 | +///////////////////////////////////////////// |
| 45 | + |
| 46 | +// Enter a MAC address and IP address for your controller below. |
| 47 | +#define NUMBER_OF_MAC 20 |
| 48 | + |
| 49 | +byte mac[][NUMBER_OF_MAC] = |
| 50 | +{ |
| 51 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, |
| 52 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, |
| 53 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, |
| 54 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, |
| 55 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, |
| 56 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, |
| 57 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, |
| 58 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, |
| 59 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, |
| 60 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, |
| 61 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, |
| 62 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, |
| 63 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, |
| 64 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, |
| 65 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, |
| 66 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, |
| 67 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, |
| 68 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, |
| 69 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, |
| 70 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, |
| 71 | +}; |
| 72 | + |
| 73 | +// Select the IP address according to your local network |
| 74 | +IPAddress myIP(192, 168, 2, 232); |
| 75 | +IPAddress myGW(192, 168, 2, 1); |
| 76 | +IPAddress mySN(255, 255, 255, 0); |
| 77 | + |
| 78 | +// Google DNS Server IP |
| 79 | +IPAddress myDNS(8, 8, 8, 8); |
| 80 | + |
| 81 | +///////////////////////////////////////////// |
| 82 | + |
| 83 | +AsyncUDP udp; |
| 84 | + |
| 85 | +void parsePacket(AsyncUDPPacket packet) |
| 86 | +{ |
| 87 | + Serial.print("UDP Packet Type: "); |
| 88 | + Serial.print(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast" : "Unicast"); |
| 89 | + Serial.print(", From: "); |
| 90 | + Serial.print(packet.remoteIP()); |
| 91 | + Serial.print(":"); |
| 92 | + Serial.print(packet.remotePort()); |
| 93 | + Serial.print(", To: "); |
| 94 | + Serial.print(packet.localIP()); |
| 95 | + Serial.print(":"); |
| 96 | + Serial.print(packet.localPort()); |
| 97 | + Serial.print(", Length: "); |
| 98 | + Serial.print(packet.length()); |
| 99 | + Serial.print(", Data: "); |
| 100 | + Serial.write(packet.data(), packet.length()); |
| 101 | + Serial.println(); |
| 102 | + //reply to the client |
| 103 | + packet.printf("Got %u bytes of data", packet.length()); |
| 104 | +} |
| 105 | + |
| 106 | +void setup() |
| 107 | +{ |
| 108 | + Serial.begin(115200); |
| 109 | + |
| 110 | + while (!Serial && (millis() < 5000)); |
| 111 | + |
| 112 | + Serial.print(F("\nStart Async_UDPClient on ")); |
| 113 | + Serial.print(ARDUINO_BOARD); |
| 114 | + Serial.print(F(" with ")); |
| 115 | + Serial.println(SHIELD_TYPE); |
| 116 | + Serial.println(WEBSERVER_ESP32_ENC_VERSION); |
| 117 | + Serial.println(ASYNC_UDP_ESP32_ENC_VERSION); |
| 118 | + |
| 119 | + Serial.setDebugOutput(true); |
| 120 | + |
| 121 | + ET_LOGWARN(F("Default SPI pinout:")); |
| 122 | + ET_LOGWARN1(F("MOSI:"), MOSI_GPIO); |
| 123 | + ET_LOGWARN1(F("MISO:"), MISO_GPIO); |
| 124 | + ET_LOGWARN1(F("SCK:"), SCK_GPIO); |
| 125 | + ET_LOGWARN1(F("CS:"), CS_GPIO); |
| 126 | + ET_LOGWARN1(F("INT:"), INT_GPIO); |
| 127 | + ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ); |
| 128 | + ET_LOGWARN(F("=========================")); |
| 129 | + |
| 130 | + /////////////////////////////////// |
| 131 | + |
| 132 | + // To be called before ETH.begin() |
| 133 | + ESP32_ENC_onEvent(); |
| 134 | + |
| 135 | + // start the ethernet connection and the server: |
| 136 | + // Use DHCP dynamic IP and random mac |
| 137 | + uint16_t index = millis() % NUMBER_OF_MAC; |
| 138 | + |
| 139 | + //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ, |
| 140 | + // int SPI_HOST, uint8_t *ENC28J60_Mac = ENC28J60_Default_Mac); |
| 141 | + //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, SPI_HOST ); |
| 142 | + ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, SPI_HOST, mac[index] ); |
| 143 | + |
| 144 | + // Static IP, leave without this line to get IP via DHCP |
| 145 | + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); |
| 146 | + //ETH.config(myIP, myGW, mySN, myDNS); |
| 147 | + |
| 148 | + ESP32_ENC_waitForConnect(); |
| 149 | + |
| 150 | + /////////////////////////////////// |
| 151 | + |
| 152 | + // Client address |
| 153 | + Serial.print("Async_UDPClient started @ IP address: "); |
| 154 | + Serial.println(ETH.localIP()); |
| 155 | + |
| 156 | + if (udp.connect(remoteIPAddress, UDP_REMOTE_PORT)) |
| 157 | + { |
| 158 | + Serial.println("UDP connected"); |
| 159 | + |
| 160 | + udp.onPacket([](AsyncUDPPacket packet) |
| 161 | + { |
| 162 | + parsePacket( packet); |
| 163 | + }); |
| 164 | + |
| 165 | + //Send unicast |
| 166 | + udp.print("Hello Server!"); |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +void loop() |
| 171 | +{ |
| 172 | + delay(10000); |
| 173 | + //Send broadcast on port UDP_REMOTE_PORT = 1234 |
| 174 | + udp.broadcastTo("Anyone here?", UDP_REMOTE_PORT); |
| 175 | +} |
0 commit comments