update
This commit is contained in:
@@ -9,6 +9,35 @@ Script to read time from RTC module and save it to DB
|
|||||||
I2C connection
|
I2C connection
|
||||||
Address 0x68
|
Address 0x68
|
||||||
/usr/bin/python3 /var/www/nebuleair_pro_4g/RTC/save_to_db.py
|
/usr/bin/python3 /var/www/nebuleair_pro_4g/RTC/save_to_db.py
|
||||||
|
|
||||||
|
This need to be run as a system service
|
||||||
|
|
||||||
|
--> sudo nano /etc/systemd/system/rtc_save_to_db.service
|
||||||
|
|
||||||
|
⬇️
|
||||||
|
[Unit]
|
||||||
|
Description=RTC Save to DB Script
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=/usr/bin/python3 /var/www/nebuleair_pro_4g/RTC/save_to_db.py
|
||||||
|
Restart=always
|
||||||
|
RestartSec=1
|
||||||
|
User=root
|
||||||
|
WorkingDirectory=/var/www/nebuleair_pro_4g
|
||||||
|
StandardOutput=append:/var/www/nebuleair_pro_4g/logs/rtc_save_to_db.log
|
||||||
|
StandardError=append:/var/www/nebuleair_pro_4g/logs/rtc_save_to_db_errors.log
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
⬆️
|
||||||
|
|
||||||
|
|
||||||
|
sudo systemctl daemon-reload
|
||||||
|
sudo systemctl start rtc_save_to_db.service
|
||||||
|
|
||||||
|
sudo systemctl status rtc_save_to_db.service
|
||||||
|
|
||||||
'''
|
'''
|
||||||
import smbus2
|
import smbus2
|
||||||
import time
|
import time
|
||||||
@@ -16,16 +45,15 @@ import json
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
# Connect to (or create if not existent) the database
|
|
||||||
conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db")
|
|
||||||
cursor = conn.cursor()
|
|
||||||
|
|
||||||
# DS3231 I2C address
|
# DS3231 I2C address
|
||||||
DS3231_ADDR = 0x68
|
DS3231_ADDR = 0x68
|
||||||
|
|
||||||
# Registers for DS3231
|
# Registers for DS3231
|
||||||
REG_TIME = 0x00
|
REG_TIME = 0x00
|
||||||
|
|
||||||
|
# Connect to (or create if not existent) the database
|
||||||
|
DB_PATH = "/var/www/nebuleair_pro_4g/sqlite/sensors.db"
|
||||||
|
|
||||||
def bcd_to_dec(bcd):
|
def bcd_to_dec(bcd):
|
||||||
return (bcd // 16 * 10) + (bcd % 16)
|
return (bcd // 16 * 10) + (bcd % 16)
|
||||||
|
|
||||||
@@ -46,9 +74,14 @@ def read_time(bus):
|
|||||||
def main():
|
def main():
|
||||||
# Read RTC time
|
# Read RTC time
|
||||||
bus = smbus2.SMBus(1)
|
bus = smbus2.SMBus(1)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# Open a new database connection inside the loop to prevent connection loss
|
||||||
|
conn = sqlite3.connect(DB_PATH)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# Try to read RTC time
|
# Try to read RTC time
|
||||||
rtc_time = read_time(bus)
|
rtc_time = read_time(bus)
|
||||||
|
|
||||||
# Get current system time
|
# Get current system time
|
||||||
system_time = datetime.now() #local
|
system_time = datetime.now() #local
|
||||||
utc_time = datetime.utcnow() #UTC
|
utc_time = datetime.utcnow() #UTC
|
||||||
@@ -77,14 +110,18 @@ def main():
|
|||||||
|
|
||||||
#print(json.dumps(time_data, indent=4))
|
#print(json.dumps(time_data, indent=4))
|
||||||
|
|
||||||
|
# Save to database
|
||||||
|
try:
|
||||||
cursor.execute("UPDATE timestamp_table SET last_updated = ? WHERE id = 1", (rtc_time_str,))
|
cursor.execute("UPDATE timestamp_table SET last_updated = ? WHERE id = 1", (rtc_time_str,))
|
||||||
|
|
||||||
# Commit and close the connection
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
|
||||||
|
|
||||||
#print("Sensor data saved successfully!")
|
#print("Sensor data saved successfully!")
|
||||||
|
|
||||||
|
except sqlite3.Error as e:
|
||||||
|
print(f"Database error: {e}")
|
||||||
|
|
||||||
|
conn.close() # Close connection to avoid database locking issues
|
||||||
|
time.sleep(1) # Wait for 1 second before reading again
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
@@ -54,7 +54,7 @@
|
|||||||
|
|
||||||
<div class="row mb-3">
|
<div class="row mb-3">
|
||||||
|
|
||||||
<div class="col-sm-4">
|
<div class="col-sm-5">
|
||||||
<div class="card text-dark bg-light">
|
<div class="card text-dark bg-light">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h5 class="card-title">Consulter la base de donnée</h5>
|
<h5 class="card-title">Consulter la base de donnée</h5>
|
||||||
@@ -71,12 +71,13 @@
|
|||||||
<button class="btn btn-primary" onclick="get_data_sqlite('data_BME280',getSelectedLimit(),false)">Mesures Temp/Hum</button>
|
<button class="btn btn-primary" onclick="get_data_sqlite('data_BME280',getSelectedLimit(),false)">Mesures Temp/Hum</button>
|
||||||
<button class="btn btn-primary" onclick="get_data_sqlite('data_NPM_5channels',getSelectedLimit(),false)">Mesures PM (5 canaux)</button>
|
<button class="btn btn-primary" onclick="get_data_sqlite('data_NPM_5channels',getSelectedLimit(),false)">Mesures PM (5 canaux)</button>
|
||||||
<button class="btn btn-primary" onclick="get_data_sqlite('data_envea',getSelectedLimit(),false)">Sonde Cairsens</button>
|
<button class="btn btn-primary" onclick="get_data_sqlite('data_envea',getSelectedLimit(),false)">Sonde Cairsens</button>
|
||||||
|
<button class="btn btn-warning" onclick="get_data_sqlite('timestamp_table',getSelectedLimit(),false)">Timestamp Table</button>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-sm-8">
|
<div class="col-sm-5">
|
||||||
<div class="card text-dark bg-light">
|
<div class="card text-dark bg-light">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h5 class="card-title">Télécharger les données</h5>
|
<h5 class="card-title">Télécharger les données</h5>
|
||||||
@@ -255,6 +256,10 @@ function get_data_sqlite(table, limit, download , startDate = "", endDate = "")
|
|||||||
<th>O3</th>
|
<th>O3</th>
|
||||||
|
|
||||||
`;
|
`;
|
||||||
|
}else if (table === "timestamp_table") {
|
||||||
|
tableHTML += `
|
||||||
|
<th>Timestamp</th>
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
tableHTML += `</tr></thead><tbody>`;
|
tableHTML += `</tr></thead><tbody>`;
|
||||||
@@ -301,6 +306,10 @@ function get_data_sqlite(table, limit, download , startDate = "", endDate = "")
|
|||||||
<td>${columns[5]}</td>
|
<td>${columns[5]}</td>
|
||||||
|
|
||||||
`;
|
`;
|
||||||
|
}else if (table === "timestamp_table") {
|
||||||
|
tableHTML += `
|
||||||
|
<td>${columns[1]}</td>
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
tableHTML += "</tr>";
|
tableHTML += "</tr>";
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ def run_script(script_name, interval, delay=0):
|
|||||||
|
|
||||||
# Define scripts and their execution intervals (seconds)
|
# Define scripts and their execution intervals (seconds)
|
||||||
SCRIPTS = [
|
SCRIPTS = [
|
||||||
("RTC/save_to_db.py", 1, 0), # SAVE RTC time every 1 second, no delay
|
#("RTC/save_to_db.py", 1, 0), # SAVE RTC time every 1 second, no delay
|
||||||
("NPM/get_data_modbus_v3.py", 10, 0), # Get NPM data (modbus 5 channels) every 10s, with 2s delay
|
("NPM/get_data_modbus_v3.py", 10, 0), # Get NPM data (modbus 5 channels) every 10s, with 2s delay
|
||||||
("envea/read_value_v2.py", 10, 0), # Get NPM data (modbus 5 channels) every 10s, with 2s delay
|
("envea/read_value_v2.py", 10, 0), # Get NPM data (modbus 5 channels) every 10s, with 2s delay
|
||||||
("loop/SARA_send_data_v2.py", 60, 1), # Send data every 60 seconds, with 2s delay
|
("loop/SARA_send_data_v2.py", 60, 1), # Send data every 60 seconds, with 2s delay
|
||||||
|
|||||||
Reference in New Issue
Block a user