diff --git a/MH-Z19/write_data.py b/MH-Z19/write_data.py
index 141fecc..cd2789d 100755
--- a/MH-Z19/write_data.py
+++ b/MH-Z19/write_data.py
@@ -5,9 +5,8 @@
| |__| |_| / __/
\____\___/_____|
-Script to get CO2 values and write it to text file
-need parameter: CO2_port
-/usr/bin/python3 /var/www/moduleair_pro_4g/MH-Z19/write_data.py ttyAMA4
+Script to get CO2 values and write it to the database
+/usr/bin/python3 /var/www/moduleair_pro_4g/MH-Z19/write_data.py
'''
import serial
@@ -16,13 +15,32 @@ import json
import sys
import subprocess
import time
+import sqlite3
-parameter = sys.argv[1:] # Exclude the script name
-#print("Parameters received:")
-port='/dev/'+parameter[0]
+
+
+
+# Connect to the SQLite database
+conn = sqlite3.connect("/var/www/moduleair_pro_4g/sqlite/sensors.db")
+cursor = conn.cursor()
+
+def load_config(config_file):
+ try:
+ with open(config_file, 'r') as file:
+ config_data = json.load(file)
+ return config_data
+ except Exception as e:
+ print(f"Error loading config file: {e}")
+ return {}
+
+
+# Load the configuration data
+config_file = '/var/www/moduleair_pro_4g/config.json'
+config = load_config(config_file)
+mh_z19_port = config.get('MH-Z19_port', '') #port du NPM solo
ser = serial.Serial(
- port=port,
+ port=mh_z19_port,
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
@@ -78,10 +96,23 @@ def main():
co2 = read_co2()
if co2 is not None:
print(f"CO2 Concentration: {co2} ppm")
+ #save to file
output_file = "/var/www/moduleair_pro_4g/matrix/input_MHZ16.txt"
with open(output_file, "w") as file:
file.write(f"{co2} \n")
print(f"Data written to {output_file}")
+ #GET RTC TIME from SQlite
+ 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'
+ #save to sqlite
+ cursor.execute('''
+ INSERT INTO data_CO2 (timestamp,CO2) VALUES (?,?)'''
+ , (rtc_time_str,co2))
+
+ # Commit and close the connection
+ conn.commit()
+
else:
print("Failed to get CO2 data.")
@@ -89,6 +120,7 @@ def main():
print("Program terminated.")
finally:
ser.close()
+ conn.close()
if __name__ == '__main__':
main()
diff --git a/SARA/reboot/start.py b/SARA/reboot/start.py
new file mode 100644
index 0000000..7b37c10
--- /dev/null
+++ b/SARA/reboot/start.py
@@ -0,0 +1,190 @@
+'''
+ ____ _ ____ _
+ / ___| / \ | _ \ / \
+ \___ \ / _ \ | |_) | / _ \
+ ___) / ___ \| _ < / ___ \
+ |____/_/ \_\_| \_\/_/ \_\
+
+Script that starts at the boot of the RPI (with cron)
+
+@reboot sleep 30 && /usr/bin/python3 /var/www/moduleair_pro_4g/SARA/reboot/start.py >> /var/www/moduleair_pro_4g/logs/app.log 2>&1
+
+/usr/bin/python3 /var/www/moduleair_pro_4g/SARA/reboot/start.py
+
+'''
+import serial
+import time
+import sys
+import json
+import re
+
+#get data from config
+def load_config(config_file):
+ try:
+ with open(config_file, 'r') as file:
+ config_data = json.load(file)
+ return config_data
+ except Exception as e:
+ print(f"Error loading config file: {e}")
+ return {}
+
+#Fonction pour mettre à jour le JSON de configuration
+def update_json_key(file_path, key, value):
+ """
+ Updates a specific key in a JSON file with a new value.
+
+ :param file_path: Path to the JSON file.
+ :param key: The key to update in the JSON file.
+ :param value: The new value to assign to the key.
+ """
+ try:
+ # Load the existing data
+ with open(file_path, "r") as file:
+ data = json.load(file)
+
+ # Check if the key exists in the JSON file
+ if key in data:
+ data[key] = value # Update the key with the new value
+ else:
+ print(f"Key '{key}' not found in the JSON file.")
+ return
+
+ # Write the updated data back to the file
+ with open(file_path, "w") as file:
+ json.dump(data, file, indent=2) # Use indent for pretty printing
+
+ print(f"💾 updating '{key}' to '{value}'.")
+ except Exception as e:
+ print(f"Error updating the JSON file: {e}")
+
+# Define the config file path
+config_file = '/var/www/moduleair_pro_4g/config.json'
+# Load the configuration data
+config = load_config(config_file)
+baudrate = config.get('SaraR4_baudrate', 115200) #baudrate du sara R4
+device_id = config.get('deviceID', '').upper() #device ID en maj
+
+ser_sara = serial.Serial(
+ port='/dev/ttyAMA2',
+ baudrate=baudrate, #115200 ou 9600
+ parity=serial.PARITY_NONE, #PARITY_NONE, PARITY_EVEN or PARITY_ODD
+ stopbits=serial.STOPBITS_ONE,
+ bytesize=serial.EIGHTBITS,
+ timeout = 2
+)
+
+def read_complete_response(serial_connection, timeout=2, end_of_response_timeout=2, wait_for_lines=None, debug=True):
+ '''
+ Fonction très importante !!!
+ Reads the complete response from a serial connection and waits for specific lines.
+ '''
+ if wait_for_lines is None:
+ wait_for_lines = [] # Default to an empty list if not provided
+
+ response = bytearray()
+ serial_connection.timeout = timeout
+ end_time = time.time() + end_of_response_timeout
+ start_time = time.time()
+
+ while True:
+ elapsed_time = time.time() - start_time # Time since function start
+ if serial_connection.in_waiting > 0:
+ data = serial_connection.read(serial_connection.in_waiting)
+ response.extend(data)
+ end_time = time.time() + end_of_response_timeout # Reset timeout on new data
+
+ # Decode and check for any target line
+ decoded_response = response.decode('utf-8', errors='replace')
+ for target_line in wait_for_lines:
+ if target_line in decoded_response:
+ if debug:
+ print(f"[DEBUG] 🔎 Found target line: {target_line} (in {elapsed_time:.2f}s)")
+ return decoded_response # Return response immediately if a target line is found
+ elif time.time() > end_time:
+ if debug:
+ print(f"[DEBUG] Timeout reached. No more data received.")
+ break
+ time.sleep(0.1) # Short sleep to prevent busy waiting
+
+ # Final response and debug output
+ total_elapsed_time = time.time() - start_time
+ if debug:
+ print(f"[DEBUG] ⏱️ elapsed time: {total_elapsed_time:.2f}s. ⏱️")
+ # Check if the elapsed time exceeded 10 seconds
+ if total_elapsed_time > 10 and debug:
+ print(f"[ALERT] 🚨 The operation took too long 🚨")
+ print(f'[ALERT] ⚠️{total_elapsed_time:.2f}s⚠️')
+
+ return response.decode('utf-8', errors='replace') # Return the full response if no target line is found
+
+try:
+ print('
Start reboot python script
')
+
+ #check modem status
+ print("⚙️Check SARA Status")
+ command = f'ATI\r'
+ ser_sara.write(command.encode('utf-8'))
+ response_SARA_ATI = read_complete_response(ser_sara, wait_for_lines=["IMEI"])
+ print(response_SARA_ATI)
+ match = re.search(r"Model:\s*(.+)", response_SARA_ATI)
+ model = match.group(1).strip() if match else "Unknown" # Strip unwanted characters
+ print(f" Model: {model}")
+ update_json_key(config_file, "modem_version", model)
+ time.sleep(1)
+
+
+ # 1. Set AIRCARTO URL
+ print('➡️Set aircarto URL')
+ aircarto_profile_id = 0
+ aircarto_url="data.nebuleair.fr"
+ command = f'AT+UHTTP={aircarto_profile_id},1,"{aircarto_url}"\r'
+ ser_sara.write(command.encode('utf-8'))
+ response_SARA_1 = read_complete_response(ser_sara, wait_for_lines=["OK"])
+ print(response_SARA_1)
+ time.sleep(1)
+
+ #2. Set uSpot URL
+ print('➡️Set uSpot URL')
+ uSpot_profile_id = 1
+ uSpot_url="api-prod.uspot.probesys.net"
+ command = f'AT+UHTTP={uSpot_profile_id},1,"{uSpot_url}"\r'
+ ser_sara.write(command.encode('utf-8'))
+ response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["OK"])
+ print(response_SARA_2)
+ time.sleep(1)
+
+ print("set port 81")
+ command = f'AT+UHTTP={uSpot_profile_id},5,81\r'
+ ser_sara.write((command + '\r').encode('utf-8'))
+ response_SARA_55 = read_complete_response(ser_sara, wait_for_lines=["OK"])
+ print(response_SARA_55)
+ time.sleep(1)
+
+ #3. Get localisation (CellLocate)
+ mode = 2
+ sensor = 2
+ response_type = 0
+ timeout_s = 2
+ accuracy_m = 1
+ command = f'AT+ULOC={mode},{sensor},{response_type},{timeout_s},{accuracy_m}\r'
+ ser_sara.write((command + '\r').encode('utf-8'))
+ response_SARA_3 = read_complete_response(ser_sara, wait_for_lines=["+UULOC"])
+ print(response_SARA_3)
+
+ match = re.search(r"\+UULOC: \d{2}/\d{2}/\d{4},\d{2}:\d{2}:\d{2}\.\d{3},([-+]?\d+\.\d+),([-+]?\d+\.\d+)", response_SARA_3)
+ if match:
+ latitude = match.group(1)
+ longitude = match.group(2)
+ print(f"📍 Latitude: {latitude}, Longitude: {longitude}")
+ else:
+ print("❌ Failed to extract coordinates.")
+
+ #update config.json
+ update_json_key(config_file, "latitude_raw", float(latitude))
+ update_json_key(config_file, "longitude_raw", float(longitude))
+
+ time.sleep(1)
+
+except Exception as e:
+ print("An error occurred:", e)
+ traceback.print_exc() # This prints the full traceback
\ No newline at end of file
diff --git a/boot_hotspot.sh b/boot_hotspot.sh
index 567cae9..a01fb42 100755
--- a/boot_hotspot.sh
+++ b/boot_hotspot.sh
@@ -8,25 +8,11 @@
OUTPUT_FILE="/var/www/moduleair_pro_4g/wifi_list.csv"
JSON_FILE="/var/www/moduleair_pro_4g/config.json"
-
-
echo "-------------------"
echo "-------------------"
echo "NebuleAir pro started at $(date)"
-# Blink GPIO 23 and 24 five times
-for i in {1..5}; do
- # Turn GPIO 23 and 24 ON
- gpioset gpiochip0 23=1 24=1
- #echo "LEDs ON"
- sleep 1
-
- # Turn GPIO 23 and 24 OFF
- gpioset gpiochip0 23=0 24=0
- #echo "LEDs OFF"
- sleep 1
-done
echo "getting SARA R4 serial number"
# Get the last 8 characters of the serial number and write to text file
@@ -34,6 +20,7 @@ serial_number=$(cat /proc/cpuinfo | grep Serial | awk '{print substr($3, length(
# Define the JSON file path
# Use jq to update the "deviceID" in the JSON file
jq --arg serial_number "$serial_number" '.deviceID = $serial_number' "$JSON_FILE" > temp.json && mv temp.json "$JSON_FILE"
+
echo "id: $serial_number"
#get the SSH port for tunneling
SSH_TUNNEL_PORT=$(jq -r '.sshTunnel_port' "$JSON_FILE")
@@ -59,7 +46,7 @@ if [ "$STATE" == "30 (disconnected)" ]; then
else
- echo "Success: wlan0 is connected!"
+ echo "🛜Success: wlan0 is connected!🛜"
CONN_SSID=$(nmcli -g GENERAL.CONNECTION device show wlan0)
echo "Connection: $CONN_SSID"
@@ -69,27 +56,27 @@ else
sudo chmod 777 "$JSON_FILE"
# Lancer le tunnel SSH
- echo "Démarrage du tunnel SSH sur le port $SSH_TUNNEL_PORT..."
+ #echo "Démarrage du tunnel SSH sur le port $SSH_TUNNEL_PORT..."
# Start the SSH agent if it's not already running
- eval "$(ssh-agent -s)"
+ #eval "$(ssh-agent -s)"
# Add your SSH private key
- ssh-add /home/airlab/.ssh/id_rsa
+ #ssh-add /home/airlab/.ssh/id_rsa
#connections details
- REMOTE_USER="airlab_server1" # Remplacez par votre nom d'utilisateur distant
- REMOTE_SERVER="aircarto.fr" # Remplacez par l'adresse de votre serveur
- LOCAL_PORT=22 # Port local à rediriger
- MONITOR_PORT=0 # Désactive la surveillance de connexion autossh
+ #REMOTE_USER="airlab_server1" # Remplacez par votre nom d'utilisateur distant
+ #REMOTE_SERVER="aircarto.fr" # Remplacez par l'adresse de votre serveur
+ #LOCAL_PORT=22 # Port local à rediriger
+ #MONITOR_PORT=0 # Désactive la surveillance de connexion autossh
#autossh -M "$MONITOR_PORT" -f -N -R "$SSH_TUNNEL_PORT:localhost:$LOCAL_PORT" "$REMOTE_USER@$REMOTE_SERVER" -p 50221
# ssh -f -N -R 52221:localhost:22 -p 50221 airlab_server1@aircarto.fr
- ssh -i /var/www/.ssh/id_rsa -f -N -R "$SSH_TUNNEL_PORT:localhost:$LOCAL_PORT" -p 50221 -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_SERVER"
+ #ssh -i /var/www/.ssh/id_rsa -f -N -R "$SSH_TUNNEL_PORT:localhost:$LOCAL_PORT" -p 50221 -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_SERVER"
#Check if the tunnel was created successfully
- if [ $? -eq 0 ]; then
- echo "Tunnel started successfully!"
- else
- echo "Error: Unable to start the tunnel!"
- exit 1
- fi
+ #if [ $? -eq 0 ]; then
+ # echo "Tunnel started successfully!"
+ #else
+ # echo "Error: Unable to start the tunnel!"
+ # exit 1
+ #fi
fi
echo "-------------------"
diff --git a/config.json.dist b/config.json.dist
index 7a1cd14..caacfff 100755
--- a/config.json.dist
+++ b/config.json.dist
@@ -5,10 +5,18 @@
"RTC/save_to_db.py": true,
"BME280/get_data_v2.py": true,
"envea/read_value_v2.py": true,
+ "MH-Z19/write_data.py": true,
"sqlite/flush_old_data.py": true,
+ "deviceName": "ModuleAir-proXXX",
"deviceID": "XXXXX",
+ "modem_version": "XXX",
+ "latitude_raw": 0,
+ "longitude_raw":0,
+ "latitude_precision": 0,
+ "longitude_precision": 0,
"SaraR4_baudrate": 115200,
"NPM_solo_port": "/dev/ttyAMA3",
+ "MH-Z19_port": "/dev/ttyAMA4",
"NextPM_ports": ["ttyAMA3"],
"CO2_serial": true,
"sensirion_SFA30": false,
@@ -19,5 +27,8 @@
"SARA_R4_general_status" : "connected",
"SARA_R4_SIM_status" : "connected",
"SARA_R4_network_status": "connected",
+ "SARA_R4_neworkID": 0,
+ "send_aircarto": true,
+ "send_uSpot": false,
"WIFI_status": "connected"
}
\ No newline at end of file
diff --git a/cron_jobs b/cron_jobs
index 1be3c50..28d0d87 100755
--- a/cron_jobs
+++ b/cron_jobs
@@ -2,7 +2,7 @@
@reboot /var/www/moduleair_pro_4g/boot_hotspot.sh >> /var/www/moduleair_pro_4g/logs/app.log 2>&1
-@reboot sleep 30 && /usr/bin/python3 /var/www/moduleair_pro_4g/SARA/sara_setURL.py ttyAMA2 data.moduleair.fr 0 >> /var/www/moduleair_pro_4g/logs/app.log 2>&1
+@reboot sleep 30 && /usr/bin/python3 /var/www/moduleair_pro_4g/SARA/reboot/start.py >> /var/www/moduleair_pro_4g/logs/app.log 2>&1
@reboot /var/www/moduleair_pro_4g/run_every_10_seconds.sh
diff --git a/html/database.html b/html/database.html
index 45ea328..696e2a0 100755
--- a/html/database.html
+++ b/html/database.html
@@ -68,6 +68,8 @@
+
+
@@ -90,6 +92,8 @@
+
+
@@ -255,6 +259,11 @@ function get_data_sqlite(table, limit, download , startDate = "", endDate = "")