School Bell System using ESP8266

Good day to all..
Please help me on how to create a code for school bell project. I have a code that normally function around two years of operating and no hassle, by this year my school head wanted me to create or reversion of coding instead of single device output she wanted me to create 2 devices output. There was a conflict of time schedule in school that composes of Junior High and Senior High and every department has its own time schedule.
anyone from this group or our master in coding can help me..
Below the sample of my stable coding and i used it for several years.. hope can you help me..

#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
void bell(int p);
const char* ssid = "(v)";
const char* password = "PNHS@1969";
const long utcOffsetInSeconds = 28800;
int p;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);

int LED = 16; // led connected to D0
int hm = 13;
WiFiServer server(80);

void setup()
{
Serial.begin(115200);
pinMode(LED, OUTPUT);

digitalWrite(LED, HIGH);

Serial.print("Connecting to Internet ");

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
timeClient.begin();

/-------- server started---------/
server.begin();
Serial.println("Server started");

/------printing ip address--------/
Serial.print("IP Address of network: ");
Serial.println(WiFi.localIP());
Serial.print("Copy and paste the following URL: https://");
Serial.print(WiFi.localIP());
Serial.println("/");
}

void loop()
{

timeClient.update();

int hh = timeClient.getHours();
int mm = timeClient.getMinutes();
int ss = timeClient.getSeconds();

Serial.print(hh);
Serial.print(":");
Serial.print(mm);
Serial.print(":");
Serial.println(ss);
//Serial.println(timeClient.getFormattedTime());

WiFiClient client = server.available();    

String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();

//*********bell time

if (hh == 7 && mm == 30)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}
else if (hh == 7 && mm == 30)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}

else if (hh == 8 && mm == 15)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}
else if (hh == 8 && mm == 15)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}

else if (hh == 9 && mm == 00)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}
else if (hh == 9 && mm == 00)
{

digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}

else if (hh == 9 && mm == 45)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}
else if (hh == 9 && mm == 45)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}
else if (hh == 10 && mm == 00)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}
else if (hh == 10 && mm == 00)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}}
else if (hh == 10 && mm == 45)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 10 && mm == 45)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 11 && mm == 30)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 11 && mm == 30)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 12 && mm == 30)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 12 && mm == 30)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 13 && mm == 15)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 13 && mm == 15)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 14 && mm == 00)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 14 && mm == 00)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 14 && mm == 45)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 14 && mm == 45)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 15 && mm == 00)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(10000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
Serial.print("BELL");
}
else if (hh == 15 && mm == 00)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(10000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
Serial.print("BELL");
}

//************bell time

int value = LOW;
if (request.indexOf("/BELL=ON") != -1)
{
digitalWrite(LED, LOW);
value = HIGH;
}
if (request.indexOf("/BELL=OFF") != -1)
{
digitalWrite(LED, HIGH);
value = LOW;
}

