Another boring noob question

Hello! I’m Matías, from Buenos Aires, Argentina. I’m an MD and have an extensive background in electronics, jazz music (electric guitar and trumpet), and fly fishing, but my experience in progamming lasts until middle 80’s writing code in Microsoft Basic.

Here’s the case:

I’m trying to write a code for Nodemcu ESP8266, which reads the time of the day from internet, and activate/deactivate a relay in a specific timelapse, from 18 hrs to 05 hrs, in order to control some lights.

Here’s the corpse:

#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char *ssid     = "whocares";
const char *password = "dontknow";

const long utcOffsetInSeconds = -10800;



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

void setup(){

  
  Serial.begin(9600);
  
  const int relay = 5;
  pinMode(relay, OUTPUT);

  WiFi.begin(ssid, password);


while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
 

  timeClient.begin();
}


}


void loop() 

{
  timeClient.update();
  
  const int r1 = 5;
  
  int H = timeClient.getHours();
  while (H > 18); (H < 5); {

    digitalWrite(r1, LOW);

  
  }
}

I see it too naive :smiley: :smiley: specially the loop that try to control the relay. The led turns on and of depending the status of

digitalWrite(r1, LOW);

Writing LOW turns on, and HIGH turns OFF, but nothing more. I’m stucked here.

Could you help me, please?

Thanks!

Matías

the boring thing with noobs is that they write boring headers.

Another boring noob question

says NOTHING about your problem.

for NTP - use the IDE example

ESP8266 | NTP-TZ-DST

try

if (tm.tm_hour >=15 || tm.tm_hour <= 5)
{
// do whatever in the night;

}
else
{
// do whatever during day

}

UPDATE: I'm supposing an static IP is needed, isn't? I appreciate some information on this topic.

Thanks again!

Matías

noiasca:
the boring thing with noobs is that they write boring headers.

Another boring noob question

says NOTHING about your problem.

for NTP - use the IDE example

ESP8266 | NTP-TZ-DST

try

if (tm.tm_hour >=15 || tm.tm_hour <= 5)
{
// do whatever in the night;

}
else
{
// do whatever during day

}

Will try, thanks!
Matías

what do yo mean with static IP?
the ESP can use a dynamic IP and will still get the time from NTP.

OK! Thanks!!

It's generally better to use DHCP to obtain IP addresses, for many reasons. One subtle reason is that some wifi routers do IP and MAC management without being asked, it can lead to configuration problems when just blindly using a static IP.