Open Jan36Nikon opened 3 years ago
A few things.
Do no use dtoverlay=i2c-rtc,ds1339=1
but dtoverlay=i2c-rtc,ds1307=1
With ds1339 the OS can clear the alarm if the time gets synchronised when connected to the internet.
The RTC also needs I2C, so before you copy the RTC time to the system the i2c bus has to be initialized.
The pijuice service startup does a check and reloads the rtc module if needed.
See lines 357-391 in pijuice_sys.py (https://github.com/PiSupply/PiJuice/blob/master/Software/Source/src/pijuice_sys.py).
Use the startup event hook (which gets called by the service after the RTC is available) to do the subprocess.call(["sudo", "hwclock", "--hctosys"]).
Your @reboot job could start too early (before RTC is initialized). Put in a delay at the start or do some form of synchronisation with the service startup event function.
Thank you for support, I just changed dtoverlay=i2c-rtc,ds1339=1 to dtoverlay=i2c-rtc,ds1307=1 and also adjust the timing of crontab to start 60 secconds after reboot. I am not really sure how to use service startup event. But basically this setup should work also.
@reboot sleep 60 && /usr/bin/python3 /mnt/OGC/001_CurrentScript/Script.py
Can I also ask if there is good aproach to use the ClearAlarmFlag on the end of the script in front of shutdown command? So the end will look like this?
.........
# Enable wakeup, otherwise power to the RPi will not be
# applied when the RTC alarm goes off
pj.rtcAlarm.SetWakeupEnabled(True)
time.sleep(0.4)
# PiJuice shuts down power to Rpi after 20 sec from now
# This leaves sufficient time to execute the shutdown sequence
pj.rtcAlarm.ClearAlarmFlag()
pj.power.SetPowerOff(20)
subprocess.call(["sudo", "poweroff"])
I will start test for 24 hours now and let you know if it will work. Thank you for your time, Jan
Good afternoon, result is that after a while device was left in turn off state. You can see it on log where last row is manual wakeup by PiJuice button.
So at 10:14:49 device boot and correctly shut itself down. After that nothing happens, device was left sleeping. But, after manual restart in PiJuice GUI, wakup enable was disabled.
I don't know what to try next, automatic start and shutdown is the reason why I bought this device, but from my current point of view it is not reliable enought, or am I doing something wrong?
Thank you for information.
2021-08-11 08:39:12 -- Started
2021-08-11 08:58:51 -- Started 0:19:39
2021-08-11 09:17:53 -- Started 0:19:02
2021-08-11 09:36:50 -- Started 0:18:57
2021-08-11 09:55:48 -- Started 0:18:58
2021-08-11 10:14:49 -- Started 0:19:01
2021-08-11 13:04:34 -- Started 2:49:45
It looks like the wakeup enabled was disabled by the time sync. So according to https://github.com/PiSupply/PiJuice/issues/362 I changed address of HAT to 52 and hope it will work OK now.
I will let you knou about result.
Above you wrote you are using a PiJuice Zero. In that case changing the EEPROM address to 0x52 has no effect
(The Pijuice Zero has no HAT EEPROM).
For the PiJuice Zero you will have to use dtoverlay=i2c-rtc,ds1307=1)
as I already wrote above.
The DS1307 has no alarm, but the clock part is identical to the DS1339. So you get internet time synchronization
of the RTC without clearing the alarm.
Let me know how it goes.
Like I wrote yesterday, after setting dtoverlay=i2c-rtc,ds1307=1 I still find raspberry in off state with wakeup enable button disabled. I had script to wake up every 19 minutes. Could short time power loss be the issue in that case?
After that I changed the script to the following on end of my post, to wake up at every hour and EEPROM address from 50 to 52 (it should have no effect). Could you please check my script if it is OK, especially the ending with
pj.rtcAlarm.ClearAlarmFlag()
pj.rtcAlarm.SetWakeupEnabled(True)
If they are in correct order. And if I need to use ClearAlarmFlag at all?
My current setup is: EEPROM address 0x52 (no effect, but it is set to this address) dtoverlay=i2c-rtc,ds1307=1 No battery connected - only micro USB connected from power outlet to PiJuice PCB
Crontab entry:
@reboot sleep 120 && /usr/bin/python3 /mnt/OGC/001_CurrentScript/Script.py
Script.py
from __future__ import print_function
import time
import pijuice
import subprocess
import datetime
import os
import sys
# Rely on RTC to keep the time
subprocess.call(["sudo", "hwclock", "--hctosys"])
# Record start time
txt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ' -- Started\n'
with open('/home/pi/test.log','a') as f:
f.write(txt)
# This script is started at reboot by cron.
# Since the start is very early in the boot sequence we wait for the i2c-1 device
while not os.path.exists('/dev/i2c-1'):
time.sleep(0.1)
try:
pj = pijuice.PiJuice(1, 0x14)
except:
print("Cannot create pijuice object")
sys.exit()
# Do the work
for i in range(180):
print('*', end='')
sys.stdout.flush()
time.sleep(1)
print()
# Set RTC alarm 5 minutes from now
# RTC is kept in UTC
a={}
a['year'] = 'EVERY_YEAR'
a['month'] = 'EVERY_MONTH'
a['day'] = 'EVERY_DAY'
a['hour'] = 'EVERY_HOUR'
a['minute'] = 0
a['second'] = 0
status = pj.rtcAlarm.SetAlarm(a)
if status['error'] != 'NO_ERROR':
print('Cannot set alarm\n')
sys.exit()
else:
print('Alarm set for ' + str(pj.rtcAlarm.GetAlarm()))
# Enable wakeup, otherwise power to the RPi will not be
# applied when the RTC alarm goes off
pj.rtcAlarm.ClearAlarmFlag()
pj.rtcAlarm.SetWakeupEnabled(True)
time.sleep(0.4)
# PiJuice shuts down power to Rpi after 20 sec from now
# This leaves sufficient time to execute the shutdown sequence
pj.power.SetPowerOff(20)
subprocess.call(["sudo", "poweroff"])
Current state of log Right now it is working for 24 hours but from last error in wakeup I changed the script only from 19 minutes interval to 1 hour interval and EEPROM address prom 50 to 52 (which should have no effect), so I am not perfectly sure that the error was solved...
Thank you for your comment, Jan
2021-08-11 16:28:17 -- Started
2021-08-11 17:02:48 -- Started
2021-08-11 18:02:49 -- Started
2021-08-11 19:02:47 -- Started
2021-08-11 20:02:48 -- Started
2021-08-11 20:44:48 -- Started
2021-08-11 21:02:49 -- Started
2021-08-11 22:02:49 -- Started
2021-08-11 23:02:50 -- Started
2021-08-12 00:02:49 -- Started
2021-08-12 01:02:49 -- Started
2021-08-12 02:02:53 -- Started
2021-08-12 03:02:46 -- Started
2021-08-12 04:02:48 -- Started
2021-08-12 05:02:50 -- Started
2021-08-12 06:02:49 -- Started
2021-08-12 07:02:48 -- Started
2021-08-12 08:02:50 -- Started
2021-08-12 09:02:50 -- Started
2021-08-12 10:02:46 -- Started
2021-08-12 11:02:50 -- Started
2021-08-12 12:02:50 -- Started
2021-08-12 13:02:47 -- Started
2021-08-12 14:02:47 -- Started
2021-08-12 15:02:52 -- Started
2021-08-12 16:02:47 -- Started
A few things about your script.
from __future__ import ...
linesubprocess.call(["sudo", "hwclock", "--hctosys"])
) after the check for /dev/i2c-1
since the hwclock (= the DS1307 in the PiJuice) needs i2c working.print('*', end='', flush=True)
and delete the sys.stdout.flush()
line.The script was probably written for python2. The remainder of the logic looks fine.
Thank you for your update, I just check the process and it looks like it again stuck in power off mode.
See the log:
2021-08-11 16:28:17 -- Started
2021-08-11 17:02:48 -- Started
2021-08-11 18:02:49 -- Started
2021-08-11 19:02:47 -- Started
2021-08-11 20:02:48 -- Started
2021-08-11 20:44:48 -- Started
2021-08-11 21:02:49 -- Started
2021-08-11 22:02:49 -- Started
2021-08-11 23:02:50 -- Started
2021-08-12 00:02:49 -- Started
2021-08-12 01:02:49 -- Started
2021-08-12 02:02:53 -- Started
2021-08-12 03:02:46 -- Started
2021-08-12 04:02:48 -- Started
2021-08-12 05:02:50 -- Started
2021-08-12 06:02:49 -- Started
2021-08-12 07:02:48 -- Started
2021-08-12 08:02:50 -- Started
2021-08-12 09:02:50 -- Started
2021-08-12 10:02:46 -- Started
2021-08-12 11:02:50 -- Started
2021-08-12 12:02:50 -- Started
2021-08-12 13:02:47 -- Started
2021-08-12 14:02:47 -- Started
2021-08-12 15:02:52 -- Started
2021-08-12 16:02:47 -- Started
2021-08-12 17:02:49 -- Started
2021-08-12 18:02:50 -- Started
2021-08-12 19:02:49 -- Started
2021-08-12 20:02:52 -- Started
2021-08-12 21:02:50 -- Started
2021-08-12 22:02:46 -- Started
2021-08-12 23:02:47 -- Started
2021-08-13 00:02:46 -- Started
2021-08-13 01:02:46 -- Started
I found it again after several hours in OFF state.
When I displayed PiJuice Zero GUI after manual boot I found this: (disabled "WakeUp enabled")
I will try to put your modification in the script so it will look like this and I will again start the test:
Is it necessary to have
time.sleep(0.4)
after
pj.rtcAlarm.SetWakeupEnabled(True)
Thank you for support. Jan
import time
import pijuice
import subprocess
import datetime
import os
import sys
# Record start time
txt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ' -- Started\n'
with open('/home/pi/test.log','a') as f:
f.write(txt)
# This script is started at reboot by cron.
# Since the start is very early in the boot sequence we wait for the i2c-1 device
while not os.path.exists('/dev/i2c-1'):
time.sleep(0.1)
try:
pj = pijuice.PiJuice(1, 0x14)
# Rely on RTC to keep the time
subprocess.call(["sudo", "hwclock", "--hctosys"])
except:
print("Cannot create pijuice object")
sys.exit()
# Do the work
for i in range(180):
print('*', end='', flush=True)
time.sleep(1)
print()
# Set RTC alarm 5 minutes from now
# RTC is kept in UTC
a={}
a['year'] = 'EVERY_YEAR'
a['month'] = 'EVERY_MONTH'
a['day'] = 'EVERY_DAY'
a['hour'] = 'EVERY_HOUR'
a['minute'] = 0
a['second'] = 0
status = pj.rtcAlarm.SetAlarm(a)
if status['error'] != 'NO_ERROR':
print('Cannot set alarm\n')
sys.exit()
else:
print('Alarm set for ' + str(pj.rtcAlarm.GetAlarm()))
# Enable wakeup, otherwise power to the RPi will not be
# applied when the RTC alarm goes off
pj.rtcAlarm.ClearAlarmFlag()
pj.rtcAlarm.SetWakeupEnabled(True)
time.sleep(0.4)
# PiJuice shuts down power to Rpi after 20 sec from now
# This leaves sufficient time to execute the shutdown sequence
pj.power.SetPowerOff(20)
subprocess.call(["sudo", "poweroff"])
It smells like the wakeup at 02:00 actually happened, but due to insufficient power the Pi startup did not succeed and hence the wakeup was left disabled. Do you have an external disk connected since your script is running from /mnt/OGC? To get a clue can you read and log the charge status (pj.status.GetChargeLevel()) and battery voltage (pj.status.GetBatteryVoltage()) in your Do the work?
Thank you for your support, important point is that I don't have any battery connected...
Yes, I have USB flash drive 128 Gb mounted, but it is not any HDD, only flashdrive. I will try to add those commands to the code and we will see.
Thank you for your support, Jan
On the HAT configuration tab make sure you have 'no battery turn on' enabled when you are running without battery. Also on your screenshot above you have Wakeup enabled: Last 08:03:16 This means the alarm flag was still set when you started the GUI around 08:03. So the alarm went off but the flag was not cleared. USB flash drive consumption can be an extra 200mA, but depends on the type of flash drive.
In configuration of HAT I enable 'no battery turn on' previously it was disabled.
How can I be sure that alarm flag will be cleared? I also changed the 230V power adapter, and we will see...
'no battery turn on' button has big affect on system function?
Thank you, Jan
From the software README: No battery turn on: If enabled pijuice will automatically power on 5V rail and trigger wake-up as soon as power appears at USB Micro Input and there is no battery. Disabled by default.
Thank you I will let you know how it works...
It looks like your last tip according to 'No battery turn on' solved the problem, right now it is working for 2 days in row.
20:02:50
21:02:47
22:02:49
23:02:47
0:02:46
1:02:46
2:02:48
3:02:49
4:02:47
5:02:49
6:02:47
7:02:47
7:54:09
8:02:46
9:02:46
10:02:48
11:02:49
12:02:50
13:02:50
14:02:49
15:02:45
16:02:50
17:02:49
17:46:21
18:02:47
19:02:50
20:02:46
21:02:47
22:02:52
23:02:49
0:02:45
1:02:50
2:02:50
3:02:50
4:02:47
5:02:47
6:02:47
7:02:49
8:02:50
9:02:49
10:02:46
11:02:47
12:02:46
Thank you for support, Jan
Interesting is that hour when device turn on twice is the same hour when I connected to it through VNC (at the first 2 minutes of that hour)
Another update from my new test...
Right now device is not failing to start...
But in log you can see that it is also waking up in different times than every hour... Do you know how to solve it?
Thanks, Jan
2021-08-15 16:55:11 -- Started
2021-08-15 17:02:50 -- Started
2021-08-15 18:02:47 -- Started
2021-08-15 19:02:49 -- Started
2021-08-15 20:02:48 -- Started
2021-08-15 21:02:49 -- Started
2021-08-15 21:18:16 -- Started
2021-08-15 22:02:46 -- Started
2021-08-15 23:02:47 -- Started
2021-08-15 23:29:37 -- Started
2021-08-16 00:02:49 -- Started
2021-08-16 00:08:33 -- Started
2021-08-16 02:02:46 -- Started
2021-08-16 03:02:48 -- Started
2021-08-16 03:52:15 -- Started
2021-08-16 04:02:48 -- Started
2021-08-16 05:02:50 -- Started
2021-08-16 06:02:47 -- Started
2021-08-16 07:02:47 -- Started
2021-08-16 08:02:48 -- Started
2021-08-16 09:02:45 -- Started
2021-08-16 10:02:45 -- Started
2021-08-16 11:02:50 -- Started
2021-08-16 12:02:46 -- Started
2021-08-16 13:02:46 -- Started
2021-08-16 14:02:49 -- Started
2021-08-16 14:13:01 -- Started
2021-08-16 15:02:49 -- Started
2021-08-16 16:02:47 -- Started
2021-08-16 16:44:31 -- Started
2021-08-16 17:02:48 -- Started
2021-08-16 18:02:46 -- Started
Current test script.py
import time
import pijuice
import subprocess
import datetime
import os
import sys
# Record start time
txt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ' -- Started\n'
with open('/home/pi/test.log','a') as f:
f.write(txt)
# This script is started at reboot by cron.
# Since the start is very early in the boot sequence we wait for the i2c-1 device
while not os.path.exists('/dev/i2c-1'):
time.sleep(0.1)
try:
pj = pijuice.PiJuice(1, 0x14)
# Rely on RTC to keep the time
subprocess.call(["sudo", "hwclock", "--hctosys"])
except:
print("Cannot create pijuice object")
sys.exit()
# Do the work
for i in range(180):
print('*', end='', flush=True)
time.sleep(1)
print()
# Set RTC alarm 5 minutes from now
# RTC is kept in UTC
a={}
a['year'] = 'EVERY_YEAR'
a['month'] = 'EVERY_MONTH'
a['day'] = 'EVERY_DAY'
a['hour'] = 'EVERY_HOUR'
a['minute'] = 0
a['second'] = 0
status = pj.rtcAlarm.SetAlarm(a)
if status['error'] != 'NO_ERROR':
print('Cannot set alarm\n')
sys.exit()
else:
print('Alarm set for ' + str(pj.rtcAlarm.GetAlarm()))
# Enable wakeup, otherwise power to the RPi will not be
# applied when the RTC alarm goes off
pj.rtcAlarm.ClearAlarmFlag()
pj.rtcAlarm.SetWakeupEnabled(True)
time.sleep(0.4)
# PiJuice shuts down power to Rpi after 20 sec from now
# This leaves sufficient time to execute the shutdown sequence
pj.power.SetPowerOff(20)
subprocess.call(["sudo", "poweroff"])
Since you have 'no battery turn on' enabled, also any power interruption on the PiJuice micro-USB input will also wake-up the pi. Could that have happened? Power brown-outs due to bad weather?
Thank you for tips I will do another tests.
Good afternoon,
I did a test and everything was working OK without battery.
Currently I am getting same problem as in issue #771 (run without power source only on battery) see log down...
hwclock: Cannot access the Hardware Clock via any known method.
hwclock: Use the --verbose option to see the details of our search for an access method.
Obviously it looks like problem is in this row:
subprocess.call(["sudo", "hwclock", "--hctosys"])
Is it necessary only when I am running without Internet connection? Or can I delete this row, because I am running crontab on reboot + 2 minutes?
According to LOG, Image was taken and also next alarm was set successfully. I am trying to run next test with force_turbo=1 in config.txt
It could be caused by USB flash drive which I am using to store my captured images, like you told in Issue #771 (I am trying not to store images on SD card, is there any other possibility?)
2021-09-10 21:00:18 | Image OK | Alarm OK | Battery level: 93%
2021-09-10 22:00:23 | Image OK | Alarm OK | Battery level: 93%
2021-09-10 23:00:19 | Image OK | Alarm OK | Battery level: 92%
2021-09-11 00:00:22 | Image OK | Alarm OK | Battery level: 92%
hwclock: Cannot access the Hardware Clock via any known method.
hwclock: Use the --verbose option to see the details of our search for an access method.
2021-09-11 01:00:20 | Image OK | Alarm OK | Battery level: 91%
2021-09-11 02:00:21 | Image OK | Alarm OK | Battery level: 90%
2021-09-11 03:00:23 | Image OK | Alarm OK | Battery level: 90%
Current Script:
import pijuice, time, os
import subprocess
import sys
from time import sleep
from datetime import datetime
from picamera import PiCamera
def CheckTime():
while not os.path.exists('/dev/i2c-1'):
time.sleep(0.1)
pj = pijuice.PiJuice(1, 0x14)
subprocess.call(["sudo", "hwclock", "--hctosys"])
return pj
def ImageCapture():
try:
ImageCaptureState = 'Image NoINFO'
camera = PiCamera()
camera.resolution = (4056, 3040)
#FullResolution = 4056 x 3040
camera.framerate = 30
camera.shutter_speed = 33333
camera.iso = 100
camera.start_preview(resolution=(400, 300))
time.sleep(30)
camera.exposure_mode = 'off'
outfile = "/mnt/OGC/RAW/%s.jpg" % (datetime.now().strftime("%Y%m%d %H%M%S"))
camera.capture(outfile)
camera.close()
ImageCaptureState = 'Image OK'
return ImageCaptureState
except Exception as err:
ImageCaptureState = 'Image ERROR'
return ImageCaptureState
def setAutomaticWakeUP(pj, ImageCaptureState):
a={}
a['year'] = 'EVERY_YEAR'
a['month'] = 'EVERY_MONTH'
a['day'] = 'EVERY_DAY'
a['hour'] = 'EVERY_HOUR'
a['minute'] = 57
a['second'] = 00
status = pj.rtcAlarm.SetAlarm(a)
battery = pj.status.GetChargeLevel()
if status['error'] != 'NO_ERROR':
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " | " + ImageCaptureState + " | Alarm ERROR | Battery level: " + str(battery['data']) + "%")
sys.exit()
else:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " | " + ImageCaptureState + " | Alarm OK | Battery level: " + str(battery['data']) + "%")
def TurnOff(pj):
pj.rtcAlarm.ClearAlarmFlag()
pj.rtcAlarm.SetWakeupEnabled(True)
time.sleep(0.4)
pj.power.SetPowerOff(20)
subprocess.call(["sudo", "poweroff"])
pj = CheckTime()
sleep(1)
ImageCaptureState = ImageCapture()
sleep(1)
setAutomaticWakeUP(pj, ImageCaptureState)
sleep(1)
TurnOff(pj)
exit()
If you have an internet connection you do not need the line: subprocess.call(["sudo", "hwclock", "--hctosys"])
.
The timesyncd will sync the system time from the internet.
Also with an internet connection you could store your picture in a tmpfs location, e.g. /run (=ram storage)
and copy/store it on another computer using e.g. scp or samba (smb mounts) and delete it after successful copy.
Thank you for your quick answer. Point is that I don't have access to internet all the time.
Right now I removed this row from my script.
subprocess.call(["sudo", "hwclock", "--hctosys"]).
And it is working OK without the internet, and only on battery even with USB flash drive connected.
I think that because I am starting Crontab 2 minutes after boot, system have plenty of time to synchronize time from RTC to system clock.
Or do I have to add this row of command and write some loop which will try several times to execute it? In case the same error occured again?
Right now it seems not to be necessary.
What is your look? Thanks, Jan
Just to circle back, my use case will put the pi-0W in the field with NO internet ever. I had copied the code above:
subprocess.call(["sudo", "hwclock", "--hctosys"])
and I also get the hwclock: Cannot access the Hardware Clock via any known method. hwclock: Use the --verbose option to see the details of our search for an access method.
Testing in-home before deploying in the field gives similar results where the pi fails to wake up. In my troubleshooting I found the code snippet about trying to set hardware clock to pi time thinking that might solve my problem.
What's not clear from the discussion above (and is my question here now): Do I need a way to set the time of the pi when it is deployed in the field without internet? If so, what options do I have?
Firmware for pijuice is up to date, fresh install of bullseye today and pijuice software.
Thanks.
Related question: above there is a discussion about this:
dtoverlay=i2c-rtc,ds1307=1)
This was not in my config.txt file. How would one know that this line of code should be added without searching for the experiences of others? I'm only asking because I feel as though I'm missing something that I should have known. thanks.
Good morning,
I want to make a device which will be power efficient and turn on only few times per day for 3 minutes do its work and shuts down.
Problem which I have is that I am still not 100% sure that PiJuice will start on specified time. I have spent last 5 days trying to get the information from Github Issues. But there is no straightforward guide.
My setup is: Raspberry Pi Zero with latest Raspbian PiJuice Zero with latest firmware Power connected to PiJuice Zero No battery now, but it will have original PiJuice battery with Solar panel in future...
Device will have internet connection, but I want from it to work also without the Internet connection. Best will be to update time from internet when accessible, and rely on RTC when Internet is not accessible.
Address for HAT 50 in config.txt I have dtoverlay=i2c-rtc,ds1339=1 In i2cdetect -y1 I am getting UU in position 68 crontab entry: @reboot /usr/bin/python3 /mnt/OGC/001_CurrentScript/Script.py
I used this post #91 which helped me a lot. https://github.com/PiSupply/PiJuice/issues/91#issuecomment-381324352
This current script worked for me with no problem for 24 hours.
But when I update few parameters: DELTA_MIN=15 and lenght of fake job for 180 sec
Result was that after few hours device was stucked in power off and no wakeup occured, more info on this log file: My Timezone is UTC + 2 Boot of Raspberry takes aproximately 1 minute because of USB mounted drive.
You can see that the period is 19 minutes (1 minute BOOT, 3 minutes JOB and 15 minutes for next START)
See last row has wrong time. Year 2000. This was the last start followed by shutdown. After that no wakeup occured.
Could you please help me to make it reliable? Thank you very much, Jan
My current code: