View: 710|Reply: 3

Battery monitor

[Copy link]

2

Threads

2

Posts

12

Credits

Newbie

Rank: 1

Credits
12
Post time 2023-8-21 23:57:37 | Show all posts |Read mode
I have created a circuit for a battery monitor using an ADS1115 analog-to-digital converter and voltage sensor. I can read the battery voltage using the sensor. I want to display the battery status on the mini OLED.
Initially, I try putting the code in the webServer.py, replacing "Adeept.com", and added the function which returns battery status (BatStat()). But this only gives battery status at the boot time because this piece of code only executes at the beginning. How can I properly place the code so that the OLED information continuously updates?
  1. try:
  2.     import OLED
  3.     screen = OLED.OLED_ctrl()
  4.     screen.start()
  5.     screen.screen_show(1, str(BatStat()))
  6. except:
  7.     OLED_connection = 0
  8.     print('OLED disconnected')
  9.     pass
Copy the Code


Reply

Use magic Report

0

Threads

4

Posts

12

Credits

Newbie

Rank: 1

Credits
12
Post time 2024-6-5 19:46:24 | Show all posts
o continuously update the battery status on the mini OLED, you should run the BatStat() function in a loop. Here’s how you can modify your webServer.py script to achieve this:

Move the OLED update code into a function: Create a function that updates the OLED with the latest battery status.

Use an infinite loop to continuously update the OLED: Ensure the loop sleeps for a short period to avoid overwhelming the CPU.

Here's an example of how you can implement this:

python
Копировать код
import time
from oled import OLED  # Assuming you have an OLED module to interface with the display
from ads1115 import ADS1115  # Assuming you have a module to interface with ADS1115

# Initialize your OLED display and ADS1115 ADC
oled = OLED()
adc = ADS1115()

def BatStat():
    # Read battery voltage using the ADC
    voltage = adc.read_voltage()
    # Calculate battery status based on voltage (implement your logic here)
    if voltage > 4.0:
        return "Battery Full"
    elif voltage > 3.5:
        return "Battery Medium"
    else:
        return "Battery Low"

def update_oled():
    while True:
        # Get battery status
        status = BatStat()
        # Clear the OLED display
        oled.clear()
        # Display the battery status on the OLED
        oled.display_text(status)
        # Sleep for a while before updating again
        time.sleep(5)  # Update every 5 seconds

if __name__ == "__main__":
    # Start the OLED update loop
    update_oled()
Explanation:
Initialization: Initialize your OLED display and ADS1115 ADC at the beginning.
BatStat() Function: This function reads the battery voltage and returns a status string based on the voltage.
update_oled() Function: This function runs an infinite loop, continuously updating the OLED display with the latest battery status. It clears the display and shows the new status every 5 seconds.
Main Block: The update_oled() function is called in the if __name__ == "__main__": block to start the loop.
Running the Script:
Ensure your script runs continuously in the background. You can use a process manager or simply run the script in a terminal window.

Additional Considerations:
Error Handling: Add appropriate error handling for robustness.
CPU Usage: Adjust the sleep duration in the loop based on how frequently you need updates.
Resource Management: Ensure proper initialization and cleanup of hardware resources to prevent issues.
This approach will keep your OLED display updated with the latest battery status continuously. Stay solar!





Reply

Use magic Report

0

Threads

3

Posts

8

Credits

Newbie

Rank: 1

Credits
8
Post time 2024-9-12 09:02:13 | Show all posts
Edited by 1726102694 at 2024-9-12 09:03 AM

To continuously update the OLED display with the battery status, you'll need to place the code that reads the battery voltage and updates the display inside a loop that runs continuously or at regular intervals. Instead of executing the battery status code once at boot time, you can incorporate a looping mechanism that periodically refreshes the display. geometry dash
Reply

Use magic Report

0

Threads

3

Posts

8

Credits

Newbie

Rank: 1

Credits
8
Post time 2024-9-12 09:04:35 | Show all posts
To continuously update the OLED display with the battery status, you'll need to place the code that reads the battery voltage and updates the display inside a loop that runs continuously or at regular intervals. Instead of executing the battery status code once at boot time, you can incorporate a looping mechanism that periodically refreshes the display.
Reply

Use magic Report

You have to log in before you can reply Login | Sign Up

Points Rules