diff --git a/Capteur-autonome/Projects/LoRaMAC_sender/.gitignore b/Capteur-autonome/Projects/LoRaMAC_sender/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/LoRaMAC_sender/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/Capteur-autonome/Projects/LoRaMAC_sender/.vscode/extensions.json b/Capteur-autonome/Projects/LoRaMAC_sender/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/LoRaMAC_sender/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/Capteur-autonome/Projects/LoRaMAC_sender/include/README b/Capteur-autonome/Projects/LoRaMAC_sender/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/LoRaMAC_sender/include/README @@ -0,0 +1,37 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the convention is to give header files names that end with `.h'. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/Capteur-autonome/Projects/LoRaMAC_sender/lib/README b/Capteur-autonome/Projects/LoRaMAC_sender/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/LoRaMAC_sender/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into the executable file. + +The source code of each library should be placed in a separate directory +("lib/your_library_name/[Code]"). + +For example, see the structure of the following example libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Example contents of `src/main.c` using Foo and Bar: +``` +#include <Foo.h> +#include <Bar.h> + +int main (void) +{ + ... +} + +``` + +The PlatformIO Library Dependency Finder will find automatically dependent +libraries by scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/Capteur-autonome/Projects/LoRaMAC_sender/platformio.ini b/Capteur-autonome/Projects/LoRaMAC_sender/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..c0aea2c37ec3086af079c696eed6c4b0bf014d6f --- /dev/null +++ b/Capteur-autonome/Projects/LoRaMAC_sender/platformio.ini @@ -0,0 +1,15 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32dev] +platform = espressif32 +board = esp32dev +framework = arduino +lib_deps = bblanchon/ArduinoJson@^7.4.1 diff --git a/Capteur-autonome/Projects/LoRaMAC_sender/src/main.cpp b/Capteur-autonome/Projects/LoRaMAC_sender/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d904df0db686e0dbadb19f182371a45676f0091 --- /dev/null +++ b/Capteur-autonome/Projects/LoRaMAC_sender/src/main.cpp @@ -0,0 +1,71 @@ +#include <ArduinoJson.h> + +static char recv_buf[512]; +static int led = 2; +int counter = 0; + +static int at_send_check_response(char *p_ack, int timeout_ms, char *p_cmd, ...) +{ + int ch; + int num = 0; + int index = 0; + int startMillis = 0; + va_list args; + memset(recv_buf, 0, sizeof(recv_buf)); + va_start(args, p_cmd); + Serial2.print(p_cmd); + Serial.print(p_cmd); + va_end(args); + delay(200); + startMillis = millis(); + + if (p_ack == NULL) + return 0; + + do + { + while (Serial2.available() > 0) + { + ch = Serial2.read(); + recv_buf[index++] = ch; + Serial.print((char)ch); + delay(2); + } + + if (strstr(recv_buf, p_ack) != NULL) + return 1; + + } while (millis() - startMillis < timeout_ms); + Serial.println(); + return 0; +} + +void setup(void) +{ + Serial.begin(9600); + pinMode(led, OUTPUT); + digitalWrite(led, LOW); + Serial2.begin(9600); + Serial.print("Serial2 LOCAL TEST\r\n"); + at_send_check_response("+AT: OK", 100, "AT\r\n"); + at_send_check_response("+MODE: TEST", 1000, "AT+MODE=TEST\r\n"); + at_send_check_response("+TEST: TXLRPKT", 5000, "AT+TEST=TXLRPKT\r\n"); + delay(200); + digitalWrite(led, HIGH); +} + +void loop(void) +{ + char cmd[128]; + counter = counter + 1; + + // Transmit HEX Value + sprintf(cmd, "AT+TEST=TXLRPKT,\"%d\"\r\n", counter); // Changer counter par les données qu'on veut envoyer + int ret = at_send_check_response("+TEST: TXLRPKT", 5000, cmd); + + if (ret) + Serial.println("Sent"); + else + Serial.println("Send failed!\r\n\r\n"); + delay(5000); +} \ No newline at end of file diff --git a/Capteur-autonome/Projects/LoRaMAC_sender/test/README b/Capteur-autonome/Projects/LoRaMAC_sender/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/LoRaMAC_sender/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html diff --git a/Capteur-autonome/Projects/LoRa_Mac_TEST1/src/main.cpp b/Capteur-autonome/Projects/LoRa_Mac_TEST1/src/main.cpp index 3869c134672a42c7cf713a90a2d5dea9bbe05bb6..639fa2fab244a0eb1bcf2a7196713858686144be 100644 --- a/Capteur-autonome/Projects/LoRa_Mac_TEST1/src/main.cpp +++ b/Capteur-autonome/Projects/LoRa_Mac_TEST1/src/main.cpp @@ -1,29 +1,53 @@ #include <HardwareSerial.h> -HardwareSerial loraSerial(1); // UART1 sur ESP32 +HardwareSerial loraSerial(1); // UART1 de l'ESP32 -void setup() { +#define LORA_RX 16 // GPIO16 connecté au TX du LoRa-E5 +#define LORA_TX 17 // GPIO17 connecté au RX du LoRa-E5 + +// 💬 Fonction d'envoi de commande AT + affichage des réponses +void sendCommand(String command) +{ + Serial.println(">> " + command); + loraSerial.println(command); + delay(100); + + unsigned long timeout = millis() + 1000; + while (millis() < timeout) + { + while (loraSerial.available()) + { + String response = loraSerial.readStringUntil('\n'); + response.trim(); + if (response.length() > 0) + { + Serial.println("<< " + response); + } + } + } +} + +void setup() +{ Serial.begin(115200); // Moniteur série loraSerial.begin(9600, SERIAL_8N1, 16, 17); // RX=16, TX=17 (à adapter à ton câblage) Serial.println("Initialisation du LoRa E5..."); - loraSerial.println("AT"); - delay(100); - loraSerial.println("AT+MODE=TEST"); // Passe en mode test (point-à-point) - delay(100); - loraSerial.println("AT+TEST=RFCFG,868,SF7,125,8,4,8,OFF,OFF,0,3000,8,OFF"); // config fréquence etc. + sendCommand("AT"); + delay(300); + sendCommand("AT+MODE=TEST"); + delay(300); + sendCommand("AT+TEST=RFCFG,868000000,12,125,8,14"); // <-- version corrigée delay(500); } -void loop() { +void loop() +{ static int counter = 0; - String message = "Ping"; - String cmd = "AT+TEST=TXLRSTR," + message; - loraSerial.println(cmd); - Serial.println("Message envoyé : " + message + String(counter++)); + String message = "Hello #" + String(counter++); + String command = "AT+TEST=TXLRSTR,\"" + message + "\""; - delay(1000); // toutes les 5 secondes + sendCommand(command); + delay(1000); // 5 secondes entre chaque message } - - diff --git a/GW-custom/LoRa_homemade/main.py b/GW-custom/LoRa_homemade/main.py index 9874dc4f80c952ebec5717fa93a2b1ae5e088816..018a4ebcb3717c2d8d07aa4158faa762d5d6c2a1 100644 --- a/GW-custom/LoRa_homemade/main.py +++ b/GW-custom/LoRa_homemade/main.py @@ -8,7 +8,7 @@ uart = UART(2, baudrate=9600, tx=17, rx=16) led = Pin(2, Pin.OUT) # GPIO 2 pour la LED bleue intégrée def send_at_command(command): - uart.write((command + "\r\n").encode()) # Envoie la commande AT au module LoRa + uart.write(("AT" + command + "\r\n").encode()) # Envoie la commande AT au module LoRa time.sleep(0.3) # Attendre la réponse du module while uart.any(): response = uart.read().decode('utf-8') # Lire la réponse @@ -21,29 +21,36 @@ def blink_led(times, interval): led.value(0) # Eteindre la LED time.sleep(interval) # Attendre un certain temps -def configure_lora(frequency, sf, bw, cr, power): - send_at_command("AT") - send_at_command("AT+MODE=TEST") +def configure_lora(frequency, sf, bw, TXPR, RXPR, TX_power): + send_at_command("") + send_at_command("+MODE=TEST") # Configurer le module LoRa avec les paramètres donnés - send_at_command(f"AT+TEST=RFCFG,{frequency},{sf},{bw},{cr},{power}") # Configurer la fréquence, le SF, le BW, le CR et la puissance + # send_at_command(f"+TEST=RFCFG,{frequency},SF{sf},{bw},{TXPR},{RXPR},{TX_power}") + +def receive_uart(): + '''Polls the uart until all data is dequeued''' + rxData=bytes() + while uart.any()>0: + rxData += uart.read(1) + time.sleep(0.002) + return rxData.decode('utf-8') # Initialisation du module LoRa blink_led(3, 0.5) # Clignoter la LED 3 fois au démarrage -print("Initialisation du module LoRa-E5...") -configure_lora(868000000, 12, 125, 8, 14) # Configurer le module LoRa avec les paramètres souhaités -send_at_command("AT+TEST=RXLRPKT") # Commencer la réception des paquets LoRa +print("\n\nInitialisation du module LoRa-E5...\n") +configure_lora(868000000, 12, 125, 12, 15, 14) # Configurer le module LoRa avec les paramètres souhaités +send_at_command("+TEST=RXLRPKT") # Commencer la réception des paquets LoRa led.value(1) # Rallumer la LED après l'initialisation -print("Module LoRa-E5 initialisé. En attente de messages...") +print("Module LoRa-E5 initialisé. En attente de messages...\n") while True: if uart.any(): # Si des données sont disponibles sur le port série - message = uart.read().decode('utf-8').strip() # Lire le message reçu + message = receive_uart() # Lire le message reçu print(f"Message reçu : {message}") + data = message.split("\"")[1] + print(f"Data : {data}\n") # Clignotement de la LED une fois pour indiquer la réception - led.value(0) # Éteindre la LED - time.sleep(0.1) # Attendre un peu - led.value(1) # Rallumer la LED - - time.sleep(0.01) # Petite pause pour éviter de saturer la boucle + blink_led(1, 0.2) # Clignoter la LED une fois + time.sleep(0.01) diff --git a/GW-custom/LoRa_homemade/recpt_to_senscom.py b/GW-custom/LoRa_homemade/recpt_to_senscom.py new file mode 100644 index 0000000000000000000000000000000000000000..7af9ba9d7d5a97023176f8c2db320f0aa2bb07ef --- /dev/null +++ b/GW-custom/LoRa_homemade/recpt_to_senscom.py @@ -0,0 +1,85 @@ +import time +from machine import UART, Pin +import json +import requests + +uart = UART(2, baudrate=9600, tx=17, rx=16) + +# Initialisation de la LED intégrée (souvent sur GPIO 2) +led = Pin(2, Pin.OUT) # GPIO 2 pour la LED bleue intégrée + +def send_at_command(command): + uart.write((command + "\r\n").encode()) # Envoie la commande AT au module LoRa + time.sleep(0.3) # Attendre la réponse du module + while uart.any(): + response = uart.read().decode('utf-8') # Lire la réponse + print(response) + +def blink_led(times, interval): + for _ in range(times): + led.value(1) # Allumer la LED + time.sleep(interval) # Attendre un certain temps + led.value(0) # Eteindre la LED + time.sleep(interval) # Attendre un certain temps + +def configure_lora(frequency, sf, bw, TXPR, RXPR, TX_power): + send_at_command("AT") + send_at_command("AT+MODE=TEST") + # Configurer le module LoRa avec les paramètres donnés + send_at_command(f"AT+TEST=RFCFG,{frequency},SF{sf},{bw},{TXPR},{RXPR},{TX_power}") + +def send_to_sensor_community(value1, value2): + # Envoi des données sur sensor community + url = "https://api.sensor.community/v1/push-sensor-data/" + headers = { + "Content-Type": "application/json", + "X-Pin": "1", + "X-Sensor": "esp32-900881266" + } + data = { + "software_version": "0.1", + "sensordatavalues": [ + {"value_type": "P1", "value": str(value1)}, + {"value_type": "P2", "value": str(value2)} + ] + } + + response = requests.post(url, headers=headers, data, timeout=10) + + print(response.status_code) # 201 = created + print(response.text) + +def try_receive(): + if uart.any(): # Si des données sont disponibles sur le port série + message = uart.read().decode('utf-8').strip() # Lire le message reçu + print(f"Message reçu : {message}") + + # Clignotement de la LED une fois pour indiquer la réception + led.value(0) # Éteindre la LED + time.sleep(0.1) # Attendre un peu + led.value(1) # Rallumer la LED + + # Traitement du message reçu (extraction des valeurs P1 et P2) + try: + values = message.split(",") + value1 = float(values[0]) + value2 = float(values[1]) + send_to_sensor_community(value1, value2) # Envoyer les valeurs à Sensor Community + except (IndexError, ValueError): + print("Erreur lors de l'extraction des valeurs du message.") + +# Initialisation du module LoRa +blink_led(3, 0.5) # Clignoter la LED 3 fois au démarrage +print("Initialisation du module LoRa-E5...") +configure_lora(868000000, 12, 125, 12, 15, 14) # Configurer le module LoRa avec les paramètres souhaités +send_at_command("AT+TEST=RXLRPKT") # Commencer la réception des paquets LoRa + +led.value(1) # Rallumer la LED après l'initialisation +print("Module LoRa-E5 initialisé. En attente de messages...") + +while True: + try_receive() # Essayer de recevoir un message + time.sleep(0.1) # Attendre un peu avant de vérifier à nouveau + + + diff --git a/GW-custom/PingPong/Pong/main.py b/GW-custom/PingPong/Pong/main.py index ea6e27c8f448236fd9f9d9c88d9a7a9f001a1ee2..77ca101729295bc332386d07f1b18b5d0643df39 100644 --- a/GW-custom/PingPong/Pong/main.py +++ b/GW-custom/PingPong/Pong/main.py @@ -1,8 +1,9 @@ # main.py -- put your code here! -from network import LoRa import socket import time +from network import LoRa + # Please pick the region that matches where you are using the device lora = LoRa(mode=LoRa.LORA, region=LoRa.EU868, adr=False) @@ -16,16 +17,19 @@ s = socket.socket(socket.AF_LORA, socket.SOCK_RAW) s.setblocking(False) # Code pour le Pong ; réception puis émission +print('Début de la réception') i = 0 while True: - if s.recv(64) == b'Ping': - print('je recois Ping du Node A') + recu = s.recv(64) + if recu != b'': + recu = recu.decode('utf-8') + print('je recois '+ recu +' du Node A') print(lora.stats()) #print(lora.coding_rate()) s.send('Pong') print('Pong {}'.format(i)) i = i+1 - time.sleep(5) + time.sleep(0.5) # Code pour le Ping ; uniquement émission # msg = 'Ping' diff --git a/GW-custom/capteur-autonome-penn-avel.code-workspace b/GW-custom/capteur-autonome-penn-avel.code-workspace index c175bc5862e3fb559ca929c59dffb10dc9866988..0d821bae0d235a2fd063eaff1d498738a817eefc 100644 --- a/GW-custom/capteur-autonome-penn-avel.code-workspace +++ b/GW-custom/capteur-autonome-penn-avel.code-workspace @@ -6,6 +6,14 @@ { "name": "Recept_LoRaMAC_0322", "path": "../../../../../PlatformIO/Projects/Recept_LoRaMAC_0322" + }, + { + "name": "LoRa_Mac_TEST1", + "path": "../Capteur-autonome/Projects/LoRa_Mac_TEST1" + }, + { + "name": "LoRaMAC_sender", + "path": "../../../../../PlatformIO/Projects/LoRaMAC_sender" } ], "settings": {}