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 TcpServer.cpp - 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 9:a156d3de5647 19 #include "UipEthernet.h"
hudakz 9:a156d3de5647 20 #include "TcpServer.h"
hudakz 9:a156d3de5647 21 extern "C"
hudakz 9:a156d3de5647 22 {
hudakz 9:a156d3de5647 23 #include "utility/uip-conf.h"
hudakz 9:a156d3de5647 24 }
hudakz 9:a156d3de5647 25 /**
hudakz 9:a156d3de5647 26 * @brief
hudakz 9:a156d3de5647 27 * @note
hudakz 9:a156d3de5647 28 * @param
hudakz 9:a156d3de5647 29 * @retval
hudakz 9:a156d3de5647 30 */
hudakz 11:647d53d146f1 31 TcpServer::TcpServer() :
hudakz 11:647d53d146f1 32 _conns(1)
hudakz 11:647d53d146f1 33 {}
hudakz 9:a156d3de5647 34
hudakz 9:a156d3de5647 35 /**
hudakz 9:a156d3de5647 36 * @brief
hudakz 9:a156d3de5647 37 * @note
hudakz 9:a156d3de5647 38 * @param
hudakz 9:a156d3de5647 39 * @retval
hudakz 9:a156d3de5647 40 */
hudakz 9:a156d3de5647 41 TcpClient* TcpServer::accept()
hudakz 9:a156d3de5647 42 {
hudakz 13:95c00132cd98 43 TcpClient* result = NULL;
hudakz 13:95c00132cd98 44
hudakz 9:a156d3de5647 45 UipEthernet::ethernet->tick();
hudakz 11:647d53d146f1 46 for (uip_userdata_t * data = &TcpClient::all_data[0]; data < &TcpClient::all_data[_conns]; data++) {
hudakz 9:a156d3de5647 47 if
hudakz 9:a156d3de5647 48 (
hudakz 9:a156d3de5647 49 data->packets_in[0] != NOBLOCK &&
hudakz 9:a156d3de5647 50 (
hudakz 9:a156d3de5647 51 ((data->state & UIP_CLIENT_CONNECTED) && uip_conns[data->state & UIP_CLIENT_SOCKETS].lport == _port) ||
hudakz 9:a156d3de5647 52 ((data->state & UIP_CLIENT_REMOTECLOSED) && ((uip_userdata_closed_t*)data)->lport == _port)
hudakz 9:a156d3de5647 53 )
hudakz 9:a156d3de5647 54 ) {
hudakz 10:e4ddab81e6a8 55 data->ripaddr[0] = uip_conns[data->state & UIP_CLIENT_SOCKETS].ripaddr[0];
hudakz 10:e4ddab81e6a8 56 data->ripaddr[1] = uip_conns[data->state & UIP_CLIENT_SOCKETS].ripaddr[1];
hudakz 13:95c00132cd98 57 result = new TcpClient(data);
hudakz 13:95c00132cd98 58 result->setInstance(result);
hudakz 13:95c00132cd98 59 return result;
hudakz 9:a156d3de5647 60 }
hudakz 9:a156d3de5647 61 }
hudakz 9:a156d3de5647 62
hudakz 13:95c00132cd98 63 return result;
hudakz 9:a156d3de5647 64 }
hudakz 9:a156d3de5647 65
hudakz 9:a156d3de5647 66 /**
hudakz 9:a156d3de5647 67 * @brief
hudakz 9:a156d3de5647 68 * @note
hudakz 9:a156d3de5647 69 * @param
hudakz 9:a156d3de5647 70 * @retval
hudakz 9:a156d3de5647 71 */
hudakz 9:a156d3de5647 72 void TcpServer::open(UipEthernet* ethernet)
hudakz 11:647d53d146f1 73 {
hudakz 11:647d53d146f1 74 if (UipEthernet::ethernet != ethernet)
hudakz 11:647d53d146f1 75 UipEthernet::ethernet = ethernet;
hudakz 11:647d53d146f1 76 }
hudakz 9:a156d3de5647 77
hudakz 9:a156d3de5647 78 /**
hudakz 9:a156d3de5647 79 * @brief
hudakz 9:a156d3de5647 80 * @note
hudakz 9:a156d3de5647 81 * @param
hudakz 9:a156d3de5647 82 * @retval
hudakz 9:a156d3de5647 83 */
hudakz 9:a156d3de5647 84 void TcpServer::bind(uint8_t port)
hudakz 9:a156d3de5647 85 {
hudakz 9:a156d3de5647 86 _port = htons(port);
hudakz 9:a156d3de5647 87 }
hudakz 9:a156d3de5647 88
hudakz 9:a156d3de5647 89 /**
hudakz 9:a156d3de5647 90 * @brief
hudakz 9:a156d3de5647 91 * @note
hudakz 9:a156d3de5647 92 * @param
hudakz 9:a156d3de5647 93 * @retval
hudakz 9:a156d3de5647 94 */
hudakz 9:a156d3de5647 95 void TcpServer::bind(const char* ip, uint8_t port)
hudakz 9:a156d3de5647 96 {
hudakz 9:a156d3de5647 97 _port = htons(port);
hudakz 9:a156d3de5647 98 }
hudakz 9:a156d3de5647 99
hudakz 9:a156d3de5647 100 /**
hudakz 9:a156d3de5647 101 * @brief
hudakz 9:a156d3de5647 102 * @note
hudakz 9:a156d3de5647 103 * @param
hudakz 9:a156d3de5647 104 * @retval
hudakz 9:a156d3de5647 105 */
hudakz 9:a156d3de5647 106 void TcpServer::listen(uint8_t conns)
hudakz 9:a156d3de5647 107 {
hudakz 9:a156d3de5647 108 _conns = _conns < UIP_CONNS ? conns : UIP_CONNS;
hudakz 9:a156d3de5647 109 uip_listen(_port);
hudakz 9:a156d3de5647 110 UipEthernet::ethernet->tick();
hudakz 9:a156d3de5647 111 }
hudakz 9:a156d3de5647 112
hudakz 9:a156d3de5647 113 /**
hudakz 9:a156d3de5647 114 * @brief
hudakz 9:a156d3de5647 115 * @note
hudakz 9:a156d3de5647 116 * @param
hudakz 9:a156d3de5647 117 * @retval
hudakz 9:a156d3de5647 118 */
hudakz 9:a156d3de5647 119 size_t TcpServer::send(uint8_t c)
hudakz 9:a156d3de5647 120 {
hudakz 9:a156d3de5647 121 return send(&c, 1);
hudakz 9:a156d3de5647 122 }
hudakz 9:a156d3de5647 123
hudakz 9:a156d3de5647 124 /**
hudakz 9:a156d3de5647 125 * @brief
hudakz 9:a156d3de5647 126 * @note
hudakz 9:a156d3de5647 127 * @param
hudakz 9:a156d3de5647 128 * @retval
hudakz 9:a156d3de5647 129 */
hudakz 9:a156d3de5647 130 size_t TcpServer::send(const uint8_t* buf, size_t size)
hudakz 9:a156d3de5647 131 {
hudakz 9:a156d3de5647 132 size_t ret = 0;
hudakz 11:647d53d146f1 133 for (uip_userdata_t * data = &TcpClient::all_data[0]; data < &TcpClient::all_data[_conns]; data++) {
hudakz 9:a156d3de5647 134 if ((data->state & UIP_CLIENT_CONNECTED) && uip_conns[data->state & UIP_CLIENT_SOCKETS].lport == _port)
hudakz 9:a156d3de5647 135 ret += TcpClient::_write(data, buf, size);
hudakz 9:a156d3de5647 136 }
hudakz 9:a156d3de5647 137
hudakz 9:a156d3de5647 138 return ret;
hudakz 9:a156d3de5647 139 }