ESP32-cam send UDP datagram

HI, everyone
I’m trying to send UDP datagram to my computer using WIFI & ESP32-CAM.
I’m using ESP32-CAM to soft AP, and my computer is associated to ESP32-CAM.
And i used code which i modified the code a little bit.

I used a “Socket test” to test, but it couldn’t receive UDP message which send from ESP32-CAM.
I don’t know what is wrong…
Please help!

캡처1

#include "esp_camera.h"
#include <WiFi.h>
#include <WiFiUdp.h>
//
// WARNING!!! Make sure that you have either selected ESP32 Wrover Module,
//            or another board which has PSRAM enabled

// Select camera model
//#define CAMERA_MODEL_WROVER_KIT
//#define CAMERA_MODEL_ESP_EYE
//#define CAMERA_MODEL_M5STACK_PSRAM_
//#define CAMERA_MODEL_M5STACK_WIDE
#define CAMERA_MODEL_AI_THINKER

#include "camera_pins.h"

const char* ssid = "ESP32-CAM Access Point";
const char* password = "123456789";
unsigned int localPort = 2390;
void startCameraServer();
uint8_t message = 0xAA;

WiFiUDP Udp;

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  Serial.println();

  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;
  //init with high specs to pre-allocate larger buffers
  if(psramFound()){
    config.frame_size = FRAMESIZE_UXGA;
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }

#if defined(CAMERA_MODEL_ESP_EYE)
  pinMode(13, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
#endif

  // camera init
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }

  sensor_t * s = esp_camera_sensor_get();
  //initial sensors are flipped vertically and colors are a bit saturated
  if (s->id.PID == OV3660_PID) {
    s->set_vflip(s, 1);//flip it back
    s->set_brightness(s, 1);//up the blightness just a bit
    s->set_saturation(s, -2);//lower the saturation
  }
  //drop down frame size for higher initial frame rate
  s->set_framesize(s, FRAMESIZE_QVGA);

#if defined(CAMERA_MODEL_M5STACK_WIDE)
  s->set_vflip(s, 1);
  s->set_hmirror(s, 1);
#endif

  WiFi.softAP(ssid, password);
  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(IP);

  startCameraServer();

  Serial.print("Camera Ready! Use 'http://");
  Serial.print(WiFi.localIP());
  Serial.println("' to connect");
  Udp.begin(localPort);
}

void loop() {
  Udp.begin(localPort);
  int A = Udp.beginPacket("my computer IP",localPort);
  Serial.println("Sending UDP packet...");
  Serial.println(A);
  Serial.println(message);
  Udp.write(message); 
  Udp.endPacket();
  delay(1000);
}

I don’t know whether this is a comment or or what you intend but “my computer IP” won’t work.

How are you assigning the IP adddresses, nothing in your code appears to set the IP address in which case they will default to a private address that is unlikely to be in the correct subnet for your PC. Where did your PC get its IP address from? DHCP from your router?

“my computer IP” is a comment and I wrote the actual IP in my file.

Yes, that’s right. My computer is connected to a wired LAN at school, and got an IP from DHCP.

If your computer is connect to a wired LAN and got its IP address from there, that IP address will be for the wired LAN interface. For the wireless interface it will have a different IP address since it appears to be trying to connect to the ESP-CAM wirelessly. If the wireless IP address of your PC is in the same subnet as your wired IP address you will have problems. How have you obtained an set your wireless IP address? How have you set your ESP-CAM wireless IP address and are the 2 in the same subnet and different to the wired IP address subnet?

Sorry @countrypaul , I’m late.

The IP address of the ESP-CAM is 192.168.4.1 (default set and automatically received).

First of all, it doesn’t look like the two are in the same subnet.
ESP-CAM is 192.168 but my PC is 192.2xx(wire LAN).

Also, what I want is to put the ESP-CAM as an AP, connect the client to this AP as a wifi, and then send image data from ESP-CAM to wifi, how can I know the IP and port of this client?

If the PC has both wired and wireless connections then you should see both adapters if you create a CMD window and type ipconfig (windows), on Linux ifconfig.

This should show you what the current address of each interface is.

If the PC connects to the ESP32 wifi AP then the ESP32 will issue the PC with an ip address using DHCP, I believe this is default so you should not need to do anything to achieve this. If you run ipconfig after connecting to the ESP32 you should be able to see the ip address assigned by the ESP32.

If the ESP32 AP is assigning an ip address that is in the subnet assigned to your wired network things will get more complicated - however lets find out what they are before going down that path. You have mentioned the IP addresses in use by the AP and PC, can you find the network mask as well? The AP is likely to be 255.255.255.0, but the mask on your wired network is likely to be different and may result in the subnets overlapping and causing problems.

Also I believe that the SSID is case sensitive (not 100% certain) so make sure the PC is trying to connect to the SSID of the ESP32 exactly as you have specified it in your sketch.

I looked at the subnet mask through ipconfig, and the subnet mask of ESP-CAM was 255.255.255.0 as you said, and the subnet mask of my wired LAN was 255.255.255.128. Can you say that this overlaps with each other?
Also, after wifi connection from PC to ESP32-CAM, I checked the ip address of wireless LAN through ipconfig, where I got the ip address of ESP32-CAM (192.168.4.2). I think there’s something wrong with this.
I want to know what ip address the PC receives when it is connected to ESP32-CAM and the port number for UDP communication. It’s because only then can UDP data be sent from ESP32-CAM to PC.

And before I write this, I let the ESP32-CAM work with the Client, not with AP, and when ESP32-CAM and my laptop were connected to my cell phone hotspot, the UDP file transfer from ESP32-CAM was delivered to my laptop!

However, my final goal is to attach ESP-CAM to drones and send image data wirelessly to Arduino parts that have different WIFI modules, but despite the situation above, it is hard to determine whether ESP32-CAM should be used as an AP or as a Client.

Thank you very much for your concern. @countrypaul

I was expecting your wired lan mask to be more like 255.255.0.0, but you should have no problems with the two you have assuming that the PC wired interface remains in the 192.2xx range.

When you use your mobile phone as a hotspot it is issuing IP addresses to both your PC (you can use ipconfig to see what address it gives you) and also to the ESP-CAM.

In AP mode the address of your ESP-CAM being 192.168.4.1 is designed so that it does not clash with the most common ranges 192.168.1.x and 192.168.0.x issued by many routers attached to the Internet.

How exactly did you check the address of the ESP-CAM using ipconfig? The address you give 192.168.4.2 is exactly what I would expect for the wireless interface on your PC if it was issued by the ESP-CAM.

Each network interface on your PC, whether it is wired Ethernet, wireless, or even serial, if using TCP/IP will have its own IP address. It is important to remember that each interface has its own IP address, not each machine, so your PC will have at least 2 (probably more with virtual interfaces such as 127.0.0.1).

On the PC if you ping 192.168.4.1 what result do you get? To use ping (I don’t know whether you know or not) create a CMD prompt and simply type:
ping 192.168.4.11

You should get 5 lines of information about what is happening followed by 4 lines of summary.

After connecting WIFI to the ESP-CAM running the PC as an AP, I ran ipconfig on the PC and saw information about the wireless LAN adapter Wi-Fi coming out of it.
Also, as you said, I opened the CMD window and carried out the PING command for 192.168.4.1 and confirmed that it was working properly.

Congratulations, have you now got your own software working as you wanted too?

I still have a long way to go to my goal, but thanks to you, I’ve crossed the difficult stage.

Thank you very much for your help.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.