Files
nebuleair_pro_4g/sqlite/set_config.py
PaulVua 5777b35770 Add WiFi and HDMI power saving features for remote sensors
Implements power saving optimizations to extend battery life on solar-powered remote air quality sensors:

- WiFi Power Saving: Disable WiFi 10 minutes after boot to save ~100-200mA
  - Configurable via web UI checkbox in admin panel
  - WiFi automatically re-enables after reboot for 10-minute configuration window
  - Systemd timer (nebuleair-wifi-powersave.timer) manages automatic disable
  - New wifi/power_save.py script checks database config and disables WiFi via nmcli

- HDMI Disable: Added hdmi_blanking=2 to boot config to save ~20-30mA
  - Automatically configured during installation

- Database: Added wifi_power_saving boolean config (default: disabled)
  - Uses INSERT OR IGNORE for safe updates to existing installations

- UI: Added checkbox control in admin.html for WiFi power saving
  - Includes helpful description of power savings and behavior

- Services: Updated setup_services.sh and update_firmware.sh to manage new timer

Total power savings: ~120-230mA when WiFi power saving enabled

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-13 12:05:21 +01:00

112 lines
3.4 KiB
Python

r'''
____ ___ _ _ _
/ ___| / _ \| | (_) |_ ___
\___ \| | | | | | | __/ _ \
___) | |_| | |___| | || __/
|____/ \__\_\_____|_|\__\___|
Script to set the config
/usr/bin/python3 /var/www/nebuleair_pro_4g/sqlite/set_config.py
in case of readonly error:
sudo chmod 777 /var/www/nebuleair_pro_4g/sqlite/sensors.db
'''
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()
print(f"Connected to database")
# Note: Using INSERT OR IGNORE to add only new configurations without overwriting existing ones
print("Adding new configurations (existing ones will be preserved)")
# Insert general configurations
config_entries = [
("modem_config_mode", "0", "bool"),
("deviceID", "XXXX", "str"),
("latitude_raw", "0", "int"),
("longitude_raw", "0", "int"),
("latitude_precision", "0", "int"),
("longitude_precision", "0", "int"),
("deviceName", "NebuleAir-proXXX", "str"),
("SaraR4_baudrate", "115200", "int"),
("NPM_solo_port", "/dev/ttyAMA5", "str"),
("sshTunnel_port", "59228", "int"),
("SARA_R4_general_status", "connected", "str"),
("SARA_R4_SIM_status", "connected", "str"),
("SARA_R4_network_status", "connected", "str"),
("SARA_R4_neworkID", "20810", "int"),
("WIFI_status", "connected", "str"),
("send_aircarto", "0", "bool"),
("send_uSpot", "0", "bool"),
("send_miotiq", "1", "bool"),
("npm_5channel", "0", "bool"),
("envea", "0", "bool"),
("windMeter", "0", "bool"),
("BME280", "1", "bool"),
("MPPT", "0", "bool"),
("NOISE", "0", "bool"),
("modem_version", "XXX", "str"),
("language", "fr", "str"),
("wifi_power_saving", "0", "bool")
]
for key, value, value_type in config_entries:
cursor.execute(
"INSERT OR IGNORE INTO config_table (key, value, type) VALUES (?, ?, ?)",
(key, value, value_type)
)
# Clean up duplicate envea sondes first (keep only first occurrence of each name)
print("Cleaning up duplicate envea sondes...")
cursor.execute("""
DELETE FROM envea_sondes_table
WHERE id NOT IN (
SELECT MIN(id)
FROM envea_sondes_table
GROUP BY name
)
""")
deleted_count = cursor.rowcount
if deleted_count > 0:
print(f"Deleted {deleted_count} duplicate envea sonde entries")
# Insert envea sondes (only if they don't already exist)
# Attention pour le H2S il y a plusieurs sondes
# H2S 1ppm -> coef 4
# H2S 20ppm -> coef 1
# H2S 200ppm -> coef 10
envea_sondes = [
(False, "ttyAMA4", "h2s", 4), #H2S
(False, "ttyAMA3", "no2", 1),
(False, "ttyAMA3", "nh3", 100),
(False, "ttyAMA3", "so2", 4),
(False, "ttyAMA2", "o3", 1)
]
for connected, port, name, coefficient in envea_sondes:
# Check if sensor with this name already exists
cursor.execute("SELECT COUNT(*) FROM envea_sondes_table WHERE name = ?", (name,))
exists = cursor.fetchone()[0] > 0
if not exists:
cursor.execute(
"INSERT INTO envea_sondes_table (connected, port, name, coefficient) VALUES (?, ?, ?, ?)",
(1 if connected else 0, port, name, coefficient)
)
print(f"Added envea sonde: {name}")
else:
print(f"Envea sonde '{name}' already exists, skipping")
# Commit and close the connection
conn.commit()
conn.close()
print("Database updated successfully!")