Mbed library for ENC28J60 Ethernet modules. Full support for TCP/IP and UDP Server, Client and HTTP server (webserver). DHCP and DNS is included.

Dependents:   mBuino_ENC28_MQTT Nucleo_Web_ENC28J60 Nucleo_Web_ENC28J60_ADC Serial_over_Ethernet ... more

Library for ENC28J60 Ethernet modules.

/media/uploads/hudakz/enc28j60_module01.jpg

Ported to mbed from Norbert Truchsess's UIPEthernet library for Arduino. Thank you Norbert!

  • Full support for persistent (streaming) TCP/IP and UDP connections Client and Server each, ARP, ICMP, DHCP and DNS.
  • Works with both Mbed OS 2 and Mbed OS 5.

Usage:

  • Import the library into your project.
  • Add #include "UipEthernet.h" to main.cpp
  • Create one instance of the UipEthernet class initialized with the MAC address you'd like to use and SPI pins of the connected Mbed board.

Example programs:

Import programWebSwitch_ENC28J60

HTTP Server serving a simple webpage which enables to remotely turn a digital output on/off. Compile, download, run and type 'IP_address/secret/' (don't forget the last '/') into your web browser and hit ENTER.

Import programHTTPServer_Echo_ENC28J60

A simple HTTP server echoing received requests. Ethernet connection is over an ENC28J60 board. Usage: Type the server's IP address into you web browser and hit <ENTER>.

Import programTcpServer_ENC28J60

Simple TCP/IP Server using the UIPEthernet library for ENC28J60 Ethernet boards.

Import programTcpClient_ENC28J60

Simple TCP/IP Client using the UIPEthernet library for ENC28J60 Ethernet boards.

Import programUdpServer_ENC28J60

Simple UDP Server using the UIPEthernet library for ENC28J60 Ethernet boards.

Import programUdpClient_ENC28J60

Simple UDP Client using the UIPEthernet library for ENC28J60 Ethernet boards.

Import programMQTT_Hello_ENC28J60

MQTT Client example program. Ethernet connection is via an ENC28J60 module.

Committer:
hudakz
Date:
Thu Jul 23 15:30:54 2020 +0000
Revision:
18:8d5738a6646e
Parent:
11:647d53d146f1
Mbed library for ENC28J60 Ethernet modules.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 9:a156d3de5647 1 // DHCP Library v0.3 - April 25, 2009
hudakz 9:a156d3de5647 2 // Author: Jordan Terrell - blog.jordanterrell.com
hudakz 11:647d53d146f1 3 #ifndef DHCPCLIENT_h
hudakz 11:647d53d146f1 4 #define DHCPCLIENT_h
hudakz 9:a156d3de5647 5
hudakz 9:a156d3de5647 6 #include "UdpSocket.h"
hudakz 9:a156d3de5647 7 #include "IpAddress.h"
hudakz 9:a156d3de5647 8
hudakz 9:a156d3de5647 9 /* DHCP state machine. */
hudakz 9:a156d3de5647 10
hudakz 9:a156d3de5647 11 #define STATE_DHCP_START 0
hudakz 9:a156d3de5647 12 #define STATE_DHCP_DISCOVER 1
hudakz 9:a156d3de5647 13 #define STATE_DHCP_REQUEST 2
hudakz 9:a156d3de5647 14 #define STATE_DHCP_LEASED 3
hudakz 9:a156d3de5647 15 #define STATE_DHCP_REREQUEST 4
hudakz 9:a156d3de5647 16 #define STATE_DHCP_RELEASE 5
hudakz 9:a156d3de5647 17
hudakz 9:a156d3de5647 18 #define DHCP_FLAGSBROADCAST 0x8000
hudakz 9:a156d3de5647 19
hudakz 9:a156d3de5647 20 /* UDP port numbers for DHCP */
hudakz 9:a156d3de5647 21
hudakz 9:a156d3de5647 22 #define DHCP_SERVER_PORT 67 /* from server to client */
hudakz 9:a156d3de5647 23
hudakz 9:a156d3de5647 24 #define DHCP_CLIENT_PORT 68 /* from client to server */
hudakz 9:a156d3de5647 25
hudakz 9:a156d3de5647 26 /* DHCP message OP code */
hudakz 9:a156d3de5647 27
hudakz 9:a156d3de5647 28 #define DHCP_BOOTREQUEST 1
hudakz 9:a156d3de5647 29 #define DHCP_BOOTREPLY 2
hudakz 9:a156d3de5647 30
hudakz 9:a156d3de5647 31 /* DHCP message type */
hudakz 9:a156d3de5647 32
hudakz 9:a156d3de5647 33 #define DHCP_DISCOVER 1
hudakz 9:a156d3de5647 34 #define DHCP_OFFER 2
hudakz 9:a156d3de5647 35 #define DHCP_REQUEST 3
hudakz 9:a156d3de5647 36 #define DHCP_DECLINE 4
hudakz 9:a156d3de5647 37 #define DHCP_ACK 5
hudakz 9:a156d3de5647 38 #define DHCP_NAK 6
hudakz 9:a156d3de5647 39 #define DHCP_RELEASE 7
hudakz 9:a156d3de5647 40 #define DHCP_INFORM 8
hudakz 9:a156d3de5647 41
hudakz 9:a156d3de5647 42 #define DHCP_HTYPE10MB 1
hudakz 9:a156d3de5647 43 #define DHCP_HTYPE100MB 2
hudakz 9:a156d3de5647 44
hudakz 9:a156d3de5647 45 #define DHCP_HLENETHERNET 6
hudakz 9:a156d3de5647 46 #define DHCP_HOPS 0
hudakz 9:a156d3de5647 47 #define DHCP_SECS 0
hudakz 9:a156d3de5647 48
hudakz 9:a156d3de5647 49 #define MAGIC_COOKIE 0x63825363
hudakz 9:a156d3de5647 50 #define MAX_DHCP_OPT 16
hudakz 9:a156d3de5647 51
hudakz 9:a156d3de5647 52 #define HOST_NAME "ENC28J"
hudakz 9:a156d3de5647 53 #define DEFAULT_LEASE (900) //default lease time in seconds
hudakz 9:a156d3de5647 54
hudakz 9:a156d3de5647 55 #define DHCP_CHECK_NONE (0)
hudakz 9:a156d3de5647 56 #define DHCP_CHECK_RENEW_FAIL (1)
hudakz 9:a156d3de5647 57 #define DHCP_CHECK_RENEW_OK (2)
hudakz 9:a156d3de5647 58 #define DHCP_CHECK_REBIND_FAIL (3)
hudakz 9:a156d3de5647 59 #define DHCP_CHECK_REBIND_OK (4)
hudakz 9:a156d3de5647 60
hudakz 9:a156d3de5647 61 enum
hudakz 9:a156d3de5647 62 {
hudakz 9:a156d3de5647 63 padOption = 0,
hudakz 9:a156d3de5647 64 subnetMask = 1,
hudakz 9:a156d3de5647 65 timerOffset = 2,
hudakz 9:a156d3de5647 66 routersOnSubnet = 3,
hudakz 9:a156d3de5647 67 timeServer = 4,
hudakz 9:a156d3de5647 68 nameServer = 5,
hudakz 9:a156d3de5647 69 dns = 6,
hudakz 9:a156d3de5647 70 logServer = 7,
hudakz 9:a156d3de5647 71 cookieServer = 8,
hudakz 9:a156d3de5647 72 lprServer = 9,
hudakz 9:a156d3de5647 73 impressServer = 10,
hudakz 9:a156d3de5647 74 resourceLocationServer = 11,
hudakz 9:a156d3de5647 75 hostName = 12,
hudakz 9:a156d3de5647 76 bootFileSize = 13,
hudakz 9:a156d3de5647 77 meritDumpFile = 14,
hudakz 9:a156d3de5647 78 domainName = 15,
hudakz 9:a156d3de5647 79 swapServer = 16,
hudakz 9:a156d3de5647 80 rootPath = 17,
hudakz 9:a156d3de5647 81 extentionsPath = 18,
hudakz 9:a156d3de5647 82 IPforwarding = 19,
hudakz 9:a156d3de5647 83 nonLocalSourceRouting = 20,
hudakz 9:a156d3de5647 84 policyFilter = 21,
hudakz 9:a156d3de5647 85 maxDgramReasmSize = 22,
hudakz 9:a156d3de5647 86 defaultIPTTL = 23,
hudakz 9:a156d3de5647 87 pathMTUagingTimeout = 24,
hudakz 9:a156d3de5647 88 pathMTUplateauTable = 25,
hudakz 9:a156d3de5647 89 ifMTU = 26,
hudakz 9:a156d3de5647 90 allSubnetsLocal = 27,
hudakz 9:a156d3de5647 91 broadcastAddr = 28,
hudakz 9:a156d3de5647 92 performMaskDiscovery = 29,
hudakz 9:a156d3de5647 93 maskSupplier = 30,
hudakz 9:a156d3de5647 94 performRouterDiscovery = 31,
hudakz 9:a156d3de5647 95 routerSolicitationAddr = 32,
hudakz 9:a156d3de5647 96 staticRoute = 33,
hudakz 9:a156d3de5647 97 trailerEncapsulation = 34,
hudakz 9:a156d3de5647 98 arpCacheTimeout = 35,
hudakz 9:a156d3de5647 99 ethernetEncapsulation = 36,
hudakz 9:a156d3de5647 100 tcpDefaultTTL = 37,
hudakz 9:a156d3de5647 101 tcpKeepaliveInterval = 38,
hudakz 9:a156d3de5647 102 tcpKeepaliveGarbage = 39,
hudakz 9:a156d3de5647 103 nisDomainName = 40,
hudakz 9:a156d3de5647 104 nisServers = 41,
hudakz 9:a156d3de5647 105 ntpServers = 42,
hudakz 9:a156d3de5647 106 vendorSpecificInfo = 43,
hudakz 9:a156d3de5647 107 netBIOSnameServer = 44,
hudakz 9:a156d3de5647 108 netBIOSdgramDistServer = 45,
hudakz 9:a156d3de5647 109 netBIOSnodeType = 46,
hudakz 9:a156d3de5647 110 netBIOSscope = 47,
hudakz 9:a156d3de5647 111 xFontServer = 48,
hudakz 9:a156d3de5647 112 xDisplayManager = 49,
hudakz 9:a156d3de5647 113 dhcpRequestedIPaddr = 50,
hudakz 9:a156d3de5647 114 dhcpIPaddrLeaseTime = 51,
hudakz 9:a156d3de5647 115 dhcpOptionOverload = 52,
hudakz 9:a156d3de5647 116 dhcpMessageType = 53,
hudakz 9:a156d3de5647 117 dhcpServerIdentifier = 54,
hudakz 9:a156d3de5647 118 dhcpParamRequest = 55,
hudakz 9:a156d3de5647 119 dhcpMsg = 56,
hudakz 9:a156d3de5647 120 dhcpMaxMsgSize = 57,
hudakz 9:a156d3de5647 121 dhcpT1value = 58,
hudakz 9:a156d3de5647 122 dhcpT2value = 59,
hudakz 9:a156d3de5647 123 dhcpClassIdentifier = 60,
hudakz 9:a156d3de5647 124 dhcpClientIdentifier = 61,
hudakz 9:a156d3de5647 125 endOption = 255
hudakz 9:a156d3de5647 126 };
hudakz 9:a156d3de5647 127
hudakz 9:a156d3de5647 128 typedef struct _RIP_MSG_FIXED
hudakz 9:a156d3de5647 129 {
hudakz 9:a156d3de5647 130 uint8_t op;
hudakz 9:a156d3de5647 131 uint8_t htype;
hudakz 9:a156d3de5647 132 uint8_t hlen;
hudakz 9:a156d3de5647 133 uint8_t hops;
hudakz 9:a156d3de5647 134 uint32_t xid;
hudakz 9:a156d3de5647 135 uint16_t secs;
hudakz 9:a156d3de5647 136 uint16_t flags;
hudakz 9:a156d3de5647 137 uint8_t ciaddr[4];
hudakz 9:a156d3de5647 138 uint8_t yiaddr[4];
hudakz 9:a156d3de5647 139 uint8_t siaddr[4];
hudakz 9:a156d3de5647 140 uint8_t giaddr[4];
hudakz 9:a156d3de5647 141 uint8_t chaddr[6];
hudakz 9:a156d3de5647 142 } RIP_MSG_FIXED;
hudakz 9:a156d3de5647 143
hudakz 9:a156d3de5647 144 class DhcpClient
hudakz 9:a156d3de5647 145 {
hudakz 9:a156d3de5647 146 private:
hudakz 9:a156d3de5647 147 uint32_t _dhcpInitialTransactionId;
hudakz 9:a156d3de5647 148 uint32_t _dhcpTransactionId;
hudakz 9:a156d3de5647 149 uint8_t _dhcpMacAddr[6];
hudakz 9:a156d3de5647 150 uint8_t _dhcpLocalIp[4];
hudakz 9:a156d3de5647 151 uint8_t _dhcpSubnetMask[4];
hudakz 9:a156d3de5647 152 uint8_t _dhcpGatewayIp[4];
hudakz 9:a156d3de5647 153 uint8_t _dhcpDhcpServerIp[4];
hudakz 9:a156d3de5647 154 uint8_t _dhcpDnsServerIp[4];
hudakz 9:a156d3de5647 155 uint32_t _dhcpLeaseTime;
hudakz 9:a156d3de5647 156 uint32_t _dhcpT1, _dhcpT2;
hudakz 9:a156d3de5647 157 time_t _renewInSec;
hudakz 9:a156d3de5647 158 time_t _rebindInSec;
hudakz 9:a156d3de5647 159 time_t _lastCheck;
hudakz 9:a156d3de5647 160 time_t _timeout;
hudakz 9:a156d3de5647 161 time_t _responseTimeout;
hudakz 9:a156d3de5647 162 time_t _secTimeout;
hudakz 9:a156d3de5647 163 uint8_t _dhcp_state;
hudakz 9:a156d3de5647 164 UdpSocket _dhcpUdpSocket;
hudakz 9:a156d3de5647 165
hudakz 9:a156d3de5647 166 int requestDhcpLease();
hudakz 9:a156d3de5647 167 void resetDhcpLease();
hudakz 9:a156d3de5647 168 void presendDhcp();
hudakz 9:a156d3de5647 169 void sendDhcpMessage(uint8_t, uint16_t);
hudakz 9:a156d3de5647 170 void printByte(char* , uint8_t);
hudakz 9:a156d3de5647 171
hudakz 18:8d5738a6646e 172 uint8_t parseDhcpResponse(time_t responseTimeout, uint32_t& transactionId);
hudakz 9:a156d3de5647 173
hudakz 9:a156d3de5647 174 public:
hudakz 9:a156d3de5647 175 IpAddress getLocalIp();
hudakz 9:a156d3de5647 176 IpAddress getSubnetMask();
hudakz 9:a156d3de5647 177 IpAddress getGatewayIp();
hudakz 9:a156d3de5647 178 IpAddress getDhcpServerIp();
hudakz 9:a156d3de5647 179 IpAddress getDnsServerIp();
hudakz 9:a156d3de5647 180
hudakz 9:a156d3de5647 181 int begin(uint8_t* mac, unsigned long timeout = 60, unsigned long responseTimeout = 4);
hudakz 9:a156d3de5647 182 int checkLease();
hudakz 9:a156d3de5647 183 };
hudakz 9:a156d3de5647 184 #endif