ESP32 using NTPClient to control a DC motor daily at a specific hour

I'm working on a small fun project to automate the door of my chicken coop :slight_smile:
But i'm a newbie in working with microcontrollers and writing code.

I want to control a DC motor with an ESP32 and a L298N at a specific time daily. Using the NTPClient.
But the motor doesn't start to rotate at the specified time.

Getting the time with the NTPClient works perfectly. (checked it trough the Serial.print)
But for some reason the assignment with the if() function doesn't go trough.

Could anyone help me with my code below?

Thanks in advance

#include <WiFi.h>

#include <SPI.h>

#include <NTPClient.h>
#include <WiFiUdp.h>
const char* ssid     = "...";
const char* password = "....";

#define NTP_OFFSET  7200 // In seconds 
#define NTP_INTERVAL 60 * 1000    // In miliseconds
#define NTP_ADDRESS  "be.pool.ntp.org"

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);

// Motor A
int motor1Pin1 = 27; 
int motor1Pin2 = 26; 
int enable1Pin = 14; 

void setup()
 // sets the pins as outputs:
{
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
    {
    delay(500);
    }
    timeClient.begin();
    delay(3000);
  Serial.begin(9600); 
 }

void loop()
{

timeClient.update();
int CurrentHour = timeClient.getHours();
int CurrentMinute = timeClient.getMinutes();
int CurrentSecond = timeClient.getSeconds();

Serial.print(CurrentHour);
Serial.print(CurrentMinute);
Serial.print(CurrentSecond);

if(CurrentHour == 19 && CurrentMinute == 0 && CurrentSecond == 0)
  {
     digitalWrite(motor1Pin1, LOW);
     digitalWrite(motor1Pin2, HIGH);
     delay(4000);
     digitalWrite(motor1Pin1, LOW);
     digitalWrite(motor1Pin2, LOW);
  }
if(CurrentHour == 7 && CurrentMinute == 0 && CurrentSecond == 0))
  {
     digitalWrite(motor1Pin1, HIGH);
     digitalWrite(motor1Pin2, LOW);
     delay(4000);
     digitalWrite(motor1Pin1, LOW);
     digitalWrite(motor1Pin2, LOW);
  }


}

I suspect that while you are able to get the time from NTP, the NTP_INTERVAL means that you would have to be very lucky to catch the moment when seconds is zero.

use
SecondsOfDay >=

#include <WiFi.h>

#include <SPI.h>

#include <NTPClient.h>
#include <WiFiUdp.h>
const char* ssid     = "...";
const char* password = "....";

#define NTP_OFFSET  7200 // In seconds 
#define NTP_INTERVAL 60 * 1000    // In miliseconds
#define NTP_ADDRESS  "be.pool.ntp.org"

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);

// Motor A
const byte motor1Pin1 = 27; // you want to make sure by using "const" that
const byte motor1Pin2 = 26; // this value can't be changed
const byte enable1Pin = 14;

int SecondsOfDayToOpen  = 7 * 3600 + 0 * 60 + 0;
int SecondsOfDayToClose = 19 * 3600 + 0 * 60 + 0;
int SecondsOfDay;

void setup()
// sets the pins as outputs:
{
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
  }
  timeClient.begin();
  delay(3000);
  Serial.begin(9600);
}

void loop(){

  timeClient.update();
  int CurrentHour = timeClient.getHours();
  int CurrentMinute = timeClient.getMinutes();
  int CurrentSecond = timeClient.getSeconds();

  Serial.print(CurrentHour);
  Serial.print(CurrentMinute);
  Serial.print(CurrentSecond);

  SecondsOfDay = CurrentHour * 3600 + CurrentMinute * 60 + CurrentSecond;
  //if(CurrentHour == 19 && CurrentMinute == 0 && CurrentSecond == 0){
  if (SecondsOfDay >= SecondsOfDayToClose) {
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
    delay(4000);
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW);
  }

  //if(CurrentHour == 7 && CurrentMinute == 0 && CurrentSecond == 0)){
  if (SecondsOfDay >= SecondsOfDayToOpen) {
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    delay(4000);
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW);
  }
}

best regards Stefan

NTP is not a clock.

So what do you want to say with that?

OK I understand.
me personal I have a DSL-WLAN-Router that acts as an NTP-server itself

They are from manufacturer AVM
https://en.avm.de/products/fritzbox/

What would be interesting is to analyse is:

The ESP32 running an RTOS has its own RTC onboard. So there is no need to make NTP-requests every few seconds. Once the ESP32-onboard-RTC has synced with NTP the ESP32-onboard-RTC can run on its own.

So the interesting question is: is there a possability to set the interval for the NTP-requests to once per hour or once per day ?

best regards Stefan

Thanks StefanL38 and ZX80!

It works perfectly now.

I considered both of your inputs in the improved code.
I also included push buttons in case I have to open or close the door manually.

#include <WiFi.h>

#include <SPI.h>

#include <NTPClient.h>
#include <WiFiUdp.h>
const char* ssid     = ".....";
const char* password = ".....";

#define NTP_OFFSET  7200 // In seconds 
#define NTP_INTERVAL 1800 * 1000    // In miliseconds
#define NTP_ADDRESS  "be.pool.ntp.org"

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);


int motor1Pin1 = 27; 
int motor1Pin2 = 26; 

const int PushButtonClose = 25;
const int PushButtonOpen = 33;

int HourToClose = 19;
int HourToOpen = 7;

void setup()

{

  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);


  pinMode(PushButtonClose, INPUT);
  pinMode(PushButtonOpen, INPUT);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
    {
    delay(500);
    }
    timeClient.begin();
    delay(3000);
  Serial.begin(9600); 
 }

void loop()
{

timeClient.update();
int CurrentHour = timeClient.getHours();

Serial.print(CurrentHour);

int PushCloseState = digitalRead(PushButtonClose);
int PushOpenState = digitalRead(PushButtonOpen);

if(PushCloseState == LOW)
  {
     digitalWrite(motor1Pin1, LOW);
     digitalWrite(motor1Pin2, HIGH);
     delay(4000);
     digitalWrite(motor1Pin1, LOW);
     digitalWrite(motor1Pin2, LOW);
  }

if(CurrentHour == HourToClose)
  {
     digitalWrite(motor1Pin1, LOW);
     digitalWrite(motor1Pin2, HIGH);
     delay(4000);
     digitalWrite(motor1Pin1, LOW);
     digitalWrite(motor1Pin2, LOW);
  }
if(CurrentHour == HourToOpen)
  {
     digitalWrite(motor1Pin1, HIGH);
     digitalWrite(motor1Pin2, LOW);
     delay(4000);
     digitalWrite(motor1Pin1, LOW);
     digitalWrite(motor1Pin2, LOW);
  }

if(PushOpenState == LOW)
   {
     digitalWrite(motor1Pin1, HIGH);
     digitalWrite(motor1Pin2, LOW);
     delay(4000);
     digitalWrite(motor1Pin1, LOW);
     digitalWrite(motor1Pin2, LOW);
  }
delay(3600000);

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.