{"action":"created","issue":{"url":"https://api.github.com/repos/espressif/arduino-esp32/issues/1237","repository_url":"https://api.github.com/repos/espressif/arduino-esp32","labels_url":"https://api.github.com/repos/espressif/arduino-esp32/issues/1237/labels{/name}","comments_url":"https://api.github.com/repos/espressif/arduino-esp32/issues/1237/comments","events_url":"https://api.github.com/repos/espressif/arduino-esp32/issues/1237/events","html_url":"https://github.com/espressif/arduino-esp32/issues/1237","id":306464716,"number":1237,"title":"Sending UDP message stops working after > 2 minute of inactivity","user":{"login":"korstiaanS","id":20791560,"avatar_url":"https://avatars1.githubusercontent.com/u/20791560?v=4","gravatar_id":"","url":"https://api.github.com/users/korstiaanS","html_url":"https://github.com/korstiaanS","followers_url":"https://api.github.com/users/korstiaanS/followers","following_url":"https://api.github.com/users/korstiaanS/following{/other_user}","gists_url":"https://api.github.com/users/korstiaanS/gists{/gist_id}","starred_url":"https://api.github.com/users/korstiaanS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/korstiaanS/subscriptions","organizations_url":"https://api.github.com/users/korstiaanS/orgs","repos_url":"https://api.github.com/users/korstiaanS/repos","events_url":"https://api.github.com/users/korstiaanS/events{/privacy}","received_events_url":"https://api.github.com/users/korstiaanS/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2018-03-19T13:35:35Z","updated_at":"2018-03-19T16:03:44Z","closed_at":null,"author_association":"NONE","body":"----------------------------- Remove above -----------------------------\r\n\r\n\r\n### Hardware:\r\nBoard:\t\t\t\t\t\t\tESP32 Thing Sparkfun\r\nCore Installation/update date:\t\t\t17/mar/2018\r\nIDE name:\t\t\t\t\t\t\tArduino IDE\r\nFlash Frequency:\t\t\t\t\t40Mhz\r\nUpload Speed:\t\t\t\t\t\t115200\r\n\r\n\r\n### Description:\r\nSending of UDP packages stops working after > 1 minute of inactivity\r\n\r\n\r\n### Sketch:\r\n```cpp\r\n\r\n//Change the code below by your sketch\r\n#include <WiFiUdp.h>\r\n#include <WiFi.h>\r\n\r\nbool udpSendMessage(IPAddress ipAddr, String udpMsg, int udpPort) {\r\n  /** WiFiUDP class for creating UDP communication */\r\n  WiFiUDP udpServer;\r\n \r\n  // Start UDP client for sending packets\r\n  int connOK = udpServer.begin(udpPort);\r\n \r\n  if (connOK == 0) {\r\n    Serial.println(\"UDP could not get socket\");\r\n    return false;\r\n  }\r\n \r\n  int beginOK = udpServer.beginPacket(ipAddr, udpPort);\r\n \r\n  if (beginOK == 0) { // Problem occured!\r\n    udpServer.stop();\r\n    Serial.println(\"UDP connection failed\");\r\n    return false;\r\n  }\r\n \r\n  int bytesSent = udpServer.print(udpMsg);\r\n  if (bytesSent == udpMsg.length()) {\r\n    udpServer.endPacket();\r\n    udpServer.stop();\r\n    return true;\r\n  } else {\r\n    Serial.println(\"Failed to send \" + udpMsg + \", sent \" + String(bytesSent) + \" of \" + String(udpMsg.length()) + \" bytes\");\r\n    udpServer.endPacket();\r\n    udpServer.stop();\r\n    return false;\r\n  }\r\n}\r\n\r\n/** UDP port */\r\nstatic const int udpBcPort = 9750;\r\n/** WiFiUDP class for listening to UDP message */\r\nWiFiUDP udpListener;\r\nchar esid[20] = \"YourSSID\";\r\nchar epass[20] = \"YourPassword\";\r\n \r\nvoid setup(void) {\r\n  // Some initialization stuff\r\n  WiFi.begin(esid, epass);\r\n  Serial.begin(115200);\r\n  // Start UDP listener\r\n  udpListener.begin(udpBcPort);\r\n  int c = 0;\r\n  while ( c < 30 ) {\r\n      if (WiFi.status() == WL_CONNECTED) { \r\n        break;\r\n      } \r\n      delay(500);\r\n      c++;\r\n  }\r\n  Serial.println(WiFi.localIP());\r\n  Serial.println(\"Started\");\r\n}\r\n \r\nvoid loop(void) {\r\n  // here is your code that is processed in a loop\r\n \r\n  // Check if message arrived\r\n  int udpMsgLength = udpListener.parsePacket(); \r\n  if (udpMsgLength != 0) {\r\n    Serial.println(\"Receiving\");\r\n    byte udpPacket[udpMsgLength+1]; // reserve a local buffer\r\n    IPAddress udpIP = udpListener.remoteIP();\r\n \r\n    // Read the received payload into the local buffer\r\n    udpListener.read(udpPacket, udpMsgLength);\r\n    udpPacket[udpMsgLength] = 0;\r\n \r\n    udpListener.flush(); // empty UDP buffer for next packet\r\n \r\n    String debugMsg = \"UDP package from \";\r\n    debugMsg += \"Sender IP: \" + String(udpIP[0]) + \".\" + String(udpIP[1]) + \".\" + String(udpIP[2]) + \".\" + String(udpIP[3]);\r\n    debugMsg += \"\\n Msg: \" + String((char *)udpPacket);\r\n    Serial.println(debugMsg);\r\n   // send message \"OK\" back to Packet Sender, this stops working after > 1 minute of not receiving a package\r\n    udpSendMessage(udpIP, \"OK\", 9750);\r\n  }\r\n}\r\n```\r\n\r\n### Debug Messages:\r\n```\r\n\r\n```\r\n\r\nTo test:\r\n\r\nFill in SSID and password.\r\nAfter reboot send UDP packet to the correct IP of the ESP (can see this in Serial monitor) to port 9750.\r\nMake sure that the UDP listener is active on the Packet Sender (port 9750).\r\nShortly after the reboot, the packet is received on the ESP and it sends an \"OK\" back to the Packet sender. This works every time. (Can be seen on Packet Sender)\r\nThen wait 1 minute.\r\nTry again, send a message to ESP.\r\nMessage is received on ESP.(check this via serial console).\r\nNO MESSAGE arrives at Packet Sender. It only will work again after a reboot.\r\n\r\nConclusion?; UDP sending stops working after 1 minute (or more) of inactivity?\r\n"},"comment":{"url":"https://api.github.com/repos/espressif/arduino-esp32/issues/comments/374266162","html_url":"https://github.com/espressif/arduino-esp32/issues/1237#issuecomment-374266162","issue_url":"https://api.github.com/repos/espressif/arduino-esp32/issues/1237","id":374266162,"user":{"login":"lbernstone","id":22748063,"avatar_url":"https://avatars3.githubusercontent.com/u/22748063?v=4","gravatar_id":"","url":"https://api.github.com/users/lbernstone","html_url":"https://github.com/lbernstone","followers_url":"https://api.github.com/users/lbernstone/followers","following_url":"https://api.github.com/users/lbernstone/following{/other_user}","gists_url":"https://api.github.com/users/lbernstone/gists{/gist_id}","starred_url":"https://api.github.com/users/lbernstone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lbernstone/subscriptions","organizations_url":"https://api.github.com/users/lbernstone/orgs","repos_url":"https://api.github.com/users/lbernstone/repos","events_url":"https://api.github.com/users/lbernstone/events{/privacy}","received_events_url":"https://api.github.com/users/lbernstone/received_events","type":"User","site_admin":false},"created_at":"2018-03-19T16:03:44Z","updated_at":"2018-03-19T16:03:44Z","author_association":"CONTRIBUTOR","body":"Sounds like it could be a memory leak, and your debugMsg stuff seems like a source of leaks.  Try this instead:\r\n```\r\n    Serial.printf(\"UDP package from Sender IP: %d.%d.%d.%d\\nMsg: %s\\n\",\r\n                   udpIP[0],udpIP[1],udpIP[2],udpIP[3],udpPacket);\r\n```"}}
{"actor":{"display_login":"lbernstone"}}