Post topics, source code that relate to the Arduino Platform

User avatar
By freddy-a
#87729 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

#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() {
  pinMode(pinRelaySpots, OUTPUT);
  pinMode(pinLightSensor, INPUT_PULLUP);
  digitalWrite(pinRelaySpots, LOW);     //Relay begint UIT
  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) {


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

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

void loop() {

  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.");
    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 =;
    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){
    } else {

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");

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..


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..
User avatar
By freddy-a
#87734 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.
User avatar
By Pablo2048
#87735 Probably it's because of when configured by DHCP, the library get ntp server address from DHCP (see ... 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 ... iSTA.h#L47 - missing DNS server address...