Giter Site home page Giter Site logo

khoih-prog / upnp_generic Goto Github PK

View Code? Open in Web Editor NEW
17.0 2.0 2.0 1.22 MB

A simple library that implements port mappings to router using UPnP SSDP for Arduino boards, running on nRF52, SAMD21/SAMD51, STM32F/L/H/G/WB/MP1, Teensy, RP2040-based boards, WT32_ETH01, Portenta_H7, etc. besides ESP8266/ESP32, using ESP WiFi, WiFiNINA, Ethernet W5x00, ESP8266/ESP32 AT-command WiFi, Portenta_H7 Murata WiFi or Vision-shield Ethernet.. It is designed to be light-weight and can run only on Ethernet or WiFi hardware/library supporting UDP Multicast. Supporting Teensy 4.1 QNEthernet now.

License: GNU General Public License v3.0

C++ 62.98% C 36.75% Shell 0.26%
upnp ssdp router port-mappings port-forwarding mbed wifi ethernet wifinina w5x00

upnp_generic's People

Contributors

khoih-prog avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

upnp_generic's Issues

ESP32 arduino with PlatformIO built in ETH.h library with ETH_LAN8720

Hi!. I try to use my esp32 with ETH_LAN8720 board.

My ethernet connection is working properly, iam using it for a while now.
I'm trying the upnp lib for this but it isn't working yet.

This is the built in eth library that iam using with ethernet so far: ETH.h GITHUB

My current simplified code from what i try to do here:

// Debug Level from 0 to 4
#define _DDNS_GENERIC_LOGLEVEL_     1
#define _UPNP_LOGLEVEL_             3

// Select DDNS_USING_WIFI for boards using built-in WiFi, such as Nano-33-IoT
#define DDNS_USING_WIFI             false
#define DDNS_USING_ETHERNET         true

#include <WiFi.h>
#include <ETH.h>
#include <UPnP_Generic.h>



void ETHEvent(WiFiEvent_t event){
        if( event == SYSTEM_EVENT_ETH_START ){
            ETH.setHostname(hostName);
        }else if( event == SYSTEM_EVENT_ETH_GOT_IP ){
            ETH_Got_IP = true;
            CurrentIP = ETH.localIP();
            displayETH();
            canScrollNoties  = true;
            if(!ETH_Timeout_Task_Deleted){ vTaskDelete(ETH_Timeout_Task_Handle); }
            gotIpMillis = millis();
            canSendRestartEmail = true;
            WiFi.softAPdisconnect(true);
            Ota_Begin();
            canSetupUPNP = true;
        }else if( event == SYSTEM_EVENT_ETH_DISCONNECTED ){
            ETH_Got_IP = false;
            displayNoIp("eth");
            ETH_Ip_Timeout_Start();
        }
 }

static const inline void ethernetBegin(){
// this is working perfectly well.

        ETH.begin(
            ETH_PHY_ADDR,
            PIN_PHY_POWER,
            PIN_SMI_MDC,
            PIN_SMI_MDIO,
            ETH_PHY_LAN8720,
            ETH_CLK_MODE
        );
        delay(10);
        if(!DHCP_Status){ // flag from a config file...
            ETH.config(ip, sub, gw);
        }
        WiFi.onEvent(ETHEvent);
 }

void setup(){
 Serial.begin(115200);
 ethernetBegin();
}

void loop(){
 startUPNP_Task();
}





/*********** UPNP THINGS FROM HERE IN DIFFERENT TASK ***********/



void onUpdateCallback(const char* oldIP, const char* newIP){
  (void) oldIP;
  Serial.print(F("DDNSGeneric - IP Change Detected: "));
  Serial.println(newIP);
}

static const inline void setupUPNP(){
    uPnP = new UPnP(30000);
    if (uPnP){
        uPnP->addPortMappingConfig(CurrentIP, 80, RULE_PROTOCOL_TCP, LEASE_DURATION, FRIENDLY_NAME);
        bool portMappingAdded = false;
        #define RETRY_TIMES 4
        int retries         = 0;
        while (!portMappingAdded && (retries < RETRY_TIMES)){
            Serial.println("Add Port Forwarding, Try # " + String(++retries));
            int result = uPnP->commitPortMappings();
            portMappingAdded = ( (result == PORT_MAP_SUCCESS) || (result == ALREADY_MAPPED) );
            if (!portMappingAdded){
                if (retries < RETRY_TIMES)
                vTaskDelay(10000);
            }
        }
        uPnP->printAllPortMappings();
        Serial.println(F("\nUPnP done"));
    }
}

static const inline void upnp_loop(){
    uPnP->updatePortMappings(600000);
}

void UPNP_Task( void * parameter ){
    setupUPNP();
    for ever{
        upnp_loop();
        vTaskDelay(1000);
    }
}

static const inline void startUPNP_Task(){
    if(canSetupUPNP){
        canSetupUPNP = false;
        xTaskCreatePinnedToCore(UPNP_Task,"UPNP_Task",5000,NULL,3,&UPNP_Task_Handle,0);
    }
}

Serial OUTPUT :

Time configured from NTP server was successful.
[UPnP] UDP_TX_PACKET_MAX_SIZE= 1000
[UPnP] UDP_TX_RESPONSE_MAX_SIZE= 8192
[UPnP] newUpnpRule= 192.168.10.110
Add Port Forwarding, Try # 1
[UPnP] Testing WiFi connection for IP : 0.0.0.0
.Generated admin Code: 95100816
....................[W][AsyncTCP.cpp:969] _poll(): rx timeout 4
...............................................................................................................................[UPnP]  ==> Timeout verify WiFi connection
[UPnP] ERROR: not connected to WiFi, can't continue
Add Port Forwarding, Try # 2
[UPnP] Testing WiFi connection for IP : 0.0.0.0
....................................................................................................................................................[UPnP]  ==> Timeout verify WiFi connection        
[UPnP] ERROR: not connected to WiFi, can't continue
Add Port Forwarding, Try # 3
[UPnP] Testing WiFi connection for IP : 0.0.0.0
....................................................................................................................................................[UPnP]  ==> Timeout verify WiFi connection        
[UPnP] ERROR: not connected to WiFi, can't continue
Add Port Forwarding, Try # 4
[UPnP] Testing WiFi connection for IP : 0.0.0.0
....................................................................................................................................................[UPnP]  ==> Timeout verify WiFi connection        
[UPnP] ERROR: not connected to WiFi, can't continue
[UPnP] isGatewayInfoValid : 0.0.0.0 , port : 0
[UPnP] Path :  , actionPort : 0
[UPnP] actionPath :  , serviceTypeName :
[UPnP] Gateway info not valid
[UPnP] Invalid router info, can't continue

UPnP done
[UPnP] Updating port mapping
[UPnP] Testing WiFi connection for IP : 0.0.0.0
....................................................................................................................................................[UPnP]  ==> Timeout verify WiFi connection        
[UPnP] ERROR: not connected to WiFi, can't continue
[UPnP] ERROR: While updating UPnP port mapping

UPNP can't see my gateway IP for some reason.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.