{"id":1897,"date":"2015-01-08T23:37:51","date_gmt":"2015-01-08T23:37:51","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=1897"},"modified":"2015-01-17T03:02:22","modified_gmt":"2015-01-17T03:02:22","slug":"esp8266-wifi-doesnt-connect","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2015\/01\/esp8266-wifi-doesnt-connect\/","title":{"rendered":"esp8266 wifi doesn&#8217;t connect"},"content":{"rendered":"<p>*SEE UPDATE BELOW*<\/p>\n<p>I spent quite some time debugging an issue where the wifi wouldn&#8217;t connect in station mode on the esp8266 today. wifi_station_get_connect_status() would return either 1 or 3 (which means CONNECTING or AP_NOT_FOUND). The issue \/appears\/ to be that wifi_station_set_config does not work reliably when called outside user_init (even if called from a function called from user_init. I.e.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid ICACHE_FLASH_ATTR wifi_config() {\r\n    \/\/ Wifi configuration\r\n    char ssid&#x5B;32] = SSID;\r\n    char password&#x5B;64] = SSID_PASSWORD;\r\n    struct station_config stationConf;\r\n\r\n    \/\/Set station mode\r\n    wifi_set_opmode( 0x1 );\r\n\r\n    \/\/Set ap settings\r\n    os_memcpy(&amp;stationConf.ssid, ssid, 32);\r\n    os_memcpy(&amp;stationConf.password, password, 64);\r\n    wifi_station_set_config(&amp;stationConf);\r\n    \/\/wifi_station_connect(); \/\/ should not be required\r\n    debug(&quot;init wifi&quot;);\r\n    debug(SSID);\r\n    debug(SSID_PASSWORD);\r\n}\r\n\r\n\/\/Init function\r\nvoid ICACHE_FLASH_ATTR user_init() {\r\n\r\n    \/\/ Set UART Speed (default appears to be rather odd 77KBPS)\r\n    uart_init(BIT_RATE_9600,BIT_RATE_9600);\r\n\r\n    wifi_config();\r\n\r\n    \/\/Start os task\r\n    system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen);\r\n    system_os_post(user_procTaskPrio, 0, 0 );\r\n\r\n    network_init();\r\n}\r\n<\/pre>\n<p>Does NOT work consistantly. Whereas:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/Init function\r\nvoid ICACHE_FLASH_ATTR user_init() {\r\n\r\n    \/\/ Set UART Speed (default appears to be rather odd 77KBPS)\r\n    uart_init(BIT_RATE_9600,BIT_RATE_9600);\r\n\r\n    \/\/wifi_config();\r\n    \/\/ Wifi configuration\r\n    char ssid&#x5B;32] = SSID;\r\n    char password&#x5B;64] = SSID_PASSWORD;\r\n    struct station_config stationConf;\r\n\r\n    \/\/Set station mode\r\n    wifi_set_opmode( 0x1 );\r\n\r\n    \/\/Set ap settings\r\n    os_memcpy(&amp;stationConf.ssid, ssid, 32);\r\n    os_memcpy(&amp;stationConf.password, password, 64);\r\n    wifi_station_set_config(&amp;stationConf);\r\n    \/\/wifi_station_connect(); \/\/ should not be required\r\n    debug(&quot;init wifi&quot;);\r\n    debug(SSID);\r\n    debug(SSID_PASSWORD);\r\n\r\n    \/\/Start os task\r\n    system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen);\r\n    system_os_post(user_procTaskPrio, 0, 0 );\r\n\r\n    network_init();\r\n}\r\n<\/pre>\n<p>Works as expected. The reason that wifi_config() above may sometimes work is that I would guess it occasionally gets inlined. I&#8217;ve no idea why the calls need to be in user_init directly though, there&#8217;s perhaps some funky stack related stuff going on.<\/p>\n<h2>UPDATE<\/h2>\n<p>Wow, Espressif saw my post and emailed me out of the blue. There suggestion was that stationConf.bssid_set should also be set to 0 in this case. Otherwise it will also check the AP mac address i.e it should be:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid wifi_config()\r\n{\r\n\/\/ Wifi configuration\r\nchar ssid&#x5B;32] = SSID;\r\nchar password&#x5B;64] = PASSWORD;\r\nstruct station_config stationConf;\r\n\r\n\/\/Set station mode\r\nwifi_set_opmode( 0x1 );\r\n\r\nstationConf.bssid_set = 0;\r\n\r\n\/\/Set ap settings\r\nos_memcpy(&amp;stationConf.ssid, ssid, 32);\r\nos_memcpy(&amp;stationConf.password, password, 64);\r\nwifi_station_set_config(&amp;stationConf);\r\n\r\n}\r\n\r\n\/\/Init function\r\nvoid user_init()\r\n{\r\nwifi_config();\r\n}\r\n<\/pre>\n<p>This also makes sense to me in terms of random fiddling fixed this issue, I guess in some cases the uninitialized memory is 0 by chance. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>*SEE UPDATE BELOW* I spent quite some time debugging an issue where the wifi wouldn&#8217;t connect in station mode on the esp8266 today. wifi_station_get_connect_status() would return either 1 or 3 (which means CONNECTING or AP_NOT_FOUND). The issue \/appears\/ to be that wifi_station_set_config does not work reliably when called outside user_init (even if called from a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1897","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-uB","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=1897"}],"version-history":[{"count":3,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1897\/revisions"}],"predecessor-version":[{"id":1906,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1897\/revisions\/1906"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=1897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=1897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=1897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}