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:
13:95c00132cd98
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 UIPClient.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 This program is free software: you can redistribute it and/or modify
hudakz 9:a156d3de5647 7 it under the terms of the GNU General Public License as published by
hudakz 9:a156d3de5647 8 the Free Software Foundation, either version 3 of the License, or
hudakz 9:a156d3de5647 9 (at your option) any later version.
hudakz 9:a156d3de5647 10
hudakz 9:a156d3de5647 11 This program is distributed in the hope that it will be useful,
hudakz 9:a156d3de5647 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
hudakz 9:a156d3de5647 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
hudakz 9:a156d3de5647 14 GNU General Public License for more details.
hudakz 9:a156d3de5647 15
hudakz 9:a156d3de5647 16 You should have received a copy of the GNU General Public License
hudakz 9:a156d3de5647 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
hudakz 9:a156d3de5647 18 */
hudakz 11:647d53d146f1 19 #ifndef TCPCLIENT_h
hudakz 11:647d53d146f1 20 #define TCPCLIENT_h
hudakz 9:a156d3de5647 21
hudakz 9:a156d3de5647 22 #include "mbed.h"
hudakz 10:e4ddab81e6a8 23 #include "IpAddress.h"
hudakz 9:a156d3de5647 24 #include "utility/MemPool.h"
hudakz 9:a156d3de5647 25
hudakz 9:a156d3de5647 26 extern "C"
hudakz 9:a156d3de5647 27 {
hudakz 9:a156d3de5647 28 #include "utility/uip.h"
hudakz 9:a156d3de5647 29 }
hudakz 9:a156d3de5647 30 #define UIP_SOCKET_DATALEN UIP_TCP_MSS
hudakz 9:a156d3de5647 31 //#define UIP_SOCKET_NUMPACKETS UIP_RECEIVE_WINDOW/UIP_TCP_MSS+1
hudakz 9:a156d3de5647 32
hudakz 9:a156d3de5647 33 #ifndef UIP_SOCKET_NUMPACKETS
hudakz 9:a156d3de5647 34 #define UIP_SOCKET_NUMPACKETS 5
hudakz 9:a156d3de5647 35 #endif
hudakz 9:a156d3de5647 36 #define UIP_CLIENT_CONNECTED 0x10
hudakz 9:a156d3de5647 37 #define UIP_CLIENT_CLOSE 0x20
hudakz 9:a156d3de5647 38 #define UIP_CLIENT_REMOTECLOSED 0x40
hudakz 9:a156d3de5647 39 #define UIP_CLIENT_RESTART 0x80
hudakz 9:a156d3de5647 40 #define UIP_CLIENT_STATEFLAGS (UIP_CLIENT_CONNECTED | UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED | UIP_CLIENT_RESTART)
hudakz 9:a156d3de5647 41 #define UIP_CLIENT_SOCKETS ~UIP_CLIENT_STATEFLAGS
hudakz 9:a156d3de5647 42
hudakz 9:a156d3de5647 43 typedef uint8_t uip_socket_ptr;
hudakz 9:a156d3de5647 44
hudakz 9:a156d3de5647 45 typedef struct
hudakz 9:a156d3de5647 46 {
hudakz 10:e4ddab81e6a8 47 uint8_t state;
hudakz 10:e4ddab81e6a8 48 memhandle packets_in[UIP_SOCKET_NUMPACKETS];
hudakz 10:e4ddab81e6a8 49 uint16_t lport; /**< The local TCP port, in network byte order. */
hudakz 9:a156d3de5647 50 } uip_userdata_closed_t;
hudakz 9:a156d3de5647 51
hudakz 9:a156d3de5647 52 typedef struct
hudakz 9:a156d3de5647 53 {
hudakz 10:e4ddab81e6a8 54 Timer pollTimer;
hudakz 10:e4ddab81e6a8 55 uint8_t state;
hudakz 10:e4ddab81e6a8 56 uip_ipaddr_t ripaddr; /**< The IP address of the remote host. */
hudakz 10:e4ddab81e6a8 57 memhandle packets_in[UIP_SOCKET_NUMPACKETS];
hudakz 10:e4ddab81e6a8 58 memhandle packets_out[UIP_SOCKET_NUMPACKETS];
hudakz 10:e4ddab81e6a8 59 memaddress out_pos;
hudakz 9:a156d3de5647 60 } uip_userdata_t;
hudakz 9:a156d3de5647 61
hudakz 13:95c00132cd98 62 class UipEthernet;
hudakz 13:95c00132cd98 63
hudakz 10:e4ddab81e6a8 64 class TcpClient
hudakz 9:a156d3de5647 65 {
hudakz 9:a156d3de5647 66 public:
hudakz 9:a156d3de5647 67 TcpClient();
hudakz 9:a156d3de5647 68 TcpClient(uip_userdata_t* conn_data);
hudakz 9:a156d3de5647 69 virtual ~TcpClient() {}
hudakz 10:e4ddab81e6a8 70
hudakz 13:95c00132cd98 71 int open(UipEthernet* ethernet);
hudakz 9:a156d3de5647 72 int connect(IpAddress ip, uint16_t port);
hudakz 9:a156d3de5647 73 int connect(const char* host, uint16_t port);
hudakz 10:e4ddab81e6a8 74 int recv(uint8_t* buf, size_t size);
hudakz 9:a156d3de5647 75 void stop();
hudakz 9:a156d3de5647 76 uint8_t connected();
hudakz 13:95c00132cd98 77 void setInstance(TcpClient* client);
hudakz 9:a156d3de5647 78 operator bool();
hudakz 9:a156d3de5647 79 virtual bool operator ==(const TcpClient& );
hudakz 9:a156d3de5647 80 virtual bool operator !=(const TcpClient& rhs) { return !this->operator ==(rhs); }
hudakz 11:647d53d146f1 81 int send(uint8_t);
hudakz 11:647d53d146f1 82 int send(const uint8_t* buf, size_t size);
hudakz 10:e4ddab81e6a8 83 size_t available();
hudakz 11:647d53d146f1 84 int recv();
hudakz 9:a156d3de5647 85 int peek();
hudakz 9:a156d3de5647 86 void flush();
hudakz 10:e4ddab81e6a8 87 const char* getpeername();
hudakz 10:e4ddab81e6a8 88 IpAddress getRemoteIp();
hudakz 9:a156d3de5647 89 void close();
hudakz 9:a156d3de5647 90
hudakz 9:a156d3de5647 91 static uip_userdata_t all_data[UIP_CONNS];
hudakz 11:647d53d146f1 92 static int _write(uip_userdata_t* , const uint8_t* buf, size_t size);
hudakz 9:a156d3de5647 93
hudakz 10:e4ddab81e6a8 94 protected:
hudakz 10:e4ddab81e6a8 95 uint8_t* rawIPAddress(IpAddress& addr) { return addr.rawAddress(); }
hudakz 10:e4ddab81e6a8 96
hudakz 9:a156d3de5647 97 private:
hudakz 9:a156d3de5647 98 uip_userdata_t* data;
hudakz 13:95c00132cd98 99 TcpClient* _instance;
hudakz 9:a156d3de5647 100 static uip_userdata_t* _allocateData();
hudakz 10:e4ddab81e6a8 101 static size_t _available(uip_userdata_t* );
hudakz 9:a156d3de5647 102 static uint8_t _currentBlock(memhandle* blocks);
hudakz 9:a156d3de5647 103 static void _eatBlock(memhandle* blocks);
hudakz 9:a156d3de5647 104 static void _flushBlocks(memhandle* blocks);
hudakz 9:a156d3de5647 105
hudakz 9:a156d3de5647 106 #ifdef UIPETHERNET_DEBUG_CLIENT
hudakz 9:a156d3de5647 107 static void _dumpAllData();
hudakz 9:a156d3de5647 108 #endif
hudakz 9:a156d3de5647 109 friend class UipEthernet;
hudakz 9:a156d3de5647 110 friend class TcpServer;
hudakz 9:a156d3de5647 111 friend void uipclient_appcall();
hudakz 9:a156d3de5647 112 };
hudakz 9:a156d3de5647 113 #endif