Ok all, so first off this is not a cry for help but rather an update to a previous post about how amazing OTA updates are. Well yes they are BUT I did not want to have to hard code my SSID and Password into each sketch. So I did some searching and found this post which was amazing but not exactly what I was looking for. I continued to search for a way to implement this sketch but not have to hard code my SSID and Password to no avail; or least nothing that I could get to work.
I set out to find a way to combine the WiFiManager library and the ArduinoOTA library. After much trial and error, and mind you I an a novice programmer, I got it to work. Albeit with just a basic sketch, but it works. The below sketches are a combination of the code requirements of ArduinoOTA and WiFiManger. I have not added Blynk functionality to this sketch yet; that is my next step but I now know that I an create smart home widgets without providing an SSID and Password in the sketch.
Here is the initial sketch done on a NodeMCU 1.0 (ESP-12E Module). My assumption is that you have the appropriate version of python and all of the necessary libraries installed.
//Basic OTA updates (Initial) without hardcoding SSID and PASSWORD
//Common libraries
#include <ESP8266WiFi.h>
//WiFiManager specific libraries
#include <DNSServer.h> //Local DNS server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve as the configuration portal
#include <WiFiManager.h> //Core library
//ArduinoOTA specific libraries
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
void setup()
{
Serial.begin(115200);
WiFi.disconnect(); //uncomment to clear SSID and PASSWORD from EEPROM
WiFi.hostname("XXXX"); //uncomment to change hostname for device. Replace XXXX with your own unique name
WiFiManager wifi; //WiFiManager local initialization
//Attempt to get SSID and PASSWORD from EEPROM and connect to network
//If it does not connect it creates an Access Point with the
//Specified name
wifi.autoConnect("XXXX"); //Replace XXXX with your own unique name
ArduinoOTA.begin();
}
void loop()
{
ArduinoOTA.handle();
}
Now after you upload this sketch you need to hard reset or remove power from your NodeMCU and then power back on via something other than your computer. It is not a requirement that it not be connected to your computer but it helps with your self esteem when you see the code update when the NodeMCU is in another room. Next you need to find and connect to the access point IP address for the NodeMCU so that you can provide the SSID and password. I think the default for all NodeMCU boards is 192.168.4.1. Once you enter that into the browser on your phone or computer, you will run through a series of prompts to provide the required information to connect to your network.
Once you are connected to your network, you will need to find the IP address assigned to the NodeMCU so that you can upload subsequent sketches. If should auto-populate in the Tools - Ports section of the Arduino IDE or you can go into your router and locate the IP address by finding the hostname your gave the NodeMCU. Once you have the IP address select it from the Tools - Ports menu and then upload the following sketch via wifi to verify that it works.
//Basic OTA updated (Subsequent Test) without hardcoding SSID and PASSWORD
//Common libraries
#include <ESP8266WiFi.h>
//WiFiManager specific libraries
#include <DNSServer.h> //Local DNS server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve as the configuration portal
#include <WiFiManager.h> //Core library
//ArduinoOTA specific libraries
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
const int ESP_BUILTIN_LED = 2;
void setup()
{
Serial.begin(115200);
//WiFi.disconnect(); //uncomment to clear SSID and PASSWORD from EEPROM
WiFi.hostname("XXXX"); //uncomment to change hostname for device. Replace XXXX with your own unique name
WiFiManager wifi; //WiFiManager local initialization
//Attempt to get SSID and PASSWORD from EEPROM and connect to network
//If it does not connect it starts creates an Access Point with the
//Specified name
wifi.autoConnect("XXXX"); //Replace XXXX with your own unique name
ArduinoOTA.begin();
pinMode(ESP_BUILTIN_LED, OUTPUT);
}
void loop()
{
ArduinoOTA.handle();
digitalWrite(ESP_BUILTIN_LED, LOW);
delay(1000);
digitalWrite(ESP_BUILTIN_LED, HIGH);
delay(1000);
}
If all goes as it did for me, you should be seeing the onboard LED blynking on your NodeMCU.
One warning: if you are using a NodeMCU that has previously been connected to your network, be sure to uncomment
WiFi.disconnect();
on the initial upload so that you can run through all of the steps required from a fresh install. On the subsequent uploads, you can re-comment it back.
Finally, I look forward to comments and improvements. I am far from an expert so I welcome all forms of critique.
Happy Blynking.
Sean