{"id":394203,"date":"2024-06-29T11:28:08","date_gmt":"2024-06-29T11:28:08","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=394203"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=394203","title":{"rendered":"<span>Sending data from Arduino Nano 33 IoT to Raspberry Pi 4 using UDP<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong>Goal<\/strong>: continuously send UDP packets from an Arduino Nano 33 IoT to a Raspberry Pi 4 to understand the reliability of this solution.<\/p>\n<p><strong>It\u2019s not important at the moment<\/strong>: software or networking performance, throughput, etc. This is a first test to check the basics, there is a lot of room for improvements in the code, protocol, approach, which will be done in future iterations and based on the discoveries done in this test.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/892\/0c1\/02f\/8920c102fee9d3bbf7e623f176970e24.jpg\" width=\"4640\" height=\"2610\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/892\/0c1\/02f\/8920c102fee9d3bbf7e623f176970e24.jpg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<h3>The experiment<\/h3>\n<p>From the Arduino, an UDP packet is sent to the Raspberry Pi. The packet contains a string with the value of an incremental counter, and the value of\u00a0micros().<\/p>\n<p>Contents of the first 3 packets were (one per line):<\/p>\n<pre><code>1,33726393 2,33728539 3,33730495<\/code><\/pre>\n<p>On the <strong>Raspberry Pi<\/strong>, <strong>netcat<\/strong> is listening for those UDP packets:<\/p>\n<pre><code class=\"bash\">$ netcat -u -s 0.0.0.0 -p 4545 -l > netcat-output.txt<\/code><\/pre>\n<p>  Once finished, the last lines of <strong>netcat-output.txt<\/strong> were: <\/p>\n<pre><code class=\"bash\">$ tail netcat-output.txt  1799991,3579137145 1799992,3579139114 1799993,3579141112 1799994,3579143035 1799995,3579144985 1799996,3579146918 1799997,3579148832 1799998,3579150780 1799999,3579152790 1800000,3579154958<\/code><\/pre>\n<p>and the output see on the <strong>Arduino console<\/strong> shows partial stats and the final stat:<\/p>\n<pre><code>(...) stats | iter=1797000 | err=9285 | msg\/sec=507 stats | iter=1798000 | err=9285 | msg\/sec=507 stats | iter=1799000 | err=9285 | msg\/sec=507 stats | iter=1800000 | err=9285 | msg\/sec=507 ENDED | Sent 1800001 UDP packets in 3545431 ms. with 9285 errors<\/code><\/pre>\n<h3>Errors<\/h3>\n<p>How the errors are counted? The return value of <strong>WiFiUDP.endPacket()<\/strong> is used: that method returns 0 if there was some error. See <a href=\"https:\/\/www.arduino.cc\/en\/Reference\/WiFiNINAUDPEndPacket\" rel=\"noopener noreferrer nofollow\">WiFiNINAUDPEndPacket<\/a>  for mor information.<\/p>\n<p>On the Raspberry Pi, we can easilly check how many packets were actually received: <\/p>\n<pre><code class=\"bash\">$ wc -l netcat-output.txt 1790712 netcat-output.txt<\/code><\/pre>\n<h3>Results<\/h3>\n<ul>\n<li>\n<p>Generated packets (Arduino): 1,800,000<\/p>\n<\/li>\n<li>\n<p>Errors reported by WiFiUDP.endPacket(): 9,285<\/p>\n<\/li>\n<li>\n<p>Successfully sent packets (measured from Arduino): 1,790,715<\/p>\n<\/li>\n<li>\n<p>Received packets (at Raspberry Pi): 1,790,712 <\/p>\n<\/li>\n<\/ul>\n<p>Errors in percentages:<\/p>\n<ul>\n<li>\n<p>errors reported by WiFiUDP.endPacket(): 9,285 of 1,800,000 = 0.5158%<\/p>\n<\/li>\n<li>\n<p>lost packets: 3 of 1,790,715 = 0.000167531% <\/p>\n<\/li>\n<\/ul>\n<h3>Next steps<\/h3>\n<ul>\n<li>\n<p>Refactor Arduino code<\/p>\n<\/li>\n<li>\n<p>Send data in binary format instead of strings<\/p>\n<\/li>\n<li>\n<p>Experiment with retries<\/p>\n<\/li>\n<li>\n<p>Experiment buffering data and send multiple data points per UDP message<\/p>\n<\/li>\n<li>\n<p>Experiment with different WiFi configuration <\/p>\n<\/li>\n<\/ul>\n<h3>Appendix: snippet of Arduino code<\/h3>\n<pre><code class=\"cpp\">void floodUdp(int iters) {     int ret;     int ratio;     counter = 1;     errors = 0;        Serial.println(\"Flooding...  :D\");     udpStart = millis();     for (; iters == -1 || counter &lt;= iters; counter++) {         if (counter % 1000 == 0) {             ratio = (counter) \/ ((millis() - udpStart) \/ 1000);             Serial.println(str + \"stats | iter=\" + counter + \" | err=\" + errors + \" | msg\/sec=\" + ratio);         }         if (sendUdpToServer(serverHostname) != 1) {             errors++;         };     }     udpEnd = millis();        Serial.println(str + \"ENDED | Sent \" + counter + \" UDP packets in \" + (millis() - udpStart) + \" ms. with \" + errors + \" errors\"); }  int sendUdpToServer(char* ip) {     if (Udp.beginPacket(ip, 4545) != 1) {         Serial.println(\"[UDP] Udp.beginPacket() failed\");         return -1;     }        String(counter).toCharArray(buf, 100);     Udp.write(buf);     Udp.write(sep);     String(micros()).toCharArray(buf, 100);     Udp.write(buf);     Udp.write(nl);        if (Udp.endPacket() != 1) {         Serial.print(\"e\");         return -1;     }        return 1; } <\/code><\/pre>\n<h3>Appendix: Wifi configuration<\/h3>\n<p>Configuration of hostapd running in Raspberry Pi: <\/p>\n<pre><code># -------------------------------------------------------------------- # 2 Ghz (Arduino Nano 33 IoT) # --------------------------------------------------------------------  interface=wlanrpi0 hw_mode=g channel=10 ieee80211d=1 country_code=US ieee80211n=1 wmm_enabled=1 ssid=******** auth_algs=1 wpa=2 wpa_key_mgmt=WPA-PSK rsn_pairwise=CCMP wpa_passphrase=********  macaddr_acl=1 accept_mac_file=\/etc\/hostapd\/accept_mac.txt<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/567920\/\"> https:\/\/habr.com\/ru\/articles\/567920\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong>Goal<\/strong>: continuously send UDP packets from an Arduino Nano 33 IoT to a Raspberry Pi 4 to understand the reliability of this solution.<\/p>\n<p><strong>It\u2019s not important at the moment<\/strong>: software or networking performance, throughput, etc. This is a first test to check the basics, there is a lot of room for improvements in the code, protocol, approach, which will be done in future iterations and based on the discoveries done in this test.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h3>The experiment<\/h3>\n<p>From the Arduino, an UDP packet is sent to the Raspberry Pi. The packet contains a string with the value of an incremental counter, and the value of\u00a0micros().<\/p>\n<p>Contents of the first 3 packets were (one per line):<\/p>\n<pre><code>1,33726393 2,33728539 3,33730495<\/code><\/pre>\n<p>On the <strong>Raspberry Pi<\/strong>, <strong>netcat<\/strong> is listening for those UDP packets:<\/p>\n<pre><code class=\"bash\">$ netcat -u -s 0.0.0.0 -p 4545 -l > netcat-output.txt<\/code><\/pre>\n<p>  Once finished, the last lines of <strong>netcat-output.txt<\/strong> were: <\/p>\n<pre><code class=\"bash\">$ tail netcat-output.txt  1799991,3579137145 1799992,3579139114 1799993,3579141112 1799994,3579143035 1799995,3579144985 1799996,3579146918 1799997,3579148832 1799998,3579150780 1799999,3579152790 1800000,3579154958<\/code><\/pre>\n<p>and the output see on the <strong>Arduino console<\/strong> shows partial stats and the final stat:<\/p>\n<pre><code>(...) stats | iter=1797000 | err=9285 | msg\/sec=507 stats | iter=1798000 | err=9285 | msg\/sec=507 stats | iter=1799000 | err=9285 | msg\/sec=507 stats | iter=1800000 | err=9285 | msg\/sec=507 ENDED | Sent 1800001 UDP packets in 3545431 ms. with 9285 errors<\/code><\/pre>\n<h3>Errors<\/h3>\n<p>How the errors are counted? The return value of <strong>WiFiUDP.endPacket()<\/strong> is used: that method returns 0 if there was some error. See <a href=\"https:\/\/www.arduino.cc\/en\/Reference\/WiFiNINAUDPEndPacket\" rel=\"noopener noreferrer nofollow\">WiFiNINAUDPEndPacket<\/a>  for mor information.<\/p>\n<p>On the Raspberry Pi, we can easilly check how many packets were actually received: <\/p>\n<pre><code class=\"bash\">$ wc -l netcat-output.txt 1790712 netcat-output.txt<\/code><\/pre>\n<h3>Results<\/h3>\n<ul>\n<li>\n<p>Generated packets (Arduino): 1,800,000<\/p>\n<\/li>\n<li>\n<p>Errors reported by WiFiUDP.endPacket(): 9,285<\/p>\n<\/li>\n<li>\n<p>Successfully sent packets (measured from Arduino): 1,790,715<\/p>\n<\/li>\n<li>\n<p>Received packets (at Raspberry Pi): 1,790,712 <\/p>\n<\/li>\n<\/ul>\n<p>Errors in percentages:<\/p>\n<ul>\n<li>\n<p>errors reported by WiFiUDP.endPacket(): 9,285 of 1,800,000 = 0.5158%<\/p>\n<\/li>\n<li>\n<p>lost packets: 3 of 1,790,715 = 0.000167531% <\/p>\n<\/li>\n<\/ul>\n<h3>Next steps<\/h3>\n<ul>\n<li>\n<p>Refactor Arduino code<\/p>\n<\/li>\n<li>\n<p>Send data in binary format instead of strings<\/p>\n<\/li>\n<li>\n<p>Experiment with retries<\/p>\n<\/li>\n<li>\n<p>Experiment buffering data and send multiple data points per UDP message<\/p>\n<\/li>\n<li>\n<p>Experiment with different WiFi configuration <\/p>\n<\/li>\n<\/ul>\n<h3>Appendix: snippet of Arduino code<\/h3>\n<pre><code class=\"cpp\">void floodUdp(int iters) {     int ret;     int ratio;     counter = 1;     errors = 0;        Serial.println(\"Flooding...  :D\");     udpStart = millis();     for (; iters == -1 || counter &lt;= iters; counter++) {         if (counter % 1000 == 0) {             ratio = (counter) \/ ((millis() - udpStart) \/ 1000);             Serial.println(str + \"stats | iter=\" + counter + \" | err=\" + errors + \" | msg\/sec=\" + ratio);         }         if (sendUdpToServer(serverHostname) != 1) {             errors++;         };     }     udpEnd = millis();        Serial.println(str + \"ENDED | Sent \" + counter + \" UDP packets in \" + (millis() - udpStart) + \" ms. with \" + errors + \" errors\"); }  int sendUdpToServer(char* ip) {     if (Udp.beginPacket(ip, 4545) != 1) {         Serial.println(\"[UDP] Udp.beginPacket() failed\");         return -1;     }        String(counter).toCharArray(buf, 100);     Udp.write(buf);     Udp.write(sep);     String(micros()).toCharArray(buf, 100);     Udp.write(buf);     Udp.write(nl);        if (Udp.endPacket() != 1) {         Serial.print(\"e\");         return -1;     }        return 1; } <\/code><\/pre>\n<h3>Appendix: Wifi configuration<\/h3>\n<p>Configuration of hostapd running in Raspberry Pi: <\/p>\n<pre><code># -------------------------------------------------------------------- # 2 Ghz (Arduino Nano 33 IoT) # --------------------------------------------------------------------  interface=wlanrpi0 hw_mode=g channel=10 ieee80211d=1 country_code=US ieee80211n=1 wmm_enabled=1 ssid=******** auth_algs=1 wpa=2 wpa_key_mgmt=WPA-PSK rsn_pairwise=CCMP wpa_passphrase=********  macaddr_acl=1 accept_mac_file=\/etc\/hostapd\/accept_mac.txt<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/567920\/\"> https:\/\/habr.com\/ru\/articles\/567920\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-394203","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/394203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=394203"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/394203\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=394203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=394203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=394203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}