{"id":12827,"date":"2022-07-12T05:13:49","date_gmt":"2022-07-12T11:13:49","guid":{"rendered":"http:\/\/fallows.ca\/wp\/?p=12827"},"modified":"2022-06-11T14:36:15","modified_gmt":"2022-06-11T20:36:15","slug":"setup-your-code-ready-to-run","status":"publish","type":"post","link":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/","title":{"rendered":"Setup Your Code &#8211; Ready to Run"},"content":{"rendered":"<p><a href=\"http:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/setup.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-12828\" src=\"http:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/setup.jpg\" alt=\"setup your code\" width=\"464\" height=\"232\" srcset=\"https:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/setup.jpg 464w, https:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/setup-300x150.jpg 300w\" sizes=\"auto, (max-width: 464px) 100vw, 464px\" \/><\/a><\/p>\n<p>Be prepared. Use the setup routine to setup your code ever time your IoT controller boots up. <!--more--><\/p>\n<p>Every time you write an Arduino or NodeMCU program, you need to program the &#8220;setup&#8221; routine. This runs once when your IoT controller boots. I find this a very useful routine.<\/p>\n<p>Among other things, you can setup your code by defining things your program needs to know. You can read stored settings from the EEPROM (read only memory). You can test GPIO pins to change the behavior of your code before it runs. For example, you can ground a digital pin to signify a certain kind of behavior. With the NodeMCU, you can use a GPIO pin to denote running as a station on your network. Or you can have the wireless setup a stand-alone network.<\/p>\n<p>Here is my setup for the relay controller.<\/p>\n<pre class=\"brush: cpp; title: ; wrap-lines: false; notranslate\" title=\"\">\r\nvoid setup()\r\n{\r\n  delay(500);\r\n#ifdef DEBUG\r\n  Serial.begin(SERIAL_BAUDRATE);\r\n  Serial.flush();\r\n#endif\r\n\r\n\/* Enable over the air firmware changes *\/\r\nArduinoOTA.begin();\r\ndelay(100);\r\n\r\n\/* Configure controller for first use *\/\r\nInitialize();\r\n\r\n\/* Connect to local network *\/\r\nwl_status_t b = wifiConnect(ssid, password);\r\nif (b == WL_CONNECTED) {\r\n  server.begin();\r\n#ifdef DEBUG\r\n  Respond(&quot;ONLINE&quot;);\r\n  Respond(WiFi.localIP().toString());\r\n#endif \r\n}\r\n\r\n\/* Make relay banks available for control *\/\r\n  B_RX.begin();\r\n  B_LOOP.begin();delay(100);\r\n}\r\n<\/pre>\n<p>If you want feedback over the serial port during startup, you can use a DEBUG definition to make sure your NodeMCU is on track. You can prepare the device for over-the-air firmware updates. Also, you can do a separate routing for initialize all key variables and objects to their proper settings.<\/p>\n<pre class=\"brush: cpp; title: ; wrap-lines: false; notranslate\" title=\"\">\r\nvoid Initialize() {\r\n\r\n  \/* Turn all Relays Off *\/\r\n  B_RX.write8(255);\r\n  B_LOOP.write8(255);\r\n  Status.Connected = false;\r\n  Status.Receiver = 0;\r\n  Status.Filter = 0;\r\n  Status.Loop1Mode = 0;\r\n  Status.Loop2Mode = 0;\r\n}\r\n<\/pre>\n<h2>Setup Your Code for Wireless Communications<\/h2>\n<p>Perhaps most important, you need to make sure your WiFi over the local network is connected. You do this with a call to &#8220;wifiConnect&#8221;, passing your network ID and password.<\/p>\n<pre class=\"brush: cpp; title: ; wrap-lines: false; notranslate\" title=\"\">\r\nwl_status_t wifiConnect(const char* id, const char* pass)\r\n{\r\n  WiFi.disconnect();\r\n  \/* Set up on LAN with a Static Server IP Address *\/\r\n  WiFi.mode(WIFI_STA);\r\n  WiFi.config(SERVERIP, GATEWAY, SUBNET, DNS1, DNS2);\r\n  WiFi.begin(id, pass);\r\n  int counter = 0;\r\n  \/* Wait up to 3 seconds for connection *\/\r\n  while (WiFi.status() != WL_CONNECTED)\r\n    {\r\n    delay(250);\r\n    counter++;\r\n    if (counter &gt; 12)\r\n    {\r\n      break;\r\n    }\r\n  }\r\n  return WiFi.status();\r\n}\r\n<\/pre>\n<p>You need to include some delays to provide enough time for your NodeMCU to be accepted on the network. Check the return code to make sure everything worked ok.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Be prepared. Use the setup routine to setup your code ever time your IoT controller boots up.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"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":"Setup Your Code - Ready to Run #Arduino #IoT #NodeMCU","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[6],"tags":[25,32,21],"series":[338],"class_list":["post-12827","post","type-post","status-publish","format-standard","hentry","category-software-projects","tag-arduino","tag-iot","tag-nodemcu","series-anatomy-of-an-iot-controllers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Setup Your Code - Ready to Run - Living On The Horizon<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Setup Your Code - Ready to Run - Living On The Horizon\" \/>\n<meta property=\"og:description\" content=\"Be prepared. Use the setup routine to setup your code ever time your IoT controller boots up.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/\" \/>\n<meta property=\"og:site_name\" content=\"Living On The Horizon\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-12T11:13:49+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/setup.jpg\" \/>\n<meta name=\"author\" content=\"John VE6EY\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@VE6EY\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John VE6EY\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/\",\"url\":\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/\",\"name\":\"Setup Your Code - Ready to Run - Living On The Horizon\",\"isPartOf\":{\"@id\":\"http:\/\/fallows.ca\/wp\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/setup.jpg\",\"datePublished\":\"2022-07-12T11:13:49+00:00\",\"author\":{\"@id\":\"http:\/\/fallows.ca\/wp\/#\/schema\/person\/9750e0ab227030255d9806757525f945\"},\"breadcrumb\":{\"@id\":\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/#primaryimage\",\"url\":\"https:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/setup.jpg\",\"contentUrl\":\"https:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/setup.jpg\",\"width\":464,\"height\":232,\"caption\":\"setup your code\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/fallows.ca\/wp\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Christmas\",\"item\":\"https:\/\/fallows.ca\/wp\/tag\/christmas\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Setup Your Code &#8211; Ready to Run\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/fallows.ca\/wp\/#website\",\"url\":\"http:\/\/fallows.ca\/wp\/\",\"name\":\"Living On The Horizon\",\"description\":\"Blogs and Stuff\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/fallows.ca\/wp\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/fallows.ca\/wp\/#\/schema\/person\/9750e0ab227030255d9806757525f945\",\"name\":\"John VE6EY\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/fallows.ca\/wp\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e4048edfe09efff51033c48b4fb951e8ac0a4dc84a25c96b25e5ae9f5b7069a5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e4048edfe09efff51033c48b4fb951e8ac0a4dc84a25c96b25e5ae9f5b7069a5?s=96&d=mm&r=g\",\"caption\":\"John VE6EY\"},\"sameAs\":[\"https:\/\/x.com\/VE6EY\"],\"url\":\"https:\/\/fallows.ca\/wp\/author\/play\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Setup Your Code - Ready to Run - Living On The Horizon","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/","og_locale":"en_US","og_type":"article","og_title":"Setup Your Code - Ready to Run - Living On The Horizon","og_description":"Be prepared. Use the setup routine to setup your code ever time your IoT controller boots up.","og_url":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/","og_site_name":"Living On The Horizon","article_published_time":"2022-07-12T11:13:49+00:00","og_image":[{"url":"http:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/setup.jpg","type":"","width":"","height":""}],"author":"John VE6EY","twitter_card":"summary_large_image","twitter_creator":"@VE6EY","twitter_misc":{"Written by":"John VE6EY","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/","url":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/","name":"Setup Your Code - Ready to Run - Living On The Horizon","isPartOf":{"@id":"http:\/\/fallows.ca\/wp\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/#primaryimage"},"image":{"@id":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/#primaryimage"},"thumbnailUrl":"http:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/setup.jpg","datePublished":"2022-07-12T11:13:49+00:00","author":{"@id":"http:\/\/fallows.ca\/wp\/#\/schema\/person\/9750e0ab227030255d9806757525f945"},"breadcrumb":{"@id":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/#primaryimage","url":"https:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/setup.jpg","contentUrl":"https:\/\/fallows.ca\/wp\/wp-content\/uploads\/2022\/06\/setup.jpg","width":464,"height":232,"caption":"setup your code"},{"@type":"BreadcrumbList","@id":"https:\/\/fallows.ca\/wp\/projects\/software-projects\/setup-your-code-ready-to-run\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/fallows.ca\/wp\/"},{"@type":"ListItem","position":2,"name":"Christmas","item":"https:\/\/fallows.ca\/wp\/tag\/christmas\/"},{"@type":"ListItem","position":3,"name":"Setup Your Code &#8211; Ready to Run"}]},{"@type":"WebSite","@id":"http:\/\/fallows.ca\/wp\/#website","url":"http:\/\/fallows.ca\/wp\/","name":"Living On The Horizon","description":"Blogs and Stuff","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/fallows.ca\/wp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/fallows.ca\/wp\/#\/schema\/person\/9750e0ab227030255d9806757525f945","name":"John VE6EY","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/fallows.ca\/wp\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e4048edfe09efff51033c48b4fb951e8ac0a4dc84a25c96b25e5ae9f5b7069a5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e4048edfe09efff51033c48b4fb951e8ac0a4dc84a25c96b25e5ae9f5b7069a5?s=96&d=mm&r=g","caption":"John VE6EY"},"sameAs":["https:\/\/x.com\/VE6EY"],"url":"https:\/\/fallows.ca\/wp\/author\/play\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6wKKr-3kT","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/posts\/12827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/comments?post=12827"}],"version-history":[{"count":3,"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/posts\/12827\/revisions"}],"predecessor-version":[{"id":12837,"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/posts\/12827\/revisions\/12837"}],"wp:attachment":[{"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/media?parent=12827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/categories?post=12827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/tags?post=12827"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/fallows.ca\/wp\/wp-json\/wp\/v2\/series?post=12827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}