做其他事情时,ESP8266 UDP parsePacket不起作用

时间:2018-11-21 15:44:56

标签: arduino udp wifi esp8266

过去几天测试我情绪的问题是,尽管我的ESP8266芯片完全能够在while循环中使用parsePacket提取数据包,但是当我希望两者都监听传入的数据包时,它完全错过了这些数据包,但同时也允许我ESP8266读取传感器并通过wifi发送这些传感器。

因此,我删除了与传感器有关的所有内容,并在下面粘贴了代码,但这就是正在发生的事情。

  1. 在Setup()中,ESP设置wifi,SoftAP节点,UDP并进行配置。
  2. 然后进入while循环,在该循环中,它通过wifi监听传入的数据包;这告诉ESP开始吐出数据。
  3. 当ESP收到数据包时(这是我的代码中的“ A. <<<”注释,可以正常工作),代码移至Loop()部分,传感器开始吐出通过wifi发送的数据

我的问题就是这个步骤:

  1. 在读取传感器并通过wifi发送数据期间,我希望ESP8266还能够通过wifi接收传入的数据包(这是我代码中的“ B. <<<”注释,不起作用),ESP一直通过wifi发送传感器数据,但没有收到任何数据包

我的猜测是A.可以正常工作,因为'while'条件将允许parsePacket始终捕获传入的数据包。而且因为B.在void循环内,所以我不能使用'while'条件,而不能使用'if'语句,因此parsePacket命令没有在正确的时间请求传入的程序包。我无法实现解决此问题的方法。相当多的搜索并没有真正帮助到我,我无法想象我一个人遇到这个问题。

我发现一些相关的东西:

非常感谢您的帮助和反馈!

这是我的代码:

#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

String SendString;
String SendBuffer = "STR ";
boolean SendSuccessful;
char SendChar[32];

// wifi connection variables
const char* password = #########;
boolean wifiConnected = false;
String ssid_string;
char ssid[10];

// UDP variables
unsigned int localPort = 8888;
WiFiUDP UDP;
boolean udpConnected = false;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[500] = "acknowledged"; // a string to send back

void setup(void) {
  Serial.begin(115200);

  //Setup all my sensors, code not relevant

  //Connect Wifi
  ssid_string = "DRILL_" + String(chip_id);
  ssid_string.toCharArray(ssid, 500);
  SetupWifi(ssid);
}

void loop(void) {

  //B. <<<< SO THIS IS THE PART IN THE LOOP THAT IS NOT WORKING
  int packetSize = UDP.parsePacket();
  packetSize = UDP.parsePacket();  
  if (packetSize) {
    Serial.println("");
    Serial.print("Received packet");

    // read the packet into packetBufffer
    UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    int value = packetBuffer[0] * 256 + packetBuffer[1];
    Serial.println(value);
  }

  //Read out all my sensors, code not relevant, paste all sensor data in one string

  //SendString over serial
  Serial.print(SendString);

  SendIntervalUDP(1); // Send SendString over UDP every x measurements (see function below)

  //Listen to serial
  if (Serial.available() > 0) {
    String Received = Serial.readString();
    Serial.println("ESP received: " + Received);
  }
}

现在这是设置wifi和通过wifi发送数据的一些支持功能

unsigned long previousWifiStatMillis;

//Connect wifi during setup
void SetupWifi(char my_ssid[]) {

  // WiFi init
  wifiConnected = createAP(my_ssid);

  udpConnected = connectUDP();

  // Wait for first packet
  Serial.println("Waiting for start");

  //A. <<<< SO THIS IS THE PART IN THE CODE THAT IS ACTUALLY WORKING
  int packetSize = UDP.parsePacket();
  while (packetSize < 1) {
    packetSize = UDP.parsePacket();
    yield(); // Allow the background functions to work
    //Listen to serial
    if (Serial.available() > 0) {
      String Received = Serial.readString();
      Serial.println("I received: " + Received);
      if (Received.substring(0) == "S") {
        Serial.println("Starting..");
        break;
      }
    }

    if (packetSize)
    {
      Serial.println("");
      Serial.print("Received packet");

      // read the packet into packetBufffer
      UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
      Serial.println("Contents:");
      int value = packetBuffer[0] * 256 + packetBuffer[1];
      Serial.println(value);

    }
  }
}


// connect to UDP – returns true if successful or false if not
boolean connectUDP() {
  boolean state = false;

  Serial.println("");
  Serial.println("Connecting to UDP");

  if (UDP.begin(localPort) == 1) {
    Serial.println("Connection successful");
    state = true;
  }
  else {
    Serial.println("Connection failed");
  }

  return state;
}

// connect to wifi – returns true if successful or false if not
boolean createAP(char my_ssid[]) {
  boolean state = true;
  int i = 0;
  WiFi.softAP(my_ssid, password);             // Start the access point
  WiFi.mode(WIFI_AP);
  Serial.print("Access Point \"");
  Serial.print(my_ssid);
  Serial.println("\" started");
  Serial.print("IP address:\t");
  Serial.println(WiFi.softAPIP());         // Send the IP address of the ESP8266 to the computer
  state = true;
  return state;
}


// connect to wifi – returns true if successful or false if not
boolean connectWifi(char my_ssid[]) {
  boolean state = true;
  int i = 0;
  WiFi.begin(my_ssid, password);
  Serial.println("");
  Serial.println("Connecting to WiFi");

  // Wait for connection
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (i > 20) {
      state = false;
      break;
    }
    i++;
  }
  if (state) {
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(my_ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("");
    Serial.println("Connection failed.");
  }
  return state;
}

void SendIntervalUDP(int interval) {
  //Send an udp packet every x packets with x the interval
  j++;
  if (j > (interval-1) ) {
    j = 0;
    //SendString over wifi
    if (wifiConnected) {
      if (udpConnected) {
        // send a reply, to predetermined hotspot
        UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
        SendBuffer.toCharArray(ReplyBuffer, 500);
        UDP.write(ReplyBuffer);
        UDP.endPacket();
      }
    }
    SendBuffer = "";
  }
}

1 个答案:

答案 0 :(得分:0)

“通用类”中的WiFi事件适用于传输层以下的OSI级别。 TCP和UDP是传输层。

要接收UDP数据包,您必须像实现Arduino UDP.parsePacket();基类的所有其他Arduino网络库一样,在lopp()(或从loop()调用的函数)中调用UDP

如果数据包可用,则调用parsePacket将数据包读入内部缓冲区并返回数据包的大小。下次对parsePacket的调用将清除缓冲区,并在缓冲区可用时填充新的数据包。您调用了parsePacket两次,第二次始终不返回任何内容。