tzapu / WiFiManager

ESP8266 WiFi Connection manager with web captive portal
http://tzapu.com/esp8266-wifi-connection-manager-library-arduino-ide/
MIT License
6.55k stars 1.97k forks source link

Using WifiManager with NTPClient #1320

Open Abhesheksh opened 2 years ago

Abhesheksh commented 2 years ago

Basic Infos

Hardware

WiFimanager Branch/Release: Master

Esp8266/Esp32: ESP8266 / NodeMCU

Hardware: ESP-12E

Core Version: 2.4.0, staging

Description

Using NTPClient to get RTC doesnt work with wifimanager. Same code works when connecting with ssid and password after removing WM.

Problem description

When using NTPClient with WM, time starts from 0:0 and then refreshes but doesn't show actual time.

Settings in IDE

Module: NodeMcu

Additional libraries:

Sketch

#BEGIN

#include <Ticker.h>
#include <ESP8266WiFi.h>
#include "ESP8266Ping.h"
#include <WiFiManager.h>
//#include <EEPROM.h>
#include <EasyButton.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

Ticker ticker;

#define MOSFET 4
#define SWITCH 0
#define DETECT A0
#define LED 2
#define GLED 5
#define DEBUG True

// Update these with values suitable for your network.
WiFiManager wifiManager;

EasyButton button(SWITCH);

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");

//WiFiClient espClient;
bool isButtonPressed = false;
void onPressedForDuration();
void led_blink();
void led_blink2();

void tick()
{
  //toggle state
  int state = digitalRead(LED);  // get the current state of GPIO1 pin
  digitalWrite(LED, !state);     // set pin to the opposite state
}

void configModeCallback (WiFiManager *myWiFiManager) {
  Serial.println("Entered config mode");
  Serial.println(WiFi.softAPIP());
  //if you used auto generated SSID, print it
  Serial.println(myWiFiManager->getConfigPortalSSID());
  //entered config mode, make led toggle faster
  ticker.attach(0.5, tick);
}

void setup() {

  pinMode(SWITCH, INPUT);     // Initialize the GPIO2 pin as an output
  pinMode(MOSFET, OUTPUT);     // Initialize the GPIO0 pin as an output
  pinMode(DETECT, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(GLED, OUTPUT);

  ticker.attach(0.1, tick);

  digitalWrite(MOSFET, LOW);   // Turn MOSFET ON (Note that LOW or HIGH is the voltage level
  //digitalWrite(LED, HIGH);
  digitalWrite(GLED, HIGH);
  button.begin();
  button.onPressedFor(5000, onPressedForDuration);

  wifiManager.setAPCallback(configModeCallback);

#ifdef DEBUG
  Serial.begin(115200);
#endif

  //wifiManager.resetSettings();
  WiFi.mode(WIFI_STA);

  wifiManager.setAPStaticIPConfig(IPAddress(192, 168, 1, 19), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));

  // fetches ssid and pass from eeprom and tries to connect
  // if it does not connect it starts an access point with the specified name
  // here  "AutoConnectAP"
  // and goes into a blocking loop awaiting configuration
  wifiManager.setConfigPortalTimeout(900);
  wifiManager.setConnectTimeout(90000);
  wifiManager.setWiFiAPHidden(false);
  wifiManager.setSTAStaticIPConfig(IPAddress(192, 168, 1, 99), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));
  wifiManager.autoConnect("TORLAJE_AP");

  if (wifiManager.autoConnect() == true)
  {
    Serial.println("OK CONNECT");
    ticker.detach();
    digitalWrite(LED, LOW);

  }
  else
  {
    Serial.println("FALSE - NO CONNECT");

  }

  // if you get here you have connected to the WiFi
  Serial.println("Connected.");
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());

  delay(200);
  timeClient.begin();
}

void loop() {

  button.read();
  timeClient.update();

   Serial.print(timeClient.getHours());
   Serial.print(":");
   Serial.println(timeClient.getMinutes());

}

void onPressedForDuration()
{
  isButtonPressed = true;
  wifiManager.resetSettings();
  ESP.eraseConfig();
  Serial.println("BUTTON PRESSED.");
  ESP.reset();
  led_blink();
  Serial.println("Button pressed");
}

void led_blink()
{
  int i = 0;
  while (i < 3)
  {
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500);
    digitalWrite(LED, HIGH);
    i = i + 1;
  }
}
void led_blink2()
{
  int i = 0;
  while (i < 2)
  {
    digitalWrite(LED, LOW);
    delay(50);
    digitalWrite(LED, HIGH);
    delay(50);
    digitalWrite(LED, LOW);
    i = i + 1;
  }
}

#END

Debug Messages

⸮⸮⸮ bp⸮⸮c$r$p⸮n⸮⸮l ⸮⸮  "n⸮|⸮$⸮⸮ ⸮c⸮|~⸮N⸮l⸮⸮l ⸮ondnr⸮⸮⸮o bl s⸮⸮N bl ⸮#⸮⸮$⸮⸮r⸮`⸮⸮o⸮WM: [1] AutoConnect WM: [2] Connecting as wifi client... WM: [3] STA static IP: 192.168.1.99 WM: [2] Custom static IP/GW/Subnet/DNS WM: [2] Custom STA IP/GW/Subnet WM: [1] STA IP set: 192.168.1.99 WM: [1] Connecting to SAVED AP: JHKAHKJAH WM: [3] Using Password: JAKHAKAH WM: [3] WiFi station enable WM: [3] enableSTA PERSISTENT ON WM: [2] 90000000 ms connectTimeout set WM: [2] 19072 ms timeout, waiting for connect... WM: [2] . WM: [2] . WM: [2] . WM: [2] Connection result: WL_CONNECTED WM: [3] lastconxresult: WL_CONNECTED WM: [1] AutoConnect: SUCCESS WM: [1] STA IP Address: 192.168.1.99 WM: [1] AutoConnect WM: [1] AutoConnect: ESP Already Connected WM: [3] STA static IP: 192.168.1.99 WM: [2] Custom static IP/GW/Subnet/DNS WM: [2] Custom STA IP/GW/Subnet WM: [1] STA IP set: 192.168.1.99 WM: [1] AutoConnect: SUCCESS *WM: [1] STA IP Address: 192.168.1.99 OK CONNECT Connected. IP address: 192.168.1.99 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0

messages here
Abhesheksh commented 2 years ago

https://github.com/tzapu/WiFiManager/issues/1097

Same issue but he solved it by removing static IP. Is there anyway I can keep the static IP?

tablatronix commented 2 years ago

Not sure why this would make a difference, it should still work

Georgie9117 commented 1 year ago

Just leave wifiManager.setSTAStaticIPConfig and use this after wifiManager.autoConnect

WiFi.begin();
WiFi.config(IPAddress(192, 168, 1, 148), IPAddress(192, 168, 1, 1), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));

It working for me on ESP8266.

samuraiyy commented 1 year ago

This is my method,it works:

 //wm.setSTAStaticIPConfig(IPAddress(192,168,2,123), IPAddress(192,168,2,1), IPAddress(255,255,255,0) ); // set static ip,gw,sn  
 wm.setShowStaticFields(true);
 wm.setShowDnsFields(true);  

or

 wm.setSTAStaticIPConfig(IPAddress(192,168,2,123), IPAddress(192,168,2,1), IPAddress(255,255,255,0), IPAddress(192,168,2,1) ); // set static ip,gw,sn  
 wm.setShowStaticFields(true);
 wm.setShowDnsFields(true);