I’m using an ESP8266-01 with the IZOKee ESP8266 v4.0 Relay Board.
Sorry if this has already been asked / answered, but I have searched and read up for days now and haven’t found much relevance to this issue.
Here goes:
My sketch (below) works well however in a bid to make it more portable to other networks I started using WiFiManager and because I won’t have access to the Serial Monitor I wanted to use a Static IP address which I can set from the WiFiManager AccessPoint.
At this point I come unstuck. If I use wifiManager(AutoConnect) everything works but I cannot assign an IP address in the AccessPoint. if I use wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn); Blynk fails to start.
have tried numerous lines of code to initiate Blynk many I have left commented out to show my reasoning. Any help pointer pages to read gladly received.
Harry
/* ------------------------------------------------------------------------------------------
Board is Generic ESP8266 Module
Tutorial by Cisco: https://www.youtube.com/watch?v=3aB85PuOQhY&t=1s
------------------------------------------------------------------------------------------*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>
#include <BlynkSimpleEsp8266.h>
#include <time.h>
#include <timeLib.h>
#include <EEPROM.h>
#include <DNSServer.h>
#include <WiFiManager.h>
//#include <ArduinoJson.h>
//#include <NTPClient.h>
#ifndef STASSID
#define STASSID ""
#define STAPSK ""
#endif
#define Header (F(" Sketch: " __FILE__ " Build: " __DATE__ " " __TIME__ " <br> "))
bool reset = true;
IPAddress _ip = IPAddress(192, 168, 0, 79);
IPAddress _gw = IPAddress(192, 168, 0, 1);
IPAddress _sn = IPAddress(255, 255, 255, 0);
ESP8266WebServer server;
int SignalPin = 0;
bool ota_flag = false;
uint16_t time_elapsed = 0;
int timezone = 1;
int dst = 0;
String LocalDeviceName;
const char* ssid = STASSID;
const char* password = STAPSK;
String MySsid; // used to read stored value in eeprom
String MyPass; // used to read stored value in eeprom
/* Blynk Function Prototypes
---------------------------------------------------------------------*/
// This function will run every time Blynk connection is established
BLYNK_CONNECTED() {
// Request Blynk server to re-send latest values for all pins
Blynk.syncAll();
// You can also update individual virtual pins like this:
//Blynk.syncVirtual(V0, V2);
// Let's write your hardware uptime to Virtual Pin 2
// int value = millis() / 1000;
// Blynk.virtualWrite(V2, value);
}
BLYNK_WRITE(V0)
{
// Use of syncAll() will cause this function to be called
// Parameter holds last slider value
bool buttonState = param.asInt();
digitalWrite(SignalPin, !buttonState);
//Blynk.virtualWrite(V0, 255);
//Blynk.syncVirtual(V0); hangs
}
void updateBlynk()
{
if (digitalRead(SignalPin) == 1){
Blynk.virtualWrite(V0, 0);
}else{
Blynk.virtualWrite(V0, 255);
}
//BLYNK_WRITE(V0);
//Blynk.syncVirtual(V0);
//EventCheck(); ------ would not compile
//Blynk.syncVirtual(V0);
}
//------------------------------------------------------------------------------
BlynkTimer timer;
//------------------------------------------------------------------------------
// Setup
//------------------------------------------------------------------------------
void setup() {
pinMode(SignalPin, OUTPUT);
digitalWrite(SignalPin, HIGH);
Serial.begin(115200);
Serial.println(F(" Sketch: " __FILE__ " \n Build: " __DATE__ " " __TIME__ " \n"));
// Wifi Manager --------------------------------------------------------------
WiFiManager wifiManager;
if(reset == true){
wifiManager.resetSettings();
}
//wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn); // adding this line stops Blynk server running
wifiManager.autoConnect("AutoConnectAP");
if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
Serial.println("failed to connect, we should reset as see if it connects");
delay(3000);
ESP.reset();
delay(5000);
}
//----------------------------------------------------------------------------
//Serial.print((String)" Connecting" + ssid);
//WiFi.config(_ip, _gw, _sn); // adding this line stops Blynk server running
//WiFi.mode(WIFI_STA);
//WiFi.begin(ssid, password);
//while ( WiFi.status() != WL_CONNECTED ) {
// delay ( 500 );
// Serial.print ( "." );
//}
//Serial.print(" Conn ");
long rssi = WiFi.RSSI();
Serial.print("\n Signal Strength: ");
Serial.print(rssi);
Serial.println(" dBm");
//Serial.print(" IP:");
//Serial.println(WiFi.localIP());
IPAddress IP = WiFi.localIP();
MySsid = wifiManager.getSSID();
MyPass = wifiManager.getPassword();
Serial.print(" Starting Blynk ");
//Blynk.begin("AuthToken Here", ssid, password);
Blynk.begin("AuthToken Here", NULL, NULL);
//Blynk.config("AuthToken Here");
//Blynk.connect();
//Blynk.begin("AuthToken Here", "SSID", "PASSWORD", "blynk-cloud.com", 8442);
//Blynk.connect();
//Blynk.config("AuthToken Here", "blynk-cloud.com", 80);
//Blynk.config(const char *auth, optional const char *domain, optional uint16_t port)
//Blynk.begin("AuthToken Here", "", "");
/*
Blynk.begin(const char *auth, const char *ssid, const char *pass, optional const char *domain, optional uint16_t port)
Blynk.begin("AuthToken Here");
*/
//Blynk.begin("AuthToken Here", NULL, NULL, "blynk-cloud.com", 80);
//Serial.println(wifiManager.getSSID());
//Serial.print(" Stored ssid: ");
//Serial.println(MySsid);
//Blynk.config("AuthToken Here", IP);
//Blynk.begin("AuthToken Here", ssid, password, _ip, 8080);
//Blynk.config("AuthToken Here", "cloud.blynk.cc", 8080);
//Blynk.config("AuthToken Here", _ip, 8080);
//Blynk.config("AuthToken Here");
//Blynk.begin("AuthToken Here", ssid, password);
//Blynk.config("AuthToken Here");
//bool result = Blynk.connect(180);
//Serial.print(" ADDRESS TEST _ip : ");
//Serial.print(_ip);
//Serial.print(" IP: ");
Serial.println( "Ok");
Blynk.notify(" {DEVICE_NAME} Is online with this IP:" + IP);
LocalDeviceName = "Device:" + (String)IP[3] + " - Unallocated - ";
timer.setInterval(1000L, updateBlynk);
configTime(timezone * 3600, 0, "pool.ntp.org", "time.nist.gov");
Serial.println("\n Waiting for time");
while (!time(nullptr)) {
Serial.print(".");
delay(1000);
}
/* Arduino OTA Defaults
---------------------------------------------------------------------*/
// Port defaults to 8266
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
// ArduinoOTA.setHostname("myesp8266");
// No authentication by default
ArduinoOTA.setPassword((const char *)"12345678");
// Password can be set with it's md5 value as well
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}
// NOTE: if updating FS this would be the place to unmount FS using FS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
Serial.println(" ArduinoOTA: Ready");
Serial.print(" IP address: ");
Serial.println(IP);
// /default homepage
//--------------------------------------------------------------------------
server.on("/", []() {
String htmlPage = String("\r\n") +
"<!DOCTYPE HTML>" +
"<html><head><meta http-equiv='refresh' content='20;url=http://" + WiFi.localIP().toString() + "/' />" +
"<style> </style></head><body><h1>" + LocalDeviceName + "</h1>" +
"<br><br> " +
"<div align='center'>" +
"<button type='submit' value='Update Firmware' class='button' onClick=location.href='http://" + WiFi.localIP().toString() + "/setflag'>Update Firmware</button>" +
"<button type='submit' value='Reboot' class='button' onClick=location.href='http://" + WiFi.localIP().toString() + "/reboot'>ReBoot</button>" +
"<button type=submit value='Toggle Socket' class='button' onClick=location.href='http://" + WiFi.localIP().toString() + "/toggle'>Toggle Socket</button>" +
"</div>" +
"</h3><h4>" +
String(Header) +
"</h4></body></html>" +
"\r\n";
server.send(200, "text/html", htmlPage);
Serial.println(" Welcome page Requested.");
Serial.println((String) " Socket Status: " + !digitalRead(SignalPin));
});
// /reboot
//--------------------------------------------------------------------------
server.on("/reboot", []() {
String htmlPage = String("\r\n") +
"<!DOCTYPE HTML>" +
"<html><head><meta http-equiv='refresh' content='25;url=http://" + WiFi.localIP().toString() + "/' />" +
"</head>" +
"Rebooting. Please wait..." +
"</html>" +
"\r\n";
server.send(200, "text/html", htmlPage);
//server.send(200, "text/plain", "Rebooting...");
Serial.println(" Rebooting...");
delay(1000);
ESP.restart();
});
// /setflag set ota update flag
//--------------------------------------------------------------------------
server.on("/setflag", []() {
String htmlPage = String("\r\n") +
"<!DOCTYPE HTML>" +
"<html><head><meta http-equiv='refresh' content='30;url=http://" + WiFi.localIP().toString() + "/' />" +
"</head>" +
"<h2> Waiting for new Firmware...<br> This Page will Refresh when done..." +
"<!-- Replace 10 with the seconds you want to count down from --><span id='countdown'>30</span>.s</h2>" +
"<script>" +
"var seconds = document.getElementById('countdown').textContent;" +
"var countdown = setInterval(function() {" +
"seconds--;" +
"document.getElementById('countdown').textContent = seconds;" +
"if (seconds <= 0) clearInterval(countdown);" +
"}, 1000);" +
"</script></html>" +
"\r\n";
server.send(200, "text/html", htmlPage);
Serial.println(" OTA_Flag set, Ready for new Firmware upload...");
ota_flag = true;
time_elapsed = 0;
});
// /toggle toggle relay
//--------------------------------------------------------------------------
server.on("/toggle", []() {
String htmlPage = String("\r\n") +
"<!DOCTYPE HTML>" +
"<html><head><meta http-equiv='refresh' content='0;url=http://" + WiFi.localIP().toString() + "/' />" +
"</head>" +
// "Rebooting. Please wait..." +
"</html>" +
"\r\n";
server.send(200, "text/html", htmlPage);
digitalWrite(SignalPin, !digitalRead(SignalPin));
if (digitalRead(SignalPin) == 1){
Blynk.virtualWrite(V0, 0);
}else{
Blynk.virtualWrite(V0, 255);
}
Serial.println(" Toggle Page requested.");
});
// /reset Factory Reset
//--------------------------------------------------------------------------
server.on("/reset", []() {
String htmlPage = String("\r\n") +
"<!DOCTYPE HTML>" +
"<html><head><meta http-equiv='refresh' content='0;url=http://" + WiFi.localIP().toString() + "/' />" +
"</head>" +
"</html>" +
"\r\n";
server.send(200, "text/html", htmlPage);
Serial.println(" ***** Factory Reset *****");
//WipeEepromRange(0x01, 512);
//delay(1000);
WiFiManager().resetSettings();
delay(1000);
ESP.restart();
});
server.begin();
Serial.println(" ESP8266WebServer: Started");
}
//------------------------------------------------------------------------------
// Handle Update Flag
//------------------------------------------------------------------------------
void HandleUpdateFlag() {
if (ota_flag)
{
uint16_t time_start = millis();
while (time_elapsed < 15000)
{
ArduinoOTA.handle();
time_elapsed = millis() - time_start;
delay(10);
}
ota_flag = false;
}
}
// -----------------------------------------------------------------------------
// Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop
// -----------------------------------------------------------------------------
void loop() {
HandleUpdateFlag();
server.handleClient();
Blynk.run();
timer.run();
}