This commit is contained in:
Your Name
2025-05-19 10:26:27 +02:00
parent 61b302fe35
commit fd1d32a62b
3 changed files with 48 additions and 82 deletions

View File

@@ -29,34 +29,21 @@ sudo apt update && sudo apt install -y git gh apache2 sqlite3 php php-sqlite3 py
info "Installing Python libraries..."
sudo pip3 install pyserial requests RPi.GPIO adafruit-circuitpython-bme280 crcmod psutil gpiozero ntplib pytz --break-system-packages || error "Failed to install Python libraries."
# Ask user if they want to set up SSH keys
read -p "Do you want to set up an SSH key for /var/www? (y/n): " answer
answer=${answer,,} # Convert to lowercase
if [[ "$answer" == "y" ]]; then
info "Setting up SSH keys..."
sudo mkdir -p /var/www/.ssh
sudo chmod 700 /var/www/.ssh
if [[ ! -f /var/www/.ssh/id_rsa ]]; then
sudo ssh-keygen -t rsa -b 4096 -f /var/www/.ssh/id_rsa -N ""
success "SSH key generated successfully."
else
warning "SSH key already exists. Skipping key generation."
fi
sudo ssh-copy-id -i /var/www/.ssh/id_rsa.pub -p 50221 airlab_server1@aircarto.fr || warning "Failed to copy SSH key. Please check the server connection."
success "SSH setup complete!"
else
warning "Skipping SSH key setup."
fi
# Clone the repository (check if it exists first)
REPO_DIR="/var/www/nebuleair_pro_4g"
if [[ -d "$REPO_DIR" ]]; then
warning "Repository already exists. Skipping clone."
warning "Repository already exists. Will update instead of clone."
# Save current directory
local current_dir=$(pwd)
# Navigate to repository directory
cd "$REPO_DIR"
# Stash any local changes
sudo git stash || warning "Failed to stash local changes"
# Pull latest changes
sudo git pull || error "Failed to pull latest changes"
# Return to original directory
cd "$current_dir"
success "Repository updated successfully!"
else
info "Cloning the NebuleAir Pro 4G repository..."
sudo git clone http://gitea.aircarto.fr/PaulVua/nebuleair_pro_4g.git "$REPO_DIR" || error "Failed to clone repository."
@@ -98,6 +85,7 @@ if [[ -f "$REPO_DIR/sqlite/set_config.py" ]]; then
else
warning "Database creation script not found."
fi
# Configure Apache
info "Configuring Apache..."
APACHE_CONF="/etc/apache2/sites-available/000-default.conf"
@@ -140,7 +128,6 @@ success "I2C ports enabled."
info "Creates sqlites databases..."
/usr/bin/python3 /var/www/nebuleair_pro_4g/sqlite/create_db.py
# Completion message
success "Setup completed successfully!"
info "System will reboot in 5 seconds..."

View File

@@ -27,8 +27,8 @@ fi
info "Set up the RTC"
/usr/bin/python3 /var/www/nebuleair_pro_4g/RTC/set_with_NTP.py
#Check SARA R4 connection
info "Check SARA R4 connection"
#Check SARA connection
info "Check SARA connection"
/usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/sara.py ttyAMA2 ATI 2
#set up SARA R4 APN
@@ -44,45 +44,19 @@ info "Connect SARA R4 to network"
python3 /var/www/nebuleair_pro_4g/SARA/sara_connectNetwork.py ttyAMA2 20810 60
#Need to create the two service
# 1. master_nebuleair
# 1. start the scripts to set-up the services
# 2. rtc_save_to_db
#1. Add master_nebuleair.service
SERVICE_FILE="/etc/systemd/system/master_nebuleair.service"
info "Setting up systemd service for master_nebuleair..."
# Create the systemd service file (overwrite if necessary)
sudo bash -c "cat > $SERVICE_FILE" <<EOF
[Unit]
Description=Master manager for the Python loop scripts
After=network.target
[Service]
ExecStart=/usr/bin/python3 /var/www/nebuleair_pro_4g/master.py
Restart=always
User=root
WorkingDirectory=/var/www/nebuleair_pro_4g
StandardOutput=append:/var/www/nebuleair_pro_4g/logs/master.log
StandardError=append:/var/www/nebuleair_pro_4g/logs/master_errors.log
[Install]
WantedBy=multi-user.target
EOF
success "Systemd service file created: $SERVICE_FILE"
# Reload systemd to recognize the new service
info "Reloading systemd daemon..."
sudo systemctl daemon-reload
# Enable the service to start on boot
info "Enabling the service to start on boot..."
sudo systemctl enable master_nebuleair.service
# Start the service immediately
info "Starting the service..."
sudo systemctl start master_nebuleair.service
#1. set-up the services (SARA, NPM, BME280, etc)
info "Setting up systemd services..."
if [[ -f "$REPO_DIR/services/setup_services.sh" ]]; then
sudo chmod +x "$REPO_DIR/services/setup_services.sh"
sudo "$REPO_DIR/services/setup_services.sh" || warning "Failed to set up systemd services"
success "Systemd services set up successfully."
else
warning "Systemd services setup script not found."
fi
#2. Add rtc_save_to_db.service
SERVICE_FILE_2="/etc/systemd/system/rtc_save_to_db.service"

