/* This is the code that makes the Ferry clock work. It has two inputs: - Time, using an NTP time server - Radar, based on doppler module attached to pin 34 It has nine LED outputs, of which three are in parallel. So that makes a total of seven outputs. The goal is to make sure that the LED's turn only on when a person is near and that the LED's indicate the time to leave to the person looking at the clock. If it is time to leave, depends on the predefined times/events that are of importance to the person (school, work times or public transport departures) You are welcome to study, copy, modify and use the code in any possible way that suits you. But don't come to me if something breaks... Douwe This sketch depends heavily on the work of Floris Wouterlood and Rui Santos: https://raw.githubusercontent.com/RuiSantosdotme */ // Wifi and time // Include the needed libraries to use wifi #include #include "time.h" // Insert the credentials you need to connect to wifi const char* ssid = "WaagGuest"; const char* password = "guest@waag"; // Get the time from a timeserver and modify it so it matches local time and DST const char* ntpServer = "pool.ntp.org"; const long gmtOffset_sec = 3600; const int daylightOffset_sec = 3600; // Here we define the pins we are going to use. #define lGreen2 34 #define lGreen3 35 #define lGreen1 32 #define lOrange1 33 #define lOrange2 25 #define lOrange3 26 #define lRed 27 // Define input pin of the radar and what it should do. #define sensorPin 15 // RCWL-0516 connected to this pin on my ESP32 int sensorVal = 0; // initial RCWL-0516 output value as seen by ESP32 int led_fire_duration = 20000; // this is how long the LED's will stay on after movement is detected. void setup(){ Serial.begin(115200); // Connect to Wi-Fi and confirm when there is connection Serial.printf("Connecting to %s ", ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(" CONNECTED"); // Get the time configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); printLocalTime(); //disconnect WiFi as it's no longer needed WiFi.disconnect(true); WiFi.mode(WIFI_OFF); // initialize digital pins as LED output. pinMode(lGreen1, OUTPUT); digitalWrite (lGreen1, LOW); // turn LED's off at start pinMode(lOrange1, OUTPUT); digitalWrite (lOrange1, LOW); // LED off at start pinMode(lGreen2, OUTPUT); digitalWrite (lGreen2, LOW); // LED off at start pinMode(lOrange2, OUTPUT); digitalWrite (lOrange2, LOW); // LED off at start pinMode(lGreen3, OUTPUT); digitalWrite (lGreen3, LOW); // LED off at start pinMode(lOrange3, OUTPUT); digitalWrite (lOrange3, LOW); // LED off at start pinMode(lRed, OUTPUT); digitalWrite (lRed, LOW); // LED off at start // initialize digital pins as radar input pinMode (sensorPin, INPUT); // RCWL-0516 output is input for esp32 } // This is the part that make it really work. void printLocalTime(){ struct tm timeinfo; if(!getLocalTime(&timeinfo)){ Serial.println("Failed to obtain time"); // this sometimes happens. Restart the ESP32 when it does. return; } Serial.println("Current time"); char timeHour[3]; strftime(timeHour,3, "%H", &timeinfo); Serial.print(timeHour); Serial.print(":"); char timeMinute[3]; strftime(timeMinute,5, "%M", &timeinfo); Serial.println(timeMinute); int hourInt = atoi(timeHour); int minuteInt = atoi(timeMinute); //int weekdayInt = atoi(timeWeekDay); if (hourInt > 5 & hourInt <= 17 & ( minuteInt == 1 | minuteInt == 2 | minuteInt == 8 | minuteInt == 9 | minuteInt == 10 | minuteInt == 16 | minuteInt == 17 | minuteInt == 23 | minuteInt == 24 | minuteInt == 25 | minuteInt == 31 | minuteInt == 32 | minuteInt == 38 | minuteInt == 39 | minuteInt == 40 | minuteInt == 46 | minuteInt == 47 | minuteInt == 53 | minuteInt == 54 | minuteInt == 55 ) ) { digitalWrite(lGreen1, HIGH); digitalWrite(lOrange2, HIGH); digitalWrite(lOrange1, HIGH); digitalWrite(lOrange3, HIGH); digitalWrite(lRed, HIGH); Serial.println ("All the time"); } else if(hourInt > 5 & hourInt <= 17 & ( minuteInt == 3 | minuteInt == 11 | minuteInt == 18 | minuteInt == 26 | minuteInt == 33 | minuteInt == 41 | minuteInt == 48 | minuteInt == 56 ) ) { digitalWrite(lGreen1, LOW); digitalWrite(lOrange1, HIGH); digitalWrite(lOrange2, HIGH); digitalWrite(lOrange3, HIGH); digitalWrite(lRed, HIGH); Serial.println ("Get ready"); } else if(hourInt > 5 & hourInt <= 17 & ( minuteInt == 4 | minuteInt == 12 | minuteInt == 19 | minuteInt == 27 | minuteInt == 34 | minuteInt == 42 | minuteInt == 49 | minuteInt == 57 ) ) { digitalWrite(lGreen1, LOW); digitalWrite(lOrange1, LOW); digitalWrite(lOrange2, HIGH); digitalWrite(lOrange3, HIGH); digitalWrite(lRed, HIGH); Serial.println ("Really time to go"); } else if(hourInt > 5 & hourInt <= 17 & ( minuteInt == 5 | minuteInt == 13 | minuteInt == 20 | minuteInt == 28 | minuteInt == 35 | minuteInt == 43 | minuteInt == 50 | minuteInt == 58 ) ) { digitalWrite(lGreen1, LOW); digitalWrite(lOrange1, LOW); digitalWrite(lOrange2, LOW); digitalWrite(lOrange3, HIGH); digitalWrite(lRed, HIGH); Serial.println ("Hurry!"); } else if (hourInt > 5 & hourInt <= 17 & ( minuteInt == 6 | minuteInt == 7 | minuteInt == 14 | minuteInt == 15 | minuteInt == 21 | minuteInt == 22 | minuteInt == 29 | minuteInt == 30 | minuteInt == 36 | minuteInt == 37 | minuteInt == 44 | minuteInt == 45 | minuteInt == 51 | minuteInt == 52 | minuteInt == 59 | minuteInt == 0 ) ) { digitalWrite(lGreen1, LOW); digitalWrite(lOrange1, LOW); digitalWrite(lOrange2, LOW); digitalWrite(lOrange3, LOW); digitalWrite(lRed, HIGH); Serial.println ("Too Late"); } /// After 6pm the ferries go less often. else if (hourInt > 17 & hourInt <= 23 & ( minuteInt == 1 | minuteInt == 2 | minuteInt == 3 | minuteInt == 4 | minuteInt == 5 | minuteInt == 6 | minuteInt == 7 | minuteInt == 8 | minuteInt == 9 | minuteInt == 10 | minuteInt == 16 | minuteInt == 17 | minuteInt == 18 | minuteInt == 19 | minuteInt == 21 | minuteInt == 22 | minuteInt == 23 | minuteInt == 24 | minuteInt == 25 | minuteInt == 31 | minuteInt == 32 | minuteInt == 33 | minuteInt == 34 | minuteInt == 35 | minuteInt == 36 | minuteInt == 37 | minuteInt == 38 | minuteInt == 39 | minuteInt == 40 | minuteInt == 46 | minuteInt == 47 | minuteInt == 48 | minuteInt == 49 | minuteInt == 50 | minuteInt == 51 | minuteInt == 52 | minuteInt == 53 | minuteInt == 54 | minuteInt == 55 ) ) { digitalWrite(lGreen1, HIGH); digitalWrite(lOrange2, HIGH); digitalWrite(lOrange1, HIGH); digitalWrite(lOrange3, HIGH); digitalWrite(lRed, HIGH); Serial.println ("All the time"); } else if (hourInt > 17 & hourInt <= 23 & ( minuteInt == 11 | minuteInt == 26 | minuteInt == 41 | minuteInt == 56 ) ) { digitalWrite(lGreen1, LOW); digitalWrite(lOrange1, HIGH); digitalWrite(lOrange2, HIGH); digitalWrite(lOrange3, HIGH); digitalWrite(lRed, HIGH); Serial.println ("Get ready"); } else if(hourInt > 17 & hourInt <= 23 & ( minuteInt == 12 | minuteInt == 27 | minuteInt == 42 | minuteInt == 57 ) ) { digitalWrite(lGreen1, LOW); digitalWrite(lOrange1, LOW); digitalWrite(lOrange2, HIGH); digitalWrite(lOrange3, HIGH); digitalWrite(lRed, HIGH); Serial.println ("Really time to go"); } else if(hourInt > 17 & hourInt <= 23 & ( minuteInt == 13 | minuteInt == 28 | minuteInt == 43 | minuteInt == 58 ) ) { digitalWrite(lGreen1, LOW); digitalWrite(lOrange1, LOW); digitalWrite(lOrange2, LOW); digitalWrite(lOrange3, HIGH); digitalWrite(lRed, HIGH); Serial.println ("Hurry!"); } else if (hourInt > 17 & hourInt <= 23 & ( minuteInt == 14 | minuteInt == 15 | minuteInt == 29 | minuteInt == 30 | minuteInt == 44 | minuteInt == 45 | minuteInt == 59 | minuteInt == 0 ) ) { digitalWrite(lGreen1, LOW); digitalWrite(lOrange1, LOW); digitalWrite(lOrange2, LOW); digitalWrite(lOrange3, LOW); digitalWrite(lRed, HIGH); Serial.println ("Too Late"); } else { digitalWrite(lGreen1, LOW); digitalWrite(lOrange1, LOW); digitalWrite(lOrange3, LOW); digitalWrite(lRed, LOW); Serial.println ("No ferry at the moment"); } } void loop(){ sensorVal = digitalRead (sensorPin); // read sensor value if (sensorVal == LOW) { Serial.println ("No one here..."); digitalWrite (lRed, LOW); // LED's off when no one is here digitalWrite (lGreen1, LOW); digitalWrite (lGreen2, LOW); digitalWrite (lGreen3, LOW); digitalWrite (lOrange1, LOW); digitalWrite (lOrange2, LOW); digitalWrite (lOrange3, LOW); delay (200); } else { Serial.println ("Somebody is here!"); printLocalTime(); delay (led_fire_duration); // during this period the led will be ON } }