fg71
I asked ChatGPT to give me Schedule Timer in Function Block Structured Text.
FUNCTION_BLOCK FB_ScheduleTimer
VAR_INPUT
    StartDateTime: DT;    // Start date and time
    Duration: TIME;       // Duration to keep the output on
    CurrentDateTime: DT;  // Current date and time
END_VAR
VAR_OUTPUT
    TimerOutput: BOOL;    // Output of the timer
END_VAR
VAR
    EndDateTime: DT;      // End date and time
    TimerStarted: BOOL;   // Flag indicating if the timer has started
END_VAR

// Initialize the end date and time
IF NOT TimerStarted THEN
    EndDateTime := StartDateTime + Duration;
    TimerStarted := TRUE;
END_IF

// Check if the current date and time is within the scheduled period
IF CurrentDateTime >= StartDateTime AND CurrentDateTime < EndDateTime THEN
    TimerOutput := TRUE;
ELSE
    TimerOutput := FALSE;
    TimerStarted := FALSE; // Reset the timer for next activation
END_IF
END_FUNCTION_BLOCK

PROGRAM Main
VAR
    ScheduleTimer: FB_ScheduleTimer; // Instance of the schedule timer function block
    CurrentTime: DT;                 // Current date and time
    OutputSignal: BOOL;              // Output signal from the timer
END_VAR

// Assume CurrentTime is being updated regularly by an RTC or system clock

// Call the schedule timer function block with example start date/time and duration
ScheduleTimer(
    StartDateTime := DT#2024-11-14-15:00:00,
    Duration := T#2h,
    CurrentDateTime := CurrentTime
);

OutputSignal := ScheduleTimer.TimerOutput;
 
I compare my implementation from plc.st file that look like this. (They are the same)
Is the data type DT = current time ??
 
I couldn't get it to work 🙁
 
TYPE
  LOGLEVEL : (CRITICAL, WARNING, INFO, DEBUG) := INFO;
END_TYPE

FUNCTION_BLOCK LOGGER
  VAR_INPUT
    TRIG : BOOL;
    MSG : STRING;
    LEVEL : LOGLEVEL := INFO;
  END_VAR
  VAR
    TRIG0 : BOOL;
  END_VAR

  IF TRIG AND NOT TRIG0 THEN
  {{
   LogMessage(GetFbVar(LEVEL),(char*)GetFbVar(MSG, .body),GetFbVar(MSG, .len));
  }}
  END_IF;
  TRIG0:=TRIG;
END_FUNCTION_BLOCK


FUNCTION_BLOCK FB_ScheduleTimer
  VAR_INPUT
    StartDateTime : DT;
    Duration : TIME;
    CurrentDateTime : DT;
  END_VAR
  VAR_OUTPUT
    TimerOutput : BOOL;
  END_VAR
  VAR
    EndDateTime : DT;
    TimerStarted : BOOL;
  END_VAR

  (* Initialize the end date and time *)
  IF NOT TimerStarted THEN
    EndDateTime := StartDateTime + Duration;
    TimerStarted := TRUE;
  END_IF;

  (* Check if the current date and time is within the scheduled period *)
  IF CurrentDateTime >= StartDateTime AND CurrentDateTime < EndDateTime THEN
    TimerOutput := TRUE;
  ELSE
    TimerOutput := FALSE;
    TimerStarted := FALSE; (* Reset the timer for next activation *)
  END_IF;
END_FUNCTION_BLOCK

PROGRAM main
  VAR
    ScheduleTimer : FB_ScheduleTimer;
    CurrentTime : DT;
  END_VAR
  VAR
    OutputSignal AT %QX0.3 : BOOL;
  END_VAR

  ScheduleTimer(StartDateTime := DT#2024-11-14-16:10:00, Duration := T#1h, CurrentDateTime := CurrentTime);
  OutputSignal := ScheduleTimer.TimerOutput;
END_PROGRAM


CONFIGURATION Config0

  RESOURCE Res0 ON PLC
    TASK task0(INTERVAL := T#20ms,PRIORITY := 0);
    PROGRAM instance0 WITH task0 : main;
  END_RESOURCE
END_CONFIGURATION
 
 
Reply 0 0
fg71
Its looks like the current date is this
[203128%2F9069961%2Fmceclip0]
 
The idea is to set pin 13 to HIGH with Schedule Time, have anyone done this before in OpenPLC ??
 
[203128%2F9069972%2Fmceclip0]
Reply 0 0
thiagoralves
CurrentTime is initialized as zero (which is 1970 in EPOCH timestamp). Unless you find a way to initialize CurrentTime to the correct time, your block won't work. OpenPLC provides the RTC block which can count time, but that also needs to be initialized properly with the correct time. For the runtime running on Windows or Linux, you can use the special %ML locations to retrieve the system time. For Arduino boards, there isn't a way to do this since they don't have any RTC hardware to keep track of time.
Reply 0 0
fg71
I will get back to you if I find a solution.
Reply 0 0
fg71
Okey I got the time correct with NTPClient in Arduino IDE, I used Arduino Nano Esp32
Maybe this could be used in OpenPLC, have to think more 😉
A interesting idea would be to connect to NTP server get the time and set a current time variable.
And compare the current time variable with a set time variable.
Custom data type inside OpenPLC, but to make is to work a user need to configure it with a client approach
The only thing I do in Arduino IDE is to make sure the board have access to my network include internet.
 
Here is the code
#include <NTPClient.h>
// change next line to use with another board/shield
#include <WiFi.h>
//#include <WiFi.h> // for WiFi shield
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
#include <WiFiUdp.h>

const char *ssid     = "<ssid>";
const char *password = "<password>";

WiFiUDP ntpUDP;

// By default 'pool.ntp.org' is used with 60 seconds update interval and
// no offset
// NTPClient timeClient(ntpUDP);

// You can specify the time server pool and the offset, (in seconds)
// additionally you can specify the update interval (in milliseconds).
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);

void setup(){
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }

  timeClient.begin();
}

void loop() {
  timeClient.update();

  Serial.println(timeClient.getFormattedTime());

  delay(1000);
}
 
Reply 0 0
fg71
Okey I find one, back to so maybe I could get it to work after all, what do you think?
 
#include <WiFi.h>
#include "time.h"

// Replace with your network credentials
const char* ssid = "<ssid>";
const char* password = "<password>";

// NTP server to request epoch time
const char* ntpServer = "europe.pool.ntp.org";

// Variable to save current epoch time
unsigned long epochTime;

// Function that gets current epoch time
unsigned long getTime() {
  time_t now;
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    //Serial.println("Failed to obtain time");
    return(0);
  }
  time(&now);
  return now;
}

// Initialize WiFi
void initWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(115200);
  initWiFi();
  configTime(0, 0, ntpServer);
}

void loop() {
  epochTime = getTime();
  Serial.print("Epoch Time: ");
  Serial.println(epochTime);
  delay(1000);
}
 
Reply 0 0
fg71
If I compare this command in raspberry pi
-> date +%s
 
with
 
The Arduino code, it give me the same number. The header time.h didn't do anything so I remove it.
Reply 0 0
thiagoralves
You could use this code on an Arduino Extension just to get the right time from NTP server (put it on setup instead of loop to get it only once) and then use that to feed the initial time for the RTC block on the PLC side.
Reply 0 0
fg71
I would look at that...
Do you know the data type I should use the code is unsigned long so I guess it would be UDINT, correct ??
Reply 0 0
thiagoralves
You can use ULINT to be sure - that's the largest integer available on the platform. Then you will have to convert that to DT (datetime) for you to use it with the RTC block:
Screenshot 2024-11-14 at 1.36.24 PM.png
 
You can do this using the conversion block ULINT_TO_DT. This is a sample code where I set my "epoch" ULINT to 1731609527 and then get today's date and time on the other side:
 
Screenshot 2024-11-14 at 1.40.04 PM.png
 
With the date and time set on a DT variable, all you have to do is connect that variable to PDT and have a rising edge on the IN input of the RTC block. The RTC will remain counting time like a clock for as long as IN is true. RTC clock is reset on the falling edge of IN.
Reply 0 0
thiagoralves
This sounded like a fun project to do on a quick break. Works like a charm on my Arduino Nano 33 IoT board. Should run on any other board that has WiFi as well. Here is my implementation:
Screenshot 2024-11-14 at 2.48.41 PM.png
 
Screenshot 2024-11-14 at 2.49.11 PM.png
 
Full project is attached on this post for anyone that wants to try it.
Reply 1 0
fg71
Do you mean something like this ??
 
[203128%2F9070230%2Fmceclip0]
Reply 0 0
fg71
I tested your script and the time is short with 1 hour. Here in Sweden we have +1, and I believe Brazil have +5
Thanks for taking time to look at it, it gives me ideas how to make it with the schedule function block.
Reply 0 0
thiagoralves
You need to adjust to your time zone appropriately (see second rung on the ladder diagram). I’m not in Brazil, I’m in the U.S. my time zone is GMT-5, so I need to reduce 5 hours from the UTC time received from the NTP server.
Reply 0 0
fg71
Did your example also include correct date?, here on my computer is wrong. Maybe I have to take a more deeper lock what you have done.
 
I know this.
I send upload 2 Arduino sketch one that return only the time. That was the version that used #include <NTPClient.h>
This script return human HH:mm:SS In other words 24 hours, 60 minutes and 60 seconds
The second sketch return rtc time.
 
You decide to use the library from the first sketch, I'm little confused. 😉 maybe you are using other method from that library.
Reply 0 0
Reply