首页 > 解决方案 > 在 ESP32 中运行主程序时如何通过 wifi 接收消息?

问题描述

我已经在一个微控制器程序(ESP32 Wroom32)中加入了我想要的多个功能,并且需要一些关于保持程序运行并在运行时接收消息的最佳方法的建议。

当前代码:

//includes and declarations
setup()
{
//setup up wifi, server
}

main(){

WiFiClient client = server.available();
byte new_command[40];

if (client)                         // If client object is created, a connection is setup
   {                            
   Serial.println("New wifi Client.");           
   String currentLine = "";         //Used to print messages
   while (client.connected())
      {

      recv_byte = client.read();
      new_command = read_incoming(&recv_byte, client); //Returns received command and check for format. If invalid, returns a 0 array
      if (new_command[0] != 0) //Checks if message is not zero, None of valid messages start with zero
      {
       execute_command(new_command);
       //new_command is set to zero
      }
      }//end of while loop
   }//end of if loop
}

这样做的缺点是 ESP32 在准备好接收新消息之前一直等到命令执行完毕。希望 ESP32 接收命令并存储它们,并按照自己的节奏执行。我计划更改当前代码以在代码运行时接收消息,如下所示:

main()
{
WiFiClient client = server.available();
byte new_command[40];
int command_count = 0;
byte command_array[50][40];

if (command_count != 0)
      {
       execute_command(command_array[0]);
       //Decrement command_count
       //Shift all commands in command_array by 1 row above
       //Set last executed command to zero
      }
}//end of main loop

def message_interrupt(int recv_byte, WiFiClient& running_client)
{
   If (running_client.connected())
      {
      recv_byte = running_client.read();
      new_command = read_incoming(&recv_byte, running_client); //Returns received command and check for format. If invalid, returns a 0 array
      //add new command to command_array after last command
      //increment command_count
      }
}

我使用哪个中断来接收消息并更新 command_array ?https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html未提及任何接收/传输事件。我也找不到任何接收/发送中断,或者我搜索了错误的术语。

标签: esp32platformio

解决方案


推荐阅读