-->
Page 1 of 2

Issue with ESPDateTime and fixed IP (WiFi.config)

PostPosted: Wed Jul 01, 2020 12:11 pm
by freddy-a
Hi all,

I hae this strange issue with my ESP8266 NodeMCU. I'm using ESPDateTime to get the time but as soon as I use a hard-coded IP (using the WiFi.config option) the ESPDateTime can not connect to the internet to get the time. I can connect to the NodeMCU on my own network (and request the HTML, for example) But with a set IP it doesn't work.
Another strange behaviour is that some IP's seemed to work and others didn't. But later (after a change in the code and a recompile) those IP numbers also didn't work anymore.
It has worked (I've used it in a couple of projects) but now it doesn't anymore. And I have no idea why..

Here's my code.
In short: I've got a NodeMCU with a temperature/humidity sensor, a light sensor and a relay. When it's dark (enough) the lights turn on and when it get's lighter, they turn off. I can also turn them on / off manually.
The temp sensor will be used by another device / programm to request the temp humidity from this device. But that's not build in yet.

Code: Select all//Netwerk connectie
#include <ESP8266WiFi.h>
const char* ssid = "My_SSID";         //aan te passen HARD CODED variabele
const char* pass = "My_Password";     //aan te passen HARD CODED variabele
IPAddress staticIP(192,168,0,30);     //aan te passen HARD CODED variabele
IPAddress gateway(192,168,0,1);       //aan te passen HARD CODED variabele
IPAddress subnet(255,255,255,0);      //aan te passen HARD CODED variabele

//Webserver
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);

//voor internet tijd via time server
#include "ESPDateTime.h"
time_t t;

//DHT-11 temp/humidity sensor
#include <DHTesp.h>
DHTesp dht;
TempAndHumidity lastValues;
String temperature;
String humidity;
unsigned long ms = millis();

//Spotjes parameters
#define pinRelaySpots   D0
#define pinLightSensor  D1
String setting = "auto";    //mogelijkheden: auto (default) | aan | uit

