Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Continuous or timed?

by Marshall (Canon)
on Dec 12, 2020 at 22:10 UTC ( [id://11125076]=note: print w/replies, xml ) Need Help??


in reply to Continuous or timed?

I think the consensus here is option #2. That's what I would do or something similar.
For #3, I wouldn't fool with Chron - just calculate number of seconds until next run and sleep() that long. That way if the system restarts, your process runs immediately and calculates the time until the next run time. That way there is no Chron file involved.

I have a Raspberry Pi. I am just starting to play with it - so I am a Pi newbie. I need more hardware to do what my intended app needs to do (record HDMI video input). My Pi doesn't have a clock chip in it. When it restarts it needs to get the time/date either from me by me typing it in or via the internet. Your app depends upon knowing the current time - what happens if that time isn't known?

There might be some startup race conditions after a power failure. I'm not sure about the Pi config options are when the Pi cannot get current date/time from the internet? Maybe Pi is "ready" before the router to the internet?

Think about a status LED - perhaps blinking means good - stuck on(or off) means bad.

Replies are listed 'Best First'.
Re^2: Continuous or timed?
by afoken (Chancellor) on Dec 13, 2020 at 15:57 UTC
    For #3, I wouldn't fool with Chron - just calculate number of seconds until next run and sleep() that long. That way if the system restarts, your process runs immediately and calculates the time until the next run time. That way there is no Chron file involved.

    You are reinventing the wheel here. cron does exactly that. Calculate the sleep time until the next job needs to run, and sleep. If it wakes up early, that almost certainly has happened because someone submitted changes to the job list (i.e. running crontab).

    Oh, and by the way, don't blindly run after sleep returns. sleep may be interrupted, so your process may have slept for a much shorter time than expected.

    There might be some startup race conditions after a power failure. I'm not sure about the Pi config options are when the Pi cannot get current date/time from the internet? Maybe Pi is "ready" before the router to the internet?

    That could be detected in startup scripts. Linux starts at unix time 0, i.e. 1970-01-01 00:00:00 UTC. So if the application detects a system time years before it was written (if (year < 2020) would be sane), it should assume a fresh boot and just wait for ntpd to do its job. Also, there are RTC chips for the Raspi, so that it adjusts the system clock very early in the boot process, before cron and other services run.

    Think about a status LED - perhaps blinking means good - stuck on(or off) means bad.

    Good idea, easy to implement. Add a cheap red LED (based on GaAs, voltage drop about 1.6 V) and a resistor to a GPIO pin. GPIO voltage level is about 3.3 V, LED current should be around 5 mA, so the resistor value is R = U / I = (3.3 V - 1.6 V) / 5 mA = 340 Ohm. Close standard values are 330 Ohm and 390 Ohm. Heat dissapation is P = U * I = 1.7 V * 5 mA = 8.5 mW for 5 mA. Any cheap resistor can easily heat away at least 125 mW. LED should be connected between GPIO pin and GND, with the resistor in series wih the LED. If the LED is too bright, decrease the current by increasing the resistor value. To get 1 mA, you need 1.7 kOhm, close standard value is 1.8 kOhm. Only very few LEDs are visible at less than 1 mA, so that's about the upper limit for the resistor. The Raspi can't sink or source significantly more than 15 mA, and has an additional limit of about 50 mA for the sum of all GPIO pins, so you don't want more that 5 to 10 mA for the LED, so the resistor should not be less than 170 Ohm.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Oh, and by the way, don't blindly run after sleep returns. sleep may be interrupted, so your process may have slept for a much shorter time than expected.

      Yes, indeed! I had forgotten about that. Sleep() returns the value of unslept seconds. So if its not zero, just sleep again for that return value. Sleep is often implemented with SIGALRM so that's another potential issue.

      Good description of power/brightness for LED's. As another comment, you can get what are called "resistor LED's". In that case, the resistor is built into the LED. Probably the most common value will be 1K Ohms which is a good choice for 5V. For 3.3V perhaps, 470 or 560 Ohms would be fine. The brightness can also be adjusted by pulsing the LED. I forget exact waveforms that I've used in the past for "ON", but as a starting point, probably a square wave with 20-30ms/on-off is about as bright as 100%. Play with different duty cycles for "on" and see what happens... Current and duty cycle of that current vs perceived brightness is a non-linear function.

      BTW, I like blinking as the "good state" -> that means that the processor is running and is able to turn the LED on/off. Also, different cadences of the LED can be used to indicate error conditions. Human brains are excellent at pattern recognition. My router has a particular flashing light sequence that I recognize as "normal" without really thinking about what each individual light means. If the LED is stuck either at ON or OFF, I would surmise that the processor is "dead". If you just have a single LED, perhaps Long blip, followed by short blip means Internet connection bad, etc.

      "Good idea, easy to implement. Add a cheap red LED (based on GaAs, voltage drop about 1.6 V) and a resistor to a GPIO pin."

      No need even for that on a Pi. In my RPi::WiringPi, I use a couple of command line commands to manipulate the power and disk I/O LEDs so that a user can identify by eye which Pi is the one currently being worked on (this was a user request I implemented). Since this is a Perl forum, I've left the CLI commands within the actual code itself. Essentially, the identify routine turns the power LED off completely, and turns the disk IO LED on solid. Using these LEDs to set them to non-standard configuration could easily indicate a problem with custom software.

      sub io_led { my ($self, $tweak) = @_; if ($tweak){ # stop disk activity from operating the green LED `echo none | sudo tee /sys/class/leds/led0/trigger`; # turn on the green LED full-time `echo 1 | sudo tee /sys/class/leds/led0/brightness`; } else { # turn off the green LED from being on full-time `echo 0 | sudo tee /sys/class/leds/led0/brightness`; # start disk activity operating the green LED `echo mmc0 | sudo tee /sys/class/leds/led0/trigger`; } } sub pwr_led { my ($self, $tweak) = @_; if ($tweak){ # turn off the red power LED `echo 0 | sudo tee /sys/class/leds/led1/brightness`; } else { # restore default power LED operation `echo input | sudo tee /sys/class/leds/led1/trigger`; } }
Re^2: Continuous or timed?
by Bod (Parson) on Dec 12, 2020 at 23:35 UTC
    Your app depends upon knowing the current time - what happens if that time isn't known

    You make a good point. I've not tried it, but I guess that the clock would initialise to sometime in the past (1st January 1970 perhaps) when the system boots until it is able to get the time from the internet. The plan is to not make any changes to the state of the curtains if the system time is before sometime in the past - probably 1st December 2020 to remind me when all this weird activity happened 😜

    Once it knows the time, and knows it knows the time, it can go to work determining what state the curtains need to be in and putting them that way. There will be some 'fiddling' needed with the setup to get everything fine tuned as I cannot develop the system with a set of powered curtains to test it.

    I have a Raspberry Pi. I am just starting to play with it - so I am a Pi newbie

    Good luck!
    It is proving to be an interesting and, at times frustrating process. Using an RPi Zero is frustrating at times as it is rather slow. Perflectly capable for the task it is to perform but very slow to set up and develop.

      A quick google search shows that RPi Zero is a cheap $10 board. I have what I guess is a Pi 4. It has an Ethernet connection, HDMI connection to my monitor, USB for keyboard and mouse. Nothing slow about it at all! By the time I bought my Pi, the case. power supply. and the cables, it was north of $80. A friend burned me an SD card for me with the O/S including Perl and Python. My Pi has a Debian flavor of Unix and it works great - very fast - no problems.
Re^2: Continuous or timed?
by Bod (Parson) on Dec 16, 2020 at 20:05 UTC
    Think about a status LED - perhaps blinking means good - stuck on(or off) means bad

    I did consider one or more LEDs when the project first came into being.
    But, given that the user is blind, there is nobody to be able to see them! Perhaps we could train Yago, his assistance dog, to look and see if there is a light on...

      For fatal Motherboard errors, there are "audible beep codes". Don't know what that means in context of a modern "all-in-one" motherboard but, a couple of computers ago, when my computer failed to boot, I called a friend and had him look up the beep codes to find out "dead video card". The stock Pi doesn't have a way to beep, but there is probably an add-on for that? If you ever do add some beep codes, think about how annoying it can get if it is beeping all the time! Another problem is ill timed beeping. I had one device that decided to beep at me every night at 3 AM. I have a lot of gizmos that can beep (smoke alarm, CO2 alarm, etc.) I had to wake up at 2:50 AM several nights in a row to wait for this thing and I gradually narrowed it down to find the "unhappy device" and feed it a new battery!
        The stock Pi doesn't have a way to beep, but there is probably an add-on for that?

        I wouldn't inflict the prospect of random bleeping on someone...especially not someone who is blind and unable to work out where the bleeping is originating from.

Re^2: Continuous or timed?
by Bod (Parson) on Dec 18, 2020 at 18:17 UTC
    I have a Raspberry Pi. I am just starting to play with it - so I am a Pi newbie. I need more hardware to do what my intended app needs to do (record HDMI video input)

    My RPi has been installed in a nice box today ready to go to its new home and be connected to the curtain motor that will be its new best friend! So I have started setting a new one up (without the GPIO pins). For the first RPi I bought an SD card with the OS on it but for the second one I have downloaded the Raspberry Pi Imager.

    Browsing through the various OS options I came across Media Player Kodi OS and thought about your application...not sure if you have come across it or not and whether it is helpful or not but thought I'd share it just in case it helps.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11125076]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-19 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found