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:
Tue Aug 27 22:08:54 2019 +0000
Revision:
10:e4ddab81e6a8
Parent:
9:a156d3de5647
Child:
11:647d53d146f1
Added methods for TcpClient to get remote client's IP address and peer name.

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