Advertisement
DrRandom

Ethernet esp32

Oct 3rd, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2.  
  3. #include "esp_eth.h"
  4. #include "eth_phy/phy.h"
  5. #include "eth_phy/phy_lan8720.h"
  6. #include <WiFi.h>
  7. #include "IPAddress.h"
  8.  
  9. /** for forwarding UPNP **/
  10. #include <TinyUPnP.h>
  11. #include <EasyDDNS.h>
  12.  
  13.     TinyUPnP tinyUPnP(60000);
  14.     #define DDNS_USERNAME "baba"
  15.     #define DDNS_PASSWORD "blabla"
  16.     #define DDNS_DOMAIN "blabla" // there are real account logins
  17.    
  18.     ESP32Ethernet ethernet;
  19.  
  20. #define ETH_PHY_CONF  phy_lan8720_default_ethernet_config
  21. #define ETH_PHY_ADDR  PHY1
  22. #define PIN_PHY_POWER 17
  23. #define PIN_SMI_MDC   23
  24. #define PIN_SMI_MDIO  18
  25.  
  26.  
  27. static eth_config_t eth_config = ETH_PHY_CONF;
  28. boolean gotip = false;
  29. boolean noIp_E = false;
  30.  
  31. static inline void ethernet_config_gpio(void){
  32.     // RMII data pinek fixek:
  33.     // CRS_DRV  = GPIO27        PULL-UP 2,2k
  34.     // TXD0     = GPIO19
  35.     // TXD1     = GPIO22
  36.     // TX_EN    = GPIO21
  37.     // RXD0     = GPIO25        PULL-UP 2,2k
  38.     // RXD1     = GPIO26        PULL-UP 2,2k
  39.     // CLK      = GPIO0
  40.     phy_rmii_configure_data_interface_pins();
  41.     // MDC      = GPIO 23
  42.     // MDIO     = GPIO 18       PULL-UP 2,2k
  43.     phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO);
  44. }
  45.  
  46. static inline void ethernet_power_enable(bool enable){
  47.     pinMode(PIN_PHY_POWER, OUTPUT);
  48.     digitalWrite(PIN_PHY_POWER, enable);
  49.     delay(1);// Kis delay hogy a power up/down működjön, min 300us
  50. }
  51.  
  52. extern void tcpipInit();
  53.  
  54.  inline void ETH_begin(){
  55.     eth_config.phy_addr = ETH_PHY_ADDR;
  56.     eth_config.gpio_config = ethernet_config_gpio;
  57.     eth_config.tcpip_input = tcpip_adapter_eth_input;
  58.     eth_config.phy_power_enable = ethernet_power_enable;
  59.     tcpipInit();
  60.     esp_err_t err = esp_eth_init(&eth_config);
  61.     if(!err){
  62.         err = esp_eth_enable();
  63.     }
  64. }
  65.  
  66. int ETH_config(IPAddress local_ip, IPAddress subnet, IPAddress gateway)
  67. {
  68.     // stop the DHCP service
  69.     if(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH) == ESP_OK) {
  70.         // continue
  71.     }
  72.     else {
  73.         return 0;
  74.     }
  75.    
  76.     // create a new TCP/IP info
  77.     tcpip_adapter_ip_info_t info;
  78.     info.ip.addr = static_cast<uint32_t>((local_ip));
  79.     info.gw.addr = static_cast<uint32_t>((gateway));
  80.     info.netmask.addr = static_cast<uint32_t>((subnet));
  81.     // apply the IP settings
  82.     if(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info) == ESP_OK) {
  83.         // continue
  84.     } else {
  85.         return 0;
  86.     }
  87.     return 1;
  88. }
  89.  
  90. IPAddress ETH_localIP()
  91. {
  92.     tcpip_adapter_ip_info_t ip;
  93.     tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);
  94.     return IPAddress(ip.ip.addr);
  95. }
  96.  
  97. IPAddress ETH_subnetMask()
  98. {
  99.     tcpip_adapter_ip_info_t ip;
  100.     tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);
  101.     return IPAddress(ip.netmask.addr);
  102. }
  103.  
  104. IPAddress ETH_gatewayIP()
  105. {
  106.     tcpip_adapter_ip_info_t ip;
  107.     tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);
  108.     return IPAddress(ip.gw.addr);
  109. }
  110.  
  111. bool ETH_setHostname(const char * hostname)
  112. {
  113.     return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, hostname) == 0;
  114. }
  115.  
  116. bool ETH_fullDuplex()
  117. {
  118.     return eth_config.phy_get_duplex_mode();
  119. }
  120.  
  121. uint8_t ETH_linkSpeed()
  122. {
  123.     return eth_config.phy_get_speed_mode()?100:10;
  124. }
  125.  
  126. String ETH_macAddress(void)
  127. {
  128.     uint8_t mac[6];
  129.     char macStr[18] = { 0 };
  130.     esp_eth_get_mac(mac);
  131.     sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  132.     return String(macStr);
  133. }
  134.  
  135.     static const inline void Init_UPNP_Task(int Stack_Depth,int Core,int Priority){
  136.         xTaskCreatePinnedToCore(UPNP_Task,"UPNP_Task",Stack_Depth,NULL,Priority,&UPNP_Task_Handle,Core);
  137.     }
  138.  
  139.     void WiFiEvent(WiFiEvent_t event){
  140.         switch(event) {
  141.             case SYSTEM_EVENT_ETH_START:
  142.                 //set eth hostname here
  143.                 ETH_setHostname(hostName);
  144.                 break;
  145.             case SYSTEM_EVENT_ETH_CONNECTED:
  146.                 //ethernet connected (if manual IP)
  147.                 //WiFi.enableIpV6();
  148.                 //tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_ETH);
  149.                 break;
  150.             case SYSTEM_EVENT_ETH_GOT_IP:
  151.                 ETH_Got_IP = true;
  152.                 Display_UI();
  153.                 //Init_Weather_API_Task(4096,0,0);
  154.                 //Init_HoliDay_API_Task(5120,1,2);
  155.                 vTaskDelete(ETH_Timeout_Task_Handle);
  156.                 Init_UPNP_Task(5024,0,0);
  157.                 break;
  158.             case SYSTEM_EVENT_ETH_DISCONNECTED:
  159.                 ETH_Got_IP = false;
  160.                 NoIp_Kiir();
  161.                 ETH_Ip_Timeout_Start();
  162.                 vTaskDelete(Weather_API_Task_Handle);
  163.                 vTaskDelete(HoliDay_API_Task_Handle);
  164.                 break;
  165.             default:
  166.                 break;
  167.         }
  168.     }
  169.  
  170.     void ETH_Timeout_Task( void * parameter ){
  171.         for ever{
  172.             if((millis() - ETH_IP_Timeout) >= 15000){
  173.                 if(!ETH_Got_IP){
  174.                     NoIp_Kiir();
  175.                     vTaskDelete(Weather_API_Task_Handle);
  176.                 }
  177.                 vTaskDelete(ETH_Timeout_Task_Handle);
  178.             }
  179.             vTaskDelay(10);
  180.         }
  181.     }
  182.  
  183. static const inline void ETH_Init(){
  184.   ETH_begin();
  185.   delay(10);
  186.   if(!DHCP_Status){
  187.     ethernet.config(ip, sub, gw);
  188.   }
  189. }
  190.  
  191.  
  192. /** UPNP **/
  193. static const inline void Init_UPNP(int LISTEN_PORT, int LEASE_DURATION, String FRIENDLY_NAME){
  194.         boolean portMappingAdded = false;
  195.         tinyUPnP.addPortMappingConfig(ETH_localIP(), LISTEN_PORT, RULE_PROTOCOL_TCP, LEASE_DURATION, FRIENDLY_NAME);
  196.         while (!portMappingAdded) {
  197.             portMappingAdded = tinyUPnP.commitPortMappings();
  198.             Serial.println("");
  199.        
  200.             if (!portMappingAdded) {
  201.                 tinyUPnP.printAllPortMappings();
  202.                 Serial.println(F("This was printed because adding the required port mapping failed"));
  203.                 vTaskDelay(3000);
  204.             }
  205.         }
  206.        
  207.         Serial.println("UPnP done");
  208.         EasyDDNS.service("noip");
  209.         EasyDDNS.client(DDNS_DOMAIN, DDNS_USERNAME, DDNS_PASSWORD);
  210.     }
  211.  
  212.     void UPNP_Task( void * parameter ){
  213.         Init_UPNP(80, 60000, "MyHomehitec");
  214.         //vTaskDelete(NULL);
  215.         for ever{
  216.             EasyDDNS.update(300000);
  217.             tinyUPnP.updatePortMappings(600000);
  218.             vTaskDelay(10);
  219.         }
  220.         vTaskDelay(1);
  221.     }
  222.  
  223. void setup(){
  224.  Serial.begin(115200);
  225.  ETH_Init();
  226.  ETH_Ip_Timeout_Start();
  227. }
  228.  
  229. void loop(){
  230. }
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement