Set up scheduled program cycles #24
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "program-cycle"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
All the keywords in the title, I guess.
Closes #8.
If using ardino-timer (https://github.com/contrem/arduino-timer), how to set this up?
subtract current time from start time in millisecondsRTC built-in alarmtimer.at(time, function_to_call);
(with time in milliseconds?)yourInputSend
. - #37function should then calltimer.every(interval, function_to_call);
and expirethetimer.every()
function is probably a wrapper to update the variables that go intomorseToSend
(and `morseToSend_blink).how long thetimer.every()
interval should behow many times to JLedRepeat()
send to fill the step lengthHTML field is datetime-local, which needs to be labeled as UTC.
https://www.w3schools.com/tags/att_input_type_datetime-local.asp
Comes out as
2023-09-02T05:34
format.Can I use JS to convert this to UTC?
I can get the submitted unix time copied into a file/variable, but it might be local time rather than UTC?
Maybe I'll go back and make it use local time everywhere.
Huh,
Date.now()
gives UTC. Maybe I can use TZ offset on one of the unix timestamps to get where I want to be. https://stackoverflow.com/a/61082536/2152245Holy buckets do js missing brackets break everything.
Oh no, DST. The time zone adjustment doesn't know about it. See #26.
All back on local time now. The current time is saved with an epoch shift according to the timezone offset in JS. The start program time is local by default from the HTML form. Both are stored as unix timestamps on the C++ side.
It's not horrible. I'd like the two time formats to match.
When I try to format the date differently than
return rtc.now().timestamp();
, liketoString()
, the device crashes.In any case, this is probably the way to go:
This message when you submit an empty date also isn't horrible, but I'd rather store the existing date until a new one is set.
Way overthinking this. Time is coming from browser in the first place. If I want to print it, print it from the browser, don't pull it from the controller.
Wow did I ever get messed up there.
If the start date isn't set, a value isn't added to the JS, so it just sees
s = ;
and breaks everything else in the script. Which means you can't put in a new value from the webform.Right now there is a hardcoded date in JS, but that needs to be fixed.
Did a Full Clean/Build Filesystem Image/Upload Filesystem Image/Upload and Monitor.
The empty start time/broken JS seems to be fixed by
b97f48858d
.Back to the double submit problem.
No luck on double submit, but I managed to
Ooooh, so close. Going to attack #15 now.
Super rad. https://stackoverflow.com/a/48307807
Now. Now maybe I can get to scheduling.
OK, so it looks like arduino-timer
.at()
uses themillis()
clock. Are there timers that use real time (like stored in RTC)?Or should I be using RTC alarms? https://garrysblog.com/2020/07/05/using-the-ds3231-real-time-clock-alarm-with-the-adafruit-rtclib-library/
https://www.arduino.cc/reference/en/libraries/category/timing/
RTC alarms seem to fire at the next opportunity, even if they happened when ESP32 was off. This is a problem I wanted to avoid.
Reset the alarm in
setup()
if it's in the past?I'm getting weird dates again. I will look at the hardware connections tomorrow. May be a short between the two boards because I sort of overlapped them!
It's also worth considering using the SQW pin: https://garrysblog.com/2020/07/05/using-the-ds3231-real-time-clock-alarm-with-the-adafruit-rtclib-library/.
It's this bit that's causing the
i2cCheckLineState()
error and reboot:ChangedNope, still crashes. Why intermittent? With the wholetrue
to1
and it is not crashing.if
statement commented out, submitting the form doesn't seem to cause crashes. I wonder if it's some race condition between form submission (setting the alarm) and testing the alarm inloop()
.Also, even though I get stuff like this, the RTC time seems to be right:
I tried the same
if ((rtc.alarmFired(2) == true) )
code on the breadboard version and get the same results (crash on form submission).I will probably breadboard in the SQW pin and see if that works better https://github.com/garrysblog/DS3231-Alarm-With-Adafruit-RTClib-Library/blob/master/DS3231-RTClib-Adafruit-Alarm-Poll-SQW/DS3231-RTClib-Adafruit-Alarm-Poll-SQW.ino.
Added SQW pin on breadboard to GPIO4.
I can set Alarm2 and have SQW notify of the alarm. I tried
But I need to make sure that the time is being held between reboots, because the alarm just takes "day of month" HH:MM:SS, not absolute date. So if it reboots and thinks it's May 2000 but the same day, the alarm will trigger anyway.
On boot:
Alarm 2 set for at 15:27:00 Mon, 08 May 2000
Feel slightly better:
Confirming that I can't set an alarm more than a month in advance (or maybe ~2 months if the current month has 30 days and the next month has 31 days, and you set it for the 31st). Wild.
https://forum.arduino.cc/t/ds3231-alarm-when-date-match/929036/5
This is probably OK. I will leave the edge case alone for now. #33.
Before I go too far, consider just comparing
rtc.now()
and the start time inloop()
to trigger the scheduled program.ETA: This is harder than it looks, so I'll avoid it for now.
Random times every once in a while again.
https://www.reddit.com/r/arduino/comments/hdg70t/ds3231_rtc_random_numbers/
I thought I had typed this all out, but
So when I set up Alarm2 but turn off cycle, if I turned cycle back on within the same minute, the alarm was triggered.
I switched to Alarm1.
To send a cycle, we need to set up the duration of each repeating message.
morseToSend
andmorseToSend_blink
reset/update for step lengthCan this be done with arduino-timer or another timer without rewriting the Jled functions in
loop()
?So close to being able to use Jled sequences natively, if I could figure out how to calculate the number of times to repeat for a given step length.
This looks helpful (https://arduino.stackexchange.com/a/22275):
Or use
millis()
directly likeJled sequences example 1 (you can repeat a sequence n times or forever) (https://github.com/jandelgado/jled/issues/103#issuecomment-1312098221):
Jled sequences example 2 (https://github.com/jandelgado/jled/blob/master/examples/sequence/sequence.ino):
Another example (https://github.com/jandelgado/jled/issues/34):
Can I rewrite this for what I need?
Neat but not what I'm looking for: https://github.com/tfeldmann/Arduino-Blinkenlight.
GTK (https://github.com/jandelgado/jled/issues/11#issuecomment-429887353):
Oh wow, you can get the duration of the Morse effect with
Serial.println(morseEffectMO5.Period());
or similar.This will let me calculate how many repeats fit into each step length.
Then hopefully I can make a sequence (from above):
Very simple example working. To get it going:
The message should send once, but really it is limited to
int repeats step_length / morseEffectTEST.Period();
and whenstep_length = 30000
(30 seconds), a ~19-second message can only be sent once.Next steps are to
// See which message we are sending
to differentiate between continuous and cycle sending.My brain needs a rest.
I am debugging this in
Sites/jled/
and there may be a bug with usingDelayAfter()
in a sequence: https://github.com/jandelgado/jled/issues/122.What's funny is that my little test file almost does everything needed but without the overhead of having it configurable away from PlatformIO.
This is basically working. Observations:
no gap between end of message repeat and beginning of next message repeat (see https://github.com/jandelgado/jled/issues/122)if switching continuous to cycle, the continuous message keeps going70decbbcca
if switching off to cycle, the cycle message keeps going70decbbcca
My use of "cycle" and "program" are very confused right now, but making progress.
So close. Trying to let the user change the cycle message and running to problems.
Things like
morse_cycle = morseEffectMOE;
just don't work inloop()
. And I can't even overwrite an initialauto morse_cycle = morseEffectMOE;
with a laterMorseEffect morse_cycle("MOE ", ms_per_dit);
I'm going to set this aside and see if anything else can be updated. If not, will need to reassess.
If it doesn't work, need to store, No, that should only be needed if we want to resume from a power loss in the middle of a program, and that will require more math to figure out when to restart the program.programRunning
in file and check on boot.Um, no, the program doesn't seem to survive a reboot. Fun.
Well, it works if I push the reset button, but didn't when I unplugged the USB cable.
Maybe this could be a function?
Sigh. RTC is off after reset. That's no good. This was at 19:05 local time.
Don't know if this is good/relevant info or not (https://arduino.stackexchange.com/a/84893):
This is the behavior I see: https://forum.arduino.cc/t/ds3231-not-keeping-time-on-battery/363363/5
Battery measures 3.8V. Is it possible the battery wiring is bad? Try the other DS3231 next.
Same value between DS3231 pin 14 (Vbat) and 13 (ground).
Reviews on what I bought:
Sounds like these might be fake/counterfeit DS3231 or just bad design with the charging circuit. I guess I can sacrifice one to see if it works better without the charging circuit: https://www.amazon.com/gp/customer-reviews/R3TPKHQJHR0RWU/ref=cm_cr_arp_d_rvw_ttl?ie=UTF8&ASIN=B09LLMYBM1
Same behavior I see (lose on time on power loss), all the 2-star reviews: https://www.amazon.com/gp/customer-reviews/R3T8ND2O2X0U0V/ref=cm_cr_getr_d_rvw_ttl?ie=UTF8&ASIN=B09LLMYBM1
I tore the charging circuit resistor out, but still seeing weird results. The chip remembers something because it's showing a time. But why that time? I unplugged it and took the battery out, then replaced it and restarted. The alarm time was reset, but where does
2023-09-09T19:22:31
come from?A bit later: now it seems to be working? I will leave this one unplugged overnight and see what time it shows in the morning.
A bit later: I set a start time and unplugged it for a few minutes, plugged back in before start time. The program started, although it seemed a second or so late.
It seems to have worked (turned on at 8:36 AM). Is it 5-6 seconds off?
After some refreshes in the browser, looks like that one is the correct time. I did the same modification to the other one and it seems to be working similarly.
These are DS3231M chips, so maybe that is removing precision.
I'm going to roll with these for a while to see if they revert to not working. If it becomes a problem, will buy something from Adafruit (https://www.adafruit.com/product/5188) or somewhere else that has quality stuff.
Only took 14 hours to "fix" this, I guess: #24 (comment).
Now it looks like the alarm will work after removing/adding power (because the time seems to be kept now).
Back to #24 (comment) now, until something else breaks.
When using
DelayAfter()
, need to take that into account forremainer_wait
andtotal_wait
math, because it's outside of the duration returned bymorse_cycle.Period()
.Switching from continuous to off while I was trying to update variables in
loop()
:I'm beginning to think there really is no way to update/replace a Jled sequence on the fly in
loop()
. So I could set up some basic options and let people choose from a list, until I can figure this out, with Jled or another way.Back to this:
I am trying to write a function to make new cycle Jled sequences.
I'm going to look for JLed alternatives, or maybe ask for help developing this function.
This causes it to reboot over and over:
https://github.com/ArduinoGetStarted/led (ezLED) is interesting, but I get errors on build. See
Sites/ezLED
.Here's an example function, I think:
97ad8480f7/igniter.ino (L118)
The igniter code works pretty well when stripped down, even if I don't understand it. See
Sites/igniter
.The problem is that for Morse, I need to know the duration of the message, which means that the
MorseEffect
needs to be set up first, then the sequence put together. I run into the same issue where it crashes over and over when I try to use the function.Get a little more information with
monitor_filters = esp32_exception_decoder
in platformio.ini.Lol, if I comment out the division, I can at least run the function.
So tomorrow I can try to use the object created.
Hmm.
morses_sequence_blink_test->Forever().Update();