maxgerhardt / pio-esp32-ethernet-ota

Demo with a ESP32 + Ethernet + OTA
10 stars 3 forks source link

Curl fails to connect to IP address #1

Closed pscott-ams closed 2 years ago

pscott-ams commented 2 years ago

Thank you for making this example, I've been struggling getting ESP32 + W5500 + OTA + PlatformIO to work.

I've done the Prerequisites:

image

Here is the curl in my PATH:

image

Here is my platformio.ini:

[env:wemos_d1_mini32]
platform = espressif32
board = wemos_d1_mini32
framework = arduino
monitor_speed = 115200
lib_deps = 
  ArduinoOTA=https://github.com/jandrassy/ArduinoOTA.git
  Ethernet=https://github.com/maxgerhardt/Ethernet.git

[env:wemos_d1_mini32_ota]
platform = espressif32
board = wemos_d1_mini32
framework = arduino
monitor_speed = 115200
lib_deps = 
  ArduinoOTA=https://github.com/jandrassy/ArduinoOTA.git
  Ethernet=https://github.com/maxgerhardt/Ethernet.git

extra_scripts = post:extra_scripts.py
upload_protocol = custom
upload_flags =
  http://arduino:password@192.168.167.101:65280/sketch

I have extra_scripts.py in the same folder as platformio.ini:

image

I uploaded your example to my ESP32. When I try to upload via OTA, I get the following message:

Building in release mode
Retrieving maximum program size .pio\build\wemos_d1_mini32_ota\firmware.elf
Checking size .pio\build\wemos_d1_mini32_ota\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   4.4% (used 14412 bytes from 327680 bytes)
Flash: [==        ]  22.8% (used 299086 bytes from 1310720 bytes)
Configuring upload protocol...
AVAILABLE: custom, esp-prog, espota, esptool, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa
CURRENT: upload_protocol = custom
Uploading .pio\build\wemos_d1_mini32_ota\firmware.bin
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:03 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:04 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:05 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:06 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:07 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:08 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:09 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:10 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:11 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:12 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:13 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:14 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:15 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:16 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:17 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:18 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:19 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:20 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:21 --:--:--     0      
curl: (28) Failed to connect to 192.168.167.101 port 65280 after 21051 ms: Timed out
*** [upload] Error 28
================================================================================================== [FAILED] Took 24.05 seconds ==================================================================================================

Environment          Status    Duration
-------------------  --------  ------------
wemos_d1_mini32_ota  FAILED    00:00:24.047
============================================================================================= 1 failed, 0 succeeded in 00:00:24.047 =============================================================================================

I modified the code to set a static IP address instead of DHCP. Here is my code:

#include <Arduino.h>
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoOTA.h>

//#define Serial SerialUSB

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x02, 0xF0, 0x0D, 0xBE, 0xEF, 0x01 };
IPAddress ip = { 192, 168, 167, 101 };

unsigned long last_time = 0;

#ifndef LED_BUILTIN
//red LED on my esp32 dev board is on IO2
#define LED_BUILTIN 2
#endif
bool led_state = false;

void setup() {
  //Initialize serial:
  Serial.begin(115200);
  while (!Serial);

  pinMode(LED_BUILTIN, OUTPUT);

  //voltage stablilizer lol
  delay(1000);

  SPI.begin();
  Ethernet.init(5);
  // start the Ethernet connection:
  Serial.println("Initializing Ethernet...");
  Ethernet.begin(mac, ip);
  Serial.print("Local IP: ");
  Serial.println(Ethernet.localIP());

  // start the OTEthernet library with internal (flash) based storage
  ArduinoOTA.begin(Ethernet.localIP(), "Arduino", "password", InternalStorage);
}

void loop() {
  // check for updates
  ArduinoOTA.poll();
  //every second, print link status and toggle LED
  if(millis() - last_time >= 500) {
      last_time = millis();
      auto link = Ethernet.linkStatus();
      Serial.print("[V2] Link status: ");
      switch (link) {
        case Unknown:
          Serial.println("Unknown");
          break;
        case LinkON:
          Serial.println("ON");
          break;
        case LinkOFF:
          Serial.println("OFF");
          break;
      }
      //toggle
      led_state = !led_state;
      digitalWrite(LED_BUILTIN, led_state ? HIGH : LOW);
  }
}

Any idea why the connection is timing out?

maxgerhardt commented 2 years ago

Can you ping the 192.168.167.101 IP? What is the output on the serial monitor?

pscott-ams commented 2 years ago

Thank you for responding

I'm unable to ping the IP:

image

Here is the serial monitor output before the OTA upload:

Initializing Ethernet...
Local IP: 192.168.167.101
[V2] Link status: ON
[V2] Link status: ON
[V2] Link status: ON
[V2] Link status: ON

When I start the OTA upload, the serial monitor closes and all I can see is the output I included in the OP:

Building in release mode
Retrieving maximum program size .pio\build\wemos_d1_mini32_ota\firmware.elf
Checking size .pio\build\wemos_d1_mini32_ota\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   4.4% (used 14412 bytes from 327680 bytes)
Flash: [==        ]  22.8% (used 299086 bytes from 1310720 bytes)
Configuring upload protocol...
AVAILABLE: custom, esp-prog, espota, esptool, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa
CURRENT: upload_protocol = custom
Uploading .pio\build\wemos_d1_mini32_ota\firmware.bin
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:03 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:04 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:05 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:06 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:07 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:08 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:09 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:10 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:11 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:12 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:13 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:14 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:15 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:16 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:17 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:18 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:19 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:20 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:21 --:--:--     0      
curl: (28) Failed to connect to 192.168.167.101 port 65280 after 21051 ms: Timed out
*** [upload] Error 28
================================================================================================== [FAILED] Took 24.05 seconds ==================================================================================================

