MicroPython on ESP8266 - RTC does not have an init method? - esp8266

The documentation says that the RTC clock can be initialized with the RTC.init call.
https://docs.micropython.org/en/latest/esp8266/library/machine.RTC.html
But it does not work that way:
>>>import machine
>>>rtc = machine.RTC()
>>>rtc.init((2018,4,10,17,30))
Traceback (most recent call last):
File "<stdin>", line 1 in <module>:
AttributeError: 'RTC' object has no attribute 'init'
So the documentation contradicts reality. The firmware version is v1.9.3 - downloaded the latest just a few days ago.
Most interestingly, dir(rtc) gives ['datetime','memory','alarm','alarm_left','irq','ALARM0']. It is missing several other methods: now, deinit, cancel
So where is the RTC init method, and how could it disappear?
UPDATE: I have figured out that the documentation is wrong, I need to use RTC.datetime instead of RTC.init. But it is still wrong:
>>>from machine import RTC
>>>rtc=RTC()
>>>rtc.datetime((2000,1,1,23,59,59,0,0))
>>>rtc.datetime()
(2000, 1, 3, 0, 11, 59, 3, 705)
In other words: 2000-01-01T23:59:59 suddenly became 2000-01-03T00:11:59. How?
I also could not find anything useful on the tzinfo parameter of the RTC.datetime method. It should be a number, that is clear. But what does it mean?
I have also tried midnight:
>>>rtc.datetime((2000,1,1,0,0,0,0,0))
>>>rtc.datetime()
(2000,1,1,5,0,0,1,155)
So at tzinfo=0, midnight becomes 05:00:00. I first thought that it means UTC+5 but it does not:
>>>rtc.datetime((2000,1,1,10,0,0,0,0))
>>>rtc.datetime()
(2000,1,1,5,0,0,1,155)
And finally:
>>>rtc.datetime((2000,1,1,5,0,0,0,0))
>>>rtc.datetime()
(2000,1,1,5,0,0,1,545)
This is insane! It looks like the hour part is totally ignored.

Wow this is unbelievable! After looking in the source code, it turns out that the fourth parameter is "day of the week", beginning with Monday. So the documentation is totally messed up!
tzinfo parameter does not exist.
The actual parameters in the tuple are:
(year,month,day,day of the week(0-6;Mon-Sun),hour(24 hour format),minute,second,microsecond)
It also seems that when you set the date and time, you can always set day of the week to zero, it will be automatically corrected from the year+month+day.
So for April 10th,2018 at 6:31 P.M. with 15 seconds and 0 microseconds:
>>>rtc.datetime((2018,4,10,0,18,31,15,0))
>>>rtc.datetime()
(2018, 4, 10, 1, 18, 31, 16, 808)
Where the fourth number=1 means it is the SECOND day of the week, Tuesday.

Related

MIDO: ValueError: variable int must be a positive integer

In my code I get
Traceback (most recent call last):
File "Midi Projects/symbolToChord_v1.py", line 160, in <module>
mo.save("songWithChords.mid")
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mido/midifiles/midifiles.py", line 432, in save
self._save(file)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mido/midifiles/midifiles.py", line 445, in _save
write_track(outfile, track)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mido/midifiles/midifiles.py", line 251, in write_track
data.extend(encode_variable_int(msg.time))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mido/midifiles/meta.py", line 112, in encode_variable_int
raise ValueError('variable int must be a positive integer')
ValueError: variable int must be a positive intege
I suppose I am running the latest version on MIDO.
pip freeze | grep mido
mido==1.2.9
what am doing wrong?
Any help would be greatly appreciated.
I am no expert, but I had a similar problem.
The time attribute in mido is a bit confusing as it can either represents ticks or time deltas. From the documentation (https://mido.readthedocs.io/en/latest/midi_files.html#about-the-time-attribute
):
The time attribute is used in several different ways:
inside a track, it is delta time in ticks. This must be an integer.
in messages yielded from play(), it is delta time in seconds (time elapsed since the last yielded message)
(only important to implementers) inside certain methods it is used for absolute time in ticks or seconds
You can also see this github issue for reference https://github.com/mido/mido/issues/189

Find the start of a given week and year with Delphi

I'm looking for a Delphi (10+) function that returns a TDate with a given year and a week number:
function StartDate(2021, 53): TDate should return 2021-01-01.
While WeekOfTheYear(EncodeDate(2021,1,1)) returns 53 (correct), I can't do the other way round with StartOfAWeek(2021,53, 1) nor StartOfAWeek(2021,53, 5) (5=it's a friday) - it's not recognized as a valid date (=exception). Any suggestions?
Edited:
I'm looking for a ISO 8601 compliant function (like the internal Delphi routines), with Monday=1 and special week consideration (like 2021-01-01), or to be more precise: the "vice versa" routine of WeekOfTheYear
The function you need is:
StartOfAWeek(Year, Week, 1)
You observe that StartOfAWeek(2021, 53, 1) raises an exception. That is correct because 2021 does not have 53 weeks. It only has 52. Week 52 ends on the last day of 2021.
You are getting confused by the result of
WeekOfTheYear(EncodeDate(2021,1,1))
This returns 53, but because the date is at the start of the year, this is week 53 of 2020.

