From ef2bd6b895e7caa9efad5f4ed8cfb842ce3a4f9d Mon Sep 17 00:00:00 2001 From: PaulVua Date: Fri, 31 Jan 2025 17:31:14 +0100 Subject: [PATCH] update --- README.md | 3 +++ config.json.dist | 1 + loop/1_NPM/send_data.py | 39 ++++++++++++++++++--------------------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 8fa1a47..96e18fb 100755 --- a/README.md +++ b/README.md @@ -82,6 +82,9 @@ sudo chmod 777 /dev/i2c-1 Attention: sometimes activation with config.txt do not work, you need to activate i2c with `sudo raspi-config` and go to "Interface" -> I2C -> enable. +It is possible to manage raspi-config only with cli: `sudo raspi-config nonint do_i2c 0` + + I2C addresses: use `sudo i2cdetect -y 1` to check the connected devices. ### BME280 diff --git a/config.json.dist b/config.json.dist index a47b920..b1277c2 100755 --- a/config.json.dist +++ b/config.json.dist @@ -12,6 +12,7 @@ "i2C_sound": false, "i2c_BME": false, "i2c_RTC": false, + "local_storage": false, "sshTunnel_port": 59228, "npm1_status": "connected", "SARA_R4_general_status": "connected", diff --git a/loop/1_NPM/send_data.py b/loop/1_NPM/send_data.py index c6db0e1..522089e 100755 --- a/loop/1_NPM/send_data.py +++ b/loop/1_NPM/send_data.py @@ -185,6 +185,7 @@ i2C_sound_config = config.get('i2C_sound', False) #présence du capteur son send_aircarto = config.get('send_aircarto', True) #envoi sur AirCarto (data.nebuleair.fr) send_uSpot = config.get('send_uSpot', False) #envoi sur MicroSpot () npm_5channel = config.get('NextPM_5channels', False) #5 canaux du NPM +local_storage = config.get('local_storage', False) #enregistrement en local des data envea_sondes = config.get('envea_sondes', []) connected_envea_sondes = [sonde for sonde in envea_sondes if sonde.get('connected', False)] @@ -236,7 +237,7 @@ if connected_envea_sondes: -def read_complete_response(serial_connection, timeout=2, end_of_response_timeout=2, wait_for_line=None): +def read_complete_response(serial_connection, timeout=2, end_of_response_timeout=2, wait_for_line=None, debug=True): response = bytearray() serial_connection.timeout = timeout end_time = time.time() + end_of_response_timeout @@ -253,17 +254,17 @@ def read_complete_response(serial_connection, timeout=2, end_of_response_timeout if wait_for_line: decoded_response = response.decode('utf-8', errors='replace') if wait_for_line in decoded_response: - print(f"[DEBUG] 🔎Found target line: {wait_for_line}") + if debug: print(f"[DEBUG] 🔎Found target line: {wait_for_line}") break elif time.time() > end_time: - print(f"[DEBUG] Timeout reached. No more data received.") + 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 - print(f"[DEBUG] ⏱️ elapsed time: {total_elapsed_time:.2f}s. ⏱️") + 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: + if total_elapsed_time > 10 and debug: print(f"[ALERT] 🚨 The operation took too long🚨") print(f'[ALERT] ⚠️{total_elapsed_time:.2f}s⚠️') @@ -458,14 +459,14 @@ 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_line=">") + response_SARA_1 = read_complete_response(ser_sara, wait_for_line=">", debug=False) print(response_SARA_1) time.sleep(1) #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_line="OK") + response_SARA_2 = read_complete_response(ser_sara, wait_for_line="OK", debug=False) print(response_SARA_2) #3. Send to endpoint (with device ID) @@ -553,7 +554,7 @@ try: print("Getting error code (11->Server connection error, 73->Secure socket connect error)") command = f'AT+UHTTPER={aircarto_profile_id}\r' ser_sara.write(command.encode('utf-8')) - response_SARA_9 = read_complete_response(ser_sara, wait_for_line="OK") + response_SARA_9 = read_complete_response(ser_sara, wait_for_line="OK", debug=False) print('

