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 fafd627e8bf9e73d478968935629579a829617f1..7eba0288cad4c252b10180ae4834f61084081425 100644 --- a/Capteur-autonome/Projects/LoRa_Mac_TEST1/src/main.cpp +++ b/Capteur-autonome/Projects/LoRa_Mac_TEST1/src/main.cpp @@ -1,29 +1,34 @@ #include <HardwareSerial.h> -HardwareSerial loraSerial(1); // UART1 de l'ESP32 +HardwareSerial loraSerial(1); // UART1 de l'ESP32 -#define LORA_RX 16 // GPIO16 connecté au TX du LoRa-E5 -#define LORA_TX 17 // GPIO17 connecté au RX du LoRa-E5 +#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) { +void sendCommand(String command) +{ Serial.println(">> " + command); loraSerial.println(command); delay(100); unsigned long timeout = millis() + 1000; - while (millis() < timeout) { - while (loraSerial.available()) { + while (millis() < timeout) + { + while (loraSerial.available()) + { String response = loraSerial.readStringUntil('\n'); response.trim(); - if (response.length() > 0) { + if (response.length() > 0) + { Serial.println("<< " + response); } } } } -void setup() { +void setup() +{ Serial.begin(115200); delay(1000); @@ -34,15 +39,16 @@ void setup() { delay(300); sendCommand("AT+MODE=TEST"); delay(300); - sendCommand("AT+TEST=RFCFG,868000000,12,125,8,14"); // <-- version corrigée + sendCommand("AT+TEST=RFCFG,868000000,12,125,8,14"); // <-- version corrigée delay(500); } -void loop() { +void loop() +{ static int counter = 0; String message = "Hello #" + String(counter++); String command = "AT+TEST=TXLRSTR,\"" + message + "\""; sendCommand(command); - delay(1000); // 5 secondes entre chaque message + delay(1000); // 5 secondes entre chaque message } diff --git a/GW-custom/LoRa_homemade/main.py b/GW-custom/LoRa_homemade/main.py index 6d1137b05b2870d0e633e3abbaa3e2f2157b3468..6db8e8bd3dd1e55c851371e7f56e51dd3534a23c 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 @@ -22,27 +22,33 @@ def blink_led(times, interval): 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") + 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{sf},{bw},{TXPR},{RXPR},{TX_power}") + # 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...") +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("AT+TEST=RXLRPKT") # Commencer la réception des paquets LoRa +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}") # 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 - + 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 index 09a9651255569d51f7ab8e988422dccf6429fe84..7af9ba9d7d5a97023176f8c2db320f0aa2bb07ef 100644 --- a/GW-custom/LoRa_homemade/recpt_to_senscom.py +++ b/GW-custom/LoRa_homemade/recpt_to_senscom.py @@ -1,9 +1,6 @@ import time - from machine import UART, Pin - import json - import requests uart = UART(2, baudrate=9600, tx=17, rx=16) 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": {}