Hello,
I am very new to programming just to start.
I have an Ardiuno UNO WiFi ver2.
I am trying to set relays to turn on or off depending on a time. I learned the ardiuno has terrible time keeping abilities. So I have found this library and added it my repositories: TimeLib.h
I have a bit of code that does work when uploaded to the Aduino. It Automatically hooks up to the wifi network and then starts a time keeping function. I then spits out periodically about network info and the time.
What I am trying to do use the set time and turn a relay on or off depending on the time. Something that would turn the relay on between 8:00 - 20:00 and then turn off. What sort of if then statement would I have with the code as is.
Here is the copied together code:
/*
This example connects to an unencrypted Wifi network.
Then it prints the MAC address of the Wifi module,
the IP address obtained, and other network details.
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
*/
#include <SPI.h>
#include <WiFiNINA.h>
#include <TimeLib.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the Wifi radio's status
int in1 = 1;
int in2 = 2;
int in3 = 3;
int in4 = 4;
int in5 = 5;
bool newSecondFireFlag = false; // rtc new second fire flag
unsigned long pitCounter = 0; // pit counter
void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(in5, OUTPUT);
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, HIGH);
digitalWrite(in5, HIGH);
InternalRTC.attachClockInterrupt(rtc_irq); // set user IRQ function fired each new second by internal RTC
// run only on megaAVR-0 series!
// note : use InternalRTC.detachClockInterrupt() for detach IRQ function
InternalRTC.attachInterrupt(pit_irq, 16); // set user IRQ function fired by internal periodic interrupt (PIT)
// (1Hz by defaut, max 8192Hz, only power of 2 number)
// run only on megaAVR-0 series!
// note : use InternalRTC.detachInterrupt() for detach IRQ function
setTime(4, 16, 00, 30, 07, 2020); // set the current system timestamp from arbitrary date/time
// the next second was fire exactly 1 second after the call at this function
Serial.begin(9600); // init serial
pinMode(LED_BUILTIN, OUTPUT); // init built-in led
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
}
void pit_irq() { // executed all pediodic interrupt
pitCounter++; // increment pit counter
}
void rtc_irq() { // executed at each RTC second interrupt
newSecondFireFlag = true; // fire flag
}
void loop() {
if ( newSecondFireFlag ) { // make actions if flag is fired:
newSecondFireFlag = false; // unfire flag
unsigned long actualTime = now(); // get actual system timestamp
// display current system date and time:
Serial.print(year(actualTime));
Serial.print("-");
Serial.print(month(actualTime));
Serial.print("-");
Serial.print(day(actualTime));
Serial.print(" ");
Serial.print(hour(actualTime));
Serial.print(":");
Serial.print(minute(actualTime));
Serial.print(":");
Serial.print(second(actualTime));
// display PIT counter at the end of line:
Serial.print(" -> ");
Serial.print(pitCounter);
Serial.println(" PIT cycles.");
// blink led:
digitalWrite(LED_BUILTIN, HIGH);
delay(50);
digitalWrite(LED_BUILTIN, LOW);
// Relay One
}
// check the network connection once every 10 seconds:
delay(10000);
printCurrentNet();
}
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
void printMacAddress(byte mac[]) {
for (int i = 5; i >= 0; i--) {
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
if (i > 0) {
Serial.print(":");
}
}
Serial.println();
}
Also, I am totally new to programming so if anybody has any suggestion for a good launching point for learning C++
Thank You Very Much For Your Time