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:
Sat Sep 07 17:42:42 2019 +0000
Revision:
15:53715cc81c63
Parent:
14:7648334eb41b
Child:
16:269f652b4d0b
Timeout parameter added for the 'connect' function, SPI speed reduced from 20 to 10 Mb/s, debug messages fixed ...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 9:a156d3de5647 1 /*
hudakz 9:a156d3de5647 2 UipEthernet.h - Arduino implementation of a UIP wrapper class.
hudakz 9:a156d3de5647 3 Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
hudakz 9:a156d3de5647 4 All rights reserved.
hudakz 9:a156d3de5647 5
hudakz 9:a156d3de5647 6 Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com>
hudakz 9:a156d3de5647 7
hudakz 9:a156d3de5647 8 This program is free software: you can redistribute it and/or modify
hudakz 9:a156d3de5647 9 it under the terms of the GNU General Public License as published by
hudakz 9:a156d3de5647 10 the Free Software Foundation, either version 3 of the License, or
hudakz 9:a156d3de5647 11 (at your option) any later version.
hudakz 9:a156d3de5647 12
hudakz 9:a156d3de5647 13 This program is distributed in the hope that it will be useful,
hudakz 9:a156d3de5647 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
hudakz 9:a156d3de5647 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
hudakz 9:a156d3de5647 16 GNU General Public License for more details.
hudakz 9:a156d3de5647 17
hudakz 9:a156d3de5647 18 You should have received a copy of the GNU General Public License
hudakz 9:a156d3de5647 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
hudakz 9:a156d3de5647 20 */
hudakz 11:647d53d146f1 21 #ifndef UIPETHERNET_h
hudakz 11:647d53d146f1 22 #define UIPETHERNET_h
hudakz 9:a156d3de5647 23
hudakz 9:a156d3de5647 24 //#define UIPETHERNET_DEBUG
hudakz 9:a156d3de5647 25 //#define UIPETHERNET_DEBUG_CHKSUM
hudakz 9:a156d3de5647 26 //#define UIPETHERNET_DEBUG_UDP
hudakz 9:a156d3de5647 27 //#define UIPETHERNET_DEBUG_CLIENT
hudakz 9:a156d3de5647 28 #include "mbed.h"
hudakz 9:a156d3de5647 29 #include "DhcpClient.h"
hudakz 9:a156d3de5647 30 #include "IpAddress.h"
hudakz 11:647d53d146f1 31 #include "utility/Enc28j60Eth.h"
hudakz 9:a156d3de5647 32 #include "TcpClient.h"
hudakz 9:a156d3de5647 33 #include "TcpServer.h"
hudakz 9:a156d3de5647 34 #include "UdpSocket.h"
hudakz 9:a156d3de5647 35
hudakz 9:a156d3de5647 36 extern "C"
hudakz 9:a156d3de5647 37 {
hudakz 9:a156d3de5647 38 #include "utility/uip_timer.h"
hudakz 9:a156d3de5647 39 #include "utility/uip.h"
hudakz 14:7648334eb41b 40 #include "utility/util.h"
hudakz 9:a156d3de5647 41 }
hudakz 9:a156d3de5647 42
hudakz 9:a156d3de5647 43 class UipEthernet
hudakz 9:a156d3de5647 44 {
hudakz 9:a156d3de5647 45 public:
hudakz 9:a156d3de5647 46 static UipEthernet* ethernet;
hudakz 9:a156d3de5647 47 static IpAddress dnsServerAddress;
hudakz 11:647d53d146f1 48 Enc28j60Eth enc28j60Eth;
hudakz 9:a156d3de5647 49
hudakz 9:a156d3de5647 50 UipEthernet (const uint8_t mac[6], PinName mosi, PinName miso, PinName sck, PinName cs);
hudakz 9:a156d3de5647 51
hudakz 15:53715cc81c63 52 int connect(unsigned long timeout = 60);
hudakz 13:95c00132cd98 53 void disconnect();
hudakz 9:a156d3de5647 54 void set_network(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4);
hudakz 9:a156d3de5647 55 void set_network(IpAddress ip);
hudakz 9:a156d3de5647 56 void set_network(IpAddress ip, IpAddress dns);
hudakz 9:a156d3de5647 57 void set_network(IpAddress ip, IpAddress dns, IpAddress gateway);
hudakz 9:a156d3de5647 58 void set_network(IpAddress ip, IpAddress dns, IpAddress gateway, IpAddress subnet);
hudakz 11:647d53d146f1 59 void set_network(const char *ip_address, const char *netmask, const char *gateway);
hudakz 9:a156d3de5647 60 void tick();
hudakz 9:a156d3de5647 61 IpAddress localIP();
hudakz 9:a156d3de5647 62 IpAddress subnetMask();
hudakz 9:a156d3de5647 63 IpAddress gatewayIP();
hudakz 9:a156d3de5647 64 IpAddress dnsServerIP();
hudakz 9:a156d3de5647 65 const char* get_ip_address();
hudakz 9:a156d3de5647 66 const char* get_netmask();
hudakz 9:a156d3de5647 67 const char* get_gateway();
hudakz 9:a156d3de5647 68 static uint16_t chksum(uint16_t sum, const uint8_t* data, uint16_t len);
hudakz 9:a156d3de5647 69 static uint16_t ipchksum();
hudakz 11:647d53d146f1 70 bool stoip4(const char *ip4addr, size_t len, void *dest);
hudakz 9:a156d3de5647 71 private:
hudakz 9:a156d3de5647 72 uint8_t *const _mac;
hudakz 9:a156d3de5647 73 IpAddress _ip;
hudakz 9:a156d3de5647 74 IpAddress _dns;
hudakz 9:a156d3de5647 75 IpAddress _gateway;
hudakz 9:a156d3de5647 76 IpAddress _subnet;
hudakz 9:a156d3de5647 77 static memhandle inPacket;
hudakz 9:a156d3de5647 78 static memhandle uipPacket;
hudakz 9:a156d3de5647 79 static uint8_t uipHeaderLen;
hudakz 9:a156d3de5647 80 static uint8_t packetState;
hudakz 9:a156d3de5647 81 DhcpClient dhcpClient;
hudakz 9:a156d3de5647 82 Timer periodicTimer;
hudakz 9:a156d3de5647 83 void init(const uint8_t* mac);
hudakz 9:a156d3de5647 84 bool network_send();
hudakz 9:a156d3de5647 85 friend class TcpServer;
hudakz 9:a156d3de5647 86 friend class TcpClient;
hudakz 9:a156d3de5647 87 friend class UdpSocket;
hudakz 9:a156d3de5647 88 #if UIP_UDP
hudakz 9:a156d3de5647 89 uint16_t upper_layer_chksum(uint8_t proto);
hudakz 9:a156d3de5647 90 #endif
hudakz 9:a156d3de5647 91 friend uint16_t uip_ipchksum();
hudakz 9:a156d3de5647 92 friend uint16_t uip_tcpchksum();
hudakz 9:a156d3de5647 93 friend uint16_t uip_udpchksum();
hudakz 9:a156d3de5647 94 friend void uipclient_appcall();
hudakz 9:a156d3de5647 95 friend void uipudp_appcall();
hudakz 9:a156d3de5647 96
hudakz 9:a156d3de5647 97 #if UIP_CONF_IPV6
hudakz 9:a156d3de5647 98 uint16_t uip_icmp6chksum();
hudakz 9:a156d3de5647 99 #endif
hudakz 9:a156d3de5647 100 };
hudakz 11:647d53d146f1 101
hudakz 9:a156d3de5647 102 #endif