update
This commit is contained in:
77
RTC/read.py
Executable file
77
RTC/read.py
Executable file
@@ -0,0 +1,77 @@
|
||||
'''
|
||||
____ _____ ____
|
||||
| _ \_ _/ ___|
|
||||
| |_) || || |
|
||||
| _ < | || |___
|
||||
|_| \_\|_| \____|
|
||||
|
||||
Script to read time from RTC module
|
||||
I2C connection
|
||||
Address 0x68
|
||||
/usr/bin/python3 /var/www/moduleair_pro_4g/RTC/read.py
|
||||
'''
|
||||
import smbus2
|
||||
import time
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
# DS3231 I2C address
|
||||
DS3231_ADDR = 0x68
|
||||
|
||||
# Registers for DS3231
|
||||
REG_TIME = 0x00
|
||||
|
||||
def bcd_to_dec(bcd):
|
||||
return (bcd // 16 * 10) + (bcd % 16)
|
||||
|
||||
def read_time(bus):
|
||||
"""Try to read and decode time from the RTC module (DS3231)."""
|
||||
try:
|
||||
data = bus.read_i2c_block_data(DS3231_ADDR, REG_TIME, 7)
|
||||
seconds = bcd_to_dec(data[0] & 0x7F)
|
||||
minutes = bcd_to_dec(data[1])
|
||||
hours = bcd_to_dec(data[2] & 0x3F)
|
||||
day = bcd_to_dec(data[4])
|
||||
month = bcd_to_dec(data[5])
|
||||
year = bcd_to_dec(data[6]) + 2000
|
||||
return datetime(year, month, day, hours, minutes, seconds)
|
||||
except OSError:
|
||||
return None # RTC module not connected
|
||||
|
||||
def main():
|
||||
# Read RTC time
|
||||
bus = smbus2.SMBus(1)
|
||||
# Try to read RTC time
|
||||
rtc_time = read_time(bus)
|
||||
|
||||
# Get current system time
|
||||
system_time = datetime.now() #local
|
||||
utc_time = datetime.utcnow() #UTC
|
||||
|
||||
# If RTC is not connected, set default message
|
||||
# Calculate time difference (in seconds) if RTC is connected
|
||||
if rtc_time:
|
||||
rtc_time_str = rtc_time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
time_difference = int((utc_time - rtc_time).total_seconds()) # Convert to int
|
||||
else:
|
||||
rtc_time_str = "not connected"
|
||||
time_difference = "N/A" # Not applicable
|
||||
|
||||
# Print both times
|
||||
#print(f"RTC module Time: {rtc_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
#print(f"Sys local Time: {system_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
#print(f"Sys UTC Time: {utc_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
|
||||
# Create JSON output
|
||||
time_data = {
|
||||
"rtc_module_time":rtc_time_str,
|
||||
"system_local_time": system_time.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
"system_utc_time": utc_time.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
"time_difference_seconds": time_difference
|
||||
}
|
||||
|
||||
print(json.dumps(time_data, indent=4))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
90
RTC/save_to_db.py
Executable file
90
RTC/save_to_db.py
Executable file
@@ -0,0 +1,90 @@
|
||||
'''
|
||||
____ _____ ____
|
||||
| _ \_ _/ ___|
|
||||
| |_) || || |
|
||||
| _ < | || |___
|
||||
|_| \_\|_| \____|
|
||||
|
||||
Script to read time from RTC module and save it to DB
|
||||
I2C connection
|
||||
Address 0x68
|
||||
/usr/bin/python3 /var/www/moduleair_pro_4g/RTC/save_to_db.py
|
||||
'''
|
||||
import smbus2
|
||||
import time
|
||||
import json
|
||||
from datetime import datetime
|
||||
import sqlite3
|
||||
|
||||
# Connect to (or create if not existent) the database
|
||||
conn = sqlite3.connect("/var/www/moduleair_pro_4g/sqlite/sensors.db")
|
||||
cursor = conn.cursor()
|
||||
|
||||
# DS3231 I2C address
|
||||
DS3231_ADDR = 0x68
|
||||
|
||||
# Registers for DS3231
|
||||
REG_TIME = 0x00
|
||||
|
||||
def bcd_to_dec(bcd):
|
||||
return (bcd // 16 * 10) + (bcd % 16)
|
||||
|
||||
def read_time(bus):
|
||||
"""Try to read and decode time from the RTC module (DS3231)."""
|
||||
try:
|
||||
data = bus.read_i2c_block_data(DS3231_ADDR, REG_TIME, 7)
|
||||
seconds = bcd_to_dec(data[0] & 0x7F)
|
||||
minutes = bcd_to_dec(data[1])
|
||||
hours = bcd_to_dec(data[2] & 0x3F)
|
||||
day = bcd_to_dec(data[4])
|
||||
month = bcd_to_dec(data[5])
|
||||
year = bcd_to_dec(data[6]) + 2000
|
||||
return datetime(year, month, day, hours, minutes, seconds)
|
||||
except OSError:
|
||||
return None # RTC module not connected
|
||||
|
||||
def main():
|
||||
# Read RTC time
|
||||
bus = smbus2.SMBus(1)
|
||||
# Try to read RTC time
|
||||
rtc_time = read_time(bus)
|
||||
|
||||
# Get current system time
|
||||
system_time = datetime.now() #local
|
||||
utc_time = datetime.utcnow() #UTC
|
||||
|
||||
# If RTC is not connected, set default message
|
||||
# Calculate time difference (in seconds) if RTC is connected
|
||||
if rtc_time:
|
||||
rtc_time_str = rtc_time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
time_difference = int((utc_time - rtc_time).total_seconds()) # Convert to int
|
||||
else:
|
||||
rtc_time_str = "not connected"
|
||||
time_difference = "N/A" # Not applicable
|
||||
|
||||
# Print both times
|
||||
#print(f"RTC module Time: {rtc_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
#print(f"Sys local Time: {system_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
#print(f"Sys UTC Time: {utc_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
|
||||
# Create JSON output
|
||||
time_data = {
|
||||
"rtc_module_time":rtc_time_str,
|
||||
"system_local_time": system_time.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
"system_utc_time": utc_time.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
"time_difference_seconds": time_difference
|
||||
}
|
||||
|
||||
#print(json.dumps(time_data, indent=4))
|
||||
|
||||
cursor.execute("UPDATE timestamp_table SET last_updated = ? WHERE id = 1", (rtc_time_str,))
|
||||
|
||||
# Commit and close the connection
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
#print("Sensor data saved successfully!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
97
RTC/set_with_NTP.py
Executable file
97
RTC/set_with_NTP.py
Executable file
@@ -0,0 +1,97 @@
|
||||
"""
|
||||
Script to set the RTC using an NTP server.
|
||||
RPI needs to be connected to the internet (WIFI).
|
||||
Requires ntplib and pytz:
|
||||
sudo pip3 install ntplib pytz --break-system-packages
|
||||
|
||||
/usr/bin/python3 /var/www/moduleair_pro_4g/RTC/set_with_NTP.py
|
||||
|
||||
"""
|
||||
import smbus2
|
||||
import time
|
||||
from datetime import datetime
|
||||
import ntplib
|
||||
import pytz # For timezone handling
|
||||
|
||||
# DS3231 I2C address
|
||||
DS3231_ADDR = 0x68
|
||||
|
||||
# Registers for DS3231
|
||||
REG_TIME = 0x00
|
||||
|
||||
def bcd_to_dec(bcd):
|
||||
"""Convert BCD to decimal."""
|
||||
return (bcd // 16 * 10) + (bcd % 16)
|
||||
|
||||
def dec_to_bcd(dec):
|
||||
"""Convert decimal to BCD."""
|
||||
return (dec // 10 * 16) + (dec % 10)
|
||||
|
||||
def set_time(bus, year, month, day, hour, minute, second):
|
||||
"""Set the RTC time."""
|
||||
# Convert the time to BCD format
|
||||
second_bcd = dec_to_bcd(second)
|
||||
minute_bcd = dec_to_bcd(minute)
|
||||
hour_bcd = dec_to_bcd(hour)
|
||||
day_bcd = dec_to_bcd(day)
|
||||
month_bcd = dec_to_bcd(month)
|
||||
year_bcd = dec_to_bcd(year - 2000) # DS3231 uses year from 2000
|
||||
|
||||
# Write time to DS3231
|
||||
bus.write_i2c_block_data(DS3231_ADDR, REG_TIME, [
|
||||
second_bcd,
|
||||
minute_bcd,
|
||||
hour_bcd,
|
||||
0x01, # Day of the week (1=Monday, etc.)
|
||||
day_bcd,
|
||||
month_bcd,
|
||||
year_bcd
|
||||
])
|
||||
|
||||
def read_time(bus):
|
||||
"""Read the RTC time."""
|
||||
data = bus.read_i2c_block_data(DS3231_ADDR, REG_TIME, 7)
|
||||
second = bcd_to_dec(data[0] & 0x7F)
|
||||
minute = bcd_to_dec(data[1])
|
||||
hour = bcd_to_dec(data[2] & 0x3F)
|
||||
day = bcd_to_dec(data[4])
|
||||
month = bcd_to_dec(data[5])
|
||||
year = bcd_to_dec(data[6]) + 2000
|
||||
return (year, month, day, hour, minute, second)
|
||||
|
||||
def get_internet_time():
|
||||
"""Get the current time from an NTP server."""
|
||||
ntp_client = ntplib.NTPClient()
|
||||
response = ntp_client.request('pool.ntp.org')
|
||||
utc_time = datetime.utcfromtimestamp(response.tx_time)
|
||||
return utc_time
|
||||
|
||||
def main():
|
||||
bus = smbus2.SMBus(1)
|
||||
|
||||
# Get the current time from the RTC
|
||||
year, month, day, hours, minutes, seconds = read_time(bus)
|
||||
rtc_time = datetime(year, month, day, hours, minutes, seconds)
|
||||
|
||||
# Get current UTC time from an NTP server
|
||||
try:
|
||||
internet_utc_time = get_internet_time()
|
||||
print(f"Time from Internet (UTC) : {internet_utc_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
except Exception as e:
|
||||
print(f"Error retrieving time from the internet: {e}")
|
||||
return
|
||||
|
||||
# Print current RTC time
|
||||
print(f"Actual RTC Time : {rtc_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
|
||||
# Set the RTC to UTC time
|
||||
set_time(bus, internet_utc_time.year, internet_utc_time.month, internet_utc_time.day,
|
||||
internet_utc_time.hour, internet_utc_time.minute, internet_utc_time.second)
|
||||
|
||||
# Read and print the new time from RTC
|
||||
year, month, day, hour, minute, second = read_time(bus)
|
||||
rtc_time_new = datetime(year, month, day, hour, minute, second)
|
||||
print(f"New RTC Time (UTC) : {rtc_time_new.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
47
RTC/set_with_browserTime.py
Executable file
47
RTC/set_with_browserTime.py
Executable file
@@ -0,0 +1,47 @@
|
||||
"""
|
||||
Script to set the RTC using the browser time.
|
||||
|
||||
/usr/bin/python3 /var/www/moduleair_pro_4g/RTC/set_with_browserTime.py '2024-01-30 12:48:39'
|
||||
|
||||
"""
|
||||
|
||||
import sys
|
||||
from datetime import datetime
|
||||
import smbus2
|
||||
|
||||
# DS3231 I2C address
|
||||
DS3231_ADDR = 0x68
|
||||
REG_TIME = 0x00
|
||||
|
||||
def dec_to_bcd(dec):
|
||||
"""Convert decimal to BCD."""
|
||||
return (dec // 10 * 16) + (dec % 10)
|
||||
|
||||
def set_rtc(bus, year, month, day, hour, minute, second):
|
||||
"""Set the RTC time."""
|
||||
second_bcd = dec_to_bcd(second)
|
||||
minute_bcd = dec_to_bcd(minute)
|
||||
hour_bcd = dec_to_bcd(hour)
|
||||
day_bcd = dec_to_bcd(day)
|
||||
month_bcd = dec_to_bcd(month)
|
||||
year_bcd = dec_to_bcd(year - 2000) # RTC stores years since 2000
|
||||
|
||||
bus.write_i2c_block_data(DS3231_ADDR, REG_TIME, [
|
||||
second_bcd, minute_bcd, hour_bcd, 0x01, day_bcd, month_bcd, year_bcd
|
||||
])
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python3 set_rtc.py 'YYYY-MM-DD HH:MM:SS'")
|
||||
sys.exit(1)
|
||||
|
||||
rtc_time_str = sys.argv[1]
|
||||
rtc_time = datetime.strptime(rtc_time_str, '%Y-%m-%d %H:%M:%S')
|
||||
|
||||
bus = smbus2.SMBus(1)
|
||||
set_rtc(bus, rtc_time.year, rtc_time.month, rtc_time.day,
|
||||
rtc_time.hour, rtc_time.minute, rtc_time.second)
|
||||
print(f"RTC updated to: {rtc_time}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user