////////////////////////// NOTES ON PROJECT: //////////////////////////
// THINGS TO ADD:
// Add periodic time sync. This will be for "heartbeat" to codesys PLC
// Add snooping feature to see which devices are on network. Better in ESP or RPI?
// Touch screen features
// Scheduling features
///////////////////////////////////////////////////////////////////////
// Libraries
#include <WiFi.h>
#include "time.h"
//-------WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//-------NPT Server
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = -28800;
const int daylightOffset_sec = 3600;
// Variables
// Physical buttons
const byte tempUp = 34; // count up push button input
const byte tempDwn = 35; // count down bush button input
const byte ledPin = 26; // led out
// Debounce function variables
byte buttonState1 = 0;
byte buttonState2 = 0;
byte lastButtonState1 = 0;
byte lastButtonState2 = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
// Count function variables
//byte countTempUp = 0;
byte tempUpCntr = 0;
byte tempDwnCntr = 0;
//int i = 0;
unsigned long countDelayTime = 5000;
unsigned long countDelayStart = 0;
byte localSP = 63;
//==================================================Debounce function
bool debounce (byte debounceIn){
// read the state of the switch into a local variable:
byte reading = digitalRead(debounceIn);
// tempUp
if (debounceIn == 34){
if (reading != lastButtonState1) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState1) {
buttonState1 = reading;
//Serial.println("button state changed");
if (buttonState1 == 1) {
lastButtonState1 = reading;
//Serial.println(buttonState1);
return(HIGH);
}
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState1 = reading;
return(LOW);
}
// tempDwn
else if(debounceIn == 35){
if (reading != lastButtonState2) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState2) {
buttonState2 = reading;
//Serial.println("button state changed");
if (buttonState2 == 1) {
lastButtonState2 = reading;
//Serial.println("the button is high");
return(HIGH);
}
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState2 = reading;
return(LOW);
}
} // End debounce
//==================================================Count function
void count(bool countIn1, bool countIn2){
//byte count(bool countIn1, bool countIn2) { // the input to this would be the output of an instance of debounce...
//since localSP is a global variable, this will be a void function
//Count number of times Up button is pressed
if (countIn1) {
countDelayStart = millis();
tempUpCntr++; //Reset below
if (tempUpCntr >= 2 && localSP < 80){ //set max localSP here
localSP += 1;
//for (int i = 0; i <= tempUpCntr; i++) { // need to reset i below
//localSP = 63 + i;
//}
Serial.print("Local SP is:");
Serial.println(localSP);
}
Serial.println(tempUpCntr);
}
//Count number of times Dwn button is pressed
if (countIn2){
countDelayStart = millis();
tempDwnCntr++;
if (tempDwnCntr >= 2 && localSP > 50){ //set min localSP here
localSP -= 1;
Serial.print("Local SP is:");
Serial.println(localSP);
}
Serial.println(tempDwnCntr);
}
// After timer expires, reset variables
if((tempUpCntr != 0 || tempDwnCntr != 0) && (millis() - countDelayStart) >= countDelayTime){
Serial.println("Count Finished");
countDelayStart += countDelayTime; // prevents drift in the delays
tempUpCntr = 0;
tempDwnCntr = 0;
}
} // End count
//==================================================printLocalTime function
void printLocalTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
Serial.print("Day of week: ");
Serial.println(&timeinfo, "%A");
Serial.print("Month: ");
Serial.println(&timeinfo, "%B");
Serial.print("Day of Month: ");
Serial.println(&timeinfo, "%d");
Serial.print("Year: ");
Serial.println(&timeinfo, "%Y");
Serial.print("Hour: ");
Serial.println(&timeinfo, "%H");
Serial.print("Hour (12 hour format): ");
Serial.println(&timeinfo, "%I");
Serial.print("Minute: ");
Serial.println(&timeinfo, "%M");
Serial.print("Second: ");
Serial.println(&timeinfo, "%S");
Serial.println("Time variables");
char timeHour[3];
strftime(timeHour,3, "%H", &timeinfo);
Serial.println(timeHour);
char timeWeekDay[10];
strftime(timeWeekDay,10, "%A", &timeinfo);
Serial.println(timeWeekDay);
Serial.println();
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// Physical button assignments
pinMode(tempUp, INPUT);
pinMode(tempDwn, INPUT);
pinMode(ledPin, OUTPUT);
// Connect to Wi-Fi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
// Init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
//disconnect WiFi as it's no longer needed <-- remove this in final project
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
} //End Void Setup
// ===============================================================================
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
// ===============================================================================
count(debounce(tempUp), debounce(tempDwn));
// the following code does not capture the output of debounce if it follows the above line calling the count function.
// may need to feed the output of debounce into a dedicated fuction that both turns on the LED and performs counts.
if (debounce(tempUp) || debounce(tempDwn)) {
//Serial.println(debounce(tempUp));
digitalWrite(ledPin, HIGH);
//Serial.println("debounce worked, the button is high")
}
else {
digitalWrite(ledPin, LOW);
}
// ===============================================================================
// ===============================================================================
} // End void loop