Rails resque scheduler run job at specific time

I have this resque job that must run at 6 AM or 11 AM or 16 PM or 20 PM o clock
or at least for example 6 am
Resque.enqueue_at <what to put here>, SendSmsJob
How do I define specific time like that? thanks
You can pass it a Time and it'll run once that time is in the past, like so:
time = Time.new(2016, 6, 11, 20, 0)
Resque.enqueue_at time, SendSmsJob

lua math.randomseed returns nothing

I have the following code:
APP.logevent('ostime:'..os.time())
APP.logevent('random:'..math.random())
APP.logevent(math.randomseed(os.time()))
When i check my logs, this is what I get:
Tue Feb 5 11:49:53 2013: ostime:1360082993
Tue Feb 5 11:49:53 2013: random:0.84018771715471
Tue Feb 5 11:49:53 2013:
machinename:/usr/share/ajj#
I'm not getting any error messages....
Can you tell me why the call to randomeseed() is failing?
I've also tried replacing the call to "os.time()" in the randomseed with a number... and that doesn't seem to work either.
Thanks.
lua math.randomseed returns nothing
It's not supposed to. Lua functions are not required to return a values, and there's no real reason for randomseed to return anything.
Can you tell me why the call to randomeseed() is failing?
It's not.
EDIT:
I thought it would create a random number for me.
That's what math.random does. randomseed seeds the random number generator, which is to say it sets an initial value that the pseudorandom number generator uses to find the next pseudorandom value. For a given seed (including the default seed, i.e. you never call randomseed), you'll get the same list of pseudorandom values back every time.

How do I compute equinox/solstice moments?

What algorithms or formulas are available for computing the equinoxes and solstices? I found one of these a few years ago and implemented it, but the precision was not great: the time of day seemed to be assumed at 00:00, 06:00, 12:00, and 18:00 UTC depending on which equinox or solstice was computed. Wikipedia gives these computed out to the minute, so something more exact must be possible. Libraries for my favorite programming language also come out to those hardcoded times, so I assume they are using the same or a similar algorithm as the one I implemented.
I also once tried using a library that gave me the solar longitude and implementing a search routine to zero in on the exact moments of 0, 90, 180, and 270 degrees; this worked down to the second but did not agree with the times in Wikipedia, so I assume there was something wrong with this approach. I am, however, pleasantly surprised to discover that Maimonides (medieval Jewish scholar) proposed an algorithm using the exact same idea a millenium ago.
A great source for the (complex!) underlying formulas and algorithms is Astronomical Algorithms by Jean Meeus.
Using the PyMeeus implementation of those algorithms, and the code below, you can get the following values for the 2018 winter solstice (where "winter" refers to the northern hemisphere).
winter solstice for 2018 in Terrestrial Time is at:
(2018, 12, 21, 22, 23, 52.493725419044495)
winter solstice for 2018 in UTC, if last leap second was (2016, 12):
(2018, 12, 21, 22, 22, 43.30972542127711)
winter solstice for 2018 in local time, if last leap second was (2016, 12)
and local time offset is -7.00 hours:
(2018, 12, 21, 15, 22, 43.30973883232218)
i.e. 2018-12-21T15:22:43.309725-07:00
Of course, the answer is not accurate down to microseconds, but I also wanted to show how to do high-precision conversions with arrow.
Code:
from pymeeus.Sun import Sun
from pymeeus.Epoch import Epoch
year = 2018 # datetime.datetime.now().year
target="winter"
# Get terrestrial time of given solstice for given year
solstice_epoch = Sun.get_equinox_solstice(year, target=target)
print("%s solstice for %d in Terrestrial Time is at:\n %s" %
(target, year, solstice_epoch.get_full_date()))
print("%s solstice for %d in UTC, if last leap second was %s:\n %s" %
(target, year, Epoch.get_last_leap_second()[:2], solstice_epoch.get_full_date(utc=True)))
solstice_local = (solstice_epoch + Epoch.utc2local()/(24*60*60))
print("%s solstice for %d in local time, if last leap second was %s\n"
" and local time offset is %.2f hours:\n %s" %
(target, year, Epoch.get_last_leap_second()[:2],
Epoch.utc2local() / 3600., solstice_local.get_full_date(utc=True)))
Using the very cool more ISO and TZ aware module Arrow: better dates and times for Python, that can be printed more nicely:
import arrow
import math
slutc = solstice_epoch.get_full_date(utc=True)
frac, whole = math.modf(slutc[5])
print("i.e. %s" % arrow.get(*slutc[:5], int(whole), round(frac * 1e6)).to('local'))
I'm not sure if this is an accurate enough solution for you, but I found a NASA website that has some code snippets for calculating the vernal equinox as well as some other astronomical-type information. I've also found some references to a book called Astronomical Algorithms which may have the answers you need if the info somehow isn't available online.
I know you're looking for something that'll paste into an answer here, but I have to mention SPICE, a toolkit produced by NAIF at JPL, funded by NASA. It might be overkill for Farmer's Almanac stuff, but you mentioned interest in precision and this toolkit is routinely used in planetary science.
I have implemented Jean Meeus' (the author of the Astronomical Algorithms referenced above) equinox and solstice algorithm in C and Java, if you're interested.

Resources