/------------------Creating html page---------------------/

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("");
client.println("");
client.println("");
client.println("

PONTEVEDRA NHS AUTOMATED SCHOOL BELL

" );
client.print("BELL is: ");

if(value == HIGH)
{
client.print("ON");
}
else
{
client.print("OFF");
}
client.println("

");
client.println("<a href="/BELL=ON"">ON");
client.println("<a href="/BELL=OFF"">OFF
");
client.print(hh);
client.print(":");
client.print(mm);
client.print(":");
client.println(ss);

client.println("");

delay(1);
Serial.println("Client disonnected");
Serial.println("");

}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help

Welcome! Sorry I hve no idea of what you posted.
Posting an annotated schematic showing exactly how you have wired it helps us help you. By showing all connections, power, components, ground and power sources you will save a lot of clarifying questions and time for
all of us. Be sure to note any logic wires over 10/25 inches/cm in length. Post links to technical information on the hardware items including the motors, shield, and Arduino. This should include all component values or model numbers and details of all power supplies used (which could be USB power for example). Posting the code following forum guidelines using code tags will also help. With this information we should be able to answer your question accurately. "How to get the best from this forum".

If you don't understand what a schematic is, please Google to find out. It is not the same thing as a wiring diagram. Hand-drawn is OK. Some bright, sharp photos of the circuit may also be useful.

The thing I see is many of the users do not want to draw schematics yet a large portion of there problems are in the connections, power etc. It appears they spend about a week or so with lots of questions and then maybe, give up, and some get lucky. It is amazing how many problems you find when
doing a real labeled schematic including Technical links. That schematic makes them look like a pro. It appears to me that most of the time when a proper schematic is posted the solution is usually within day or so. Good schematic capture software can be gotten for the downloading. Months later a wire comes off, looking at the schematic and putting it back saves lots of headaches.

What is a schematic What Is a Schematic Diagram?
How to Read a Schematic - SparkFun Learn
HOW to READ a Schematic: https://www.youtube.com/watch?v=9cps7Q_IrX0&t=15s

KiCad: There are many out there and you will find just like color different people like different colors,some blue, red, ..etc. CAD (Computer Aided Design) programs are like that. You will need to learn a schematic capture program that will work for you.

Proposed Additional
here is the block diagram, hope you can help me..

#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
void bell(int p);
const char* ssid = "(v)";
const char* password = "PNHS@1969";
const long utcOffsetInSeconds = 28800;
int p;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);

int LED = 16; // led connected to D0
int hm = 13;
WiFiServer server(80);

void setup()
{
Serial.begin(115200);
pinMode(LED, OUTPUT);

digitalWrite(LED, HIGH);

Serial.print("Connecting to Internet ");

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
timeClient.begin();

/-------- server started---------/
server.begin();
Serial.println("Server started");

/------printing ip address--------/
Serial.print("IP Address of network: ");
Serial.println(WiFi.localIP());
Serial.print("Copy and paste the following URL: https://");
Serial.print(WiFi.localIP());
Serial.println("/");
}

void loop()
{

timeClient.update();
int hh = timeClient.getHours();
int mm = timeClient.getMinutes();
int ss = timeClient.getSeconds();

Serial.print(hh);
Serial.print(":");
Serial.print(mm);
Serial.print(":");
Serial.println(ss);
//Serial.println(timeClient.getFormattedTime());

WiFiClient client = server.available();    
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();

//*********bell time

if (hh == 7 && mm == 30)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}
else if (hh == 7 && mm == 30)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}

else if (hh == 8 && mm == 15)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}
else if (hh == 8 && mm == 15)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}

else if (hh == 9 && mm == 00)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}
else if (hh == 9 && mm == 00)
{

digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}

else if (hh == 9 && mm == 45)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}
else if (hh == 9 && mm == 45)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}
else if (hh == 10 && mm == 00)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
}
else if (hh == 10 && mm == 00)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}}
else if (hh == 10 && mm == 45)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 10 && mm == 45)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 11 && mm == 30)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 11 && mm == 30)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 12 && mm == 30)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 12 && mm == 30)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 13 && mm == 15)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 13 && mm == 15)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 14 && mm == 00)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 14 && mm == 00)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 14 && mm == 45)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 14 && mm == 45)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(8000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

} }
else if (hh == 15 && mm == 00)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(10000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
Serial.print("BELL");
}
else if (hh == 15 && mm == 00)
{
{
digitalWrite(LED, LOW);
Serial.print("BELL ON");
delay(10000);
digitalWrite(LED, HIGH);
Serial.print("BELL OFF");
delay(120000);

}
Serial.print("BELL");
}

//************bell time

int value = LOW;
if (request.indexOf("/BELL=ON") != -1)
{
digitalWrite(LED, LOW);
value = HIGH;
}
if (request.indexOf("/BELL=OFF") != -1)
{
digitalWrite(LED, HIGH);
value = LOW;
}

