Dependancies
Pre-requisites
Buy the components
ntp-time-arduino-esp32c3.ino
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "Secret.h"
// char ssid[] = "secret";
// char pass[] = "secret";
// User defined parameters
const long timeZoneOffset = 28800; // GMT+8 (8 hours * 60 minutes * 60 seconds)
#define START_TIME 9 // 9am or 0900h
#define END_TIME 21 // 9pm or 2100h
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
int currentHour;
void setup() {
// (Optional) Press reset button
// on the dev board to see these print statements
Serial.begin(115200);
while (!Serial) { }
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.useStaticBuffers(true);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to WiFi");
timeClient.begin();
timeClient.setTimeOffset(timeZoneOffset);
currentHour = (timeClient.getHours() + (timeZoneOffset / 3600)) % 24;
}
void loop() {
timeClient.update();
Serial.println(timeClient.getFormattedTime());
if (currentHour >= END_TIME || currentHour < START_TIME) {
Serial.println("It's currently between 9 PM and 9 AM.");
} else {
Serial.println("It's currently outside the specified time range.");
}
delay(1000);
}
BOARD?=esp32:esp32:esp32c3
PORT?=/dev/cu.usbserial-*
BUILD=build
## Pass in V=--verbose to output more information than default
.PHONY: help default lint compile upload clean
default: clean lint compile upload
help: ## Show help message
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[$$()% 0-9a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
lint: ## Lint code using cpplint
cpplint --extensions=ino --filter=-legal/copyright,-runtime/int *.ino
compile: ## Compile code and create the firmware binary
arduino-cli compile $(V) --fqbn $(BOARD) --output-dir $(BUILD) ./
upload: ## Upload the firmware to the board
arduino-cli upload $(V) --fqbn $(BOARD) --port $(PORT) --input-dir $(BUILD)
clean: ## Remove all built files
rm -rf build
Time example will print NTP time with timezone offset timeZoneOffset
. It will also check if the time is within specified hours.