') print(response_SARA_9) print("

") @@ -590,7 +591,7 @@ try: #4. Read reply from server print("Reply from server:") ser_sara.write(b'AT+URDFILE="server_response.txt"\r') - response_SARA_4 = read_complete_response(ser_sara, wait_for_line="OK") + response_SARA_4 = read_complete_response(ser_sara, wait_for_line="OK", debug=False) print('

') print(response_SARA_4) print('

') @@ -605,7 +606,7 @@ try: #5. empty json print("Empty SARA memory:") ser_sara.write(b'AT+UDELFILE="sensordata_csv.json"\r') - response_SARA_5 = read_complete_response(ser_sara, wait_for_line="OK") + response_SARA_5 = read_complete_response(ser_sara, wait_for_line="OK", debug=False) print(response_SARA_5) ''' @@ -622,7 +623,7 @@ try: print("SET URL") command = f'AT+UHTTP={uSpot_profile_id},1,"api-prod.uspot.probesys.net"\r' ser_sara.write((command + '\r').encode('utf-8')) - response_SARA_5 = read_complete_response(ser_sara, wait_for_line="OK") + response_SARA_5 = read_complete_response(ser_sara, wait_for_line="OK", debug=False) print(response_SARA_5) time.sleep(1) @@ -631,7 +632,7 @@ try: print("SET SSL") command = f'AT+UHTTP={uSpot_profile_id},6,0\r' ser_sara.write(command.encode('utf-8')) - response_SARA_5 = read_complete_response(ser_sara, wait_for_line="OK") + response_SARA_5 = read_complete_response(ser_sara, wait_for_line="OK", debug=False) print(response_SARA_5) time.sleep(1) @@ -640,7 +641,7 @@ try: print("SET PORT") 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_line="OK") + response_SARA_55 = read_complete_response(ser_sara, wait_for_line="OK", debug=False) print(response_SARA_55) time.sleep(1) @@ -654,14 +655,14 @@ try: size_of_string = len(payload_string) command = f'AT+UDWNFILE="sensordata_json.json",{size_of_string}\r' ser_sara.write((command + '\r').encode('utf-8')) - response_SARA_1 = read_complete_response(ser_sara, wait_for_line=">") + response_SARA_1 = read_complete_response(ser_sara, wait_for_line=">", debug=False) print(response_SARA_1) time.sleep(1) #2. Write to shell print("Write to memory:") ser_sara.write(payload_string.encode()) - response_SARA_2 = read_complete_response(ser_sara, wait_for_line="OK") + response_SARA_2 = read_complete_response(ser_sara, wait_for_line="OK", debug=False) print(response_SARA_2) #step 4: trigger the request (http_command=1 for GET and http_command=1 for POST) @@ -680,7 +681,7 @@ try: print("****") print("Read reply from server") ser_sara.write(b'AT+URDFILE="http.resp"\r') - response_SARA_7 = read_complete_response(ser_sara, wait_for_line="OK") + response_SARA_7 = read_complete_response(ser_sara, wait_for_line="OK", debug=False) print('

') print(response_SARA_7) print('

') @@ -689,19 +690,15 @@ try: #5. empty json print("Empty SARA memory:") ser_sara.write(b'AT+UDELFILE="sensordata_json.json"\r') - response_SARA_8 = read_complete_response(ser_sara, wait_for_line="OK") + response_SARA_8 = read_complete_response(ser_sara, wait_for_line="OK", debug=False) print(response_SARA_8) - - # Calculate and print the elapsed time elapsed_time = time.time() - start_time_script print(f"Elapsed time: {elapsed_time:.2f} seconds") print("
") - - except Exception as e: print("An error occurred:", e)