void setup() {
  Serial.begin(115200);
  pinMode(pinRelaySpots, OUTPUT);
  pinMode(pinLightSensor, INPUT_PULLUP);
  digitalWrite(pinRelaySpots, LOW);     //Relay begint UIT
 
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
//  WiFi.config(staticIP, gateway, subnet); //When I unremark this line, time can't be retreived
  Serial.print("WiFi Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  WiFi.hostname("Licht_sensor_dak");
  Serial.println();
  Serial.print("IP:");Serial.println(WiFi.localIP());
  Serial.print("MAC:");Serial.println(WiFi.macAddress());

  DateTime.setServer("europe.pool.ntp.org");
  DateTime.setTimeZone(-2);
  DateTime.begin();

  dht.setup(D3, DHTesp::DHT11);

  server.on("/", HTTP_POST, setSetting);
  server.on("/", sendHTML);
  server.begin();
}

void loop() {
  server.handleClient();

  if((setting =="auto" and digitalRead(pinLightSensor) == HIGH) or setting == "aan"){
    digitalWrite(pinRelaySpots, HIGH);  //HIGH = AAN
  } else if((setting =="auto" and digitalRead(pinLightSensor) == LOW) or setting == "uit"){
    digitalWrite(pinRelaySpots, LOW);  //LOW = UIT   
  }

  if (millis() - ms > 5000) {  //check time and temp every 5 seconds
    ms = millis();
    if (!DateTime.isTimeValid()) {
      Serial.println("Failed to get time from server, retry.");
      DateTime.begin();
    }
   
    lastValues = dht.getTempAndHumidity();  //één call voor beide waarden!
    temperature = String(lastValues.temperature, 1);
    humidity = String(lastValues.humidity, 1);

    //Serial monitor output, only for debugging
    t = DateTime.now();
    Serial.println(DateFormatter::format("Het is %H:%M:%S %d-%m-%Y", t));
    Serial.println("Het is " + temperature + " C (RV is " + humidity + "%)");
    Serial.println("Setting: " + setting);
    Serial.print("Lightsensor is: ");
    if(digitalRead(pinLightSensor) == HIGH){
      Serial.println("hoog");
    } else {
      Serial.println("laag");
    }
  }
}

void sendHTML(){
  String lichtsensor;
  if(digitalRead(pinLightSensor) == HIGH){
    lichtsensor = "actief";
  } else {
    lichtsensor = "inactief";
  }
 
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.1, user-scalable=yes\">\n";
  ptr +="<title>Buitenverlichting</title>\n";
//  ptr +=" <meta http-equiv=\"refresh\" content=\"30\">\n";
  ptr +="</head>\n";
  ptr +="<body bgcolor=burlywood style=\"font-family: Verdana; font-size: 14px;\">\n";
  ptr +="<center>\n";
  ptr +="<h3>Buitenverlichting</h3>\n";
  ptr +="<p>Het is " + DateFormatter::format("%H:%M, %d-%m-%Y", t) + "<br>\n";
  ptr +="<p>Het is <b>" + temperature + String(char(176)) + "C</b>\n";
  ptr +="(RV is <b>" + humidity + "%</b>)<br>\n";
  ptr +="<p>Lichtsensor signaal: <b>" + lichtsensor + "</b><br>\n";
  ptr +="<p>Huidige setting: <b>" + setting + "</b><br>\n";
  ptr +="<form method=\"POST\" action=\"/\">";  //lege actie, POST is voldoende
  ptr +="Wijzig setting in: \n";
  ptr +="<button type=\"submit\" name=\"setting\" value=\"auto\">auto</button> \n";
  ptr +="<button type=\"submit\" name=\"setting\" value=\"aan\">aan</button> \n";
  ptr +="<button type=\"submit\" name=\"setting\" value=\"uit\">uit</button><br>\n";
  ptr +="</form>";
  ptr +="<center>\n";
  ptr +="</body>\n";
  ptr +="</html>\n";

  server.send(200, "text/html", ptr);
}

void setSetting(){
  setting = server.arg("setting");
  sendHTML();
}


I'm not sure if it's because of my network, but I haven't changed anything in my router / DHCP. So that's not the first thing that comes into mind. Also, it has worked OK. All of a sudden it didn't anymore..
It's driving me crazy! :? It's sooooo much easier to have fixed IP's, because then I know what page to go to to configure settings, or request data from..

Thanks!


Oh, some comments / output are in Dutch. But I think it should be understandable the way it is, the code is pretty simple and straight forward..

Re: Issue with ESPDateTime and fixed IP (WiFi.config)

PostPosted: Thu Jul 02, 2020 2:42 am
by schufti
I think that is not the way to use timeserver according the core dev team.
look at the examples provided with the core
https://github.com/esp8266/Arduino/blob ... TZ-DST.ino
for recommended usage.

Re: Issue with ESPDateTime and fixed IP (WiFi.config)

PostPosted: Thu Jul 02, 2020 3:47 am
by freddy-a
I'm not sure what you mean..
If I look at the code (from your link) I see that the showTime function is called from the main loop. That showTime function calls the time server. So that's not really different from what I'm doing.

I folowed the example from the ESPDateTime library.


Besides, my issue is that, with an IP given by the DHCP there's no probem, but with a fixed IP it doesn't work.

Re: Issue with ESPDateTime and fixed IP (WiFi.config)

PostPosted: Thu Jul 02, 2020 3:54 am
by Pablo2048
Probably it's because of when configured by DHCP, the library get ntp server address from DHCP (see https://support.ntp.org/bin/view/Suppor ... ction_6.12. ), but when You did manual/static configuration, then the ntp server address is not set, so You have to set it manually. BTW better way is to use @schufti recommendation...
Update: It seems like the library uses hardcoded ntp servers, so the problem is not with NTP server address, but DNS server address and/or Gateway address.
Update2: After reading Your code, the problem is here https://github.com/esp8266/Arduino/blob ... iSTA.h#L47 - missing DNS server address...