ESP32通过UDP对测试程序进行调用,方便调试使用(含源码)

本文章基础为 ESP32 无线调参功能文章修改而来。其 无线调参 的基本实现就是通过调用函数。

无线调参:ESP32超详细学习记录:实现无线调参的简单方法(提供源码)_喜暖知寒的博客-CSDN博客esp32的无线调参功能简单实现,提供源码。https://blog.csdn.net/qq_41650023/article/details/125024210?spm=1001.2014.3001.5501🚚 🚚 🚚 本质来说相差不大。

 假设需要执行的函数为:

void function_A(void)
{
    //函数功能
}

void function_B(void)
{
    //函数功能
}

函数处理

通过注册函数列表来调用函数。

typedef void (* CommandCallback)(char*);     //回调函数指针

CommandCallback call_list[20];               //回调函数指针列表
char* call_ids[20];                          //回调函数代名列表
int call_count;                              //用于计算有多少函数写入序列

注册函数到函数列表

void Command_add(char* id, CommandCallback onCommand)
{
  call_list[call_count] = onCommand; 
  call_ids[call_count] = id; 
  call_count++;
}

调用函数

void Command_run(char* str)
{
    for(int i=0; i < call_count; i++)
    {
        if(isSentinel(call_ids[i],str))
        {  
          call_list[i];         //回调函数
          break;
        }
    }
}

bool Command_isSentinel(char* ch,char* str)
{
    char s[strlen(ch)+1];    
    strncpy(s,str,strlen(ch));
    s[strlen(ch)] = '\0';     
    if(strcmp(ch, s) == 0)
       return true;
    else 
       return false;
}

和wifi&UDP相关

AsyncUDP udp;                     //创建UDP实例
unsigned int localUdpPort = 2333; //本地端口号 
IPAddress apIP(192, 168, 4, 4);   //设置AP的IP地址

const char *ssid = "esp32";             //账号
const char *password = "12345678";      //密码

WiFi.mode(WIFI_AP);   //设置为接入点模式AP
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); 
while(!WiFi.softAP(ssid, password)){}; //启动AP,函数获取并打印软AP的IP
Serial.println("AP启动成功");
while (!udp.listen(localUdpPort)){}; //等待udp监听设置成
udp.onPacket(onPacketCallBack); //注册收到数据包事件

代码例程(已验证)

(我自己写的,可能有部分不足之处。比如没验证开启AP,端口设置什么的都没有验证)

#include <WiFi.h>
#include <AsyncUDP.h> //引用以使用异步UDP

typedef void (* CommandCallback)(void);     //回调函数指针
CommandCallback call_list[20];               //回调函数指针列表
char* call_ids[20];                          //回调函数代名列表
int call_count;                              //用于计算有多少函数写入序列

AsyncUDP udp;                     //创建UDP实例
unsigned int localUdpPort = 2333; //本地端口号 

const char *ssid = "esp32";             //账号
const char *password = "12345678";      //密码
IPAddress apIP(192, 168, 4, 4);         //设置AP的IP地址

void Command_add(char* id, CommandCallback onCommand)
{
  call_list[call_count] = onCommand; 
  call_ids[call_count] = id; 
  call_count++;
}

bool Command_isSentinel(char* ch,char* str)
{
    char s[strlen(ch)+1];    
    strncpy(s,str,strlen(ch));
    s[strlen(ch)] = '\0';     
    if(strcmp(ch, s) == 0)
       return true;
    else 
       return false;
}

void Command_run(char* str)
{
    for(int i=0; i < call_count; i++)
    {
        if(Command_isSentinel(call_ids[i],str))
        {  
          call_list[i]();         //回调函数
          break;
        }
    }
}

void function_A(void)
{
  Serial.println("功能A执行");
}
void function_B(void)
{
  Serial.println("功能B执行");
}

void onPacketCallBack(AsyncUDPPacket packet)
{
  char* da;
  da= (char*)(packet.data());
  Serial.print("读到信息:");
  Serial.println(da);   
  Command_run(da);     
}

void setup() 
{
  Serial.begin(115200);
  
  WiFi.mode(WIFI_AP);   //设置为接入点模式AP
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); 
  while(!WiFi.softAP(ssid, password)){}; //启动AP,函数获取并打印软AP的IP
  Serial.println("AP启动成功");
  while (!udp.listen(localUdpPort)){}; //等待udp监听设置成
  udp.onPacket(onPacketCallBack); //注册收到数据包事件

  Command_add("FUNA",function_A);
  Command_add("FUNB",function_B);
  Serial.println("IP:192.168.4.4");
  Serial.println("输入FUNA或者FUNB");
  
}

void loop() 
{
  while(1);

}

说明:运行结果

通过UDP发送FUNA,串口打印 功能A执行 。通过UDP发送FUNB,串口打印 功能B执行 。

串口打印:(是在UDP写入FUNA和FUNB之后的结果

手机UDP助手:连接IP 192.168.4.4 ,端口号:2333。

 OK,这样基本功能就实现了。并且也间接验证了无线调参功能。

UDP_Sender文件下载:(0积分)

https://download.csdn.net/download/qq_41650023/85502153

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的ESP32 UDP图传程序示例,该程序使用Arduino IDE编写: ``` #include <WiFi.h> #include <WiFiUdp.h> #include <esp_camera.h> // Replace with your network credentials const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; // UDP port for image transmission unsigned int localPort = 12345; // Create a UDP object WiFiUDP udp; void setup() { Serial.begin(115200); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting..."); } // Start the camera camera_config_t config; config.led_pin = -1; esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("Camera init failed with error 0x%x", err); return; } // Start the UDP server udp.begin(localPort); Serial.printf("UDP server started on port %d", localPort); } void loop() { // Capture an image camera_fb_t * fb = esp_camera_fb_get(); if (!fb) { Serial.println("Camera capture failed"); return; } // Send the image over UDP udp.beginPacket(udp.remoteIP(), udp.remotePort()); udp.write(fb->buf, fb->len); udp.endPacket(); // Free the camera buffer esp_camera_fb_return(fb); // Wait for a moment before capturing the next image delay(1000); } ``` 此程序连接到Wi-Fi网络,然后使用ESP32摄像头捕获图像,并通过UDP将图像发送到指定的IP地址和端口。您需要将代码中的“your_SSID”和“your_PASSWORD”替换为您的Wi-Fi网络凭据,并将“udp.remoteIP()”和“udp.remotePort()”更改为接收图像的目标IP地址和端口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值