{"id":2123,"date":"2015-06-25T15:41:35","date_gmt":"2015-06-25T15:41:35","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2123"},"modified":"2026-06-17T09:15:25","modified_gmt":"2026-06-17T08:15:25","slug":"check-interface-connection-and-trigger-reconnect","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/tcpip\/check-interface-connection-and-trigger-reconnect","title":{"rendered":"Check interface connection and trigger reconnect"},"content":{"rendered":"<p>\nIf WiFi is lost raspbian&nbsp;doesn&#39;t seem to currently reconnect if an access point is available again. &nbsp;You need to setup a script to check it and cause it to be reset if it has dropped out. &nbsp;See <a href=\"http:\/\/alexba.in\/blog\/2015\/01\/14\/automatically-reconnecting-wifi-on-a-raspberrypi\/\">here<\/a>.\n<\/p>\n<h4>\nC Code To Check&nbsp;Connection&nbsp;Status &amp;&nbsp;Trigger Reconnect Of Dropped&nbsp;WiFi Interface<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\r\n#define TCPIP_INTERFACE_RESET_SECONDS_TIME\t\t(5 * 60)\t\t\/\/If interface is not connected for # seconds cause a reset of the interface to ensure it will reconnect to new connections\r\n#define TCPIP_INTERFACE_CHECK_SECONDS_TIME\t\t15\t\t\t\t\/\/Check the conencterion every # seconds (so we can flag to our applicaiton if it is connected or not)\r\n\r\nint wlan0_is_connected = 0;\r\nint wlan0_connection_check_state_1sec_timer = 3;\t\t\t\/\/Initial check delay after startup\r\nint wlan0_connection_do_reset_1sec_timer = TCPIP_INTERFACE_RESET_SECONDS_TIME;\r\n\r\n\t\/\/##### ADD TO 1 SECOND HEARTBEAT #####\r\n\tif (wlan0_connection_check_state_1sec_timer)\r\n\t\twlan0_connection_check_state_1sec_timer--;\r\n\r\n\tif (wlan0_connection_do_reset_1sec_timer)\r\n\t\twlan0_connection_do_reset_1sec_timer--;\r\n\t\r\n\t\r\n\t\/\/----------------------------------\r\n\t\/\/----------------------------------\r\n\t\/\/----- CHECK WLAN0 CONNECTION -----\r\n\t\/\/----------------------------------\r\n\t\/\/----------------------------------\r\n\tif (wlan0_connection_check_state_1sec_timer == 0)\r\n\t{\r\n\t\t\/\/--------------------------------------\r\n\t\t\/\/----- CHECK THE CONNECTION STATE -----\r\n\t\t\/\/--------------------------------------\r\n\t\twlan0_connection_check_state_1sec_timer = TCPIP_INTERFACE_CHECK_SECONDS_TIME;\r\n\r\n\t\tCommandResult = do_console_command_get_result((char*)&quot;cat \/sys\/class\/net\/wlan0\/operstate&quot;);\r\n\t\tif (CommandResult.find(&quot;up&quot;) == 0)\t\t\/\/If first character is &#39;1&#39; then interface is connected (command returns: &#39;1&#39;, &#39;0&#39; or a &#39;not found&#39; error message)\r\n\t\t{\r\n\t\t\tif (!wlan0_is_connected)\r\n\t\t\t\tstd::cout &lt;&lt; &quot;wlan0 is now connected&quot; &lt;&lt; std::endl;\r\n\t\t\t\r\n\t\t\twlan0_is_connected = 1;\r\n\t\t\twlan0_connection_do_reset_1sec_timer = TCPIP_INTERFACE_RESET_SECONDS_TIME;\t\t\/\/We don&#39;t want to reset the connection while it is connected\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tif (wlan0_is_connected)\r\n\t\t\t{\r\n\t\t\t\tstd::cout &lt;&lt; &quot;wlan0 is no longer connected&quot; &lt;&lt; std::endl;\r\n\t\t\t\twlan0_connection_do_reset_1sec_timer = 20;\t\t\t\t\t\t\t\t\t\/\/&lt;Do a reset quite quickly after a good connection is lost\r\n\t\t\t}\r\n\t\t\twlan0_is_connected = 0;\r\n\t\t}\r\n\t}\r\n\t\r\n\tif (wlan0_connection_do_reset_1sec_timer == 0)\r\n\t{\r\n\t\t\/\/------------------------------------------\r\n\t\t\/\/----- NOT CONNECTED - RESET INTERACE -----\r\n\t\t\/\/------------------------------------------\r\n\t\twlan0_connection_do_reset_1sec_timer = TCPIP_INTERFACE_RESET_SECONDS_TIME;\r\n\t\twlan0_connection_check_state_1sec_timer = TCPIP_INTERFACE_CHECK_SECONDS_TIME;\r\n\t\t\r\n\t\tstd::cout &lt;&lt; &quot;Resetting wlan0 interface (currently not connected, ensure it will re-connect when possible)&quot; &lt;&lt; std::endl;\r\n\t\tsystem(&quot;sudo ifdown wlan0 &amp;&quot;);\t\t\t\/\/&#39;&amp;&#39; means do in background\r\n\t\tsystem(&quot;sudo ifup wlan0 &amp;&quot;);\t\t\t\/\/&#39;&amp;&#39; means do in background\r\n\t\tstd::cout &lt;&lt; &quot;Reset wlan0 was started&quot; &lt;&lt; std::endl;\r\n\r\n\t\t\/\/The wlan0 will now pick up a new connection when next able to (it tries, then sleeps, but keeps checking for a known WiFi access point).\r\n\t\t\/\/We do this reset every now and then just to be sure it the interface is always trying\r\n\t}\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<pre>\r\n<code>\r\nstring do_console_command_get_result (char* command)\r\n{\r\n\tFILE* pipe = popen(command, &quot;r&quot;);\r\n\tif (!pipe)\r\n\t\treturn &quot;ERROR&quot;;\r\n\t\r\n\tchar buffer[128];\r\n\tstring result = &quot;&quot;;\r\n\twhile(!feof(pipe))\r\n\t{\r\n\t\tif(fgets(buffer, 128, pipe) != NULL)\r\n\t\t\tresult += buffer;\r\n\t}\r\n\tpclose(pipe);\r\n\treturn(result);\r\n}\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If WiFi is lost raspbian&nbsp;doesn&#39;t seem to currently reconnect if an access point is available again. &nbsp;You need to setup a script to check it and cause it to be reset if it has dropped out. &nbsp;See here. C Code To Check&nbsp;Connection&nbsp;Status &amp;&nbsp;Trigger Reconnect Of Dropped&nbsp;WiFi Interface #define TCPIP_INTERFACE_RESET_SECONDS_TIME (5 * 60) \/\/If interface is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,31],"tags":[],"class_list":["post-2123","post","type-post","status-publish","format-standard","hentry","category-network-settings","category-tcpip"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2123"}],"version-history":[{"count":9,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2123\/revisions"}],"predecessor-version":[{"id":2807,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2123\/revisions\/2807"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}