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.

Revision:
14:7648334eb41b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SocketAddress.h	Tue Sep 03 09:16:55 2019 +0000
@@ -0,0 +1,192 @@
+/*
+ * Copyright (c) 2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/** @file SocketAddress.h SocketAddress class */
+/** \addtogroup netsocket
+ * @{*/
+
+#ifndef SOCKET_ADDRESS_H
+#define SOCKET_ADDRESS_H
+
+#include "nsapi_types.h"
+#include "mbed_toolchain.h"
+
+// Predeclared classes
+class NetworkStack;
+class NetworkInterface;
+
+/** SocketAddress class
+ *
+ *  Representation of an IP address and port pair.
+ */
+class SocketAddress {
+public:
+    /** Create a SocketAddress from a hostname and port
+     *
+     *  The hostname may be either a domain name or an IP address. If the
+     *  hostname is an IP address, no network transactions will be performed.
+     *
+     *  On failure, the IP address and port will be set to zero
+     *
+     *  @tparam S       Type of the Network stack
+     *  @param stack    Network stack to use for DNS resolution
+     *  @param host     Hostname to resolve
+     *  @param port     Optional 16-bit port, defaults to 0
+     *  @deprecated
+     *      Constructors hide possible errors. Replaced by
+     *      NetworkInterface::gethostbyname.
+     */
+    template <typename S>
+    MBED_DEPRECATED_SINCE("mbed-os-5.1.3",
+                          "Constructors hide possible errors. Replaced by "
+                          "NetworkInterface::gethostbyname.")
+    SocketAddress(S *stack, const char *host, uint16_t port = 0)
+    {
+        _SocketAddress(nsapi_create_stack(stack), host, port);
+    }
+
+    /** Create a SocketAddress from a raw IP address and port
+     *
+     *  @note To construct from a host name, use NetworkInterface::gethostbyname
+     *
+     *  @param addr     Raw IP address
+     *  @param port     Optional 16-bit port, defaults to 0
+     */
+    SocketAddress(nsapi_addr_t addr = nsapi_addr_t(), uint16_t port = 0);
+
+    /** Create a SocketAddress from an IP address and port
+     *
+     *  @param addr     Null-terminated representation of the IP address
+     *  @param port     Optional 16-bit port, defaults to 0
+     */
+    SocketAddress(const char *addr, uint16_t port = 0);
+
+    /** Create a SocketAddress from raw IP bytes, IP version, and port
+     *
+     *  @param bytes    Raw IP address in big-endian order
+     *  @param version  IP address version, NSAPI_IPv4 or NSAPI_IPv6
+     *  @param port     Optional 16-bit port, defaults to 0
+     */
+    SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port = 0);
+
+    /** Create a SocketAddress from another SocketAddress
+     *
+     *  @param addr  SocketAddress to copy
+     */
+    SocketAddress(const SocketAddress &addr);
+
+    /** Destructor */
+    ~SocketAddress();
+
+    /** Set the IP address
+     *
+     *  @param addr     Null-terminated represention of the IP address
+     *  @return         True if address is a valid representation of an IP address,
+     *                  otherwise False and SocketAddress is set to null
+     */
+    bool set_ip_address(const char *addr);
+
+    /** Set the raw IP bytes and IP version
+     *
+     *  @param bytes    Raw IP address in big-endian order
+     *  @param version  IP address version, NSAPI_IPv4 or NSAPI_IPv6
+     */
+    void set_ip_bytes(const void *bytes, nsapi_version_t version);
+
+    /** Set the raw IP address
+     *
+     *  @param addr     Raw IP address
+     */
+    void set_addr(nsapi_addr_t addr);
+
+    /** Set the port
+     *
+     *  @param port     16-bit port
+     */
+    void set_port(uint16_t port);
+
+    /** Get the human-readable IP address
+     *
+     *  Allocates memory for a string and converts binary address to
+     *  human-readable format. String is freed in the destructor.
+     *
+     *  @return         Null-terminated representation of the IP Address
+     */
+    const char *get_ip_address() const;
+
+    /** Get the raw IP bytes
+     *
+     *  @return         Raw IP address in big-endian order
+     */
+    const void *get_ip_bytes() const;
+
+    /** Get the IP address version
+     *
+     *  @return         IP address version, NSAPI_IPv4 or NSAPI_IPv6
+     */
+    nsapi_version_t get_ip_version() const;
+
+    /** Get the raw IP address
+     *
+     *  @return         Raw IP address
+     */
+    nsapi_addr_t get_addr() const;
+
+    /** Get the port
+     *
+     *  @return         The 16-bit port
+     */
+    uint16_t get_port() const;
+
+    /** Test if address is zero
+     *
+     *  @return         True if address is not zero
+     */
+    operator bool() const;
+
+    /** Copy address from another SocketAddress
+     *
+     * @param addr  SocketAddress to copy
+     */
+    SocketAddress &operator=(const SocketAddress &addr);
+
+    /** Compare two addresses for equality
+     *
+     *  @return         True if both addresses are equal
+     */
+    friend bool operator==(const SocketAddress &a, const SocketAddress &b);
+
+    /** Compare two addresses for equality
+     *
+     *  @return         True if both addresses are not equal
+     */
+    friend bool operator!=(const SocketAddress &a, const SocketAddress &b);
+
+private:
+    //void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
+
+    /** Initialize memory */
+    void mem_init(void);
+
+    mutable char *_ip_address;
+    nsapi_addr_t _addr;
+    uint16_t _port;
+};
+
+
+#endif
+
+/** @}*/