This commit is contained in:
Your Name
2025-01-15 17:25:45 +01:00
parent 376a143428
commit e2c522af13

View File

@@ -216,6 +216,8 @@ def read_complete_response(serial_connection, timeout=2, end_of_response_timeout
# Open and read the JSON file # Open and read the JSON file
try: try:
# Send the command to request data (e.g., data for 60 seconds) # Send the command to request data (e.g., data for 60 seconds)
print("SART LOOP")
print("Getting NPM values")
ser_NPM.write(b'\x81\x12\x6D') ser_NPM.write(b'\x81\x12\x6D')
# Read the response # Read the response
@@ -242,6 +244,7 @@ try:
# Sonde BME280 connected # Sonde BME280 connected
if bme_280_config: if bme_280_config:
print("Getting BME280 values")
#on récupère les infos du BME280 et on les ajoute au payload_csv #on récupère les infos du BME280 et on les ajoute au payload_csv
i2c = busio.I2C(board.SCL, board.SDA) i2c = busio.I2C(board.SCL, board.SDA)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=0x76) bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=0x76)
@@ -314,7 +317,7 @@ try:
print(f"No serial connection for {name}") print(f"No serial connection for {name}")
# Getting the LTE Signal # Getting the LTE Signal
print("-> Getting signal <-") print("-> Getting LTE signal <-")
ser_sara.write(b'AT+CSQ\r') ser_sara.write(b'AT+CSQ\r')
response2 = read_complete_response(ser_sara) response2 = read_complete_response(ser_sara)
print("Response:") print("Response:")
@@ -349,7 +352,7 @@ try:
ser_sara.write(csv_string.encode()) ser_sara.write(csv_string.encode())
response_SARA_2 = read_complete_response(ser_sara) response_SARA_2 = read_complete_response(ser_sara)
if need_to_log: if need_to_log:
print("Write to memory:") print("Write data to memory:")
print(response_SARA_2) print(response_SARA_2)
#3. Send to endpoint (with device ID) #3. Send to endpoint (with device ID)
@@ -357,114 +360,112 @@ try:
command= f'AT+UHTTPC=0,4,"/pro_4G/data.php?sensor_id={device_id}","server_response.txt","sensordata_csv.json",4\r' command= f'AT+UHTTPC=0,4,"/pro_4G/data.php?sensor_id={device_id}","server_response.txt","sensordata_csv.json",4\r'
ser_sara.write(command.encode('utf-8')) ser_sara.write(command.encode('utf-8'))
print("Waiting for +UUHTTPCR response...") response_SARA_3 = read_complete_response(ser_sara, timeout=5)
response_received = False print(response_SARA_3)
while not response_received: if "+UUHTTPCR" in response_SARA_3:
response_SARA_3 = read_complete_response(ser_sara, timeout=5) print("Received +UUHTTPCR response.")
print(response_SARA_3) response_received = True
if "+UUHTTPCR" in response_SARA_3:
response_received = True
# Les types de réponse # Les types de réponse
# 1.La commande n'a pas fonctionné # 1.La commande n'a pas fonctionné
# +CME ERROR: No connection to phone # +CME ERROR: No connection to phone
# +CME ERROR: Operation not allowed # +CME ERROR: Operation not allowed
# 2.La commande fonctionne: elle renvoie un code # 2.La commande fonctionne: elle renvoie un code
# +UUHTTPCR: <profile_id>,<http_command>,<http_result> # +UUHTTPCR: <profile_id>,<http_command>,<http_result>
# <http_result>: 1 pour sucess et 0 pour fail # <http_result>: 1 pour sucess et 0 pour fail
# +UUHTTPCR: 0,4,1 -> OK # +UUHTTPCR: 0,4,1 -> OK
# +UUHTTPCR: 0,4,0 -> error # +UUHTTPCR: 0,4,0 -> error
# Split response into lines # Split response into lines
lines = response_SARA_3.strip().splitlines() lines = response_SARA_3.strip().splitlines()
# 1.Vérifier si la réponse contient un message d'erreur CME # 1.Vérifier si la réponse contient un message d'erreur CME
if "+CME ERROR" in lines[-1]: if "+CME ERROR" in lines[-1]:
print("*****")
print('<span style="color: red;font-weight: bold;">ATTENTION: CME ERROR</span>')
print("error:", lines[-1])
print("*****")
#update status
update_json_key(config_file, "SARA_R4_network_status", "disconnected")
# Gestion de l'erreur spécifique
if "No connection to phone" in lines[-1]:
print("No connection to the phone. Retrying or reset may be required.")
# Actions spécifiques pour ce type d'erreur (par exemple, réinitialiser ou tenter de reconnecter)
# need to reconnect to network
# and reset HTTP profile (AT+UHTTP=0) -> ne fonctionne pas..
# tester un reset avec CFUN 15
# 1.Reconnexion au réseau (AT+COPS)
command = f'AT+COPS=1,2,"{selected_networkID}"\r'
ser_sara.write(command.encode('utf-8'))
responseReconnect = read_complete_response(ser_sara)
print("Response reconnect:")
print(responseReconnect)
print("End response reconnect")
elif "Operation not allowed" in lines[-1]:
print("Operation not allowed. This may require a different configuration.")
# Actions spécifiques pour ce type d'erreur
# Clignotement LED en cas d'erreur
GPIO.output(23, GPIO.LOW) # Éteindre la LED définitivement
for _ in range(4):
GPIO.output(23, GPIO.HIGH) # Allumer la LED
time.sleep(0.1)
GPIO.output(23, GPIO.LOW) # Éteindre la LED
time.sleep(0.1)
GPIO.output(23, GPIO.LOW) # Turn off the LED
else:
# 2.Si la réponse contient une réponse HTTP valide
# Extract HTTP response code from the last line
# ATTENTION: lines[-1] renvoie l'avant dernière ligne et il peut y avoir un soucis avec le OK
# rechercher plutot
http_response = lines[-1] # "+UUHTTPCR: 0,4,0"
parts = http_response.split(',')
# 2.1 code 0 (HTTP failed)
if len(parts) == 3 and parts[-1] == '0': # The third value indicates success
print("*****") print("*****")
print('<span style="color: red;font-weight: bold;">ATTENTION: CME ERROR</span>') print('<span style="color: red;font-weight: bold;">ATTENTION: HTTP operation failed</span>')
print("error:", lines[-1])
print("*****")
#update status
update_json_key(config_file, "SARA_R4_network_status", "disconnected") update_json_key(config_file, "SARA_R4_network_status", "disconnected")
print("*****")
# Gestion de l'erreur spécifique print("resetting the URL (domain name):")
if "No connection to phone" in lines[-1]: print("Turning off the blue LED...")
print("No connection to the phone. Retrying or reset may be required.") for _ in range(4): # Faire clignoter 4 fois
# Actions spécifiques pour ce type d'erreur (par exemple, réinitialiser ou tenter de reconnecter)
# need to reconnect to network
# and reset HTTP profile (AT+UHTTP=0) -> ne fonctionne pas..
# tester un reset avec CFUN 15
# 1.Reconnexion au réseau (AT+COPS)
command = f'AT+COPS=1,2,"{selected_networkID}"\r'
ser_sara.write(command.encode('utf-8'))
responseReconnect = read_complete_response(ser_sara)
print("Response reconnect:")
print(responseReconnect)
print("End response reconnect")
elif "Operation not allowed" in lines[-1]:
print("Operation not allowed. This may require a different configuration.")
# Actions spécifiques pour ce type d'erreur
# Clignotement LED en cas d'erreur
GPIO.output(23, GPIO.LOW) # Éteindre la LED définitivement
for _ in range(4):
GPIO.output(23, GPIO.HIGH) # Allumer la LED GPIO.output(23, GPIO.HIGH) # Allumer la LED
time.sleep(0.1) time.sleep(0.1) # Attendre 100 ms
GPIO.output(23, GPIO.LOW) # Éteindre la LED GPIO.output(23, GPIO.LOW) # Éteindre la LED
time.sleep(0.1) time.sleep(0.1) # Attendre 100 ms
GPIO.output(23, GPIO.LOW) # Turn off the LED GPIO.output(23, GPIO.LOW) # Turn off the LED
command = f'AT+UHTTP=0,1,"{url_nebuleair}"\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_31 = read_complete_response(ser_sara)
if need_to_log:
print(response_SARA_31)
# 2.2 code 1 (HHTP succeded)
else: else:
# 2.Si la réponse contient une réponse HTTP valide # Si la commande HTTP a réussi
# Extract HTTP response code from the last line print('<span class="badge text-bg-success">HTTP operation successful.</span>')
# ATTENTION: lines[-1] renvoie l'avant dernière ligne et il peut y avoir un soucis avec le OK update_json_key(config_file, "SARA_R4_network_status", "connected")
# rechercher plutot print("Turning on the blue LED...")
http_response = lines[-1] # "+UUHTTPCR: 0,4,0" for _ in range(4): # Faire clignoter 4 fois
parts = http_response.split(',') GPIO.output(23, GPIO.HIGH) # Allumer la LED
time.sleep(0.1) # Attendre 100 ms
# 2.1 code 0 (HTTP failed) GPIO.output(23, GPIO.LOW) # Éteindre la LED
if len(parts) == 3 and parts[-1] == '0': # The third value indicates success time.sleep(0.1) # Attendre 100 ms
print("*****") GPIO.output(23, GPIO.HIGH) # Turn on the LED
print('<span style="color: red;font-weight: bold;">ATTENTION: HTTP operation failed</span>') #4. Read reply from server
update_json_key(config_file, "SARA_R4_network_status", "disconnected") ser_sara.write(b'AT+URDFILE="server_response.txt"\r')
print("*****") response_SARA_4 = read_complete_response(ser_sara)
print("resetting the URL (domain name):") if need_to_log:
print("Turning off the blue LED...") print("Reply from server:")
for _ in range(4): # Faire clignoter 4 fois print('<p class="text-success">')
GPIO.output(23, GPIO.HIGH) # Allumer la LED print(response_SARA_4)
time.sleep(0.1) # Attendre 100 ms print('</p>')
GPIO.output(23, GPIO.LOW) # Éteindre la LED
time.sleep(0.1) # Attendre 100 ms
GPIO.output(23, GPIO.LOW) # Turn off the LED
command = f'AT+UHTTP=0,1,"{url_nebuleair}"\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_31 = read_complete_response(ser_sara)
if need_to_log:
print(response_SARA_31)
# 2.2 code 1 (HHTP succeded)
else:
# Si la commande HTTP a réussi
print('<span class="badge text-bg-success">HTTP operation successful.</span>')
update_json_key(config_file, "SARA_R4_network_status", "connected")
print("Turning on the blue LED...")
for _ in range(4): # Faire clignoter 4 fois
GPIO.output(23, GPIO.HIGH) # Allumer la LED
time.sleep(0.1) # Attendre 100 ms
GPIO.output(23, GPIO.LOW) # Éteindre la LED
time.sleep(0.1) # Attendre 100 ms
GPIO.output(23, GPIO.HIGH) # Turn on the LED
#4. Read reply from server
ser_sara.write(b'AT+URDFILE="server_response.txt"\r')
response_SARA_4 = read_complete_response(ser_sara)
if need_to_log:
print("Reply from server:")
print('<p class="text-success">')
print(response_SARA_4)
print('</p>')
#5. empty json #5. empty json
ser_sara.write(b'AT+UDELFILE="sensordata_csv.json"\r') ser_sara.write(b'AT+UDELFILE="sensordata_csv.json"\r')