Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include "esp_eth.h"
- #include "eth_phy/phy.h"
- #include "eth_phy/phy_lan8720.h"
- #include <WiFi.h>
- #include "IPAddress.h"
- /** for forwarding UPNP **/
- #include <TinyUPnP.h>
- #include <EasyDDNS.h>
- TinyUPnP tinyUPnP(60000);
- #define DDNS_USERNAME "baba"
- #define DDNS_PASSWORD "blabla"
- #define DDNS_DOMAIN "blabla" // there are real account logins
- ESP32Ethernet ethernet;
- #define ETH_PHY_CONF phy_lan8720_default_ethernet_config
- #define ETH_PHY_ADDR PHY1
- #define PIN_PHY_POWER 17
- #define PIN_SMI_MDC 23
- #define PIN_SMI_MDIO 18
- static eth_config_t eth_config = ETH_PHY_CONF;
- boolean gotip = false;
- boolean noIp_E = false;
- static inline void ethernet_config_gpio(void){
- // RMII data pinek fixek:
- // CRS_DRV = GPIO27 PULL-UP 2,2k
- // TXD0 = GPIO19
- // TXD1 = GPIO22
- // TX_EN = GPIO21
- // RXD0 = GPIO25 PULL-UP 2,2k
- // RXD1 = GPIO26 PULL-UP 2,2k
- // CLK = GPIO0
- phy_rmii_configure_data_interface_pins();
- // MDC = GPIO 23
- // MDIO = GPIO 18 PULL-UP 2,2k
- phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO);
- }
- static inline void ethernet_power_enable(bool enable){
- pinMode(PIN_PHY_POWER, OUTPUT);
- digitalWrite(PIN_PHY_POWER, enable);
- delay(1);// Kis delay hogy a power up/down működjön, min 300us
- }
- extern void tcpipInit();
- inline void ETH_begin(){
- eth_config.phy_addr = ETH_PHY_ADDR;
- eth_config.gpio_config = ethernet_config_gpio;
- eth_config.tcpip_input = tcpip_adapter_eth_input;
- eth_config.phy_power_enable = ethernet_power_enable;
- tcpipInit();
- esp_err_t err = esp_eth_init(ð_config);
- if(!err){
- err = esp_eth_enable();
- }
- }
- int ETH_config(IPAddress local_ip, IPAddress subnet, IPAddress gateway)
- {
- // stop the DHCP service
- if(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH) == ESP_OK) {
- // continue
- }
- else {
- return 0;
- }
- // create a new TCP/IP info
- tcpip_adapter_ip_info_t info;
- info.ip.addr = static_cast<uint32_t>((local_ip));
- info.gw.addr = static_cast<uint32_t>((gateway));
- info.netmask.addr = static_cast<uint32_t>((subnet));
- // apply the IP settings
- if(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info) == ESP_OK) {
- // continue
- } else {
- return 0;
- }
- return 1;
- }
- IPAddress ETH_localIP()
- {
- tcpip_adapter_ip_info_t ip;
- tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);
- return IPAddress(ip.ip.addr);
- }
- IPAddress ETH_subnetMask()
- {
- tcpip_adapter_ip_info_t ip;
- tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);
- return IPAddress(ip.netmask.addr);
- }
- IPAddress ETH_gatewayIP()
- {
- tcpip_adapter_ip_info_t ip;
- tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);
- return IPAddress(ip.gw.addr);
- }
- bool ETH_setHostname(const char * hostname)
- {
- return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, hostname) == 0;
- }
- bool ETH_fullDuplex()
- {
- return eth_config.phy_get_duplex_mode();
- }
- uint8_t ETH_linkSpeed()
- {
- return eth_config.phy_get_speed_mode()?100:10;
- }
- String ETH_macAddress(void)
- {
- uint8_t mac[6];
- char macStr[18] = { 0 };
- esp_eth_get_mac(mac);
- sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
- return String(macStr);
- }
- static const inline void Init_UPNP_Task(int Stack_Depth,int Core,int Priority){
- xTaskCreatePinnedToCore(UPNP_Task,"UPNP_Task",Stack_Depth,NULL,Priority,&UPNP_Task_Handle,Core);
- }
- void WiFiEvent(WiFiEvent_t event){
- switch(event) {
- case SYSTEM_EVENT_ETH_START:
- //set eth hostname here
- ETH_setHostname(hostName);
- break;
- case SYSTEM_EVENT_ETH_CONNECTED:
- //ethernet connected (if manual IP)
- //WiFi.enableIpV6();
- //tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_ETH);
- break;
- case SYSTEM_EVENT_ETH_GOT_IP:
- ETH_Got_IP = true;
- Display_UI();
- //Init_Weather_API_Task(4096,0,0);
- //Init_HoliDay_API_Task(5120,1,2);
- vTaskDelete(ETH_Timeout_Task_Handle);
- Init_UPNP_Task(5024,0,0);
- break;
- case SYSTEM_EVENT_ETH_DISCONNECTED:
- ETH_Got_IP = false;
- NoIp_Kiir();
- ETH_Ip_Timeout_Start();
- vTaskDelete(Weather_API_Task_Handle);
- vTaskDelete(HoliDay_API_Task_Handle);
- break;
- default:
- break;
- }
- }
- void ETH_Timeout_Task( void * parameter ){
- for ever{
- if((millis() - ETH_IP_Timeout) >= 15000){
- if(!ETH_Got_IP){
- NoIp_Kiir();
- vTaskDelete(Weather_API_Task_Handle);
- }
- vTaskDelete(ETH_Timeout_Task_Handle);
- }
- vTaskDelay(10);
- }
- }
- static const inline void ETH_Init(){
- ETH_begin();
- delay(10);
- if(!DHCP_Status){
- ethernet.config(ip, sub, gw);
- }
- }
- /** UPNP **/
- static const inline void Init_UPNP(int LISTEN_PORT, int LEASE_DURATION, String FRIENDLY_NAME){
- boolean portMappingAdded = false;
- tinyUPnP.addPortMappingConfig(ETH_localIP(), LISTEN_PORT, RULE_PROTOCOL_TCP, LEASE_DURATION, FRIENDLY_NAME);
- while (!portMappingAdded) {
- portMappingAdded = tinyUPnP.commitPortMappings();
- Serial.println("");
- if (!portMappingAdded) {
- tinyUPnP.printAllPortMappings();
- Serial.println(F("This was printed because adding the required port mapping failed"));
- vTaskDelay(3000);
- }
- }
- Serial.println("UPnP done");
- EasyDDNS.service("noip");
- EasyDDNS.client(DDNS_DOMAIN, DDNS_USERNAME, DDNS_PASSWORD);
- }
- void UPNP_Task( void * parameter ){
- Init_UPNP(80, 60000, "MyHomehitec");
- //vTaskDelete(NULL);
- for ever{
- EasyDDNS.update(300000);
- tinyUPnP.updatePortMappings(600000);
- vTaskDelay(10);
- }
- vTaskDelay(1);
- }
- void setup(){
- Serial.begin(115200);
- ETH_Init();
- ETH_Ip_Timeout_Start();
- }
- void loop(){
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement