issue:
WiFi.config(local_ip, gateway, subnet); corrupts
it always shows 1970 date/time:
Thu Jan 1 01:00:02 1970
Thu Jan 1 01:00:02 1970
Thu Jan 1 01:00:03 1970
Thu Jan 1 01:00:03 1970
board: ESP32 core 1.0.4 (Adafruit Feather ESP32)
Arduino IDE 1.8.9
default settings
OTOH, when out-commenting WiFi.config(local_ip, gateway, subnet);
it works correctly!
Wed Dec 25 11:11:59 2019
Wed Dec 25 11:12:00 2019
Wed Dec 25 11:12:00 2019
Wed Dec 25 11:12:01 2019
Wed Dec 25 11:12:01 2019
code:
#include "time.h"
//#include <ESP8266WiFi.h>
#include <WiFi.h>
const char* ssid = "*****"; // your network SSID (name)
const char* password = "**********"; // your network password
IPAddress local_ip(192, 168, 2, 222);
IPAddress gateway (192, 168, 2, 1);
IPAddress subnet (255, 255, 255, 0);
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 0;
void setup()
{
Serial.begin(115200);
delay(10);
Serial.print("\n\nConnecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.config(local_ip, gateway, subnet); // <<<<<<<<<< !!!! works only if out-commented
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
Serial.println("\nWaiting for time");
unsigned start = millis();
while (!time(nullptr))
{
Serial.print(".");
delay(1000);
}
delay(1000);
Serial.println("Time...");
}
void loop()
{
time_t current = time(nullptr);
Serial.println(ctime(¤t));
delay(500);
}
You haven't set a dns server, and are using a name for the ntp server.
why is that indispensible?
how to set a dns server?
thank you, I'll try it!
do I need to set 2 dns servers?
update, I set 2 identical dns servers, appearently it worked!
many thanks again!
PS,
it would be helpful if that issue about setting additional dns servers had been mentioned somewhere in a sketch comment:
either don't use Wifi.config() at all,
or use
WiFi.config(local_ip, gateway, subnet, gateway, dns1, dns2);
but don't ever use
WiFi.config(local_ip, gateway, subnet, gateway)
dsyleixa, try this sketch.
#include "time.h"
//#include <ESP8266WiFi.h>
#include <WiFi.h>
const char* ssid = "*****"; // your network SSID (name)
const char* password = "**********"; // your network password
IPAddress local_ip(192, 168, 2, 222);
IPAddress gateway (192, 168, 2, 1);
IPAddress subnet (255, 255, 255, 0);
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 0;
void WiFiEvent(WiFiEvent_t event){
switch (event) {
case SYSTEM_EVENT_STA_START:
WiFi.config(local_ip, gateway, subnet); // <<<<<<<<<< !!!! works only if out-commented
break;
}
}
void setup()
{
Serial.begin(115200);
delay(10);
Serial.print("\n\nConnecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.onEvent(WiFiEvent);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
Serial.println("\nWaiting for time");
unsigned start = millis();
while (!time(nullptr))
{
Serial.print(".");
delay(1000);
}
delay(1000);
Serial.println("Time...");
}
void loop()
{
time_t current = time(nullptr);
Serial.println(ctime(¤t));
delay(500);
}
@rel1ct
would you perhaps comment on this a little more, please?
(
comment parameter -v
8)
)
I don鈥檛 see the point.
Your previous post:
WiFi.config(local_ip, gateway, subnet, gateway, dns1, dns2)
WiFi.config(local_ip, gateway, subnet, gateway)
Comment:
I see this nonsense not the first time.
@rel1ct
I rate your post as senseless, accusing and insulting. You changed my TOP code, in my reported context the issue report is valid, as also stated here: https://github.com/espressif/arduino-esp32/issues/3600#issuecomment-568907808
@dsyleixa you should only need to add one DNS server and it should be the fourth parameter and not gateway as seen in @rel1ct post above.
Calling config from the event handler is an alternative way to configure it whereas you code set the mode and then config before starting the SSID connection process.
@atanisoft,
thank you for your reply, and your advice works now, too.
My point actually was:
1) having not used WiFi.config() at all, the time.h functions work well,
2) using WiFi.config() with a constant local_IP +gateway+subnet it fails,
3) instead WiFi.config() then needed an extra dns-server setting.
If Wifi.begin works witout any WiFi.config() (1) , why does it fail then for (2)?
Obviously the dns-settings of (1) are made automatically correct by default (probably using the gateway IP) ,
so why doesn't it do it for (2)?
Expectedly, if no dns-server is set, then the gateway IP is supposed to be taken for the dns-server as well.
OTOH, using an event handler is actually totally weird, no common Arduino user (i.e., hobby programmers like pupils, craftsmen, or artists) would ever do that for that purpose.
By not calling WiFi.config() it defaults to DHCP which configures all parameters based on what the AP provides as a response to the DHCP request, it does not use the gateway as a default.
By calling WiFi.config() without a DNS server address the WiFi driver will not be able to resolve hostnames. There is no default value used when not configured, it will not use the gateway for DNS. I don't know where that might be coming from as the code does not do that today. The API should require at least one DNS address to be provided or it should provide a default (8.8.8.8/1.1.1.1 as example)
As for event handlers, there are a few Arduino libraries that use them. But in general, it is a bit of an advanced topic that is not common in most AVR Arduino examples. That is mostly due to limitations of the platform (flash and ram size mostly).
ok, I see, thanks for clarification.
OTOH, using an event handler is actually totally weird, no common Arduino user (i.e., hobby programmers like pupils, craftsmen, or artists) would ever do that for that purpose.
You have been offered a solution and you are unhappy.
Good luck finding non-existent bugs.
@rel1ct:
is there a forum's limitation to arrogance, smugness, or stubbornness ...? ;)
edit:
fyi, the issue is meanwhile resolved...
Please just close the issue if it is resolved.
Most helpful comment
Please just close the issue if it is resolved.