A very small UPnP IGD implementation for ESP8266 and ESP32.
TinyUPnP
Simply clone or download as zip, then copy the folder TinyUPnP to your Arduino IDE "libraries" folder e.g "D:\Arduino\libraries". If you are still unable to include the package, go to Arduino IDE preferences and make sure that sketchbook location points to the correct Arduino directory.
Prerequisites
Boards:
USB to UART Bridge Drivers:
Other Libraries:
Please refer to the examples bundled with the TinyUPnP library.
Include
#include "TinyUPnP.h"
#include <EasyDDNS.h> // optional
Declare
TinyUPnP *tinyUPnP = new TinyUPnP(20000); // -1 for blocking (preferably, use a timeout value in [ms])
Setup
// you may repeat 'addPortMappingConfig' more than once
tinyUPnP->addPortMappingConfig(WiFi.localIP(), LISTEN_PORT, RULE_PROTOCOL_TCP, LEASE_DURATION, FRIENDLY_NAME);
// finally, commit the port mappings to the IGD
portMappingAdded = tinyUPnP->commitPortMappings();
Loop
// update UPnP port mapping every ms internal
// you can provide an optional method for reconnecting to the WiFi (otherwise leave as NULL)
tinyUPnP->updatePortMappings(600000, &connectWiFi); // 10 minutes
API
This is specific for the example code, you can do what you like here
http://<IP or DDNS>:<LISTEN_PORT>/?percentage=<0..100>
// print all the port mappings that were configured using 'addPortMappingConfig' in the setup step
tinyUPnP->printPortMappingConfig();
// print all the current port mappings from the IGD
tinyUPnP->printAllPortMappings();
Debug
You can turn off debug prints by setting UPNP_DEBUG
to false
in TinyUPnP.h#L16
When reporting issues, attach full log (i.e UPNP_DEBUG
is set to true
) and add the serial output to the issue as a text file attachment.
If you like what I got, you can consider donating here, you can change the amount as you like.
:star: If not, starring this project will go a long way to help me too!
Location
is a link to an XML file containing the IGD (Internet Gateway Device) API in order to create the needed calls which will add the new port mapping to your gateway router.<serviceType>urn:schemas-upnp-org:service:WANPPPConnection:1</serviceType>
which is what the library is looking for.eventSubURL
tag which is a link to your router's IGD API. (The base URL is also depicted in the same file under the tag URLBase)
"POST " + <link to service command from XML> + " HTTP/1.1"
"Content-Type: text/xml; charset=\"utf-8\""
"SOAPAction: \"urn:schemas-upnp-org:service:WANPPPConnection:1#AddPortMapping\""
"Content-Length: " + body.length()
Body:
"<?xml version=\"1.0\"?>\r\n"
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n"
"
Referenced from my answer here:
https://stackoverflow.com/a/46267791/4295037
# Detailed Document Released by UPnP Forum
http://upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v2.0.pdf
# DDNS
You will also need a DDNS update service
I use this https://github.com/ayushsharma82/EasyDDNS
You can also see its usage in my example code [PWM_LEDServer.ino](https://github.com/ofekp/TinyUPnP/blob/master/examples/PWM_LEDServer/PWM_LEDServer.ino)
# Special thanks
[@ajwtech](https://github.com/ajwtech) - for contributing to the package by noting the need to use `constrolURL` instead of `eventSubURL`
[@Lan-Hekary](https://github.com/Lan-Hekary) - for improving the API
[@GIPdA](https://github.com/GIPdA) - for adding ESP32 support