/------------------Creating html page---------------------/

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("");
client.println("");
client.println("");
client.println("

PONTEVEDRA NHS AUTOMATED SCHOOL BELL
" );
client.print("BELL is: ");
if(value == HIGH)
{
client.print("ON");
}
else
{
client.print("OFF");
}
client.println("

");
client.println("<a href="/BELL=ON"">ON");
client.println("<a href="/BELL=OFF"">OFF
");
client.print(hh);
client.print(":");
client.print(mm);
client.print(":");
client.println(ss);

client.println("");

delay(1);
Serial.println("Client disonnected");
Serial.println("");

}

Your NTP could do with an overhaul. Most of those time libraries are obsolete. NTP is now included in the ESP core. So don't use NTPClient, WiFiUDP, timeClient, etc.

All you need for basic NTP is one line in setup().
configTime("<+08>-8", "pool.ntp.org"); // example: Singapore
and two lines in loop().

#include <ESP8266WiFi.h> // ESP8266
unsigned long prevTime;
time_t now; // epoch
tm tm; // time struct

void setup() {
  Serial.begin(115200);
  WiFi.begin("SSID", "PNHS@1969"); // WiFi credentials
  configTime("PST-8", "pool.ntp.org"); // example: Manila
  // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
}

void loop() {
  time(&now); // now
  localtime_r(&now, &tm); // add local time (with DST)

  if (now != prevTime) { // execute if time (seconds) changes
    prevTime = now; // remember
    printf("\n%02u:%02u:%02u", tm.tm_hour, tm.tm_min, tm.tm_sec);
  }
}

Repetitive code for the LED should be in a function.
The use the function call in the if() statements.
Leo..

Edit: TZ string corrected.

2 Likes

Please edit your post and use code tags around your code

what should i do? i am not well in coding..

Did the simple NTP clock work?
Then simply use tm.tm_hour and tm.tm_min instead of hh and mm in your code.

Maybe ask the school to put a team together, to help you with coding.
Leo..

1 Like

You might get the shop teacher to help you draw a schematic. What you posted may tell you what is there but I cannot see anything and the lines on your picture go into obliteration. Did you follow the links posted? Go to your school library and check out a copy of the Arduino Cookbook, if they do not have it ask them to get a copy. Then read it from cover to cover and do the experiments pertinent to your project.

describe in short sentences what your requirements are.

for example
The ESP should controll two bells named "junior bell" and "senior bell"

The junior bell is connected to pin XX
The senior bell is connected to pin YY

Daily (?) at 08:15 the junior bell should ring for 8 Seconds
Daily (?) at 09:00 the junior bell should ring for 8 Seconds
Daily (?) at 09:45 the junior bell should ring for 8 Seconds
...

conclude that list for the junior bell and the senior bell

A Webpage should show

  • the current state of the Bell
  • a nutton to switch on or off manually (for how long?!?)
  • the current time

The ESP time should be set by NTP.
The time should be set for my timezone in country/city xxx
There is daylight saving time (or not) in that timezone.

If I feel happy with your requirements I might find some time to give you sketch.

Yes, this is what I mean..
The ESP should controll two bells named "junior bell" and "senior bell"

The junior bell is connected to pin 16 // Bell 1 connected to D0
The senior bell is connected to pin YY // Bell 2 connected to D7

Daily (Monday to Friday) at 07:00 - 07:30 the junior bell should ring for 8 Seconds in single ring
Daily (M-F) at 07:30 - 08:15 the junior bell should ring for 8 Seconds in single ring
Daily (M-F) at 08:15 - 09:00 the junior bell should ring for 8 Seconds in single ring
Daily (M-F) at 09:00 - 09:45 the junior bell should ring for 8 Seconds in single ring
Daily (M-F) at 09:45 - 10:15 the junior bell should ring for 8 Seconds in single ring
Daily (M-F) at 10:15 - 11:00 the junior bell should ring for 8 Seconds in single ring
Daily (M-F) at 11:00 - 11:45 the junior bell should ring for 8 Seconds in single ring
Daily (M-F) at 11:45 - 12:30 the junior bell should ring for 8 Seconds in single ring (Lunch Break)
Daily (M-F) at 12:30 - 1:15 the junior bell should ring for 8 Seconds in single ring
Daily (M-F) at 01:15 - 02:00 the junior bell should ring for 8 Seconds in single ring
Daily (M-F) at 02:00 - 02:45 the junior bell should ring for 8 Seconds in single ring
Daily (M-F) at 02:45 - 03:30 the junior bell should ring for 8 Seconds in single ring
Daily (M-F) at 03:30 - 04:00 the junior bell should ring for 8 Seconds in single ring

Daily (Monday to Friday) at 07:00 - 07:30 the senior bell should ring for 3 Seconds in twice ringing
Daily (M-F) at 07:30 - 08:30 the senior bell should ring for 3 Seconds in twice ringing
Daily (M-F) at 08:30 - 09:30 the senior bell should ring for 3 Seconds in twice ringing
Daily (M-F) at 09:30 - 09:45 the senior bell should ring for 3 Seconds in twice ringing
Daily (M-F) at 09:45 - 10:45 the senior bell should ring for 3 Seconds in twice ringing
Daily (M-F) at 10:45 - 11:45 the senior bell should ring for 3 Seconds in twice ringing
Daily (M-F) at 11:45 - 12:30 the senior bell should ring for 3 Seconds in twice ringing (Lunch Break)
Daily (M-F) at 12:30 - 1:30 the senior bell should ring for 3 Seconds in twice ringing
Daily (M-F) at 01:30 - 02:30 the senior bell should ring for 3 Seconds in twice ringing
Daily (M-F) at 02:30 - 03:30 the senior bell should ring for 3 Seconds in twice ringing
Daily (M-F) at 03:30 - 04:30 the senior bell should ring for 3 Seconds in twice ringing
Daily (M-F) at 04:30 - 05:00 the senior bell should ring for 3 Seconds in twice ringing

...

conclude that list for the junior bell and the senior bell

A Webpage should show

  • the current state of the Bell
  • a nutton to switch on or off manually (for how long?!?)
  • the current time

The ESP time should be set by NTP.
The time should be set for my timezone in Philippines/Bacolod City
There is daylight saving time (or not) in that timezone. +08 UTC

I don't understand this.
07:00 - 07:30 spans half an hour.
So when should the bell start and when should it stop.

Did you already try the simple clock, and did it work.
I already entered the Philippines timezone there, because of the password hint.
Leo..

Edit: Timezone string corrected. Please try again.

  • how is the '3 seconds twice ring' done ( I mean is 1 second on, 1 off and 1 on, or... )?
  • also should all ringing done only monday-friday?

Note
Your original program has 2 main problems:

  1. the use of the 'synchronous webserver', it blocks the main loop for 5 seconds
  2. the use of very long delays ( 8 seconds, longest 120 seconds ) in the main loop

It 'worked' in the original version... but as soon as you try to modify it you will find that is 'impossible' ( it won't works easily )
So the underlying statement is: don't use long delays/blocking functions ( unless you know what it means ; - )

@jie_mags

please answer all questions from the previous writers, as your description leaves some grey areas.

However, I have written a demo sketch containing

  • an output class Bell which can ring once or twice depending on the action you request
  • creating two instances named bell as an array of objects
  • a structure timetable wherein you define what should happen at what time to which bell
  • you can "test" the sketch when you adopt times or manually if you enter o or t to Serial.

everything without a blocking delay.

Summary
/*
  ESP NTP Schoolbell
  NetWork Time Protocol - Time Zone - Daylight Saving Time

  Our target for this MINI sketch is:
  - get the SNTP request running in the defined time zone
  - ring two bells based on a timetable
  - open/todo: add the ESP Webserver

  microcontroller:
  - ESP32 core 1.0.5, 1.0.6, 2.0.2
  - ESP8266 core 2.7.4

  by noiasca
  2024-08-07 initial version
*/

//#include <credentials.h>                               // if you have a config file with your credentials

#ifndef STASSID
#define STASSID "your-ssid"                            // set your SSID
#define STAPSK  "your-password"                        // set your wifi password
#endif

/* Configuration of NTP */
// choose the best fitting NTP server pool for your country
#define MY_NTP_SERVER "pool.ntp.org"

// choose your time zone from this list
// https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
//#define MY_TZ "CET-1CEST,M3.5.0/02,M10.5.0/03" // Berlin, Vienna, Rom, ...
#define MY_TZ "PST-8"                        // Phillipines/Manila

/* Necessary Includes */
#ifdef ARDUINO_ARCH_ESP32           // for the ESP32
#include <WiFi.h>                   // we need wifi to get internet access
#endif
#ifdef ARDUINO_ARCH_ESP8266         // for the ESP8266
#include <ESP8266WiFi.h>            // we need wifi to get internet access
#endif
#include <time.h>                   // time() ctime()

// a bell can be started to ring for once or twice
// it will shut off after the time is over (when you call .upate() in loop)
class Bell {
  protected:
    const uint8_t pin;                   // the GPIO the bell is connected to
    enum State {IDLE, LONG, SHORT_A, PAUSE, SHORT_B} state;  // current state of bell
    uint32_t previousMillis = 0;         // time management

    const uint16_t longInterval = 8000;
    const uint16_t shortInterval = 3000;

  public:
    Bell (uint8_t pin) : pin(pin) {}

    void begin() {
      pinMode(pin, OUTPUT);
      digitalWrite(pin, HIGH);
    }

    State getState() {
      return state;
    }

    void ringOnce() {
      previousMillis = millis();
      state = LONG;
      digitalWrite(pin, LOW);
      Serial.print("ringOnce pin "); Serial.println(pin);
    }

    void ringTwice() {
      previousMillis = millis();
      state = SHORT_A;
      digitalWrite(pin, LOW);
      Serial.print("ringTwice pin "); Serial.println(pin);
    }

    void update(uint32_t currentMillis = millis()) {
      switch (state) {
        case IDLE :                                     // do nothing in IDLE
          break;
        case LONG:
          if (currentMillis - previousMillis > longInterval) {  // time to switch off
            digitalWrite(pin, HIGH);
            state = IDLE;
          }
          break;
        case SHORT_A:
          if (currentMillis - previousMillis > shortInterval) { // time to switch off
            previousMillis = currentMillis;            // update timestamp for next state
            digitalWrite(pin, HIGH);
            state = PAUSE;
          }
          break;
        case PAUSE:
          if (currentMillis - previousMillis > shortInterval) { // time to switch on
            previousMillis = currentMillis;            // update timestamp for next state
            digitalWrite(pin, LOW);
            state = SHORT_B;
          }
          break;
        case SHORT_B:
          if (currentMillis - previousMillis > shortInterval) { // time to switch off finally
            digitalWrite(pin, HIGH);
            state = IDLE;
          }
      }
    }
};

// now lets create two objects representing the bells:
Bell bell[] {
  {D0},         // GPIO for one bell  (D0/16)
  {D7}          // GPIO for another bell (D7/
};

//constexpr uint8_t junior {0};       // index 0 of the array bell is for junior
//constexpr uint8_t senior {1};       // index 1 of the array bell is for senior
enum Bells {JUNIOR, SENIOR};
enum Action {ONCE, TWICE};

// this defines one line of the time table
struct Timetable {
  uint16_t start;                   // start time
  uint8_t bell;                     // which bell
  uint8_t action;                   // what to do mode (1 one long, 2 two short rings)
};

// in the timetable array each line represents one particular time, bell and action to do
// the entries don't need to be sorted, 
// you can define several actions for the same minute
Timetable timetable[] {
  //start, bell, action
  {730, JUNIOR, ONCE},
  {815, JUNIOR, ONCE},
  {900, JUNIOR, ONCE},
  {945, JUNIOR, ONCE},
  {1015, JUNIOR, ONCE},
  {1100, JUNIOR, ONCE},
  {1145, JUNIOR, ONCE},
  {1230, JUNIOR, ONCE},
  {1315, JUNIOR, ONCE},
  {1400, JUNIOR, ONCE},
  {1445, JUNIOR, ONCE},
  {1530, JUNIOR, ONCE},
  {1600, JUNIOR, ONCE},
  {700, SENIOR, TWICE},
  {730, SENIOR, TWICE},
  {830, SENIOR, TWICE},
  {930, SENIOR, TWICE},
  {945, SENIOR, TWICE},
  {1045, SENIOR, TWICE},
  {1145, SENIOR, TWICE},
  {1230, SENIOR, TWICE},
  {1330, SENIOR, TWICE},
  {1430, SENIOR, TWICE},
  {1530, SENIOR, TWICE},
  {1630, SENIOR, TWICE},
  {1700, SENIOR, TWICE},

  {1818, JUNIOR, ONCE}, // the following entries are just for testing - you can modify and finally delete them
  {1820, JUNIOR, TWICE},
};

// checks the timetable once a minute if something is to do
void checkTimetable() {
  time_t now;                       // this are the seconds since Epoch (1970) - UTC
  tm tm;                            // the structure tm holds time information in a more convenient way
  time(&now);                       // read the current time
  localtime_r(&now, &tm);           // update the structure tm with the current time
  static int previousMinute = 62;   // when was the timetable checked previously?
  //Serial.print(tm.tm_min); Serial.print(" "); Serial.println(previousMinute); delay(500);
  if (tm.tm_min != previousMinute) {
    previousMinute = tm.tm_min;
    Serial.println("checkTimetable");
    showTime();
    uint16_t timestamp =  tm.tm_hour * 100 + tm.tm_min; // make a HHMM timestamp for easier compare
    for (auto &t : timetable) {
      if (t.start == timestamp) {
        Serial.println(" found entry ");
        switch (t.action) {
          case ONCE :  bell[t.bell].ringOnce();  break;
          case TWICE : bell[t.bell].ringTwice(); break;
        }
        // I decided against a break to enable several entries for the same time
      }
    }
  }
}

// that is just for testing via Serial
void checkSerial(){
  switch (Serial.read()){
    case -1: break;
    case 'o': bell[JUNIOR].ringOnce(); break;
    case 't': bell[JUNIOR].ringTwice(); break;
    case '?': Serial.println(bell[JUNIOR].getState()); break;
  }
}

// just the output of the time in 12:00 and 24:00 format
void showTime() {
  time_t now;                       // this are the seconds since Epoch (1970) - UTC
  tm tm;                            // the structure tm holds time information in a more convenient way
  time(&now);                       // read the current time
  localtime_r(&now, &tm);           // update the structure tm with the current time
  char buffer[42] {0};              // a buffer large enough to hold your output
  strftime (buffer, sizeof(buffer), "%I:%M%p (%H:%M)", &tm);  // for different formats see https://cplusplus.com/reference/ctime/strftime/
  Serial.println(buffer);
}

void setup() {
  Serial.begin(115200);
  delay(200); // just give Serial some time to start
  Serial.println(F("\nESP NTP Schoolbell"));

#ifdef ARDUINO_ARCH_ESP32            // for the ESP32 (needs 3 lines)
  configTime(0, 0, MY_NTP_SERVER);   // 0, 0 because we will use TZ in the next line
  setenv("TZ", MY_TZ, 1);            // Set environment variable with your time zone
  tzset();
#endif
#ifdef ARDUINO_ARCH_ESP8266         // for the ESP8266
  configTime(MY_TZ, MY_NTP_SERVER); // the ESP8266 only needs this IMPORTANT ONE LINER in your sketch!
#endif

  // start network
  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);
  WiFi.begin(STASSID, STAPSK);
  while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print('.');
  }
  Serial.println(F("\nWiFi connected"));

  for (auto &b : bell) b.begin();   // start each bell
}

void loop() {
  uint32_t currentMillis = millis();
  checkTimetable();
  checkSerial();
  for (auto &b : bell) b.update(currentMillis);   // tick/run each bell
}
//

Just to make it clear, the "if weekdays" and the webserver part is not ready yet. First step should be that we have a clear understanding regarding your desired time control.

Hey thats not fair ( I modified the sketch too last night... sorry I couldn't resist ; - )
Replying my own question I think the two rings should be each 3 seconds with a 1 second delay in between

I guess I don't need to tell you the line which could be adopted :wink:

However imho it would be important that @jie_mags comes back to us, answers all open questions, and tries what is available currently.

  • The basic NTP sketch from @Wawa to see that NTP is working with the core functions without external libraries,
  • my sketch to see what's needed regarding the bells.

This is the sketch I propose:

  • as said removed the synchronous web server ( and used the asynchronous, non blocking one )
  • also removed the super long delays ( with long delays your sketch won't behave as expected )
  • removed the long if chain in the main loop
  • a table ( bell1Def ) defines the 'bell ringing events': start time, duration, repetition( add as many rows as needed )
  • a function ( checkTimerBell1 ) scan the table and 'fires' the right event
    As you are a student ( and have to study ; - ) the skecth is not complete 100%
  • you have to complete the bell1 event table ( bell1Def ), bell1 is the junior bell, see the notes
  • also you have to write the bell2 table, bell2 is the senior bell, see the notes
  • also you have to write the function checkTimerBell2, see the notes

NOTE
The following two libraries need to be installed:

  • ESPAsyncTCP
  • ESPAsyncWebServer
//
// 'Tubular Bells' aka PONTEVEDRA NHS SCHOOL BELL
//
// Bell(O)matic - automatic bell system
// 
// Derived from ESPAsyncWebServer library example:
// A simple http server implementation showing how to:
//  * serve static messages
//  * read GET and POST parameters
//  * handle missing pages / 404s
//

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>        // these 2 libraries are used for the asyncronous web server ( you need an asyncronous one )
#include <ESPAsyncWebServer.h>  //
//#include <NTPClient.h>
//#include <WiFiUdp.h>

AsyncWebServer server(80);

const char* ssid = "(v)";
const char* password = "PNHS@1969";
//const long utcOffsetInSeconds = 28800;

// insert here your homepage ( modify as needed )       
const char index_html[] PROGMEM = "<h1>PONTEVEDRA NHS AUTOMATED SCHOOL BELL</h1><br><h2>%TIME%</h2><br>BELL1 is: %BELL1%<br><a href='/?BELL1ON'><button style='background:green;color:white;font-size:30px;'>TURN ON</button></a><br><a href='/?BELL1OFF'><button style='background:red;color:white;font-size:30px;'>TURN OFF</button></a><br><br>BELL2 is: %BELL2%<br><a href='/?BELL2ON'><button style='background:green;color:white;font-size:30px;'>TURN ON</button></a><br><a href='/?BELL2OFF'><button style='background:red;color:white;font-size:30px;'>TURN OFF</button></a>";

//WiFiUDP ntpUDP;
//NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
time_t now; // epoch
tm tm;      // time struct

#define  BELL1PIN 16  // led connected to D0 ( junior bell )
#define  BELL2PIN 5   // D1                  ( senior bell )

#define DELAY_REPEAT 1000   // deley between repeated/multiple rings ( in milliseconds )

#define ON  1
#define OFF 0

int bell1 = OFF;
int bell2 = OFF;
unsigned long bell1OffTimer;
unsigned long bell2OffTimer;
unsigned long bell1Period;
unsigned long bell2Period;
int bell1Count = 0;
int bell2Count = 0;

#define TT_START 0
#define TT_PERIOD 1
#define TT_REPS 2
#define TT_COMPL 3

// the table is not complete, add missing lines after lunch...
// junior bell 'bell1' alarm table
// SSSS, P, R, 0
// SSSS = start time hhmm ( es: 730 for 7:30 )
// P = bell ringing period ( in seconds )
// R = repetitions
// 0 leave at 0
int bell1Def[][4] = {
      {  700,  8, 1, 0 },
      {  730,  8, 1, 0 },
      {  815,  8, 1, 0 },
      {  900,  8, 1, 0 },
      {  945,  8, 1, 0 },
      { 1015,  8, 1, 0 },
      { 1100,  8, 1, 0 },
      { 1145,  8, 1, 0 },
      { 1230,  8, 1, 0 },
      { 2400,  0, 0, 0 }  // ends the table ( leave it as is )
};

// here insert bell2Def table, uncomment the following lines and complete the table
/*int bell2Def[][4] = {
      {  700,  3, 2, 0 },
      {  730,  3, 2, 0 },
      { 2400,  0, 0, 0 }  // ends the table ( leave it as is )
};*/

void printBell1Info()
{
  Serial.print(millis());
  Serial.print(" / ");
  Serial.print(bell1OffTimer);
  Serial.print(" J ");
  Serial.print(bell1);
  Serial.print(" / ");
  Serial.println(bell1Count);
}


void printBell2Info()
{
  Serial.print(millis());
  Serial.print(" / ");
  Serial.print(bell2OffTimer);
  Serial.print(" S ");
  Serial.print(bell2);
  Serial.print(" / ");
  Serial.println(bell2Count);
}


// ring the bell for d milliseconds with reps repetitions
// to change output polarity change only the digitalWrite statement
// in the below 4 functions
void bell1ON(unsigned long d, int reps)
{
  digitalWrite(BELL1PIN, LOW);
  bell1 = ON;
  bell1OffTimer = millis() + d;
  bell1Period = d;
  bell1Count = reps;
  printBell1Info();
  Serial.println("Bell1: ON");
}


void bell1OFF()
{
  digitalWrite(BELL1PIN, HIGH);
  bell1 = OFF;
  printBell1Info();
  Serial.println("Bell1: OFF");
}


void bell2ON(unsigned long d, int reps)
{
  digitalWrite(BELL2PIN, LOW);
  bell2 = ON;
  bell2OffTimer = millis() + d;
  bell2Period = d;
  bell2Count = reps;
  printBell2Info();
  Serial.println("Bell2: ON");
}


void bell2OFF()
{
  digitalWrite(BELL2PIN, HIGH);
  bell2 = OFF;
  printBell2Info();
  Serial.println("Bell2: OFF");
}


// check if bell1 should start ringing
// you have to define a similar function for bell2
// copy this rename to checkTimerBell1 and substitute all references from bell1 to bell2
int checkTimerBell1(int hh, int mm)
{
int i = 0;
int t = hh*100+mm;

while ( bell1Def[i][TT_PERIOD] > 0 )
  {
  if (bell1Def[i][TT_START] < t) {bell1Def[i][TT_COMPL] = 0; i++;}
  if (bell1Def[i][TT_START] > t) {bell1Def[i][TT_COMPL] = 0; return 0;}
  if (bell1Def[i][TT_START] == t) 
    {
    if (bell1Def[i][TT_COMPL] == 0) 
      {
      bell1ON(bell1Def[i][TT_PERIOD]*1000,bell1Def[i][TT_REPS]); 
      bell1Def[i][TT_COMPL] = 1;
      return 1;
      }
    return 0;
    }
  }
return 0;
}


// make substitutions in index_html page
// all strings starting and ending with% will be passed here for substitution
String processor(const String& var)
{
  if(var == "BELL1") return bell1==ON?"ON":"OFF";
  if(var == "BELL2") return bell2==ON?"ON":"OFF";
  if(var == "TIME")  time(&now); return ctime(&now);
  return String();
}

void notFound(AsyncWebServerRequest *request) {
    request->send(404, "text/plain", "Not found");
}




void setup() 
    {
    Serial.begin(115200);
    //delay(500);
    pinMode(BELL1PIN, OUTPUT);
    pinMode(BELL2PIN, OUTPUT);
    bell1OFF();
    bell2OFF();

    Serial.print("Connecting to Internet ");
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
        Serial.printf("WiFi Failed!\n");
        return;
    }

    Serial.print("IP Address: ");
    Serial.println(WiFi.localIP());

    // ntp
    //timeClient.begin();
    configTime("PST-8", "pool.ntp.org"); // example: Manila
    //configTime("CET-1CEST", "pool.ntp.org"); // example: Rome

    // pages 'served' by http server
    server.on("/help", HTTP_GET, [](AsyncWebServerRequest *request){
        request->send(200, "text/plain", "This is an help page...");
    });

    // Send a GET request to <IP>/?XXXX> where XXXX could be: BELL1ON BELL1OFF BELL2ON BELL2OFF
    server.on("/", HTTP_GET, [] (AsyncWebServerRequest *request) {
        String msg;
        if (request->hasParam("BELL1ON")) 
            {bell1ON(8000,1);} 
        else if (request->hasParam("BELL1OFF")) 
            {bell1OFF(); bell1Count = 0;}
        else if (request->hasParam("BELL2ON")) 
            {bell2ON(3000,2);}
        else if (request->hasParam("BELL2OFF")) 
            {bell2OFF(); bell2Count = 0;}
        request->send_P(200, "text/html", index_html, processor);
    });

    server.onNotFound(notFound);

    server.begin();
}

void loop() 
  {
  static unsigned long counter = 0;
    
  /*timeClient.update();
  int dd = timeClient.getDay();
  int hh = timeClient.getHours();
  int mm = timeClient.getMinutes();
  int ss = timeClient.getSeconds();*/
  time(&now); // now
  localtime_r(&now, &tm); // add local time (with DST)
  int dd = tm.tm_wday;
  int hh = tm.tm_hour;
  int mm = tm.tm_min;
  int ss = tm.tm_sec;


  // prints on the serial debug every 10 cycles
  if (counter==0)
    {
    Serial.print(dd);
    Serial.print(" ");
    Serial.print(hh);
    Serial.print(":");
    Serial.print(mm);
    Serial.print(":");
    Serial.println(ss);
    //Serial.println(timeClient.getFormattedTime());
    }
  // check if bell1 should start ringing ( duplicate for bell2 changing any reference from bell1 to bell2 )
  if ( (dd >= 1) && (dd <= 5) && checkTimerBell1(hh,mm) )
    {printBell1Info();}
  // turn off bells
  if ( (bell1) && (millis() > bell1OffTimer) ) {bell1OFF(); bell1Count--; bell1OffTimer += DELAY_REPEAT;}
  if ( (bell2) && (millis() > bell2OffTimer) ) {bell2OFF(); bell2Count--; bell2OffTimer += DELAY_REPEAT;}
  // repeated rings
  if ( (bell1Count>0) && (!bell1) && (millis() > bell1OffTimer) ) {bell1ON(bell1Period, bell1Count);}
  if ( (bell2Count>0) && (!bell2) && (millis() > bell2OffTimer) ) {bell2ON(bell2Period, bell2Count);}

  delay(500); // small delay, main loop does noting
  counter = (counter+1) % 10;   // count from 0 to 9
  }

Here is an image of the html page ( the template is in index_html string, you can modify )

@jie_mags

are you still following your thread?

If you were able to solve the problem please mark one answer as solution and say thank you to posts which help to solve the problem by pressing the like/heart under the resp. post.

If you still have questions it would be smart to ask.

Thank you, I will try this after class hour..