View File

@@ -165,7 +165,7 @@ def blink_led(pin, blink_count, delay=1):
finally:
GPIO.output(pin, GPIO.LOW) # Ensure LED is off
print(f"LED on GPIO {pin} turned OFF (cleanup avoided)")
#print(f"LED on GPIO {pin} turned OFF (cleanup avoided)")
#get config data from SQLite table
def load_config_sqlite():
@@ -652,17 +652,17 @@ try:
'''
print('<h3>START LOOP</h3>')
print(f'Modem version: {modem_version}')
#print(f'Modem version: {modem_version}')
#Local timestamp
#ATTENTION:
# -> RTC module can be deconnected ""
# -> RTC module can be out of time like "2000-01-01T00:55:21Z"
print("Getting local timestamp")
cursor.execute("SELECT * FROM timestamp_table LIMIT 1")
row = cursor.fetchone() # Get the first (and only) row
rtc_time_str = row[1] # '2025-02-07 12:30:45' ou '2000-01-01 00:55:21' ou 'not connected'
print(rtc_time_str)
print(f"Getting local timestamp: {rtc_time_str}")
if rtc_time_str == 'not connected':
print("⛔ Atttention RTC module not connected⛔")
@@ -676,14 +676,14 @@ try:
print("⛔ Attention: RTC has been reset to default date ⛔")
rtc_status = "reset"
else:
print("✅ RTC timestamp is valid")
#print("✅ RTC timestamp is valid")
rtc_status = "valid"
# Always convert to InfluxDB format
# Convert to InfluxDB RFC3339 format with UTC 'Z' suffix
influx_timestamp = dt_object.strftime('%Y-%m-%dT%H:%M:%SZ')
rtc_status = "valid"
print(influx_timestamp)
#print(influx_timestamp)
#NEXTPM
# We take the last measures (order by rowid and not by timestamp)
@@ -826,21 +826,22 @@ try:
else:
print("No data available in the database.")
print("Verify SARA R4 connection")
#print("Verify SARA connection (AT)")
# Getting the LTE Signal (AT+CSQ)
print("Getting SARA LTE signal")
# Getting the LTE Signal
print("Getting LTE signal")
command = f'AT+CSQ\r'
ser_sara.write((command + '\r').encode('utf-8'))
response2 = read_complete_response(ser_sara, wait_for_lines=["OK", "ERROR", "+CME ERROR","Socket:bind"])
print('<p class="text-danger-emphasis">')
print(response2)
print("</p>", end="")
#Here it's possible that the SARA do not repond at all or send a error message
#-> TO DO : harware reboot
#-> send notification
#-> hardware reboot
#-> end loop, no need to continue
#1. No answer at all form SARA
@@ -851,6 +852,12 @@ try:
#Send notification (WIFI)
send_error_notification(device_id, "SERIAL ISSUE ->no answer from sara")
#Hardware Reboot
hardware_reboot_success = modem_hardware_reboot()
if hardware_reboot_success:
print("✅Modem successfully rebooted and reinitialized")
else:
print("⛔There were issues with the modem reboot/reinitialize process")
#end loop
sys.exit()
@@ -874,7 +881,7 @@ try:
print("<hr>")
#Send notification (WIFI)
send_error_notification(device_id, "SERIAL ISSUE -> Treck TCP/IP stack error")
#Software Reboot
#hardware reboot
hardware_reboot_success = modem_hardware_reboot()
if hardware_reboot_success:
print("✅Modem successfully rebooted and reinitialized")
@@ -932,7 +939,7 @@ try:
print("Open JSON:")
command = f'AT+UDWNFILE="sensordata_csv.json",{size_of_string}\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_1 = read_complete_response(ser_sara, wait_for_lines=[">"], debug=True)
response_SARA_1 = read_complete_response(ser_sara, wait_for_lines=[">"], debug=False)
print('<p class="text-danger-emphasis">')
print(response_SARA_1)
print("</p>", end="")
@@ -942,10 +949,8 @@ try:
#2. Write to shell
print("Write data to memory:")
ser_sara.write(csv_string.encode())
response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["OK"], debug=True)
print('<p class="text-danger-emphasis">')
print(response_SARA_2)
print("</p>", end="")
response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["OK"], debug=False)
print(f'<p class="text-danger-emphasis">{response_SARA_2.strip()}</p>', end="")
#3. Send to endpoint (with device ID)
print("Send data (POST REQUEST):")