Environment          Status    Duration
-------------------  --------  ------------
wemos_d1_mini32_ota  FAILED    00:00:24.047
============================================================================================= 1 failed, 0 succeeded in 00:00:24.047 =============================================================================================

Could it be the network settings? Here are the properties of the usb-ethernet adapter I'm using:

image

image

maxgerhardt commented 2 years ago

https://user-images.githubusercontent.com/91959309/159103930-665777bc-9d92-4ca8-b219-b16889aa9231.png

Well how can your adapter have the same IP as the ESP32? That's a collision.

Also you're directly connecting your computer with the ESP via a ethernet cable? You may need a crossover cable because here it says the W5500 can't do automatic polarity inversion.

Does it not work if you re-activate DHCP in the sketch and connect it to your home router?

pscott-ams commented 2 years ago

Thanks again for the quick response

I changed the IP of the adapter to 192.168.167.99

When I re-activated DHCP and connect the W5500 directly to the router this is what the serial monitor shows:

Initializing Ethernet...
Local IP: 192.168.1.73
[V2] Link status: ON
[V2] Link status: ON
[V2] Link status: ON
[V2] Link status: ON

And then when I try to upload OTA, I get the same curl timeout error. Unfortunately, I do not have a crossover cable with me at the moment. I'll be able to get one on Monday.

maxgerhardt commented 2 years ago

Is the device pingable in this network configuration?

pscott-ams commented 2 years ago

Sorry, I forgot to change the IP address in the platformio.ini With DHCP re-activated, and the W5500 connected directly to the router, I was able to successfully use OTA upload:

Uploading .pio\build\wemos_d1_mini32_ota\firmware.bin
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
 86  295k    0     0   86  256k      0   264k  0:00:01 --:--:--  0:00:01  264k
100  295k    0     0  100  295k      0   118k  0:00:02  0:00:02 --:--:--  118k
100  295k    0     2  100  295k      0   114k  0:00:02  0:00:02 --:--:--  114k
================================================================================================= [SUCCESS] Took 14.50 seconds =================================================================================================

I am able to ping the device as well:

C:\Users\PatsCot>ping 192.168.1.73

Pinging 192.168.1.73 with 32 bytes of data:
Reply from 192.168.1.73: bytes=32 time=2ms TTL=128
Reply from 192.168.1.73: bytes=32 time=2ms TTL=128
Reply from 192.168.1.73: bytes=32 time=1ms TTL=128
Reply from 192.168.1.73: bytes=32 time=2ms TTL=128

Ping statistics for 192.168.1.73:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 1ms, Maximum = 2ms, Average = 1ms

I would still like to be able to set a static IP address, as it is required for the project I am working on. Perhaps the crossover cable will fix this?

maxgerhardt commented 2 years ago

Does it work when connected to the router but with a static IP?

pscott-ams commented 2 years ago

No, the connection times out during OTA upload.

I'm also unable to ping the IP address.

pscott-ams commented 2 years ago

Here is my computer's network settings when I type ipconfig in command prompt:

Wireless LAN adapter Wi-Fi 3:

   Connection-specific DNS Suffix  . : attlocal.net
   IPv6 Address. . . . . . . . . . . : 2600:1700:a110:7a90::49
   IPv6 Address. . . . . . . . . . . : 2600:1700:a110:7a90:7090:7c62:dbfb:5471
   Temporary IPv6 Address. . . . . . : 2600:1700:a110:7a90:a473:7aea:bfdc:b0bc
   Link-local IPv6 Address . . . . . : fe80::7090:7c62:dbfb:5471%11
   IPv4 Address. . . . . . . . . . . : 192.168.1.70
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : fe80::eec3:2ff:fe2f:7cf1%11
                                       192.168.1.254

When I assign a static IP to the ESP32 that has a value of 192.168.1.xxx, then I'm able to ping the device and upload using OTA successfully.

Is there a way for this to work when I assign an IP address of 192.168.167.xxx?

maxgerhardt commented 2 years ago

Do you set your ethernet adapter on the PC to have the 192.168.167.1 IP address? (And the ESP32 to have a 192.168.167.xxx address but xxx != 1 or 0 or 255)

pscott-ams commented 2 years ago

I'm not really sure why, but it is working now!

These are the network settings of my USB-ethernet adapter:

image

I'm also able to ping the device:

C:\Users\PatsCot>ping 192.168.167.101

Pinging 192.168.167.101 with 32 bytes of data:
Reply from 192.168.167.101: bytes=32 time<1ms TTL=128
Reply from 192.168.167.101: bytes=32 time<1ms TTL=128
Reply from 192.168.167.101: bytes=32 time<1ms TTL=128
Reply from 192.168.167.101: bytes=32 time<1ms TTL=128

Ping statistics for 192.168.167.101:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

And here is the serial monitor displaying the different network addresses:

Initializing Ethernet...
Local IP: 192.168.167.101
DNS: 192.168.167.1
Gateway: 192.168.167.1
Subnet Mask: 255.255.255.0
[V2] Link status: ON

Thank you so much max, I've been struggling with this project for weeks now!

ZZKK000 commented 2 years ago

must be in the same network segment, otherwise the subnet mask may be set like 255.255.252.0, if you want to connect to more devices than 255.