I wrote a program to read the temperature, humidity, and pressure sensors and log value to the file. It was working fine but I just tested it by running it for few days on a triple AAA battery pack. It ran for 2+ days and I could see from the display that it was working properly.
After it had depleted its batteries, I connected the board to the computer to check out the log file but the only files on the CIRCUITPYTHON volume were a boot_out.txt, an empty log folder, and the default code.py that prints out "Hello World".
What can this kind of reset to default files to occur. Is this a know problem with letting a CP program run on batteries, particularly one that's writing to the CP volume?
Thanks,
Dave
What can cause the CP files to be lost/reset?
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- mikeysklar
- Posts: 13936
- Joined: Mon Aug 01, 2016 8:10 pm
Re: What can cause the CP files to be lost/reset?
Dave,
Was your code doing a write() followed by a flush()?
https://learn.adafruit.com/cpu-temperat ... emperature
There is also an adafruit_logger module to consider if you wanted to do more elaborate types of logging.
https://learn.adafruit.com/a-logger-for ... g-a-logger
Was your code doing a write() followed by a flush()?
Code: Select all
import time
import board
import digitalio
import microcontroller
led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()
try:
with open("/temperature.txt", "a") as fp:
while True:
temp = microcontroller.cpu.temperature
# do the C-to-F conversion here if you would like
fp.write('{0:f}\n'.format(temp))
fp.flush()
led.value = not led.value
time.sleep(1)
except OSError as e:
delay = 0.5
if e.args[0] == 28:
delay = 0.25
while True:
led.value = not led.value
time.sleep(delay)
There is also an adafruit_logger module to consider if you wanted to do more elaborate types of logging.
https://learn.adafruit.com/a-logger-for ... g-a-logger
- DaveAZ
- Posts: 23
- Joined: Sun Apr 15, 2018 7:14 pm
Re: What can cause the CP files to be lost/reset?
Thanks for the reply,
My code doesn't do a flush after the write but it does close the file after the write.
So each time through the loop I read the sensor values, display the on the Clue board display, then if I am able to open the file it appends a line of sensor values and then closes the file. It then did a time.sleep for one minute and the ran the loop again.
I did confirm that it logs data correctly even if I abruptly turn off the battery pack but when the batteries run out, it looses all the files on the CP Volume and reverts to its default state. One of the students, using the same code, also observed the same failure when his device ran until the battery voltage was too low.
I'll try adding a flush after the write and before the close to see if that addresses this problem.
My code doesn't do a flush after the write but it does close the file after the write.
So each time through the loop I read the sensor values, display the on the Clue board display, then if I am able to open the file it appends a line of sensor values and then closes the file. It then did a time.sleep for one minute and the ran the loop again.
I did confirm that it logs data correctly even if I abruptly turn off the battery pack but when the batteries run out, it looses all the files on the CP Volume and reverts to its default state. One of the students, using the same code, also observed the same failure when his device ran until the battery voltage was too low.
I'll try adding a flush after the write and before the close to see if that addresses this problem.
- danhalbert
- Posts: 4652
- Joined: Tue Aug 08, 2017 12:37 pm
Re: What can cause the CP files to be lost/reset?
When the batteries run down, if it's writing, the filesystem can be corrupted. Also, the CPU may work down to a lower voltage than the flash chip, so CircuitPython may still be running, but the flash chip may have done bad writes, etc.
If you are going to run until battery run-down, it's a good idea to check the battery voltage before you do the write, and just stop doing what you are doing if it's getting too low. The CLUE doesn't have a battery monitoring pin, but you can connect two high value resistors in series (say 100k each) between and connect them to the battery and to ground, to make a voltage divider. Then monitor the voltage at the junction of the two resistors with an AnalogIn. The reason to use a divider is to get the voltage down to 3.3V or less, so you won't fry the AnalogIn pin. So the voltage measured will be half of the actual battery voltage.
If you are going to run until battery run-down, it's a good idea to check the battery voltage before you do the write, and just stop doing what you are doing if it's getting too low. The CLUE doesn't have a battery monitoring pin, but you can connect two high value resistors in series (say 100k each) between and connect them to the battery and to ground, to make a voltage divider. Then monitor the voltage at the junction of the two resistors with an AnalogIn. The reason to use a divider is to get the voltage down to 3.3V or less, so you won't fry the AnalogIn pin. So the voltage measured will be half of the actual battery voltage.
Please be positive and constructive with your questions and comments.