diff --git a/.DS_Store b/.DS_Store index 8c9fcfe9f81e081ae39aed8de2fe28c88fb4a357..292e69f16af90597379885eccd1926c525a9bafd 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Capteur-autonome/Projects/.DS_Store b/Capteur-autonome/Projects/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..794910b492078a243daff0f7ac0da65f856ec92f Binary files /dev/null and b/Capteur-autonome/Projects/.DS_Store differ diff --git a/Capteur-autonome/Projects/ID_ESP32/.gitignore b/Capteur-autonome/Projects/ID_ESP32/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/ID_ESP32/.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/ID_ESP32/.vscode/extensions.json b/Capteur-autonome/Projects/ID_ESP32/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/ID_ESP32/.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/ID_ESP32/include/README b/Capteur-autonome/Projects/ID_ESP32/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/ID_ESP32/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/ID_ESP32/lib/README b/Capteur-autonome/Projects/ID_ESP32/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/ID_ESP32/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/ID_ESP32/platformio.ini b/Capteur-autonome/Projects/ID_ESP32/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..7af5645f31a2e8d347412c367a38ccb018376b85 --- /dev/null +++ b/Capteur-autonome/Projects/ID_ESP32/platformio.ini @@ -0,0 +1,17 @@ +; 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 +monitor_speed = 115200 +upload_speed = 115200 + diff --git a/Capteur-autonome/Projects/ID_ESP32/src/main.cpp b/Capteur-autonome/Projects/ID_ESP32/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c08a09712f6c6c07bb722df8025ab95c3f783a4 --- /dev/null +++ b/Capteur-autonome/Projects/ID_ESP32/src/main.cpp @@ -0,0 +1,40 @@ +#include <Arduino.h> + +void setup() { + Serial.begin(115200); + delay(1000); // Attente pour laisser le temps au port série de s'initialiser + + // Récupération de l'identifiant unique (64 bits) + uint64_t chipid = ESP.getEfuseMac(); + + // Réduction à 32 bits (optionnel, mais pratique pour un ID plus court) + uint32_t sensor_id = (uint32_t)(chipid >> 32) ^ (uint32_t)chipid; + + // Affichage + Serial.println(); + Serial.println("=== Identifiant unique de l'ESP32 ==="); + Serial.printf("ChipID (64 bits hex) : %04X%08X\n", (uint32_t)(chipid >> 32), (uint32_t)chipid); + Serial.printf("Sensor ID (32 bits) : %u\n", sensor_id); + Serial.printf("X-Sensor : esp32-%u\n", sensor_id); + Serial.println("======================================"); +} + +void loop() { + Serial.begin(115200); + delay(1000); // Attente pour laisser le temps au port série de s'initialiser + + // Récupération de l'identifiant unique (64 bits) + uint64_t chipid = ESP.getEfuseMac(); + + // Réduction à 32 bits (optionnel, mais pratique pour un ID plus court) + uint32_t sensor_id = (uint32_t)(chipid >> 32) ^ (uint32_t)chipid; + + // Affichage + Serial.println(); + Serial.println("=== Identifiant unique de l'ESP32 ==="); + Serial.printf("ChipID (64 bits hex) : %04X%08X\n", (uint32_t)(chipid >> 32), (uint32_t)chipid); + Serial.printf("Sensor ID (32 bits) : %u\n", sensor_id); + Serial.printf("X-Sensor : esp32-%u\n", sensor_id); + Serial.println("======================================"); +} + diff --git a/Capteur-autonome/Projects/ID_ESP32/test/README b/Capteur-autonome/Projects/ID_ESP32/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/ID_ESP32/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-E5-CodeUIE/.DS_Store b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..53a529e84a737b0d6e00079ef9c46117081d03a6 Binary files /dev/null and b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/.DS_Store differ diff --git a/Capteur-autonome/Projects/LoRa-E5-CodeUIE/.gitignore b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/.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/LoRa-E5-CodeUIE/.vscode/extensions.json b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/.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/LoRa-E5-CodeUIE/include/README b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/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/LoRa-E5-CodeUIE/lib/README b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/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/LoRa-E5-CodeUIE/platformio.ini b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..16216e96a4bc49939f3b6470a158f3eb97e9b3f8 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/platformio.ini @@ -0,0 +1,17 @@ +; 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 +upload_speed = 115200 +monitor_speed = 115200 + diff --git a/Capteur-autonome/Projects/LoRa-E5-CodeUIE/src/main.cpp b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a080d3203815ad105947222a4830c63bb59a12db --- /dev/null +++ b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/src/main.cpp @@ -0,0 +1,27 @@ +#include <Arduino.h> + +#define RXD2 16 // RX de l'ESP32 (TX du LoRa-E5) +#define TXD2 17 // TX de l'ESP32 (RX du LoRa-E5) +HardwareSerial SerialAT(2); // Port série 2 de l'ESP32 + +void setup() { + Serial.begin(115200); // Moniteur série (PC) + SerialAT.begin(9600, SERIAL_8N1, RXD2, TXD2); // Initialiser LoRa-E5 en 9600 bauds + + delay(2000); + Serial.println("Envoi de la commande AT pour obtenir le DevEui..."); + + SerialAT.println("AT+ID=DevEui"); // Envoi de la commande au LoRa-E5 +} + +void loop() { + // Lecture de la réponse et affichage sur le moniteur série + if (SerialAT.available()) { + String response = ""; + while (SerialAT.available()) { + response += (char)SerialAT.read(); + } + Serial.println("Réponse du LoRa-E5: " + response); + } + delay(1000); +} diff --git a/Capteur-autonome/Projects/LoRa-E5-CodeUIE/test/README b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa-E5-CodeUIE/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/LoRaMac_Receiver/.gitignore b/Capteur-autonome/Projects/LoRaMac_Receiver/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/LoRaMac_Receiver/.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_Receiver/.vscode/extensions.json b/Capteur-autonome/Projects/LoRaMac_Receiver/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/LoRaMac_Receiver/.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_Receiver/include/README b/Capteur-autonome/Projects/LoRaMac_Receiver/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/LoRaMac_Receiver/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_Receiver/lib/README b/Capteur-autonome/Projects/LoRaMac_Receiver/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/LoRaMac_Receiver/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_Receiver/platformio.ini b/Capteur-autonome/Projects/LoRaMac_Receiver/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..67444aff36f319466a8ec979a560a002daf87e14 --- /dev/null +++ b/Capteur-autonome/Projects/LoRaMac_Receiver/platformio.ini @@ -0,0 +1,16 @@ +; 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 +monitor_speed = 115200 +upload_speed = 115200 diff --git a/Capteur-autonome/Projects/LoRaMac_Receiver/src/main.cpp b/Capteur-autonome/Projects/LoRaMac_Receiver/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95b149533cbbe05733745d0ffd8707b8877f64be --- /dev/null +++ b/Capteur-autonome/Projects/LoRaMac_Receiver/src/main.cpp @@ -0,0 +1,45 @@ +#include <HardwareSerial.h> + +HardwareSerial loraSerial(1); +const int led = 2; + +void readResponse() { + delay(100); + while (loraSerial.available()) { + Serial.write(loraSerial.read()); + } +} + +void setup() { + delay(1000); + Serial.begin(115200); + loraSerial.begin(9600, SERIAL_8N1, 16, 17); + pinMode(led, OUTPUT); + + Serial.println("Initialisation du récepteur..."); + + loraSerial.println("AT"); + readResponse(); + + loraSerial.println("AT+MODE=TEST"); + readResponse(); + + loraSerial.println("AT+TEST=RFCFG,868,SF12,125,8,15,14,ON,OFF,0,3000,8,ON"); + readResponse(); + + loraSerial.println("AT+TEST=RXLRSTR"); // 🟢 ICI LE CHANGEMENT + readResponse(); + + Serial.println("Récepteur prêt !"); +} + +void loop() { + while (loraSerial.available()) { + String msg = loraSerial.readStringUntil('\n'); + Serial.println("Message reçu : " + msg); + digitalWrite(led, HIGH); + delay(200); + digitalWrite(led, LOW); + } +} + diff --git a/Capteur-autonome/Projects/LoRaMac_Receiver/test/README b/Capteur-autonome/Projects/LoRaMac_Receiver/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/LoRaMac_Receiver/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/.DS_Store b/Capteur-autonome/Projects/LoRa_Mac_TEST1/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..7e3721dd8af61a9a3444f21db342df1c1e99694e Binary files /dev/null and b/Capteur-autonome/Projects/LoRa_Mac_TEST1/.DS_Store differ diff --git a/Capteur-autonome/Projects/LoRa_Mac_TEST1/.gitignore b/Capteur-autonome/Projects/LoRa_Mac_TEST1/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_Mac_TEST1/.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/LoRa_Mac_TEST1/.vscode/extensions.json b/Capteur-autonome/Projects/LoRa_Mac_TEST1/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_Mac_TEST1/.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/LoRa_Mac_TEST1/include/README b/Capteur-autonome/Projects/LoRa_Mac_TEST1/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_Mac_TEST1/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/LoRa_Mac_TEST1/lib/README b/Capteur-autonome/Projects/LoRa_Mac_TEST1/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_Mac_TEST1/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/LoRa_Mac_TEST1/platformio.ini b/Capteur-autonome/Projects/LoRa_Mac_TEST1/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..67444aff36f319466a8ec979a560a002daf87e14 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_Mac_TEST1/platformio.ini @@ -0,0 +1,16 @@ +; 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 +monitor_speed = 115200 +upload_speed = 115200 diff --git a/Capteur-autonome/Projects/LoRa_Mac_TEST1/src/main.cpp b/Capteur-autonome/Projects/LoRa_Mac_TEST1/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff20661b18a4bb6dea960fa2024a5ad394974f00 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_Mac_TEST1/src/main.cpp @@ -0,0 +1,28 @@ +#include <HardwareSerial.h> + +HardwareSerial loraSerial(1); // UART1 sur ESP32 + +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,SF12,125,8,15,14,ON,OFF,0,3000,8,ON"); // config fréquence etc. + delay(500); +} + +void loop() { + static int counter = 0; + String message = "Hello #" + String(counter++); + + String cmd = "AT+TEST=TXLRSTR,\"" + message + "\""; + loraSerial.println(cmd); + Serial.println("Message envoyé : " + message); + + delay(1000); // toutes les 5 secondes +} diff --git a/Capteur-autonome/Projects/LoRa_Mac_TEST1/test/README b/Capteur-autonome/Projects/LoRa_Mac_TEST1/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_Mac_TEST1/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_to_TTN_Test1.zip b/Capteur-autonome/Projects/LoRa_to_TTN_Test1.zip new file mode 100644 index 0000000000000000000000000000000000000000..5a830bd7021117c4473d7a85ac3806e59aaf22fc Binary files /dev/null and b/Capteur-autonome/Projects/LoRa_to_TTN_Test1.zip differ diff --git a/Capteur-autonome/Projects/LoRa_to_TTN_Test1/.DS_Store b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..bc46e36ec896479c6cf0d9e34fd6a1e52af16aa5 Binary files /dev/null and b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/.DS_Store differ diff --git a/Capteur-autonome/Projects/LoRa_to_TTN_Test1/.gitignore b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/.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/LoRa_to_TTN_Test1/.vscode/extensions.json b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/.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/LoRa_to_TTN_Test1/include/.DS_Store b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/include/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/include/.DS_Store differ diff --git a/Capteur-autonome/Projects/LoRa_to_TTN_Test1/include/README b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/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/LoRa_to_TTN_Test1/include/config_application.h b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/include/config_application.h new file mode 100644 index 0000000000000000000000000000000000000000..cd76c7a98269e54518272d928887132e4e8b0de5 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/include/config_application.h @@ -0,0 +1,25 @@ +/***********************************************************************/ +/* Please see README page on https://github.com/SylvainMontagny/LoRaE5 */ +/***********************************************************************/ + +#define REGION EU868 +#define ACTIVATION_MODE OTAA +#define CLASS CLASS_A +#define SPREADING_FACTOR 7 +#define ADAPTIVE_DR false +#define CONFIRMED false +#define PORT_UP 15 + +#define SEND_BY_PUSH_BUTTON false +#define FRAME_DELAY 20000 + +String devEUI = "70B3D57ED00630C5"; + +// Configuration for ABP Activation Mode +String devAddr = "00000000"; +String nwkSKey = "00000000000000000000000000000000"; +String appSKey = "00000000000000000000000000000000"; + +// Configuration for OTAA Activation Mode +String appKey = "FFC756C21917F54E3993167F8E68AB22"; +String appEUI = "000000000000000"; diff --git a/Capteur-autonome/Projects/LoRa_to_TTN_Test1/include/config_board.h b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/include/config_board.h new file mode 100644 index 0000000000000000000000000000000000000000..dab90556f3b544b86142ba434023865d04fb7fec --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/include/config_board.h @@ -0,0 +1,119 @@ +#ifndef CONFIG_BOARD_H +#define CONFIG_BOARD_H + +////////////////////////////////////////////// +// If you use an ARDUINO or ESP32 board +////////////////////////////////////////////// + #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_RENESAS) || defined(ARDUINO_WIO_TERMINAL) + #define Debug_Serial Serial // Select the right SerialX for Debug + + // Arduino's Serial Documentation : https://www.arduino.cc/reference/en/language/functions/communication/serial/ + // ESP32 Documentation : https://microcontrollerslab.com/esp32-uart-communication-pins-example/ + #define LoRa_Serial Serial2 // Select the right SerialX for the LoRaE5 connection + + + +// If you use a STM32 NUCLEO board +////////////////////////////////////////////// + #elif defined(ARDUINO_ARCH_STM32) + #define RX_PIN_DEBUG PA3 // Select UART RX Pin for Debug + #define TX_PIN_DEBUG PA2 // Select UART TX Pin for Debug + HardwareSerial Debug_Serial(RX_PIN_DEBUG, TX_PIN_DEBUG); + + + #define RX_PIN_LORA PA10 // Select UART RX Pin for the LoRaE5 connection + #define TX_PIN_LORA PA9 // Select UART TX Pin for the LoRaE5 connection + HardwareSerial LoRa_Serial(RX_PIN_LORA, TX_PIN_LORA); + + +////////////////////////////////////////////// +// /!\ Your card has never been tested yet ! +// Please let us know if you find the PIN configuration of your board +// So we can add it to the next version of this code +////////////////////////////////////////////// + + #else + #warning "Your board has never been tested with this lib" + #warning "Select the right Serial for Debug and LoRaE5 connection in config_board.h" + + #define Debug_Serial Serial + #define LoRa_Serial Serial1 + + #endif + +#endif //CONFIG_BOARD_H + +//////////////////////////////// +/* Uart Pin list for known board +//////////////////////////////// + +NUCLEO-F446RE : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +USART3_RX = PC5 Alternate PC11 +USART3_TX = PB10 Alternate PC10 + +// + +NUCLEO-L073RZ : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) Alternate PA15 +USART2_TX = PA2 (ST-Link) Alternate PA14 + +// + +NUCLEO-F411RE : +USART1_RX = PA10 Alternate PB7 Alternate PB3 +USART1_TX = PA9 Alternate PB6 Alternate PA15 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-WL55JC1 : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-G031K8 : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-L433RC-P : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) Alternate PA15 +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-F746ZG : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) Alternate PD6 +USART2_TX = PA2 (ST-Link) Alternate PD5 + +USART3_RX = PD9 Alternate PB11 Alternate PC11 +USART3_TX = PD8 Alternate PB10 Alternate PC10 + +//////////////////////////////// +Uart Pin list for known board */ +//////////////////////////////// \ No newline at end of file diff --git a/Capteur-autonome/Projects/LoRa_to_TTN_Test1/lib/README b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/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/LoRa_to_TTN_Test1/platformio.ini b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..dbe80eb008030658c5ad70265852d978c1d8fb7e --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/platformio.ini @@ -0,0 +1,19 @@ +; 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 +upload_speed = 115200 +monitor_speed = 115200 +build_flags = -Iinclude + + diff --git a/Capteur-autonome/Projects/LoRa_to_TTN_Test1/src/.DS_Store b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/src/.DS_Store differ diff --git a/Capteur-autonome/Projects/LoRa_to_TTN_Test1/src/lorae5.cpp b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/src/lorae5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d235c0cc0ae402586a1fd126176bbf6dbe645a53 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/src/lorae5.cpp @@ -0,0 +1,804 @@ +#include <Arduino.h> +#include <HardwareSerial.h> +#include "lorae5.h" + +LORAE5::LORAE5(String devEUI, String appEUI, String appKey, String devAddr, String nwkSKey, String appSKey){ + this->devEUI = devEUI; + this->appEUI = appEUI; + this->appKey = appKey; + this->devAddr = devAddr; + this->nwkSKey = nwkSKey; + this->appSKey = appSKey; +} + +#if defined(ARDUINO_AVR_LEONARDO) +/// @brief Allow to set the hardware setup for our transmission +/// @param myDebugSerial is the debug serial link +/// @param myLoRaSerial is the LoRaE5 serial link +void LORAE5::setup_hardware(Serial_* myDebugSerial, HardwareSerial* myLoRaSerial){ + unsigned long startTime = millis(); + + this->serialD = myDebugSerial; + this->serialL = myLoRaSerial; + + (this->serialD)->begin(115200); + (this->serialL)->begin(9600); + + while (millis() - startTime < 2000); +} +#elif defined(ARDUINO_WIO_TERMINAL) +/// @brief Allow to set the hardware setup for our transmission +/// @param myDebugSerial is the debug serial link +/// @param myLoRaSerial is the LoRaE5 serial link +void LORAE5::setup_hardware(Serial_* myDebugSerial, Uart* myLoRaSerial){ + unsigned long startTime = millis(); + + this->serialD = myDebugSerial; + this->serialL = myLoRaSerial; + + (this->serialD)->begin(115200); + (this->serialL)->begin(9600); + + while (millis() - startTime < 2000); +} +#else +/// @brief Allow to set the hardware setup for our transmission +/// @param myDebugSerial is the debug serial link +/// @param myLoRaSerial is the LoRaE5 serial link +void LORAE5::setup_hardware(HardwareSerial* myDebugSerial, HardwareSerial* myLoRaSerial){ + unsigned long startTime = millis(); + + this->serialD = myDebugSerial; + this->serialL = myLoRaSerial; + + (this->serialD)->begin(115200); + (this->serialL)->begin(9600); + + while (millis() - startTime < 2000); +} +#endif + +/// @brief Allow to set the LoRaWAN setup for our transmission +/// @param region is the region selected by the user +/// @param mode is the mode selected by the user +/// @param devClass is the class of the device selected by the user +/// @param sf is the Spreading Factor selected by the user +/// @param adr is to enable or disable the Adaptive Data Rate +/// @param confirmed is to enable or disable the confirmed message +/// @param portUp is the uplink port selected by the user +/// @param SEND_BY_PUSH_BUTTON is the push button condition selected by the user +/// @param FRAME_DELAY is the frame delay selected by the user +void LORAE5::setup_lorawan(uint8_t region, bool mode, uint8_t devClass, uint8_t sf, bool adr, bool confirmed, uint8_t portUp, bool SEND_BY_PUSH_BUTTON, uint32_t FRAME_DELAY){ + + while(!checkBoard()); + + setDevEUI(devEUI); + setAppEUI(appEUI); + setAppKey(appKey); + + setDevAddr(devAddr); + setNwkSKey(nwkSKey); + setAppSKey(appSKey); + + this->region = region; + + setRXDelay(); + setFrequencyBand(); + setMode(mode); + setClass(devClass); + setSF(sf); + setADR(adr); + setPortUp(portUp); + + this->confirmed = confirmed; + this->SEND_BY_PUSH_BUTTON = SEND_BY_PUSH_BUTTON; + this->FRAME_DELAY = FRAME_DELAY; +} + +/// @brief Allow to set the RX delay +void LORAE5::setRXDelay(){ + readResponse(1000,NO_DEBUG); + SERIAL_L("AT+DELAY=RX1,1000"); + readResponse(400,NO_DEBUG); + SERIAL_L("AT+DELAY=RX2,2000"); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the mode of the device +/// @param mode is the mode selected by the user +void LORAE5::setMode(bool mode){ + String strMode; + + this->mode = mode; + if (mode == OTAA) strMode = "LWOTAA"; + if (mode == ABP) strMode = "LWABP"; + + SERIAL_L("AT+MODE=" + strMode); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the class of the device +/// @param devClass is the the class of the device selected by the user +void LORAE5::setClass(uint8_t devClass){ + String strClass; + + this->devClass = devClass; + if (devClass == CLASS_A) strClass = "A"; + if (devClass == CLASS_C) strClass = "C"; + SERIAL_L("AT+CLASS=" + strClass); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the Spreading Factor (SF) of the device +/// @param sf is the Spreading factor selected by the user +void LORAE5::setSF(uint8_t sf){ + this->sf = sf; + if ((this->region) == EU868 || (this->region) == CN779 || (this->region) == EU433 || (this->region) == AU915 || (this->region) == CN470 || (this->region) == AS923 || (this->region) == KR920 || (this->region) == IN865 || (this->region) == RU864 || (this->region) == CN470PREQUEL || (this->region) == STE920){ + SERIAL_L("AT+DR=DR" + String(-sf+12)); + } + + if ((this->region) == US915 || (this->region) == US915HYBRID || (this->region) == AU915OLD){ + SERIAL_L("AT+DR=DR" + String(-sf+10)); + } + + String response = (this->serialL)->readStringUntil('\n'); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to process and set the Spreading Factor (SF) received from the downlink +void LORAE5::getSetSF(){ + String response; + (this->serialL)->setTimeout(500); + SERIAL_L("AT+DR"); + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 2 + SERIAL_D(response); // DEBUG + #endif + if (this->adr == true){ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 2 + SERIAL_D(response); // DEBUG + #endif + } + + if ((this->region) == EU868 || (this->region) == CN779 || (this->region) == EU433 || (this->region) == AU915 || (this->region) == CN470 || (this->region) == AS923 || (this->region) == KR920 || (this->region) == IN865 || (this->region) == RU864 || (this->region) == CN470PREQUEL || (this->region) == STE920){ + if ( response.indexOf("DR0") != -1 ) this->sf = 12; + else if ( response.indexOf("DR1") != -1 ) this->sf = 11; + else if ( response.indexOf("DR2") != -1 ) this->sf = 10; + else if ( response.indexOf("DR3") != -1 ) this->sf = 9; + else if ( response.indexOf("DR4") != -1 ) this->sf = 8; + else if ( response.indexOf("DR5") != -1 ) this->sf = 7; + } + + if ((this->region) == US915 || (this->region) == US915HYBRID || (this->region) == AU915OLD){ + if ( response.indexOf("DR0") != -1 ) this->sf = 10; + else if ( response.indexOf("DR1") != -1 ) this->sf = 9; + else if ( response.indexOf("DR2") != -1 ) this->sf = 8; + else if ( response.indexOf("DR3") != -1 ) this->sf = 7; + } + + readResponse(100,NO_DEBUG); +} + +/// @brief Allow to set Adaptative Data Rate (ADR) +/// @param adr is to enable or disable the Adaptive Data Rate +void LORAE5::setADR(bool adr){ + this->adr = adr; + SERIAL_L("AT+ADR=" + ((adr == true)? String("ON") : String("OFF"))); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Device EUI +/// @param deveui is the Device EUI +void LORAE5::setDevEUI(String deveui){ + SERIAL_L("AT+ID=DEVEUI," + devEUI); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Application EUI +/// @param appeui is the Application EUI +void LORAE5::setAppEUI(String appeui){ + SERIAL_L("AT+ID=APPEUI," + appeui); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Application Key +/// @param appkey is the Application Key +void LORAE5::setAppKey(String appkey){ + SERIAL_L("AT+KEY=APPKEY," + appkey); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Device Address +/// @param devaddr is the Device Address +void LORAE5::setDevAddr(String devaddr){ + SERIAL_L("AT+ID=DEVADDR," + devaddr); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Network Session Key +/// @param nwkskey is the Network Session Key +void LORAE5::setNwkSKey(String nwkskey){ + SERIAL_L("AT+KEY=NWKSKEY," + nwkskey); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Application Session Key +/// @param appskey is the Application Session Key +void LORAE5::setAppSKey(String appskey){ + SERIAL_L("AT+KEY=APPSKEY," + appskey); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the uplink port +/// @param portUp is the uplink port selected by the user +void LORAE5::setPortUp(uint8_t portUp){ + this->portUp = portUp; + SERIAL_L("AT+PORT=" + String(portUp)); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to check if a board is connected +/// @return Board status +bool LORAE5::checkBoard(){ + bool success; + delay(500); + SERIAL_L("AT"); + SERIAL_D("\n\n\n\n"); + if ( (success = checkResponse(400,"+AT: OK\r\n",NO_DEBUG)) == false){ + SERIAL_D("> LoRa-E5 board not detected ..."); + } + else{ + SERIAL_D("> LoRa-E5 board detected ...\n"); + } + return success; +} + +/// @brief Allow to check if join procedure is successful +/// @return Joined status +bool LORAE5::join(){ + bool joined = false, failed = false, done = false; + String response, strDevAddr; + uint32_t index; + + delay(2000); + (this->serialL)->setTimeout(10000); + SERIAL_L("AT+JOIN"); + + do{ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 1 + SERIAL_D(response); // DEBUG + #endif + + // Analyse DevAddr response + if ( (index = response.indexOf("DevAddr")) != -1 ){ + index += strlen("DevAddr "); + strDevAddr += response.charAt(index + 0); + strDevAddr += response.charAt(index + 1); + strDevAddr += ":"; + strDevAddr += response.charAt(index + 3); + strDevAddr += response.charAt(index + 4); + strDevAddr += ":"; + strDevAddr += response.charAt(index + 6); + strDevAddr += response.charAt(index + 7); + strDevAddr += ":"; + strDevAddr += response.charAt(index + 9); + strDevAddr += response.charAt(index + 10); + SERIAL_D("> SUCCESS JOINED OTAA !!!"); + SERIAL_D("> DevAddr = " + strDevAddr); + joined = true; + } + // Analyse end of Join procedure + else if ( (index = response.indexOf("Done")) != -1 ){ + done = true; + } + // Analyse failed join + else if ( (index = response.indexOf("Join failed")) != -1 ){ + SERIAL_D("> JOIN FAILED ..."); + failed = true; + } + } while((!joined || !done) && !failed ); + SERIAL_D(""); + return joined; +} + +/// @brief Allow to send uplink data +/// @param payloadUp is the data to send +/// @param sizePayloadUp is the size of the data to send +void LORAE5::sendPayloadUp(uint8_t* payloadUp, uint8_t sizePayloadUp) { + SERIAL_D("Sending " + String(this->confirmed ? "Confirmed " : "Unconfirmed ") + String("Data Up")); + (this->serialD)->print("- Payload UP "); + + String stringPayloadUp = ""; + for (uint8_t i = 0; i < sizePayloadUp; i++) { + (this->serialD)->print(" 0x"); + if (payloadUp[i] < 0x10) { + (this->serialD)->print('0'); + stringPayloadUp += 0; + } + (this->serialD)->print(payloadUp[i], HEX); + (this->serialD)->print(" "); + stringPayloadUp += String(payloadUp[i], HEX); + } + + SERIAL_D(""); + SERIAL_D("- PortUp\t" + String(this->portUp)); + SERIAL_D("- SF\t\t" + String(this->sf)); + SERIAL_D(""); + + SERIAL_L("AT+" + String(this->confirmed ? "C" : "") + String("MSGHEX") + String("=") + stringPayloadUp); +} + +/// @brief Allow to manage the sending of uplink data +/// @param payloadUp is the data to send +/// @param sizePayloadUp is the size of the data to send +void LORAE5::sendData(uint8_t* payloadUp, uint8_t sizePayloadUp){ + bool transmissionDone = false; + String response; + uint32_t index; + (this->serialL)->setTimeout(500); + + getSetSF(); + sendPayloadUp(payloadUp, sizePayloadUp); + do{ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 3 + SERIAL_D(response); // DEBUG + #endif + + if ( (index = response.indexOf("Start")) != -1 ){ + SERIAL_D("Transmission Done\nWaiting for Downlink..."); + transmissionDone = true; + } + + if ( (index = response.indexOf("Length error 0")) != -1 ){ + SERIAL_L("AT+MSG"); + } + + if ( (index = response.indexOf("LoRaWAN modem is busy")) != -1 ){ + SERIAL_D("LoRaWAN modem is busy"); + } + + } while (!transmissionDone); + + if(confirmed == true){ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 3 + SERIAL_D(response); // DEBUG + #endif + if ( (index = response.indexOf("Wait ACK")) != -1 ){ + SERIAL_D("Waiting for ACK..."); + } + } + SERIAL_D(""); +} + +/// @brief Allow to debug or consume characters from the LoRaE5 module +/// @param timeOut is the maximum allocated time for the function +/// @param debug is to enable or disable the display +void LORAE5::readResponse(uint32_t timeOut, bool debug){ + uint32_t tstart = millis(); + + while (millis()-tstart < timeOut){ + if((this->serialL)->available() > 0) { + if (debug == DEBUG){ + (this->serialD)->print((char)(this->serialL)->read()); + } + else{ + (this->serialL)->read(); + } + } + } +} + +/// @brief Allow to check if a string is present in the log +/// @param timeOut is the maximum waiting time +/// @param strCheck is the String we are looking for +/// @param debug is to enable or disable the display +/// @return if the requested string is present or not +bool LORAE5::checkResponse(uint32_t timeOut, char *strCheck, bool debug){ + uint32_t tstart = millis(); + uint8_t i=0, checkSize = strlen(strCheck); + char c; + bool success = false; + + while (millis()-tstart < timeOut){ + if((this->serialL)->available() > 0) { + c = (this->serialL)->read(); + + if (debug == DEBUG){ + (this->serialD)->print(c); + } + + if( c == strCheck[i] ){ + if (i == (checkSize-1)){ + success = true; + } + else{ + i++; + } + } + else{ + i=0; + } + } + } +return success; +} + +/// @brief Displays the port up +/// @param string is the information received from the device and stored in a buffer memory +/// @return port up +uint8_t LORAE5::getPortUp(){ + return this->portUp; +} + +/// @brief Displays the port down +/// @param string is the information received from the device and stored in a buffer memory +/// @return port down +void LORAE5::getPortDown(String response){ + + String strPortDown; + uint32_t index; + uint32_t indexSemicolon = response.indexOf(';'); + index = response.indexOf("PORT: "); + index += strlen ("PORT: "); + + for (uint8_t i = 0 ; i < (indexSemicolon - index ) ; i++){ + strPortDown +=response.charAt(index + i); + } + uint8_t portDown = strPortDown.toInt(); + + SERIAL_D("- Port " + String(portDown)); + this->portDown = portDown; +} + +/// @brief Allow to process and display the payload down +/// @param payloadDown is downlink message +/// @param sizePayloadDown is size of the downlink message +/// @param response is the information received from the device and stored in a buffer memory +/// @return Payload down +void LORAE5::getPayloadDown(uint8_t* payloadDown, uint8_t* sizePayloadDown, String response){ + + // Get Downlink Payload + String strPayloadDown; + uint32_t index; + uint32_t indexQuote = response.lastIndexOf("\""); + index = response.indexOf("RX: \""); + index+= strlen("RX: \""); + for (uint8_t i = 0 ; i < (indexQuote - index ) ; i++){ + strPayloadDown +=response.charAt(index + i); + } + *sizePayloadDown = strPayloadDown.length() / 2 ; + for (uint8_t i = 0 ; i < *sizePayloadDown ; i++){ + String strByte =strPayloadDown.substring(i * 2, i * 2 + 2); + payloadDown[i] = strtoul(strByte.c_str(), nullptr, 16); + } + (this->serialD)->print("- Payload DOWN "); + for (uint8_t i = 0 ; i < *sizePayloadDown ; i++){ + (this->serialD)->print(" 0x"); + if (payloadDown[i] < 0x10){ + (this->serialD)->print('0'); + } + (this->serialD)->print(payloadDown[i], HEX); + (this->serialD)->print(" "); + } + SERIAL_D(""); +} + +/// @brief Displays downlink channel +/// @param response is the information received from the device and stored in a buffer memory +void LORAE5::getChannelDown(String response){ + + String strChannelDown; + uint32_t index; + uint32_t indexSemicolon = response.indexOf(", RSSI "); + index = response.indexOf(" RXWIN"); + index += strlen (" RXWIN"); + + for (uint8_t i = 0 ; i < (indexSemicolon - index ) ; i++){ + strChannelDown +=response.charAt(index + i); + } + uint8_t channelDown = strChannelDown.toInt(); + + if (channelDown == 0){ + SERIAL_D("- Slot RXC"); + } else { + SERIAL_D("- Slot RX" + String(channelDown)); + } + SERIAL_D(""); +} + +/// @brief Updates the Device Class +/// @return Device Class +uint8_t LORAE5::getClass(){ + return this->devClass; +} + +/// @brief Updates the Data Rate +/// @return Data rate +uint8_t LORAE5::getADR(){ + return this->adr; +} + +/// @brief Summarizes device parameters +void LORAE5::printInfo(){ +#ifndef ARDUINO_AVR_LEONARDO + SERIAL_D("# For more information visit : https://github.com/SylvainMontagny/LoRaE5"); + + if(this->mode == OTAA) {SERIAL_D("> OTAA");} + else {SERIAL_D("> ABP");} + + if(this->devClass == CLASS_A) {SERIAL_D("> CLASS_A");} + if(this->devClass == CLASS_C) {SERIAL_D("> CLASS_C");} + + SERIAL_D("> SF " + String(this->sf)); + + if(this->adr == true) {SERIAL_D("> ADR ON");} + else {SERIAL_D("> ADR OFF");} + + SERIAL_D(((this->confirmed == CONF)? String("> Confirmed") : String("> Unconfirmed"))); + SERIAL_D("> Port " + String(this->portUp)); + SERIAL_D(); + SERIAL_D( "* DevEUI 0x " + this->devEUI); + + if (this->mode == ABP) { + SERIAL_D("* DevAddr 0x " + this->devAddr); + SERIAL_D("* NwkSKey 0x " + this->nwkSKey); + SERIAL_D("* AppSKey 0x " + this->appSKey); + } + + if (this->mode == OTAA){ + SERIAL_D("* AppKey 0x " + this->appKey); + SERIAL_D("* AppEUI-JoinEUI 0x " + this->appEUI); + } + SERIAL_D(); +#endif +} + +/// @brief Waiting for Class_A downlink +/// @param payloadDown is downlink message +/// @param sizePayloadDown is size of the downlink message +/// @return Downlink data information (RET_T/RET_TIMEOUT/RET_DOWNLINK/RET_NO_DOWNLINK) +uint8_t LORAE5::awaitForDownlinkClass_A(uint8_t* payloadDown, uint8_t* sizePayloadDown){ + uint32_t index; + String response; + bool isDoneReceived = false; + bool isAckReceived = false; + + (this->serialL)->setTimeout(500); + + do{ + response += (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 4 + SERIAL_D(response); // DEBUG + #endif + + /****** Test Done *******/ + if ( (index = response.indexOf("Done")) != -1 ){ + isDoneReceived = true; + } + + }while(isDoneReceived == false); + + //Confirmed part + if ((confirmed == true)){ + if (index = response.indexOf("ACK Received") != -1 ){ + SERIAL_D("ACK received"); + isAckReceived = true; + } + else { + SERIAL_D("No ACK received\n"); + return RET_NO_ACK; + } + } + + //Processing data received + if ((index = response.indexOf("PORT: ")) != -1) { + SERIAL_D("Receiving Data Down:"); + getPortDown(response); + getPayloadDown(payloadDown, sizePayloadDown, response); + getChannelDown(response); + return RET_DOWNLINK; + } + + else if ( ((index = response.indexOf("RXWIN")) != -1) && (isAckReceived == false) ) { + SERIAL_D("MAC command received :"); + getChannelDown(response); + return RET_NO_DOWNLINK; + } + + else { + if(isAckReceived == false){ + SERIAL_D("No Class_A Data Received."); + } + SERIAL_D(); + return RET_NO_DOWNLINK; + } + +} + +/// @brief Waiting for Class_C downlink +/// @param payloadDown is downlink message +/// @param sizePayloadDown is size of the downlink message +/// @return Downlink data information (RET_T/RET_TIMEOUT/RET_DOWNLINK) +uint8_t LORAE5::awaitForDownlinkClass_C(uint8_t* payloadDown, uint8_t* sizePayloadDown){ + uint32_t index; + String response; + bool isDoneReceived = false; + uint32_t startTime = millis(); + + (this->serialL)->setTimeout(500); + +#ifndef ARDUINO_AVR_LEONARDO + if( this->SEND_BY_PUSH_BUTTON == true ){ + SERIAL_D("> Press 't' or receive a downlink.\n"); + }else if( this->SEND_BY_PUSH_BUTTON == false ){ + SERIAL_D("> Press 't', receive a downlink or wait " + String(this->FRAME_DELAY) + String(" ms.\n")); + } +#endif + + do{ + response += (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 4 + SERIAL_D(response); // DEBUG + #endif + + /****** Test Done *******/ + if ( (index = response.indexOf("Done")) != -1 ){ + isDoneReceived = true; + } + + /****** Test 't' : transmission request *******/ + if ( (this->serialD)->available() > 0 ) { + char userInput = (this->serialD)->read(); + if (userInput == 't' || userInput == 'T') { + SERIAL_D("# User input 't'.\n"); + return RET_T; + } + } + + /****** Test Timeout *******/ + if ((this->SEND_BY_PUSH_BUTTON == false) && ((millis() - startTime) >= (this->FRAME_DELAY))){ + SERIAL_D("No Class_C Data Received.\n"); + SERIAL_D("# Timeout reached.\n"); + return RET_TIMEOUT; + } + + }while(isDoneReceived == false); + + //Processing data received + if ((index = response.indexOf("PORT: ")) != -1) { + SERIAL_D("Receiving Data Down:"); + getPortDown(response); + getPayloadDown(payloadDown, sizePayloadDown, response); + getChannelDown(response); + return RET_DOWNLINK; + } +} + +/// @brief Allow to wait and put the LoRaE5 module in Low Power mode ON/OFF +/// @return Downlink data information (RET_T/RET_TIMEOUT) +uint8_t LORAE5::sleep(){ + uint32_t startTime = millis(); + SERIAL_L("AT+LOWPOWER"); + String response = (this->serialL)->readStringUntil('\n'); + readResponse(5, NO_DEBUG); + + if (this->SEND_BY_PUSH_BUTTON == false) { + while((millis() - startTime) < (this->FRAME_DELAY)){ + if ((this->serialD)->available() > 0) { + char userInput = (this->serialD)->read(); + if (userInput == 't' || userInput == 'T') { + SERIAL_L("WAKE-UP"); + response = (this->serialL)->readStringUntil('\n'); + readResponse(5,NO_DEBUG); + SERIAL_D("# User input 't'.\n"); + return RET_T; + } + } + } + SERIAL_L("WAKE-UP"); + response = (this->serialL)->readStringUntil('\n'); + readResponse(5,NO_DEBUG); + SERIAL_D("# Timeout reached.\n"); + return RET_TIMEOUT; + } else { + bool userTest = false; + while (userTest == false){ + if ((this->serialD)->available() > 0) { + char userInput = (this->serialD)->read(); + if (userInput == 't' || userInput == 'T') { + SERIAL_L("WAKE-UP"); + response = (this->serialL)->readStringUntil('\n'); + readResponse(5,NO_DEBUG); + userTest = true; + SERIAL_D("# User input 't'.\n"); + return RET_T; + } + } + } + } +} + +/// @brief Allow to change frequency band +void LORAE5::setFrequencyBand(){ + String response; + + if ((this->region) == EU868){ + SERIAL_L("AT+DR=EU868"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == US915){ + SERIAL_L("AT+DR=US915"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == US915HYBRID){ + SERIAL_L("AT+DR=US915HYBRID"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == CN779){ + SERIAL_L("AT+DR=CN779"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == EU433){ + SERIAL_L("AT+DR=EU433"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == AU915){ + SERIAL_L("AT+DR=AU915"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == AU915OLD){ + SERIAL_L("AT+DR=AU915OLD"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == CN470){ + SERIAL_L("AT+DR=CN470"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == CN470PREQUEL){ + SERIAL_L("AT+DR=CN470PREQUEL"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == AS923){ + SERIAL_L("AT+DR=AS923"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == KR920){ + SERIAL_L("AT+DR=KR920"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == IN865){ + SERIAL_L("AT+DR=IN865"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == RU864){ + SERIAL_L("AT+DR=RU864"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == STE920){ + SERIAL_L("AT+DR=STE920"); + response = (this->serialL)->readStringUntil('\n'); + } + + #if DEBUG_LEVEL == 5 + SERIAL_L("AT+CH"); + response = (this->serialL)->readStringUntil('\n'); + SERIAL_D(response); + #endif +} \ No newline at end of file diff --git a/Capteur-autonome/Projects/LoRa_to_TTN_Test1/src/lorae5.h b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/src/lorae5.h new file mode 100644 index 0000000000000000000000000000000000000000..cd5c3d63acf35ec3eb0f6e7a64df3e036cdb75a1 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/src/lorae5.h @@ -0,0 +1,133 @@ +#define SERIAL_D(x) this->serialD->println(x) +#define SERIAL_L(x) this->serialL->println(x) + +#define EU868 0 +#define US915 1 +#define CN779 2 +#define EU433 3 +#define AU915 4 +#define CN470 5 +#define AS923 6 +#define KR920 7 +#define IN865 8 +#define RU864 9 +#define CN470PREQUEL 10 +#define STE920 11 +#define US915HYBRID 12 +#define AU915OLD 13 + +#define ABP 0 +#define OTAA 1 + +#define CLASS_A 0 +#define CLASS_B 1 +#define CLASS_C 2 + +#define DEBUG true +#define NO_DEBUG false + +#define DEBUG_LEVEL 0 +/* list for DEBUG_LEVEL: +- join : 1; +- getSetSF : 2; +- sendData : 3; +- awaitForDownlink : 4; +- setFrequencyBand : 5; +*/ + +#define CONF true +#define UNCONF false + +#define _HEXA 0 +#define _STRING 1 + +enum RETURN { RET_DOWNLINK, RET_NO_DOWNLINK, RET_NO_ACK, RET_T, RET_TIMEOUT }; + +class LORAE5{ + +public: + // uint8_t downlink[50]; + +private: + uint8_t region; + bool mode; + uint8_t devClass; + uint8_t sf; + bool adr; + bool confirmed; + uint8_t portUp; + uint8_t portDown; + +#if defined(ARDUINO_AVR_LEONARDO) || defined(ARDUINO_WIO_TERMINAL) + Serial_* serialD; +#else + HardwareSerial* serialD; +#endif + +#if defined(ARDUINO_WIO_TERMINAL) + Uart* serialL; +#else + HardwareSerial* serialL; +#endif + + String devEUI; + String devAddr; + String nwkSKey; + String appSKey; + String appKey; + String appEUI; + + bool SEND_BY_PUSH_BUTTON; + uint32_t FRAME_DELAY; + + void readResponse(uint32_t timeOut, bool debug); + bool checkResponse(uint32_t timeOut, char *strCheck, bool debug); + bool checkBoard(void); + + void setDevEUI(String deveui); + void setAppKey(String appkey); + void setAppEUI(String appeui); + void setDevAddr(String devaddr); + void setNwkSKey(String nwkskey); + void setAppSKey(String appskey); + void setMode(bool mode); + void setClass(uint8_t devClass); + void setRXDelay(); + void setSF(uint8_t sf); + void setADR(bool adr); + void setPortUp(uint8_t portUp); + void setFrequencyBand(void); + + void getMode(void); + void getSetSF(void); + void getPortDown(String response); + void getPayloadDown(uint8_t* payloadDown, uint8_t* sizePayloadDown, String response); + void getChannelDown(String response); + uint8_t getADR(void); + uint8_t getPortUp(void); + uint8_t getClass(void); + + void sendPayloadUp(uint8_t* payloadUp, uint8_t sizePayloadUp); + +public: + LORAE5(String devEUI, String appEUI, String appKey, String devAddr, String nwkSKey, String appSKey); + +#if defined(ARDUINO_AVR_LEONARDO) + void setup_hardware(Serial_* myDebugSerial, HardwareSerial* myLoRaSerial); +#elif defined(ARDUINO_WIO_TERMINAL) +void setup_hardware(Serial_* myDebugSerial, Uart* myLoRaSerial); +#else + void setup_hardware(HardwareSerial* myDebugSerial, HardwareSerial* myLoRaSerial); +#endif + + void setup_lorawan(uint8_t band, bool mode, uint8_t devClass, uint8_t sf, bool adr, bool messageType, uint8_t portUp, bool SEND_BY_PUSH_BUTTON, uint32_t FRAME_DELAY); + void printInfo(void); + + bool join(void); + + void sendData(uint8_t* payloadUp, uint8_t sizePayloadUp); + uint8_t awaitForDownlinkClass_A(uint8_t* payloadDown, uint8_t* sizePayloadDown); + uint8_t awaitForDownlinkClass_C(uint8_t* payloadDown, uint8_t* sizePayloadDown); + uint8_t sleep(void); + +}; \ No newline at end of file diff --git a/Capteur-autonome/Projects/LoRa_to_TTN_Test1/src/main.cpp b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28955cf91aeab752c8cb7ee1762681216dbfb2c8 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/src/main.cpp @@ -0,0 +1,66 @@ +#include <Arduino.h> +#include "lorae5.h" +#include "config_application.h" +#include "config_board.h" + +uint8_t sizePayloadUp = 4; +uint8_t sizePayloadDown = 0; + +uint8_t payloadUp[20] = {0x00, 0x01, 0x02, 0x03}; +uint8_t payloadDown[20] ={0}; + +LORAE5 lorae5(devEUI, appEUI, appKey, devAddr, nwkSKey, appSKey); + +/***********************************************************************/ +/* Please see README page on https://github.com/SylvainMontagny/LoRaE5 */ +/***********************************************************************/ + +// Code de GPT +#define RXD2 16 // Broche RX de l'ESP32 (connectée au TX du LoRa-E5) +#define TXD2 17 // Broche TX de l'ESP32 (connectée au RX du LoRa-E5) +HardwareSerial SerialAT(2); // Utilisation de Serial2 pour LoRa-E5 + + + +void setup() { + lorae5.setup_hardware(&Debug_Serial, &LoRa_Serial); + lorae5.setup_lorawan(REGION, ACTIVATION_MODE, CLASS, SPREADING_FACTOR, ADAPTIVE_DR, CONFIRMED, PORT_UP, SEND_BY_PUSH_BUTTON, FRAME_DELAY); + lorae5.printInfo(); + Debug_Serial.begin(115200); + LoRa_Serial.begin(9600, SERIAL_8N1, RXD2, TXD2); + + + if(ACTIVATION_MODE == OTAA){ + Debug_Serial.println("Join Procedure in progress..."); + while(lorae5.join() == false); + delay(2000); + } +} +void processDownlink() { + // You have received "sizePayloadDown" bytes stored in the table "payloadDown" +} + + +void loop() { + // change of data : + // 🔥 Simulation de capteur : température entre 20 et 30°C, humidité entre 40 et 80% + uint8_t temperature = random(20, 30); // Température entre 20 et 30°C + uint8_t humidite = random(40, 80); // Humidité entre 40% et 80% + + payloadUp[1] = temperature; + payloadUp[2] = humidite; + + + + // Send data + lorae5.sendData(payloadUp, sizePayloadUp); + + // Check downlink on RX1-RX2 and call processDownlink() + if (lorae5.awaitForDownlinkClass_A(payloadDown, &sizePayloadDown) == RET_DOWNLINK){ + processDownlink(); + }; + + // Wait in Low Power mode + lorae5.sleep(); +} + diff --git a/Capteur-autonome/Projects/LoRa_to_TTN_Test1/test/README b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/LoRa_to_TTN_Test1/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-E5-Test2/.DS_Store b/Capteur-autonome/Projects/Lora-E5-Test2/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..62a7251809f323800df9b4f38025c964ea95aaa3 Binary files /dev/null and b/Capteur-autonome/Projects/Lora-E5-Test2/.DS_Store differ diff --git a/Capteur-autonome/Projects/Lora-E5-Test2/.gitignore b/Capteur-autonome/Projects/Lora-E5-Test2/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/Lora-E5-Test2/.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/Lora-E5-Test2/.vscode/extensions.json b/Capteur-autonome/Projects/Lora-E5-Test2/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/Lora-E5-Test2/.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/Lora-E5-Test2/include/README b/Capteur-autonome/Projects/Lora-E5-Test2/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/Lora-E5-Test2/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/Lora-E5-Test2/lib/README b/Capteur-autonome/Projects/Lora-E5-Test2/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/Lora-E5-Test2/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/Lora-E5-Test2/platformio.ini b/Capteur-autonome/Projects/Lora-E5-Test2/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..01b2307e05d1d6c7dbce6fea71fedae230581d4c --- /dev/null +++ b/Capteur-autonome/Projects/Lora-E5-Test2/platformio.ini @@ -0,0 +1,17 @@ +; 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.3.1 +monitor_speed = 115200 +upload_speed= 115200 \ No newline at end of file diff --git a/Capteur-autonome/Projects/Lora-E5-Test2/src/main.cpp b/Capteur-autonome/Projects/Lora-E5-Test2/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..541cb7f72fd4e338be5c3d78aef7d28fff133cfc --- /dev/null +++ b/Capteur-autonome/Projects/Lora-E5-Test2/src/main.cpp @@ -0,0 +1,182 @@ +#include <ArduinoJson.h> + +static char recv_buf[512]; +static bool is_exist = false; +static bool is_join = false; +static int led = 2; + +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.printf(p_cmd, args); + Serial.printf(p_cmd, args); + 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); + return 0; +} + +static void recv_prase(char *p_msg) +{ + if (p_msg == NULL) + { + Serial.println("Received null"); + return; + } + char *p_start = NULL; + char data[128]; // To hold the received bytes as characters + + int bytes_len=0; + p_start = strstr(p_msg, "RX"); + if (p_start && (1 == sscanf(p_start, "RX: \"%s", &data))) + { + for (int i=0; i<sizeof(data); i++) { + if(int(data[i+1])==0) { + bytes_len = i; + break; + } + } + + // Convert the characters to a byteArray + int message_len = bytes_len/2+1; + byte out[message_len]; + auto getNum = [](char c){ return c > '9' ? c - 'A' + 10 : c - '0'; }; + for (int x=0, y=0; x<bytes_len; ++x, ++y) + out[y] = (getNum(data[x++]) << 4) + getNum(data[x]); + out[message_len] = '\0'; + + // Create a JSON document of specified capacity <100> and load the byteArray to it + StaticJsonDocument<100> doc; + deserializeJson(doc, out); + + // Print the received JSON message by serializing it + serializeJson(doc, Serial); + Serial.println(); + + // Access a specific key value from the JSON formatted message + Serial.println((const char *)doc["name"]); + } +} + +void setup(void) +{ + Serial.begin(115200); + pinMode(2, OUTPUT); + digitalWrite(2, LOW); + + Serial2.begin(9600); + Serial.print("E5 LORAWAN TEST\r\n"); + + if (at_send_check_response("+AT: OK", 100, "AT\r\n")) + { + is_exist = true; + at_send_check_response("+ID: AppEui", 1000, "AT+ID\r\n"); + at_send_check_response("+MODE: LWOTAA", 1000, "AT+MODE=LWOTAA\r\n"); + at_send_check_response("+DR: EU868", 1000, "AT+DR=EU868\r\n"); + at_send_check_response("+CH: NUM", 1000, "AT+CH=NUM,0-2\r\n"); + at_send_check_response("+KEY: APPKEY", 1000, "AT+KEY=APPKEY,\"6086778DE9E06192F4FEBF145081D0ED\"\r\n"); + at_send_check_response("+CLASS: C", 1000, "AT+CLASS=A\r\n"); + at_send_check_response("+PORT: 8", 1000, "AT+PORT=8\r\n"); + delay(200); + is_join = true; + digitalWrite(2, HIGH); + } + else + { + is_exist = false; + Serial.print("No E5 module found.\r\n"); + } +} + +void loop(void) +{ + if (is_exist) + { + int ret = 0; + if (is_join) + { + ret = at_send_check_response("+JOIN: Network joined", 12000, "AT+JOIN\r\n"); + if (ret) + is_join = false; + else + { + at_send_check_response("+ID: AppEui", 1000, "AT+ID\r\n"); + Serial.print("JOIN failed!\r\n\r\n"); + delay(5000); + } + } + else + { + StaticJsonDocument<200> json; + json["state"]["temp"] = 27.4; + json["state"]["humi"] = 89; + + char charArray[64]; + int len = serializeJson(json, charArray); + char buildBuffer[2] = {0}; + char compositionBuffer[len*3+1] = {0}; // this will hold a string we build + + for (int i = 0; i < len; i++) { + sprintf( buildBuffer, "%02X ", (uint8_t)charArray[i]); + strcat( compositionBuffer, buildBuffer); + } + + char cmd[512]; + sprintf(cmd, "AT+CMSGHEX=\""); + strcat(cmd, compositionBuffer); + strcat(cmd, "\"\r\n"); + ret = at_send_check_response("Done", 5000, cmd); + + if (ret) + recv_prase(recv_buf); + else + Serial.print("Send failed!\r\n\r\n"); + delay(5000); + } + } + else + delay(1000); +} + + +/* +Mosquitto Publish - Downlink + +mosquitto_pub -h eu1.cloud.thethings.network -p 1883 -d -u thingsschooltest@ttn \ +-P NNSXS.FKLIO54M55UVH4SLUDNI2ZCOXPG2XV6H6O6OH2A.AQ2UBWKJMXYKJ2VTJPRF7VBEZTLKF7I2OI6CPR64ROFY2LNCBSEA \ +-t v3/thingsschooltest@ttn/devices/grove-lora-e5/down/push \ +-m "{\"downlinks\":[{\"f_port\": 1, \"frm_payload\":\"eyJuYW1lIjoiQm9zcyJ9\" , \"priority\":\"NORMAL\"}]}" + */ + + +/* +Mosquitto Subscribe - Uplink + +mosquitto_sub -h eu1.cloud.thethings.network -p 1883 -v -d -u thingsschooltest@ttn \ +-P NNSXS.FKLIO54M55UVH4SLUDNI2ZCOXPG2XV6H6O6OH2A.AQ2UBWKJMXYKJ2VTJPRF7VBEZTLKF7I2OI6CPR64ROFY2LNCBSEA \ +-t # + */ \ No newline at end of file diff --git a/Capteur-autonome/Projects/Lora-E5-Test2/test/README b/Capteur-autonome/Projects/Lora-E5-Test2/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/Lora-E5-Test2/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/SDS011_ESP32_TEST_VEILLE/.DS_Store b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f3709fc1c46d6dd36382a677de75d5af542acee9 Binary files /dev/null and b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/.DS_Store differ diff --git a/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/.gitignore b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/.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/SDS011_ESP32_TEST_VEILLE/.vscode/extensions.json b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/.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/SDS011_ESP32_TEST_VEILLE/include/README b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/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/SDS011_ESP32_TEST_VEILLE/lib/README b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/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/SDS011_ESP32_TEST_VEILLE/platformio.ini b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..3b056594b03adac04758dd1649f42a6549b8861f --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/platformio.ini @@ -0,0 +1,18 @@ +; 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 = lewapek/Nova Fitness Sds dust sensors library@^1.5.1 +upload_speed = 115200 +monitor_speed = 115200 + diff --git a/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/src/.DS_Store b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/src/.DS_Store differ diff --git a/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/src/main.cpp b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..53865b94e107cf7a6bda8cb518e8c643018e6d21 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/src/main.cpp @@ -0,0 +1,92 @@ +#include <Arduino.h> +#include <HardwareSerial.h> +#include "SdsDustSensor.h" +#include "esp_sleep.h" +#include <Preferences.h> + +#define RX_PIN 19 +#define TX_PIN 18 +#define LED_PIN 2 // LED intégrée sur la plupart des ESP32 + +HardwareSerial SDS_Serial(1); +SdsDustSensor sds(SDS_Serial); +Preferences prefs; + +void clignoteLED(int nbFois, int duree) { + for (int i = 0; i < nbFois; i++) { + digitalWrite(LED_PIN, HIGH); + delay(duree); + digitalWrite(LED_PIN, LOW); + delay(duree); + } +} + +void setup() { + Serial.begin(115200); + delay(500); + + pinMode(LED_PIN, OUTPUT); + digitalWrite(LED_PIN, LOW); + + // === Initialisation du capteur === + SDS_Serial.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); + sds.begin(); + sds.wakeup(); + + digitalWrite(LED_PIN, HIGH); // LED allumée pendant stabilisation + delay(25000); + + // === Lecture d'une mesure === + PmResult pm = sds.readPm(); + float pm25 = -1, pm10 = -1; + + if (pm.isOk()) { + pm25 = pm.pm25; + pm10 = pm.pm10; + Serial.printf("📊 Mesure obtenue : PM2.5 = %.1f µg/m³, PM10 = %.1f µg/m³\n", pm25, pm10); + clignoteLED(3, 300); // 3 clignotements lents = mesure OK + } else { + Serial.println("❌ Erreur de lecture du capteur SDS011."); + clignoteLED(3, 100); // 3 clignotements rapides = erreur + } + + sds.sleep(); + + // === Stockage en mémoire NVS === + prefs.begin("mesures", false); + int cycle = prefs.getInt("cycle", 0); + prefs.putFloat(("pm25_" + String(cycle)).c_str(), pm25); + prefs.putFloat(("pm10_" + String(cycle)).c_str(), pm10); + prefs.putInt("cycle", cycle + 1); + prefs.end(); + + Serial.printf("✅ Mesure stockée en NVS [cycle %d]\n", cycle + 1); + + // === Si 5 cycles atteints === + if (cycle + 1 >= 5) { + Serial.println("\n📥 Récupération des 5 mesures stockées :"); + + prefs.begin("mesures", false); + for (int i = 0; i < 5; i++) { + float val_pm25 = prefs.getFloat(("pm25_" + String(i)).c_str(), -1); + float val_pm10 = prefs.getFloat(("pm10_" + String(i)).c_str(), -1); + Serial.printf("📝 Mesure %d : PM2.5 = %.1f, PM10 = %.1f\n", i + 1, val_pm25, val_pm10); + } + + prefs.clear(); // nettoyage pour redémarrer à 0 + prefs.end(); + + clignoteLED(5, 200); // signal de fin de série + } + + digitalWrite(LED_PIN, LOW); // LED éteinte avant de dormir + + Serial.println("😴 Mise en veille de l’ESP32 pour 5 secondes..."); + Serial.flush(); + esp_sleep_enable_timer_wakeup(5 * 1000000); + esp_deep_sleep_start(); +} + +void loop() { + // Vide +} diff --git a/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/test/README b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_ESP32_TEST_VEILLE/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/SDS011_TEST copie/.DS_Store b/Capteur-autonome/Projects/SDS011_TEST copie/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f3709fc1c46d6dd36382a677de75d5af542acee9 Binary files /dev/null and b/Capteur-autonome/Projects/SDS011_TEST copie/.DS_Store differ diff --git a/Capteur-autonome/Projects/SDS011_TEST copie/.gitignore b/Capteur-autonome/Projects/SDS011_TEST copie/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_TEST copie/.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/SDS011_TEST copie/.vscode/extensions.json b/Capteur-autonome/Projects/SDS011_TEST copie/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_TEST copie/.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/SDS011_TEST copie/include/README b/Capteur-autonome/Projects/SDS011_TEST copie/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_TEST copie/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/SDS011_TEST copie/lib/README b/Capteur-autonome/Projects/SDS011_TEST copie/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_TEST copie/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/SDS011_TEST copie/platformio.ini b/Capteur-autonome/Projects/SDS011_TEST copie/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..7e325a9241fba9b3b9ed92d6ae78c768f29fc070 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_TEST copie/platformio.ini @@ -0,0 +1,17 @@ +; 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:nodemcuv2] +platform = espressif8266 +board = nodemcuv2 +framework = arduino +lib_deps = lewapek/Nova Fitness Sds dust sensors library@^1.5.1 +upload_speed = 115200 +monitor_speed = 115200 + diff --git a/Capteur-autonome/Projects/SDS011_TEST copie/src/.DS_Store b/Capteur-autonome/Projects/SDS011_TEST copie/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/Capteur-autonome/Projects/SDS011_TEST copie/src/.DS_Store differ diff --git a/Capteur-autonome/Projects/SDS011_TEST copie/src/main.cpp b/Capteur-autonome/Projects/SDS011_TEST copie/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a72a563342e70924871d2f816625dfdbb7e245b2 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_TEST copie/src/main.cpp @@ -0,0 +1,60 @@ +#include <Arduino.h> +#include <SoftwareSerial.h> // 👈 LIGNE 1 — Changement de bibliothèque +#include "SdsDustSensor.h" + +// Définition des broches UART pour le SDS011 +#define RX_PIN D5 // GPIO14 // 👈 LIGNE 2 — Changement de pin +#define TX_PIN D6 // GPIO12 // 👈 LIGNE 3 — Changement de pin + +// Création d’un port série logiciel +SoftwareSerial SDS_Serial(RX_PIN, TX_PIN); // 👈 LIGNE 4 — Utilisation de SoftwareSerial + +// Création de l'objet SDS011 avec le bon SoftwareSerial +SdsDustSensor sds(SDS_Serial); + +void setup() { + Serial.begin(115200); // Moniteur série USB + + // Initialisation du port série pour le SDS011 + SDS_Serial.begin(9600); // 👈 LIGNE 5 — Simplifiée pour SoftwareSerial + + // Initialisation du capteur SDS011 + sds.begin(); + + Serial.println("SDS011 Initialisé !"); + Serial.println(sds.queryFirmwareVersion().toString()); + Serial.println(sds.setActiveReportingMode().toString()); + Serial.println(sds.setContinuousWorkingPeriod().toString()); +} + +void loop() { + Serial.println("Réveil du SDS011..."); + sds.wakeup(); + delay(10000); // Attente initiale (10s) pour stabilisation minimale + + // 🔁 Faire 3 mesures toutes les 10 secondes + for (int i = 0; i < 3; i++) { + PmResult pm = sds.readPm(); + if (pm.isOk()) { + Serial.print("Mesure "); + Serial.print(i + 1); + Serial.print(" - PM2.5 = "); + Serial.print(pm.pm25); + Serial.print(" µg/m³, PM10 = "); + Serial.print(pm.pm10); + Serial.println(" µg/m³"); + } else { + Serial.print("Erreur mesure "); + Serial.print(i + 1); + Serial.print(" : "); + Serial.println(pm.statusToString()); + } + + delay(10000); // ⏳ Pause de 10 secondes entre chaque mesure + } + + Serial.println("Mise en veille du SDS011..."); + sds.sleep(); + + delay(30000); // 💤 Attente 30 secondes en mode sommeil +} diff --git a/Capteur-autonome/Projects/SDS011_TEST copie/test/README b/Capteur-autonome/Projects/SDS011_TEST copie/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_TEST copie/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/SDS011_TEST/.DS_Store b/Capteur-autonome/Projects/SDS011_TEST/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f3709fc1c46d6dd36382a677de75d5af542acee9 Binary files /dev/null and b/Capteur-autonome/Projects/SDS011_TEST/.DS_Store differ diff --git a/Capteur-autonome/Projects/SDS011_TEST/.gitignore b/Capteur-autonome/Projects/SDS011_TEST/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_TEST/.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/SDS011_TEST/.vscode/extensions.json b/Capteur-autonome/Projects/SDS011_TEST/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_TEST/.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/SDS011_TEST/include/README b/Capteur-autonome/Projects/SDS011_TEST/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_TEST/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/SDS011_TEST/lib/README b/Capteur-autonome/Projects/SDS011_TEST/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_TEST/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/SDS011_TEST/platformio.ini b/Capteur-autonome/Projects/SDS011_TEST/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..3b056594b03adac04758dd1649f42a6549b8861f --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_TEST/platformio.ini @@ -0,0 +1,18 @@ +; 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 = lewapek/Nova Fitness Sds dust sensors library@^1.5.1 +upload_speed = 115200 +monitor_speed = 115200 + diff --git a/Capteur-autonome/Projects/SDS011_TEST/src/.DS_Store b/Capteur-autonome/Projects/SDS011_TEST/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/Capteur-autonome/Projects/SDS011_TEST/src/.DS_Store differ diff --git a/Capteur-autonome/Projects/SDS011_TEST/src/main.cpp b/Capteur-autonome/Projects/SDS011_TEST/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f91db2d418cf2aa792707cf26fc442c29f3badb1 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_TEST/src/main.cpp @@ -0,0 +1,46 @@ +#include <Arduino.h> +#include <HardwareSerial.h> +#include "SdsDustSensor.h" + +// Définition des broches UART pour le SDS011 +#define RX_PIN 19 // GPIO19 pour RX +#define TX_PIN 18 // GPIO18 pour TX + +// Création du port série UART1 pour le capteur SDS011 +HardwareSerial SDS_Serial(1); + +// Création de l'objet SDS011 avec le bon HardwareSerial +SdsDustSensor sds(SDS_Serial); + +void setup() { + Serial.begin(115200); // Moniteur série USB + + // Initialisation du port série pour le SDS011 + SDS_Serial.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); + + // Initialisation du capteur SDS011 + sds.begin(); + + Serial.println("SDS011 Initialisé !"); + Serial.println(sds.queryFirmwareVersion().toString()); + Serial.println(sds.setActiveReportingMode().toString()); + Serial.println(sds.setContinuousWorkingPeriod().toString()); +} + +void loop() { + + PmResult pm = sds.readPm(); + if (pm.isOk()) { + Serial.print("PM2.5 = "); + Serial.print(pm.pm25); + Serial.print(" µg/m³, PM10 = "); + Serial.print(pm.pm10); + Serial.println(" µg/m³"); + Serial.println(pm.toString()); + } else { + Serial.print("Erreur de lecture SDS011 : "); + Serial.println(pm.statusToString()); + } + + delay(1000); +} diff --git a/Capteur-autonome/Projects/SDS011_TEST/test/README b/Capteur-autonome/Projects/SDS011_TEST/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/SDS011_TEST/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/Test loraE5-3/.DS_Store b/Capteur-autonome/Projects/Test loraE5-3/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b96b0e352d864b9f0618b110ba12a9443cd31cc5 Binary files /dev/null and b/Capteur-autonome/Projects/Test loraE5-3/.DS_Store differ diff --git a/Capteur-autonome/Projects/Test loraE5-3/.gitignore b/Capteur-autonome/Projects/Test loraE5-3/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/Test loraE5-3/.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/Test loraE5-3/.vscode/extensions.json b/Capteur-autonome/Projects/Test loraE5-3/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/Test loraE5-3/.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/Test loraE5-3/include/README b/Capteur-autonome/Projects/Test loraE5-3/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/Test loraE5-3/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/Test loraE5-3/lib/README b/Capteur-autonome/Projects/Test loraE5-3/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/Test loraE5-3/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/Test loraE5-3/platformio.ini b/Capteur-autonome/Projects/Test loraE5-3/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..4b30716cdfbb08fca9cc4e045e779f7df0c4cbc8 --- /dev/null +++ b/Capteur-autonome/Projects/Test loraE5-3/platformio.ini @@ -0,0 +1,14 @@ +; 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 diff --git a/Capteur-autonome/Projects/Test loraE5-3/src/main.cpp b/Capteur-autonome/Projects/Test loraE5-3/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb9fbba46e2fddd293e9056f65b63f69ce859b83 --- /dev/null +++ b/Capteur-autonome/Projects/Test loraE5-3/src/main.cpp @@ -0,0 +1,18 @@ +#include <Arduino.h> + +// put function declarations here: +int myFunction(int, int); + +void setup() { + // put your setup code here, to run once: + int result = myFunction(2, 3); +} + +void loop() { + // put your main code here, to run repeatedly: +} + +// put function definitions here: +int myFunction(int x, int y) { + return x + y; +} \ No newline at end of file diff --git a/Capteur-autonome/Projects/Test loraE5-3/test/README b/Capteur-autonome/Projects/Test loraE5-3/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/Test loraE5-3/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/Test_Integration_TTN_ET_SDS011/.DS_Store b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d1db41456480cf17f050dd1c49735854df8bea08 Binary files /dev/null and b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/.DS_Store differ diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/.gitignore b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/.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/Test_Integration_TTN_ET_SDS011/.vscode/extensions.json b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/.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/Test_Integration_TTN_ET_SDS011/include/README b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/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/Test_Integration_TTN_ET_SDS011/include/config_application.h b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/include/config_application.h new file mode 100644 index 0000000000000000000000000000000000000000..cd76c7a98269e54518272d928887132e4e8b0de5 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/include/config_application.h @@ -0,0 +1,25 @@ +/***********************************************************************/ +/* Please see README page on https://github.com/SylvainMontagny/LoRaE5 */ +/***********************************************************************/ + +#define REGION EU868 +#define ACTIVATION_MODE OTAA +#define CLASS CLASS_A +#define SPREADING_FACTOR 7 +#define ADAPTIVE_DR false +#define CONFIRMED false +#define PORT_UP 15 + +#define SEND_BY_PUSH_BUTTON false +#define FRAME_DELAY 20000 + +String devEUI = "70B3D57ED00630C5"; + +// Configuration for ABP Activation Mode +String devAddr = "00000000"; +String nwkSKey = "00000000000000000000000000000000"; +String appSKey = "00000000000000000000000000000000"; + +// Configuration for OTAA Activation Mode +String appKey = "FFC756C21917F54E3993167F8E68AB22"; +String appEUI = "000000000000000"; diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/include/config_board.h b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/include/config_board.h new file mode 100644 index 0000000000000000000000000000000000000000..dab90556f3b544b86142ba434023865d04fb7fec --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/include/config_board.h @@ -0,0 +1,119 @@ +#ifndef CONFIG_BOARD_H +#define CONFIG_BOARD_H + +////////////////////////////////////////////// +// If you use an ARDUINO or ESP32 board +////////////////////////////////////////////// + #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_RENESAS) || defined(ARDUINO_WIO_TERMINAL) + #define Debug_Serial Serial // Select the right SerialX for Debug + + // Arduino's Serial Documentation : https://www.arduino.cc/reference/en/language/functions/communication/serial/ + // ESP32 Documentation : https://microcontrollerslab.com/esp32-uart-communication-pins-example/ + #define LoRa_Serial Serial2 // Select the right SerialX for the LoRaE5 connection + + + +// If you use a STM32 NUCLEO board +////////////////////////////////////////////// + #elif defined(ARDUINO_ARCH_STM32) + #define RX_PIN_DEBUG PA3 // Select UART RX Pin for Debug + #define TX_PIN_DEBUG PA2 // Select UART TX Pin for Debug + HardwareSerial Debug_Serial(RX_PIN_DEBUG, TX_PIN_DEBUG); + + + #define RX_PIN_LORA PA10 // Select UART RX Pin for the LoRaE5 connection + #define TX_PIN_LORA PA9 // Select UART TX Pin for the LoRaE5 connection + HardwareSerial LoRa_Serial(RX_PIN_LORA, TX_PIN_LORA); + + +////////////////////////////////////////////// +// /!\ Your card has never been tested yet ! +// Please let us know if you find the PIN configuration of your board +// So we can add it to the next version of this code +////////////////////////////////////////////// + + #else + #warning "Your board has never been tested with this lib" + #warning "Select the right Serial for Debug and LoRaE5 connection in config_board.h" + + #define Debug_Serial Serial + #define LoRa_Serial Serial1 + + #endif + +#endif //CONFIG_BOARD_H + +//////////////////////////////// +/* Uart Pin list for known board +//////////////////////////////// + +NUCLEO-F446RE : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +USART3_RX = PC5 Alternate PC11 +USART3_TX = PB10 Alternate PC10 + +// + +NUCLEO-L073RZ : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) Alternate PA15 +USART2_TX = PA2 (ST-Link) Alternate PA14 + +// + +NUCLEO-F411RE : +USART1_RX = PA10 Alternate PB7 Alternate PB3 +USART1_TX = PA9 Alternate PB6 Alternate PA15 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-WL55JC1 : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-G031K8 : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-L433RC-P : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) Alternate PA15 +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-F746ZG : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) Alternate PD6 +USART2_TX = PA2 (ST-Link) Alternate PD5 + +USART3_RX = PD9 Alternate PB11 Alternate PC11 +USART3_TX = PD8 Alternate PB10 Alternate PC10 + +//////////////////////////////// +Uart Pin list for known board */ +//////////////////////////////// \ No newline at end of file diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/lib/README b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/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/Test_Integration_TTN_ET_SDS011/platformio.ini b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..e604bbdbbaf32ab6a7aec59f01ae9fe3755f3a67 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/platformio.ini @@ -0,0 +1,18 @@ +; 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 +upload_speed = 115200 +monitor_speed = 115200 +build_flags = -Iinclude +lib_deps = lewapek/Nova Fitness Sds dust sensors library@^1.5.1 diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/src/.DS_Store b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/src/.DS_Store differ diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/src/lorae5.cpp b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/src/lorae5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d235c0cc0ae402586a1fd126176bbf6dbe645a53 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/src/lorae5.cpp @@ -0,0 +1,804 @@ +#include <Arduino.h> +#include <HardwareSerial.h> +#include "lorae5.h" + +LORAE5::LORAE5(String devEUI, String appEUI, String appKey, String devAddr, String nwkSKey, String appSKey){ + this->devEUI = devEUI; + this->appEUI = appEUI; + this->appKey = appKey; + this->devAddr = devAddr; + this->nwkSKey = nwkSKey; + this->appSKey = appSKey; +} + +#if defined(ARDUINO_AVR_LEONARDO) +/// @brief Allow to set the hardware setup for our transmission +/// @param myDebugSerial is the debug serial link +/// @param myLoRaSerial is the LoRaE5 serial link +void LORAE5::setup_hardware(Serial_* myDebugSerial, HardwareSerial* myLoRaSerial){ + unsigned long startTime = millis(); + + this->serialD = myDebugSerial; + this->serialL = myLoRaSerial; + + (this->serialD)->begin(115200); + (this->serialL)->begin(9600); + + while (millis() - startTime < 2000); +} +#elif defined(ARDUINO_WIO_TERMINAL) +/// @brief Allow to set the hardware setup for our transmission +/// @param myDebugSerial is the debug serial link +/// @param myLoRaSerial is the LoRaE5 serial link +void LORAE5::setup_hardware(Serial_* myDebugSerial, Uart* myLoRaSerial){ + unsigned long startTime = millis(); + + this->serialD = myDebugSerial; + this->serialL = myLoRaSerial; + + (this->serialD)->begin(115200); + (this->serialL)->begin(9600); + + while (millis() - startTime < 2000); +} +#else +/// @brief Allow to set the hardware setup for our transmission +/// @param myDebugSerial is the debug serial link +/// @param myLoRaSerial is the LoRaE5 serial link +void LORAE5::setup_hardware(HardwareSerial* myDebugSerial, HardwareSerial* myLoRaSerial){ + unsigned long startTime = millis(); + + this->serialD = myDebugSerial; + this->serialL = myLoRaSerial; + + (this->serialD)->begin(115200); + (this->serialL)->begin(9600); + + while (millis() - startTime < 2000); +} +#endif + +/// @brief Allow to set the LoRaWAN setup for our transmission +/// @param region is the region selected by the user +/// @param mode is the mode selected by the user +/// @param devClass is the class of the device selected by the user +/// @param sf is the Spreading Factor selected by the user +/// @param adr is to enable or disable the Adaptive Data Rate +/// @param confirmed is to enable or disable the confirmed message +/// @param portUp is the uplink port selected by the user +/// @param SEND_BY_PUSH_BUTTON is the push button condition selected by the user +/// @param FRAME_DELAY is the frame delay selected by the user +void LORAE5::setup_lorawan(uint8_t region, bool mode, uint8_t devClass, uint8_t sf, bool adr, bool confirmed, uint8_t portUp, bool SEND_BY_PUSH_BUTTON, uint32_t FRAME_DELAY){ + + while(!checkBoard()); + + setDevEUI(devEUI); + setAppEUI(appEUI); + setAppKey(appKey); + + setDevAddr(devAddr); + setNwkSKey(nwkSKey); + setAppSKey(appSKey); + + this->region = region; + + setRXDelay(); + setFrequencyBand(); + setMode(mode); + setClass(devClass); + setSF(sf); + setADR(adr); + setPortUp(portUp); + + this->confirmed = confirmed; + this->SEND_BY_PUSH_BUTTON = SEND_BY_PUSH_BUTTON; + this->FRAME_DELAY = FRAME_DELAY; +} + +/// @brief Allow to set the RX delay +void LORAE5::setRXDelay(){ + readResponse(1000,NO_DEBUG); + SERIAL_L("AT+DELAY=RX1,1000"); + readResponse(400,NO_DEBUG); + SERIAL_L("AT+DELAY=RX2,2000"); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the mode of the device +/// @param mode is the mode selected by the user +void LORAE5::setMode(bool mode){ + String strMode; + + this->mode = mode; + if (mode == OTAA) strMode = "LWOTAA"; + if (mode == ABP) strMode = "LWABP"; + + SERIAL_L("AT+MODE=" + strMode); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the class of the device +/// @param devClass is the the class of the device selected by the user +void LORAE5::setClass(uint8_t devClass){ + String strClass; + + this->devClass = devClass; + if (devClass == CLASS_A) strClass = "A"; + if (devClass == CLASS_C) strClass = "C"; + SERIAL_L("AT+CLASS=" + strClass); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the Spreading Factor (SF) of the device +/// @param sf is the Spreading factor selected by the user +void LORAE5::setSF(uint8_t sf){ + this->sf = sf; + if ((this->region) == EU868 || (this->region) == CN779 || (this->region) == EU433 || (this->region) == AU915 || (this->region) == CN470 || (this->region) == AS923 || (this->region) == KR920 || (this->region) == IN865 || (this->region) == RU864 || (this->region) == CN470PREQUEL || (this->region) == STE920){ + SERIAL_L("AT+DR=DR" + String(-sf+12)); + } + + if ((this->region) == US915 || (this->region) == US915HYBRID || (this->region) == AU915OLD){ + SERIAL_L("AT+DR=DR" + String(-sf+10)); + } + + String response = (this->serialL)->readStringUntil('\n'); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to process and set the Spreading Factor (SF) received from the downlink +void LORAE5::getSetSF(){ + String response; + (this->serialL)->setTimeout(500); + SERIAL_L("AT+DR"); + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 2 + SERIAL_D(response); // DEBUG + #endif + if (this->adr == true){ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 2 + SERIAL_D(response); // DEBUG + #endif + } + + if ((this->region) == EU868 || (this->region) == CN779 || (this->region) == EU433 || (this->region) == AU915 || (this->region) == CN470 || (this->region) == AS923 || (this->region) == KR920 || (this->region) == IN865 || (this->region) == RU864 || (this->region) == CN470PREQUEL || (this->region) == STE920){ + if ( response.indexOf("DR0") != -1 ) this->sf = 12; + else if ( response.indexOf("DR1") != -1 ) this->sf = 11; + else if ( response.indexOf("DR2") != -1 ) this->sf = 10; + else if ( response.indexOf("DR3") != -1 ) this->sf = 9; + else if ( response.indexOf("DR4") != -1 ) this->sf = 8; + else if ( response.indexOf("DR5") != -1 ) this->sf = 7; + } + + if ((this->region) == US915 || (this->region) == US915HYBRID || (this->region) == AU915OLD){ + if ( response.indexOf("DR0") != -1 ) this->sf = 10; + else if ( response.indexOf("DR1") != -1 ) this->sf = 9; + else if ( response.indexOf("DR2") != -1 ) this->sf = 8; + else if ( response.indexOf("DR3") != -1 ) this->sf = 7; + } + + readResponse(100,NO_DEBUG); +} + +/// @brief Allow to set Adaptative Data Rate (ADR) +/// @param adr is to enable or disable the Adaptive Data Rate +void LORAE5::setADR(bool adr){ + this->adr = adr; + SERIAL_L("AT+ADR=" + ((adr == true)? String("ON") : String("OFF"))); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Device EUI +/// @param deveui is the Device EUI +void LORAE5::setDevEUI(String deveui){ + SERIAL_L("AT+ID=DEVEUI," + devEUI); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Application EUI +/// @param appeui is the Application EUI +void LORAE5::setAppEUI(String appeui){ + SERIAL_L("AT+ID=APPEUI," + appeui); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Application Key +/// @param appkey is the Application Key +void LORAE5::setAppKey(String appkey){ + SERIAL_L("AT+KEY=APPKEY," + appkey); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Device Address +/// @param devaddr is the Device Address +void LORAE5::setDevAddr(String devaddr){ + SERIAL_L("AT+ID=DEVADDR," + devaddr); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Network Session Key +/// @param nwkskey is the Network Session Key +void LORAE5::setNwkSKey(String nwkskey){ + SERIAL_L("AT+KEY=NWKSKEY," + nwkskey); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Application Session Key +/// @param appskey is the Application Session Key +void LORAE5::setAppSKey(String appskey){ + SERIAL_L("AT+KEY=APPSKEY," + appskey); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the uplink port +/// @param portUp is the uplink port selected by the user +void LORAE5::setPortUp(uint8_t portUp){ + this->portUp = portUp; + SERIAL_L("AT+PORT=" + String(portUp)); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to check if a board is connected +/// @return Board status +bool LORAE5::checkBoard(){ + bool success; + delay(500); + SERIAL_L("AT"); + SERIAL_D("\n\n\n\n"); + if ( (success = checkResponse(400,"+AT: OK\r\n",NO_DEBUG)) == false){ + SERIAL_D("> LoRa-E5 board not detected ..."); + } + else{ + SERIAL_D("> LoRa-E5 board detected ...\n"); + } + return success; +} + +/// @brief Allow to check if join procedure is successful +/// @return Joined status +bool LORAE5::join(){ + bool joined = false, failed = false, done = false; + String response, strDevAddr; + uint32_t index; + + delay(2000); + (this->serialL)->setTimeout(10000); + SERIAL_L("AT+JOIN"); + + do{ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 1 + SERIAL_D(response); // DEBUG + #endif + + // Analyse DevAddr response + if ( (index = response.indexOf("DevAddr")) != -1 ){ + index += strlen("DevAddr "); + strDevAddr += response.charAt(index + 0); + strDevAddr += response.charAt(index + 1); + strDevAddr += ":"; + strDevAddr += response.charAt(index + 3); + strDevAddr += response.charAt(index + 4); + strDevAddr += ":"; + strDevAddr += response.charAt(index + 6); + strDevAddr += response.charAt(index + 7); + strDevAddr += ":"; + strDevAddr += response.charAt(index + 9); + strDevAddr += response.charAt(index + 10); + SERIAL_D("> SUCCESS JOINED OTAA !!!"); + SERIAL_D("> DevAddr = " + strDevAddr); + joined = true; + } + // Analyse end of Join procedure + else if ( (index = response.indexOf("Done")) != -1 ){ + done = true; + } + // Analyse failed join + else if ( (index = response.indexOf("Join failed")) != -1 ){ + SERIAL_D("> JOIN FAILED ..."); + failed = true; + } + } while((!joined || !done) && !failed ); + SERIAL_D(""); + return joined; +} + +/// @brief Allow to send uplink data +/// @param payloadUp is the data to send +/// @param sizePayloadUp is the size of the data to send +void LORAE5::sendPayloadUp(uint8_t* payloadUp, uint8_t sizePayloadUp) { + SERIAL_D("Sending " + String(this->confirmed ? "Confirmed " : "Unconfirmed ") + String("Data Up")); + (this->serialD)->print("- Payload UP "); + + String stringPayloadUp = ""; + for (uint8_t i = 0; i < sizePayloadUp; i++) { + (this->serialD)->print(" 0x"); + if (payloadUp[i] < 0x10) { + (this->serialD)->print('0'); + stringPayloadUp += 0; + } + (this->serialD)->print(payloadUp[i], HEX); + (this->serialD)->print(" "); + stringPayloadUp += String(payloadUp[i], HEX); + } + + SERIAL_D(""); + SERIAL_D("- PortUp\t" + String(this->portUp)); + SERIAL_D("- SF\t\t" + String(this->sf)); + SERIAL_D(""); + + SERIAL_L("AT+" + String(this->confirmed ? "C" : "") + String("MSGHEX") + String("=") + stringPayloadUp); +} + +/// @brief Allow to manage the sending of uplink data +/// @param payloadUp is the data to send +/// @param sizePayloadUp is the size of the data to send +void LORAE5::sendData(uint8_t* payloadUp, uint8_t sizePayloadUp){ + bool transmissionDone = false; + String response; + uint32_t index; + (this->serialL)->setTimeout(500); + + getSetSF(); + sendPayloadUp(payloadUp, sizePayloadUp); + do{ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 3 + SERIAL_D(response); // DEBUG + #endif + + if ( (index = response.indexOf("Start")) != -1 ){ + SERIAL_D("Transmission Done\nWaiting for Downlink..."); + transmissionDone = true; + } + + if ( (index = response.indexOf("Length error 0")) != -1 ){ + SERIAL_L("AT+MSG"); + } + + if ( (index = response.indexOf("LoRaWAN modem is busy")) != -1 ){ + SERIAL_D("LoRaWAN modem is busy"); + } + + } while (!transmissionDone); + + if(confirmed == true){ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 3 + SERIAL_D(response); // DEBUG + #endif + if ( (index = response.indexOf("Wait ACK")) != -1 ){ + SERIAL_D("Waiting for ACK..."); + } + } + SERIAL_D(""); +} + +/// @brief Allow to debug or consume characters from the LoRaE5 module +/// @param timeOut is the maximum allocated time for the function +/// @param debug is to enable or disable the display +void LORAE5::readResponse(uint32_t timeOut, bool debug){ + uint32_t tstart = millis(); + + while (millis()-tstart < timeOut){ + if((this->serialL)->available() > 0) { + if (debug == DEBUG){ + (this->serialD)->print((char)(this->serialL)->read()); + } + else{ + (this->serialL)->read(); + } + } + } +} + +/// @brief Allow to check if a string is present in the log +/// @param timeOut is the maximum waiting time +/// @param strCheck is the String we are looking for +/// @param debug is to enable or disable the display +/// @return if the requested string is present or not +bool LORAE5::checkResponse(uint32_t timeOut, char *strCheck, bool debug){ + uint32_t tstart = millis(); + uint8_t i=0, checkSize = strlen(strCheck); + char c; + bool success = false; + + while (millis()-tstart < timeOut){ + if((this->serialL)->available() > 0) { + c = (this->serialL)->read(); + + if (debug == DEBUG){ + (this->serialD)->print(c); + } + + if( c == strCheck[i] ){ + if (i == (checkSize-1)){ + success = true; + } + else{ + i++; + } + } + else{ + i=0; + } + } + } +return success; +} + +/// @brief Displays the port up +/// @param string is the information received from the device and stored in a buffer memory +/// @return port up +uint8_t LORAE5::getPortUp(){ + return this->portUp; +} + +/// @brief Displays the port down +/// @param string is the information received from the device and stored in a buffer memory +/// @return port down +void LORAE5::getPortDown(String response){ + + String strPortDown; + uint32_t index; + uint32_t indexSemicolon = response.indexOf(';'); + index = response.indexOf("PORT: "); + index += strlen ("PORT: "); + + for (uint8_t i = 0 ; i < (indexSemicolon - index ) ; i++){ + strPortDown +=response.charAt(index + i); + } + uint8_t portDown = strPortDown.toInt(); + + SERIAL_D("- Port " + String(portDown)); + this->portDown = portDown; +} + +/// @brief Allow to process and display the payload down +/// @param payloadDown is downlink message +/// @param sizePayloadDown is size of the downlink message +/// @param response is the information received from the device and stored in a buffer memory +/// @return Payload down +void LORAE5::getPayloadDown(uint8_t* payloadDown, uint8_t* sizePayloadDown, String response){ + + // Get Downlink Payload + String strPayloadDown; + uint32_t index; + uint32_t indexQuote = response.lastIndexOf("\""); + index = response.indexOf("RX: \""); + index+= strlen("RX: \""); + for (uint8_t i = 0 ; i < (indexQuote - index ) ; i++){ + strPayloadDown +=response.charAt(index + i); + } + *sizePayloadDown = strPayloadDown.length() / 2 ; + for (uint8_t i = 0 ; i < *sizePayloadDown ; i++){ + String strByte =strPayloadDown.substring(i * 2, i * 2 + 2); + payloadDown[i] = strtoul(strByte.c_str(), nullptr, 16); + } + (this->serialD)->print("- Payload DOWN "); + for (uint8_t i = 0 ; i < *sizePayloadDown ; i++){ + (this->serialD)->print(" 0x"); + if (payloadDown[i] < 0x10){ + (this->serialD)->print('0'); + } + (this->serialD)->print(payloadDown[i], HEX); + (this->serialD)->print(" "); + } + SERIAL_D(""); +} + +/// @brief Displays downlink channel +/// @param response is the information received from the device and stored in a buffer memory +void LORAE5::getChannelDown(String response){ + + String strChannelDown; + uint32_t index; + uint32_t indexSemicolon = response.indexOf(", RSSI "); + index = response.indexOf(" RXWIN"); + index += strlen (" RXWIN"); + + for (uint8_t i = 0 ; i < (indexSemicolon - index ) ; i++){ + strChannelDown +=response.charAt(index + i); + } + uint8_t channelDown = strChannelDown.toInt(); + + if (channelDown == 0){ + SERIAL_D("- Slot RXC"); + } else { + SERIAL_D("- Slot RX" + String(channelDown)); + } + SERIAL_D(""); +} + +/// @brief Updates the Device Class +/// @return Device Class +uint8_t LORAE5::getClass(){ + return this->devClass; +} + +/// @brief Updates the Data Rate +/// @return Data rate +uint8_t LORAE5::getADR(){ + return this->adr; +} + +/// @brief Summarizes device parameters +void LORAE5::printInfo(){ +#ifndef ARDUINO_AVR_LEONARDO + SERIAL_D("# For more information visit : https://github.com/SylvainMontagny/LoRaE5"); + + if(this->mode == OTAA) {SERIAL_D("> OTAA");} + else {SERIAL_D("> ABP");} + + if(this->devClass == CLASS_A) {SERIAL_D("> CLASS_A");} + if(this->devClass == CLASS_C) {SERIAL_D("> CLASS_C");} + + SERIAL_D("> SF " + String(this->sf)); + + if(this->adr == true) {SERIAL_D("> ADR ON");} + else {SERIAL_D("> ADR OFF");} + + SERIAL_D(((this->confirmed == CONF)? String("> Confirmed") : String("> Unconfirmed"))); + SERIAL_D("> Port " + String(this->portUp)); + SERIAL_D(); + SERIAL_D( "* DevEUI 0x " + this->devEUI); + + if (this->mode == ABP) { + SERIAL_D("* DevAddr 0x " + this->devAddr); + SERIAL_D("* NwkSKey 0x " + this->nwkSKey); + SERIAL_D("* AppSKey 0x " + this->appSKey); + } + + if (this->mode == OTAA){ + SERIAL_D("* AppKey 0x " + this->appKey); + SERIAL_D("* AppEUI-JoinEUI 0x " + this->appEUI); + } + SERIAL_D(); +#endif +} + +/// @brief Waiting for Class_A downlink +/// @param payloadDown is downlink message +/// @param sizePayloadDown is size of the downlink message +/// @return Downlink data information (RET_T/RET_TIMEOUT/RET_DOWNLINK/RET_NO_DOWNLINK) +uint8_t LORAE5::awaitForDownlinkClass_A(uint8_t* payloadDown, uint8_t* sizePayloadDown){ + uint32_t index; + String response; + bool isDoneReceived = false; + bool isAckReceived = false; + + (this->serialL)->setTimeout(500); + + do{ + response += (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 4 + SERIAL_D(response); // DEBUG + #endif + + /****** Test Done *******/ + if ( (index = response.indexOf("Done")) != -1 ){ + isDoneReceived = true; + } + + }while(isDoneReceived == false); + + //Confirmed part + if ((confirmed == true)){ + if (index = response.indexOf("ACK Received") != -1 ){ + SERIAL_D("ACK received"); + isAckReceived = true; + } + else { + SERIAL_D("No ACK received\n"); + return RET_NO_ACK; + } + } + + //Processing data received + if ((index = response.indexOf("PORT: ")) != -1) { + SERIAL_D("Receiving Data Down:"); + getPortDown(response); + getPayloadDown(payloadDown, sizePayloadDown, response); + getChannelDown(response); + return RET_DOWNLINK; + } + + else if ( ((index = response.indexOf("RXWIN")) != -1) && (isAckReceived == false) ) { + SERIAL_D("MAC command received :"); + getChannelDown(response); + return RET_NO_DOWNLINK; + } + + else { + if(isAckReceived == false){ + SERIAL_D("No Class_A Data Received."); + } + SERIAL_D(); + return RET_NO_DOWNLINK; + } + +} + +/// @brief Waiting for Class_C downlink +/// @param payloadDown is downlink message +/// @param sizePayloadDown is size of the downlink message +/// @return Downlink data information (RET_T/RET_TIMEOUT/RET_DOWNLINK) +uint8_t LORAE5::awaitForDownlinkClass_C(uint8_t* payloadDown, uint8_t* sizePayloadDown){ + uint32_t index; + String response; + bool isDoneReceived = false; + uint32_t startTime = millis(); + + (this->serialL)->setTimeout(500); + +#ifndef ARDUINO_AVR_LEONARDO + if( this->SEND_BY_PUSH_BUTTON == true ){ + SERIAL_D("> Press 't' or receive a downlink.\n"); + }else if( this->SEND_BY_PUSH_BUTTON == false ){ + SERIAL_D("> Press 't', receive a downlink or wait " + String(this->FRAME_DELAY) + String(" ms.\n")); + } +#endif + + do{ + response += (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 4 + SERIAL_D(response); // DEBUG + #endif + + /****** Test Done *******/ + if ( (index = response.indexOf("Done")) != -1 ){ + isDoneReceived = true; + } + + /****** Test 't' : transmission request *******/ + if ( (this->serialD)->available() > 0 ) { + char userInput = (this->serialD)->read(); + if (userInput == 't' || userInput == 'T') { + SERIAL_D("# User input 't'.\n"); + return RET_T; + } + } + + /****** Test Timeout *******/ + if ((this->SEND_BY_PUSH_BUTTON == false) && ((millis() - startTime) >= (this->FRAME_DELAY))){ + SERIAL_D("No Class_C Data Received.\n"); + SERIAL_D("# Timeout reached.\n"); + return RET_TIMEOUT; + } + + }while(isDoneReceived == false); + + //Processing data received + if ((index = response.indexOf("PORT: ")) != -1) { + SERIAL_D("Receiving Data Down:"); + getPortDown(response); + getPayloadDown(payloadDown, sizePayloadDown, response); + getChannelDown(response); + return RET_DOWNLINK; + } +} + +/// @brief Allow to wait and put the LoRaE5 module in Low Power mode ON/OFF +/// @return Downlink data information (RET_T/RET_TIMEOUT) +uint8_t LORAE5::sleep(){ + uint32_t startTime = millis(); + SERIAL_L("AT+LOWPOWER"); + String response = (this->serialL)->readStringUntil('\n'); + readResponse(5, NO_DEBUG); + + if (this->SEND_BY_PUSH_BUTTON == false) { + while((millis() - startTime) < (this->FRAME_DELAY)){ + if ((this->serialD)->available() > 0) { + char userInput = (this->serialD)->read(); + if (userInput == 't' || userInput == 'T') { + SERIAL_L("WAKE-UP"); + response = (this->serialL)->readStringUntil('\n'); + readResponse(5,NO_DEBUG); + SERIAL_D("# User input 't'.\n"); + return RET_T; + } + } + } + SERIAL_L("WAKE-UP"); + response = (this->serialL)->readStringUntil('\n'); + readResponse(5,NO_DEBUG); + SERIAL_D("# Timeout reached.\n"); + return RET_TIMEOUT; + } else { + bool userTest = false; + while (userTest == false){ + if ((this->serialD)->available() > 0) { + char userInput = (this->serialD)->read(); + if (userInput == 't' || userInput == 'T') { + SERIAL_L("WAKE-UP"); + response = (this->serialL)->readStringUntil('\n'); + readResponse(5,NO_DEBUG); + userTest = true; + SERIAL_D("# User input 't'.\n"); + return RET_T; + } + } + } + } +} + +/// @brief Allow to change frequency band +void LORAE5::setFrequencyBand(){ + String response; + + if ((this->region) == EU868){ + SERIAL_L("AT+DR=EU868"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == US915){ + SERIAL_L("AT+DR=US915"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == US915HYBRID){ + SERIAL_L("AT+DR=US915HYBRID"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == CN779){ + SERIAL_L("AT+DR=CN779"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == EU433){ + SERIAL_L("AT+DR=EU433"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == AU915){ + SERIAL_L("AT+DR=AU915"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == AU915OLD){ + SERIAL_L("AT+DR=AU915OLD"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == CN470){ + SERIAL_L("AT+DR=CN470"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == CN470PREQUEL){ + SERIAL_L("AT+DR=CN470PREQUEL"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == AS923){ + SERIAL_L("AT+DR=AS923"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == KR920){ + SERIAL_L("AT+DR=KR920"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == IN865){ + SERIAL_L("AT+DR=IN865"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == RU864){ + SERIAL_L("AT+DR=RU864"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == STE920){ + SERIAL_L("AT+DR=STE920"); + response = (this->serialL)->readStringUntil('\n'); + } + + #if DEBUG_LEVEL == 5 + SERIAL_L("AT+CH"); + response = (this->serialL)->readStringUntil('\n'); + SERIAL_D(response); + #endif +} \ No newline at end of file diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/src/lorae5.h b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/src/lorae5.h new file mode 100644 index 0000000000000000000000000000000000000000..cd5c3d63acf35ec3eb0f6e7a64df3e036cdb75a1 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/src/lorae5.h @@ -0,0 +1,133 @@ +#define SERIAL_D(x) this->serialD->println(x) +#define SERIAL_L(x) this->serialL->println(x) + +#define EU868 0 +#define US915 1 +#define CN779 2 +#define EU433 3 +#define AU915 4 +#define CN470 5 +#define AS923 6 +#define KR920 7 +#define IN865 8 +#define RU864 9 +#define CN470PREQUEL 10 +#define STE920 11 +#define US915HYBRID 12 +#define AU915OLD 13 + +#define ABP 0 +#define OTAA 1 + +#define CLASS_A 0 +#define CLASS_B 1 +#define CLASS_C 2 + +#define DEBUG true +#define NO_DEBUG false + +#define DEBUG_LEVEL 0 +/* list for DEBUG_LEVEL: +- join : 1; +- getSetSF : 2; +- sendData : 3; +- awaitForDownlink : 4; +- setFrequencyBand : 5; +*/ + +#define CONF true +#define UNCONF false + +#define _HEXA 0 +#define _STRING 1 + +enum RETURN { RET_DOWNLINK, RET_NO_DOWNLINK, RET_NO_ACK, RET_T, RET_TIMEOUT }; + +class LORAE5{ + +public: + // uint8_t downlink[50]; + +private: + uint8_t region; + bool mode; + uint8_t devClass; + uint8_t sf; + bool adr; + bool confirmed; + uint8_t portUp; + uint8_t portDown; + +#if defined(ARDUINO_AVR_LEONARDO) || defined(ARDUINO_WIO_TERMINAL) + Serial_* serialD; +#else + HardwareSerial* serialD; +#endif + +#if defined(ARDUINO_WIO_TERMINAL) + Uart* serialL; +#else + HardwareSerial* serialL; +#endif + + String devEUI; + String devAddr; + String nwkSKey; + String appSKey; + String appKey; + String appEUI; + + bool SEND_BY_PUSH_BUTTON; + uint32_t FRAME_DELAY; + + void readResponse(uint32_t timeOut, bool debug); + bool checkResponse(uint32_t timeOut, char *strCheck, bool debug); + bool checkBoard(void); + + void setDevEUI(String deveui); + void setAppKey(String appkey); + void setAppEUI(String appeui); + void setDevAddr(String devaddr); + void setNwkSKey(String nwkskey); + void setAppSKey(String appskey); + void setMode(bool mode); + void setClass(uint8_t devClass); + void setRXDelay(); + void setSF(uint8_t sf); + void setADR(bool adr); + void setPortUp(uint8_t portUp); + void setFrequencyBand(void); + + void getMode(void); + void getSetSF(void); + void getPortDown(String response); + void getPayloadDown(uint8_t* payloadDown, uint8_t* sizePayloadDown, String response); + void getChannelDown(String response); + uint8_t getADR(void); + uint8_t getPortUp(void); + uint8_t getClass(void); + + void sendPayloadUp(uint8_t* payloadUp, uint8_t sizePayloadUp); + +public: + LORAE5(String devEUI, String appEUI, String appKey, String devAddr, String nwkSKey, String appSKey); + +#if defined(ARDUINO_AVR_LEONARDO) + void setup_hardware(Serial_* myDebugSerial, HardwareSerial* myLoRaSerial); +#elif defined(ARDUINO_WIO_TERMINAL) +void setup_hardware(Serial_* myDebugSerial, Uart* myLoRaSerial); +#else + void setup_hardware(HardwareSerial* myDebugSerial, HardwareSerial* myLoRaSerial); +#endif + + void setup_lorawan(uint8_t band, bool mode, uint8_t devClass, uint8_t sf, bool adr, bool messageType, uint8_t portUp, bool SEND_BY_PUSH_BUTTON, uint32_t FRAME_DELAY); + void printInfo(void); + + bool join(void); + + void sendData(uint8_t* payloadUp, uint8_t sizePayloadUp); + uint8_t awaitForDownlinkClass_A(uint8_t* payloadDown, uint8_t* sizePayloadDown); + uint8_t awaitForDownlinkClass_C(uint8_t* payloadDown, uint8_t* sizePayloadDown); + uint8_t sleep(void); + +}; \ No newline at end of file diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/src/main.cpp b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d34c051eece485badac94780da4ea0b92acb6a6d --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/src/main.cpp @@ -0,0 +1,95 @@ +#include <Arduino.h> +#include "lorae5.h" +#include "config_application.h" +#include "config_board.h" +#include <HardwareSerial.h> +#include "SdsDustSensor.h" + +// Définition des broches UART pour le SDS011 +#define RX_PIN_SDS 19 // GPIO19 pour RX du SDS011 +#define TX_PIN_SDS 18 // GPIO18 pour TX du SDS011 + +// Définition des broches UART pour le LoRa-E5 +#define RXD2 16 // GPIO16 pour RX du LoRa-E5 +#define TXD2 17 // GPIO17 pour TX du LoRa-E5 + +// Déclaration des objets Serial +HardwareSerial SDS_Serial(1); // UART1 pour SDS011 + +// Création des objets capteur et LoRa +SdsDustSensor sds(SDS_Serial); +LORAE5 lorae5(devEUI, appEUI, appKey, devAddr, nwkSKey, appSKey); + +// Buffer pour l'envoi des données (4 octets, avec uint8_t pour bien gérer les octets) +uint8_t payloadUp[4]; + +void setup() { + Serial.begin(115200); + + // Initialisation du port série pour le SDS011 + SDS_Serial.begin(9600, SERIAL_8N1, RX_PIN_SDS, TX_PIN_SDS); + sds.begin(); + Serial.println("SDS011 Initialisé !"); + Serial.println(sds.queryFirmwareVersion().toString()); + Serial.println(sds.setActiveReportingMode().toString()); + Serial.println(sds.setContinuousWorkingPeriod().toString()); + + // Initialisation du module LoRa-E5 + lorae5.setup_hardware(&Debug_Serial, &Serial2); + lorae5.setup_lorawan(REGION, ACTIVATION_MODE, CLASS, SPREADING_FACTOR, ADAPTIVE_DR, CONFIRMED, PORT_UP, SEND_BY_PUSH_BUTTON, FRAME_DELAY); + lorae5.printInfo(); + + Debug_Serial.begin(115200); + Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); + + // Connexion OTAA si activée + if (ACTIVATION_MODE == OTAA) { + Debug_Serial.println("Join Procedure in progress..."); + while (!lorae5.join()); + delay(2000); + } +} + +void loop() { + PmResult pm = sds.readPm(); + + //On imprime les données sur le moniteur série + if (pm.isOk()) { + Serial.print("PM2.5 = "); + Serial.print(pm.pm25); + Serial.print(" µg/m³, PM10 = "); + Serial.print(pm.pm10); + Serial.println(" µg/m³"); + + // Conversion en uint16_t avec *10 pour garder 1 chiffre après la virgule + uint16_t pm25 = static_cast<uint16_t>(pm.pm25 * 10); // Valeurs de PM2.5 + uint16_t pm10 = static_cast<uint16_t>(pm.pm10 * 10); // Valeurs de PM10 + + // Stockage dans le payload en respectant l'ordre Little-Endian + payloadUp[0] = (pm25 >> 8) & 0xFF; // Octet fort en premier ✅ + payloadUp[1] = pm25 & 0xFF; // Octet faible en second ✅ + payloadUp[2] = (pm10 >> 8) & 0xFF; // Octet fort en premier ✅ + payloadUp[3] = pm10 & 0xFF; // Octet faible en second ✅ + + // Affichage du payload avant envoi (debug) + Serial.print("Payload envoyé (HEX) : "); + for (int i = 0; i < sizeof(payloadUp); i++) { + Serial.print("0x"); + Serial.print(payloadUp[i], HEX); + Serial.print(" "); + } + Serial.println(); + + // Envoi via LoRa + lorae5.sendData(payloadUp, sizeof(payloadUp)); + + // Pause avant le prochain envoi (modifiable) + delay(30000); + } else { + Serial.print("Erreur de lecture SDS011 : "); + Serial.println(pm.statusToString()); + } + + // Mise en veille pour économie d'énergie + lorae5.sleep(); +} diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/test/README b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011/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/Test_Integration_TTN_ET_SDS011_Cayenne/.DS_Store b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d1db41456480cf17f050dd1c49735854df8bea08 Binary files /dev/null and b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/.DS_Store differ diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/.gitignore b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/.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/Test_Integration_TTN_ET_SDS011_Cayenne/.vscode/extensions.json b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/.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/Test_Integration_TTN_ET_SDS011_Cayenne/include/README b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/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/Test_Integration_TTN_ET_SDS011_Cayenne/include/config_application.h b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/include/config_application.h new file mode 100644 index 0000000000000000000000000000000000000000..cd76c7a98269e54518272d928887132e4e8b0de5 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/include/config_application.h @@ -0,0 +1,25 @@ +/***********************************************************************/ +/* Please see README page on https://github.com/SylvainMontagny/LoRaE5 */ +/***********************************************************************/ + +#define REGION EU868 +#define ACTIVATION_MODE OTAA +#define CLASS CLASS_A +#define SPREADING_FACTOR 7 +#define ADAPTIVE_DR false +#define CONFIRMED false +#define PORT_UP 15 + +#define SEND_BY_PUSH_BUTTON false +#define FRAME_DELAY 20000 + +String devEUI = "70B3D57ED00630C5"; + +// Configuration for ABP Activation Mode +String devAddr = "00000000"; +String nwkSKey = "00000000000000000000000000000000"; +String appSKey = "00000000000000000000000000000000"; + +// Configuration for OTAA Activation Mode +String appKey = "FFC756C21917F54E3993167F8E68AB22"; +String appEUI = "000000000000000"; diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/include/config_board.h b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/include/config_board.h new file mode 100644 index 0000000000000000000000000000000000000000..dab90556f3b544b86142ba434023865d04fb7fec --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/include/config_board.h @@ -0,0 +1,119 @@ +#ifndef CONFIG_BOARD_H +#define CONFIG_BOARD_H + +////////////////////////////////////////////// +// If you use an ARDUINO or ESP32 board +////////////////////////////////////////////// + #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_RENESAS) || defined(ARDUINO_WIO_TERMINAL) + #define Debug_Serial Serial // Select the right SerialX for Debug + + // Arduino's Serial Documentation : https://www.arduino.cc/reference/en/language/functions/communication/serial/ + // ESP32 Documentation : https://microcontrollerslab.com/esp32-uart-communication-pins-example/ + #define LoRa_Serial Serial2 // Select the right SerialX for the LoRaE5 connection + + + +// If you use a STM32 NUCLEO board +////////////////////////////////////////////// + #elif defined(ARDUINO_ARCH_STM32) + #define RX_PIN_DEBUG PA3 // Select UART RX Pin for Debug + #define TX_PIN_DEBUG PA2 // Select UART TX Pin for Debug + HardwareSerial Debug_Serial(RX_PIN_DEBUG, TX_PIN_DEBUG); + + + #define RX_PIN_LORA PA10 // Select UART RX Pin for the LoRaE5 connection + #define TX_PIN_LORA PA9 // Select UART TX Pin for the LoRaE5 connection + HardwareSerial LoRa_Serial(RX_PIN_LORA, TX_PIN_LORA); + + +////////////////////////////////////////////// +// /!\ Your card has never been tested yet ! +// Please let us know if you find the PIN configuration of your board +// So we can add it to the next version of this code +////////////////////////////////////////////// + + #else + #warning "Your board has never been tested with this lib" + #warning "Select the right Serial for Debug and LoRaE5 connection in config_board.h" + + #define Debug_Serial Serial + #define LoRa_Serial Serial1 + + #endif + +#endif //CONFIG_BOARD_H + +//////////////////////////////// +/* Uart Pin list for known board +//////////////////////////////// + +NUCLEO-F446RE : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +USART3_RX = PC5 Alternate PC11 +USART3_TX = PB10 Alternate PC10 + +// + +NUCLEO-L073RZ : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) Alternate PA15 +USART2_TX = PA2 (ST-Link) Alternate PA14 + +// + +NUCLEO-F411RE : +USART1_RX = PA10 Alternate PB7 Alternate PB3 +USART1_TX = PA9 Alternate PB6 Alternate PA15 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-WL55JC1 : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-G031K8 : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-L433RC-P : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) Alternate PA15 +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-F746ZG : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) Alternate PD6 +USART2_TX = PA2 (ST-Link) Alternate PD5 + +USART3_RX = PD9 Alternate PB11 Alternate PC11 +USART3_TX = PD8 Alternate PB10 Alternate PC10 + +//////////////////////////////// +Uart Pin list for known board */ +//////////////////////////////// \ No newline at end of file diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/lib/README b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/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/Test_Integration_TTN_ET_SDS011_Cayenne/platformio.ini b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..e604bbdbbaf32ab6a7aec59f01ae9fe3755f3a67 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/platformio.ini @@ -0,0 +1,18 @@ +; 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 +upload_speed = 115200 +monitor_speed = 115200 +build_flags = -Iinclude +lib_deps = lewapek/Nova Fitness Sds dust sensors library@^1.5.1 diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/src/.DS_Store b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/src/.DS_Store differ diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/src/lorae5.cpp b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/src/lorae5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d235c0cc0ae402586a1fd126176bbf6dbe645a53 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/src/lorae5.cpp @@ -0,0 +1,804 @@ +#include <Arduino.h> +#include <HardwareSerial.h> +#include "lorae5.h" + +LORAE5::LORAE5(String devEUI, String appEUI, String appKey, String devAddr, String nwkSKey, String appSKey){ + this->devEUI = devEUI; + this->appEUI = appEUI; + this->appKey = appKey; + this->devAddr = devAddr; + this->nwkSKey = nwkSKey; + this->appSKey = appSKey; +} + +#if defined(ARDUINO_AVR_LEONARDO) +/// @brief Allow to set the hardware setup for our transmission +/// @param myDebugSerial is the debug serial link +/// @param myLoRaSerial is the LoRaE5 serial link +void LORAE5::setup_hardware(Serial_* myDebugSerial, HardwareSerial* myLoRaSerial){ + unsigned long startTime = millis(); + + this->serialD = myDebugSerial; + this->serialL = myLoRaSerial; + + (this->serialD)->begin(115200); + (this->serialL)->begin(9600); + + while (millis() - startTime < 2000); +} +#elif defined(ARDUINO_WIO_TERMINAL) +/// @brief Allow to set the hardware setup for our transmission +/// @param myDebugSerial is the debug serial link +/// @param myLoRaSerial is the LoRaE5 serial link +void LORAE5::setup_hardware(Serial_* myDebugSerial, Uart* myLoRaSerial){ + unsigned long startTime = millis(); + + this->serialD = myDebugSerial; + this->serialL = myLoRaSerial; + + (this->serialD)->begin(115200); + (this->serialL)->begin(9600); + + while (millis() - startTime < 2000); +} +#else +/// @brief Allow to set the hardware setup for our transmission +/// @param myDebugSerial is the debug serial link +/// @param myLoRaSerial is the LoRaE5 serial link +void LORAE5::setup_hardware(HardwareSerial* myDebugSerial, HardwareSerial* myLoRaSerial){ + unsigned long startTime = millis(); + + this->serialD = myDebugSerial; + this->serialL = myLoRaSerial; + + (this->serialD)->begin(115200); + (this->serialL)->begin(9600); + + while (millis() - startTime < 2000); +} +#endif + +/// @brief Allow to set the LoRaWAN setup for our transmission +/// @param region is the region selected by the user +/// @param mode is the mode selected by the user +/// @param devClass is the class of the device selected by the user +/// @param sf is the Spreading Factor selected by the user +/// @param adr is to enable or disable the Adaptive Data Rate +/// @param confirmed is to enable or disable the confirmed message +/// @param portUp is the uplink port selected by the user +/// @param SEND_BY_PUSH_BUTTON is the push button condition selected by the user +/// @param FRAME_DELAY is the frame delay selected by the user +void LORAE5::setup_lorawan(uint8_t region, bool mode, uint8_t devClass, uint8_t sf, bool adr, bool confirmed, uint8_t portUp, bool SEND_BY_PUSH_BUTTON, uint32_t FRAME_DELAY){ + + while(!checkBoard()); + + setDevEUI(devEUI); + setAppEUI(appEUI); + setAppKey(appKey); + + setDevAddr(devAddr); + setNwkSKey(nwkSKey); + setAppSKey(appSKey); + + this->region = region; + + setRXDelay(); + setFrequencyBand(); + setMode(mode); + setClass(devClass); + setSF(sf); + setADR(adr); + setPortUp(portUp); + + this->confirmed = confirmed; + this->SEND_BY_PUSH_BUTTON = SEND_BY_PUSH_BUTTON; + this->FRAME_DELAY = FRAME_DELAY; +} + +/// @brief Allow to set the RX delay +void LORAE5::setRXDelay(){ + readResponse(1000,NO_DEBUG); + SERIAL_L("AT+DELAY=RX1,1000"); + readResponse(400,NO_DEBUG); + SERIAL_L("AT+DELAY=RX2,2000"); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the mode of the device +/// @param mode is the mode selected by the user +void LORAE5::setMode(bool mode){ + String strMode; + + this->mode = mode; + if (mode == OTAA) strMode = "LWOTAA"; + if (mode == ABP) strMode = "LWABP"; + + SERIAL_L("AT+MODE=" + strMode); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the class of the device +/// @param devClass is the the class of the device selected by the user +void LORAE5::setClass(uint8_t devClass){ + String strClass; + + this->devClass = devClass; + if (devClass == CLASS_A) strClass = "A"; + if (devClass == CLASS_C) strClass = "C"; + SERIAL_L("AT+CLASS=" + strClass); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the Spreading Factor (SF) of the device +/// @param sf is the Spreading factor selected by the user +void LORAE5::setSF(uint8_t sf){ + this->sf = sf; + if ((this->region) == EU868 || (this->region) == CN779 || (this->region) == EU433 || (this->region) == AU915 || (this->region) == CN470 || (this->region) == AS923 || (this->region) == KR920 || (this->region) == IN865 || (this->region) == RU864 || (this->region) == CN470PREQUEL || (this->region) == STE920){ + SERIAL_L("AT+DR=DR" + String(-sf+12)); + } + + if ((this->region) == US915 || (this->region) == US915HYBRID || (this->region) == AU915OLD){ + SERIAL_L("AT+DR=DR" + String(-sf+10)); + } + + String response = (this->serialL)->readStringUntil('\n'); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to process and set the Spreading Factor (SF) received from the downlink +void LORAE5::getSetSF(){ + String response; + (this->serialL)->setTimeout(500); + SERIAL_L("AT+DR"); + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 2 + SERIAL_D(response); // DEBUG + #endif + if (this->adr == true){ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 2 + SERIAL_D(response); // DEBUG + #endif + } + + if ((this->region) == EU868 || (this->region) == CN779 || (this->region) == EU433 || (this->region) == AU915 || (this->region) == CN470 || (this->region) == AS923 || (this->region) == KR920 || (this->region) == IN865 || (this->region) == RU864 || (this->region) == CN470PREQUEL || (this->region) == STE920){ + if ( response.indexOf("DR0") != -1 ) this->sf = 12; + else if ( response.indexOf("DR1") != -1 ) this->sf = 11; + else if ( response.indexOf("DR2") != -1 ) this->sf = 10; + else if ( response.indexOf("DR3") != -1 ) this->sf = 9; + else if ( response.indexOf("DR4") != -1 ) this->sf = 8; + else if ( response.indexOf("DR5") != -1 ) this->sf = 7; + } + + if ((this->region) == US915 || (this->region) == US915HYBRID || (this->region) == AU915OLD){ + if ( response.indexOf("DR0") != -1 ) this->sf = 10; + else if ( response.indexOf("DR1") != -1 ) this->sf = 9; + else if ( response.indexOf("DR2") != -1 ) this->sf = 8; + else if ( response.indexOf("DR3") != -1 ) this->sf = 7; + } + + readResponse(100,NO_DEBUG); +} + +/// @brief Allow to set Adaptative Data Rate (ADR) +/// @param adr is to enable or disable the Adaptive Data Rate +void LORAE5::setADR(bool adr){ + this->adr = adr; + SERIAL_L("AT+ADR=" + ((adr == true)? String("ON") : String("OFF"))); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Device EUI +/// @param deveui is the Device EUI +void LORAE5::setDevEUI(String deveui){ + SERIAL_L("AT+ID=DEVEUI," + devEUI); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Application EUI +/// @param appeui is the Application EUI +void LORAE5::setAppEUI(String appeui){ + SERIAL_L("AT+ID=APPEUI," + appeui); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Application Key +/// @param appkey is the Application Key +void LORAE5::setAppKey(String appkey){ + SERIAL_L("AT+KEY=APPKEY," + appkey); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Device Address +/// @param devaddr is the Device Address +void LORAE5::setDevAddr(String devaddr){ + SERIAL_L("AT+ID=DEVADDR," + devaddr); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Network Session Key +/// @param nwkskey is the Network Session Key +void LORAE5::setNwkSKey(String nwkskey){ + SERIAL_L("AT+KEY=NWKSKEY," + nwkskey); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Application Session Key +/// @param appskey is the Application Session Key +void LORAE5::setAppSKey(String appskey){ + SERIAL_L("AT+KEY=APPSKEY," + appskey); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the uplink port +/// @param portUp is the uplink port selected by the user +void LORAE5::setPortUp(uint8_t portUp){ + this->portUp = portUp; + SERIAL_L("AT+PORT=" + String(portUp)); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to check if a board is connected +/// @return Board status +bool LORAE5::checkBoard(){ + bool success; + delay(500); + SERIAL_L("AT"); + SERIAL_D("\n\n\n\n"); + if ( (success = checkResponse(400,"+AT: OK\r\n",NO_DEBUG)) == false){ + SERIAL_D("> LoRa-E5 board not detected ..."); + } + else{ + SERIAL_D("> LoRa-E5 board detected ...\n"); + } + return success; +} + +/// @brief Allow to check if join procedure is successful +/// @return Joined status +bool LORAE5::join(){ + bool joined = false, failed = false, done = false; + String response, strDevAddr; + uint32_t index; + + delay(2000); + (this->serialL)->setTimeout(10000); + SERIAL_L("AT+JOIN"); + + do{ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 1 + SERIAL_D(response); // DEBUG + #endif + + // Analyse DevAddr response + if ( (index = response.indexOf("DevAddr")) != -1 ){ + index += strlen("DevAddr "); + strDevAddr += response.charAt(index + 0); + strDevAddr += response.charAt(index + 1); + strDevAddr += ":"; + strDevAddr += response.charAt(index + 3); + strDevAddr += response.charAt(index + 4); + strDevAddr += ":"; + strDevAddr += response.charAt(index + 6); + strDevAddr += response.charAt(index + 7); + strDevAddr += ":"; + strDevAddr += response.charAt(index + 9); + strDevAddr += response.charAt(index + 10); + SERIAL_D("> SUCCESS JOINED OTAA !!!"); + SERIAL_D("> DevAddr = " + strDevAddr); + joined = true; + } + // Analyse end of Join procedure + else if ( (index = response.indexOf("Done")) != -1 ){ + done = true; + } + // Analyse failed join + else if ( (index = response.indexOf("Join failed")) != -1 ){ + SERIAL_D("> JOIN FAILED ..."); + failed = true; + } + } while((!joined || !done) && !failed ); + SERIAL_D(""); + return joined; +} + +/// @brief Allow to send uplink data +/// @param payloadUp is the data to send +/// @param sizePayloadUp is the size of the data to send +void LORAE5::sendPayloadUp(uint8_t* payloadUp, uint8_t sizePayloadUp) { + SERIAL_D("Sending " + String(this->confirmed ? "Confirmed " : "Unconfirmed ") + String("Data Up")); + (this->serialD)->print("- Payload UP "); + + String stringPayloadUp = ""; + for (uint8_t i = 0; i < sizePayloadUp; i++) { + (this->serialD)->print(" 0x"); + if (payloadUp[i] < 0x10) { + (this->serialD)->print('0'); + stringPayloadUp += 0; + } + (this->serialD)->print(payloadUp[i], HEX); + (this->serialD)->print(" "); + stringPayloadUp += String(payloadUp[i], HEX); + } + + SERIAL_D(""); + SERIAL_D("- PortUp\t" + String(this->portUp)); + SERIAL_D("- SF\t\t" + String(this->sf)); + SERIAL_D(""); + + SERIAL_L("AT+" + String(this->confirmed ? "C" : "") + String("MSGHEX") + String("=") + stringPayloadUp); +} + +/// @brief Allow to manage the sending of uplink data +/// @param payloadUp is the data to send +/// @param sizePayloadUp is the size of the data to send +void LORAE5::sendData(uint8_t* payloadUp, uint8_t sizePayloadUp){ + bool transmissionDone = false; + String response; + uint32_t index; + (this->serialL)->setTimeout(500); + + getSetSF(); + sendPayloadUp(payloadUp, sizePayloadUp); + do{ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 3 + SERIAL_D(response); // DEBUG + #endif + + if ( (index = response.indexOf("Start")) != -1 ){ + SERIAL_D("Transmission Done\nWaiting for Downlink..."); + transmissionDone = true; + } + + if ( (index = response.indexOf("Length error 0")) != -1 ){ + SERIAL_L("AT+MSG"); + } + + if ( (index = response.indexOf("LoRaWAN modem is busy")) != -1 ){ + SERIAL_D("LoRaWAN modem is busy"); + } + + } while (!transmissionDone); + + if(confirmed == true){ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 3 + SERIAL_D(response); // DEBUG + #endif + if ( (index = response.indexOf("Wait ACK")) != -1 ){ + SERIAL_D("Waiting for ACK..."); + } + } + SERIAL_D(""); +} + +/// @brief Allow to debug or consume characters from the LoRaE5 module +/// @param timeOut is the maximum allocated time for the function +/// @param debug is to enable or disable the display +void LORAE5::readResponse(uint32_t timeOut, bool debug){ + uint32_t tstart = millis(); + + while (millis()-tstart < timeOut){ + if((this->serialL)->available() > 0) { + if (debug == DEBUG){ + (this->serialD)->print((char)(this->serialL)->read()); + } + else{ + (this->serialL)->read(); + } + } + } +} + +/// @brief Allow to check if a string is present in the log +/// @param timeOut is the maximum waiting time +/// @param strCheck is the String we are looking for +/// @param debug is to enable or disable the display +/// @return if the requested string is present or not +bool LORAE5::checkResponse(uint32_t timeOut, char *strCheck, bool debug){ + uint32_t tstart = millis(); + uint8_t i=0, checkSize = strlen(strCheck); + char c; + bool success = false; + + while (millis()-tstart < timeOut){ + if((this->serialL)->available() > 0) { + c = (this->serialL)->read(); + + if (debug == DEBUG){ + (this->serialD)->print(c); + } + + if( c == strCheck[i] ){ + if (i == (checkSize-1)){ + success = true; + } + else{ + i++; + } + } + else{ + i=0; + } + } + } +return success; +} + +/// @brief Displays the port up +/// @param string is the information received from the device and stored in a buffer memory +/// @return port up +uint8_t LORAE5::getPortUp(){ + return this->portUp; +} + +/// @brief Displays the port down +/// @param string is the information received from the device and stored in a buffer memory +/// @return port down +void LORAE5::getPortDown(String response){ + + String strPortDown; + uint32_t index; + uint32_t indexSemicolon = response.indexOf(';'); + index = response.indexOf("PORT: "); + index += strlen ("PORT: "); + + for (uint8_t i = 0 ; i < (indexSemicolon - index ) ; i++){ + strPortDown +=response.charAt(index + i); + } + uint8_t portDown = strPortDown.toInt(); + + SERIAL_D("- Port " + String(portDown)); + this->portDown = portDown; +} + +/// @brief Allow to process and display the payload down +/// @param payloadDown is downlink message +/// @param sizePayloadDown is size of the downlink message +/// @param response is the information received from the device and stored in a buffer memory +/// @return Payload down +void LORAE5::getPayloadDown(uint8_t* payloadDown, uint8_t* sizePayloadDown, String response){ + + // Get Downlink Payload + String strPayloadDown; + uint32_t index; + uint32_t indexQuote = response.lastIndexOf("\""); + index = response.indexOf("RX: \""); + index+= strlen("RX: \""); + for (uint8_t i = 0 ; i < (indexQuote - index ) ; i++){ + strPayloadDown +=response.charAt(index + i); + } + *sizePayloadDown = strPayloadDown.length() / 2 ; + for (uint8_t i = 0 ; i < *sizePayloadDown ; i++){ + String strByte =strPayloadDown.substring(i * 2, i * 2 + 2); + payloadDown[i] = strtoul(strByte.c_str(), nullptr, 16); + } + (this->serialD)->print("- Payload DOWN "); + for (uint8_t i = 0 ; i < *sizePayloadDown ; i++){ + (this->serialD)->print(" 0x"); + if (payloadDown[i] < 0x10){ + (this->serialD)->print('0'); + } + (this->serialD)->print(payloadDown[i], HEX); + (this->serialD)->print(" "); + } + SERIAL_D(""); +} + +/// @brief Displays downlink channel +/// @param response is the information received from the device and stored in a buffer memory +void LORAE5::getChannelDown(String response){ + + String strChannelDown; + uint32_t index; + uint32_t indexSemicolon = response.indexOf(", RSSI "); + index = response.indexOf(" RXWIN"); + index += strlen (" RXWIN"); + + for (uint8_t i = 0 ; i < (indexSemicolon - index ) ; i++){ + strChannelDown +=response.charAt(index + i); + } + uint8_t channelDown = strChannelDown.toInt(); + + if (channelDown == 0){ + SERIAL_D("- Slot RXC"); + } else { + SERIAL_D("- Slot RX" + String(channelDown)); + } + SERIAL_D(""); +} + +/// @brief Updates the Device Class +/// @return Device Class +uint8_t LORAE5::getClass(){ + return this->devClass; +} + +/// @brief Updates the Data Rate +/// @return Data rate +uint8_t LORAE5::getADR(){ + return this->adr; +} + +/// @brief Summarizes device parameters +void LORAE5::printInfo(){ +#ifndef ARDUINO_AVR_LEONARDO + SERIAL_D("# For more information visit : https://github.com/SylvainMontagny/LoRaE5"); + + if(this->mode == OTAA) {SERIAL_D("> OTAA");} + else {SERIAL_D("> ABP");} + + if(this->devClass == CLASS_A) {SERIAL_D("> CLASS_A");} + if(this->devClass == CLASS_C) {SERIAL_D("> CLASS_C");} + + SERIAL_D("> SF " + String(this->sf)); + + if(this->adr == true) {SERIAL_D("> ADR ON");} + else {SERIAL_D("> ADR OFF");} + + SERIAL_D(((this->confirmed == CONF)? String("> Confirmed") : String("> Unconfirmed"))); + SERIAL_D("> Port " + String(this->portUp)); + SERIAL_D(); + SERIAL_D( "* DevEUI 0x " + this->devEUI); + + if (this->mode == ABP) { + SERIAL_D("* DevAddr 0x " + this->devAddr); + SERIAL_D("* NwkSKey 0x " + this->nwkSKey); + SERIAL_D("* AppSKey 0x " + this->appSKey); + } + + if (this->mode == OTAA){ + SERIAL_D("* AppKey 0x " + this->appKey); + SERIAL_D("* AppEUI-JoinEUI 0x " + this->appEUI); + } + SERIAL_D(); +#endif +} + +/// @brief Waiting for Class_A downlink +/// @param payloadDown is downlink message +/// @param sizePayloadDown is size of the downlink message +/// @return Downlink data information (RET_T/RET_TIMEOUT/RET_DOWNLINK/RET_NO_DOWNLINK) +uint8_t LORAE5::awaitForDownlinkClass_A(uint8_t* payloadDown, uint8_t* sizePayloadDown){ + uint32_t index; + String response; + bool isDoneReceived = false; + bool isAckReceived = false; + + (this->serialL)->setTimeout(500); + + do{ + response += (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 4 + SERIAL_D(response); // DEBUG + #endif + + /****** Test Done *******/ + if ( (index = response.indexOf("Done")) != -1 ){ + isDoneReceived = true; + } + + }while(isDoneReceived == false); + + //Confirmed part + if ((confirmed == true)){ + if (index = response.indexOf("ACK Received") != -1 ){ + SERIAL_D("ACK received"); + isAckReceived = true; + } + else { + SERIAL_D("No ACK received\n"); + return RET_NO_ACK; + } + } + + //Processing data received + if ((index = response.indexOf("PORT: ")) != -1) { + SERIAL_D("Receiving Data Down:"); + getPortDown(response); + getPayloadDown(payloadDown, sizePayloadDown, response); + getChannelDown(response); + return RET_DOWNLINK; + } + + else if ( ((index = response.indexOf("RXWIN")) != -1) && (isAckReceived == false) ) { + SERIAL_D("MAC command received :"); + getChannelDown(response); + return RET_NO_DOWNLINK; + } + + else { + if(isAckReceived == false){ + SERIAL_D("No Class_A Data Received."); + } + SERIAL_D(); + return RET_NO_DOWNLINK; + } + +} + +/// @brief Waiting for Class_C downlink +/// @param payloadDown is downlink message +/// @param sizePayloadDown is size of the downlink message +/// @return Downlink data information (RET_T/RET_TIMEOUT/RET_DOWNLINK) +uint8_t LORAE5::awaitForDownlinkClass_C(uint8_t* payloadDown, uint8_t* sizePayloadDown){ + uint32_t index; + String response; + bool isDoneReceived = false; + uint32_t startTime = millis(); + + (this->serialL)->setTimeout(500); + +#ifndef ARDUINO_AVR_LEONARDO + if( this->SEND_BY_PUSH_BUTTON == true ){ + SERIAL_D("> Press 't' or receive a downlink.\n"); + }else if( this->SEND_BY_PUSH_BUTTON == false ){ + SERIAL_D("> Press 't', receive a downlink or wait " + String(this->FRAME_DELAY) + String(" ms.\n")); + } +#endif + + do{ + response += (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 4 + SERIAL_D(response); // DEBUG + #endif + + /****** Test Done *******/ + if ( (index = response.indexOf("Done")) != -1 ){ + isDoneReceived = true; + } + + /****** Test 't' : transmission request *******/ + if ( (this->serialD)->available() > 0 ) { + char userInput = (this->serialD)->read(); + if (userInput == 't' || userInput == 'T') { + SERIAL_D("# User input 't'.\n"); + return RET_T; + } + } + + /****** Test Timeout *******/ + if ((this->SEND_BY_PUSH_BUTTON == false) && ((millis() - startTime) >= (this->FRAME_DELAY))){ + SERIAL_D("No Class_C Data Received.\n"); + SERIAL_D("# Timeout reached.\n"); + return RET_TIMEOUT; + } + + }while(isDoneReceived == false); + + //Processing data received + if ((index = response.indexOf("PORT: ")) != -1) { + SERIAL_D("Receiving Data Down:"); + getPortDown(response); + getPayloadDown(payloadDown, sizePayloadDown, response); + getChannelDown(response); + return RET_DOWNLINK; + } +} + +/// @brief Allow to wait and put the LoRaE5 module in Low Power mode ON/OFF +/// @return Downlink data information (RET_T/RET_TIMEOUT) +uint8_t LORAE5::sleep(){ + uint32_t startTime = millis(); + SERIAL_L("AT+LOWPOWER"); + String response = (this->serialL)->readStringUntil('\n'); + readResponse(5, NO_DEBUG); + + if (this->SEND_BY_PUSH_BUTTON == false) { + while((millis() - startTime) < (this->FRAME_DELAY)){ + if ((this->serialD)->available() > 0) { + char userInput = (this->serialD)->read(); + if (userInput == 't' || userInput == 'T') { + SERIAL_L("WAKE-UP"); + response = (this->serialL)->readStringUntil('\n'); + readResponse(5,NO_DEBUG); + SERIAL_D("# User input 't'.\n"); + return RET_T; + } + } + } + SERIAL_L("WAKE-UP"); + response = (this->serialL)->readStringUntil('\n'); + readResponse(5,NO_DEBUG); + SERIAL_D("# Timeout reached.\n"); + return RET_TIMEOUT; + } else { + bool userTest = false; + while (userTest == false){ + if ((this->serialD)->available() > 0) { + char userInput = (this->serialD)->read(); + if (userInput == 't' || userInput == 'T') { + SERIAL_L("WAKE-UP"); + response = (this->serialL)->readStringUntil('\n'); + readResponse(5,NO_DEBUG); + userTest = true; + SERIAL_D("# User input 't'.\n"); + return RET_T; + } + } + } + } +} + +/// @brief Allow to change frequency band +void LORAE5::setFrequencyBand(){ + String response; + + if ((this->region) == EU868){ + SERIAL_L("AT+DR=EU868"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == US915){ + SERIAL_L("AT+DR=US915"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == US915HYBRID){ + SERIAL_L("AT+DR=US915HYBRID"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == CN779){ + SERIAL_L("AT+DR=CN779"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == EU433){ + SERIAL_L("AT+DR=EU433"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == AU915){ + SERIAL_L("AT+DR=AU915"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == AU915OLD){ + SERIAL_L("AT+DR=AU915OLD"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == CN470){ + SERIAL_L("AT+DR=CN470"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == CN470PREQUEL){ + SERIAL_L("AT+DR=CN470PREQUEL"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == AS923){ + SERIAL_L("AT+DR=AS923"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == KR920){ + SERIAL_L("AT+DR=KR920"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == IN865){ + SERIAL_L("AT+DR=IN865"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == RU864){ + SERIAL_L("AT+DR=RU864"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == STE920){ + SERIAL_L("AT+DR=STE920"); + response = (this->serialL)->readStringUntil('\n'); + } + + #if DEBUG_LEVEL == 5 + SERIAL_L("AT+CH"); + response = (this->serialL)->readStringUntil('\n'); + SERIAL_D(response); + #endif +} \ No newline at end of file diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/src/lorae5.h b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/src/lorae5.h new file mode 100644 index 0000000000000000000000000000000000000000..cd5c3d63acf35ec3eb0f6e7a64df3e036cdb75a1 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/src/lorae5.h @@ -0,0 +1,133 @@ +#define SERIAL_D(x) this->serialD->println(x) +#define SERIAL_L(x) this->serialL->println(x) + +#define EU868 0 +#define US915 1 +#define CN779 2 +#define EU433 3 +#define AU915 4 +#define CN470 5 +#define AS923 6 +#define KR920 7 +#define IN865 8 +#define RU864 9 +#define CN470PREQUEL 10 +#define STE920 11 +#define US915HYBRID 12 +#define AU915OLD 13 + +#define ABP 0 +#define OTAA 1 + +#define CLASS_A 0 +#define CLASS_B 1 +#define CLASS_C 2 + +#define DEBUG true +#define NO_DEBUG false + +#define DEBUG_LEVEL 0 +/* list for DEBUG_LEVEL: +- join : 1; +- getSetSF : 2; +- sendData : 3; +- awaitForDownlink : 4; +- setFrequencyBand : 5; +*/ + +#define CONF true +#define UNCONF false + +#define _HEXA 0 +#define _STRING 1 + +enum RETURN { RET_DOWNLINK, RET_NO_DOWNLINK, RET_NO_ACK, RET_T, RET_TIMEOUT }; + +class LORAE5{ + +public: + // uint8_t downlink[50]; + +private: + uint8_t region; + bool mode; + uint8_t devClass; + uint8_t sf; + bool adr; + bool confirmed; + uint8_t portUp; + uint8_t portDown; + +#if defined(ARDUINO_AVR_LEONARDO) || defined(ARDUINO_WIO_TERMINAL) + Serial_* serialD; +#else + HardwareSerial* serialD; +#endif + +#if defined(ARDUINO_WIO_TERMINAL) + Uart* serialL; +#else + HardwareSerial* serialL; +#endif + + String devEUI; + String devAddr; + String nwkSKey; + String appSKey; + String appKey; + String appEUI; + + bool SEND_BY_PUSH_BUTTON; + uint32_t FRAME_DELAY; + + void readResponse(uint32_t timeOut, bool debug); + bool checkResponse(uint32_t timeOut, char *strCheck, bool debug); + bool checkBoard(void); + + void setDevEUI(String deveui); + void setAppKey(String appkey); + void setAppEUI(String appeui); + void setDevAddr(String devaddr); + void setNwkSKey(String nwkskey); + void setAppSKey(String appskey); + void setMode(bool mode); + void setClass(uint8_t devClass); + void setRXDelay(); + void setSF(uint8_t sf); + void setADR(bool adr); + void setPortUp(uint8_t portUp); + void setFrequencyBand(void); + + void getMode(void); + void getSetSF(void); + void getPortDown(String response); + void getPayloadDown(uint8_t* payloadDown, uint8_t* sizePayloadDown, String response); + void getChannelDown(String response); + uint8_t getADR(void); + uint8_t getPortUp(void); + uint8_t getClass(void); + + void sendPayloadUp(uint8_t* payloadUp, uint8_t sizePayloadUp); + +public: + LORAE5(String devEUI, String appEUI, String appKey, String devAddr, String nwkSKey, String appSKey); + +#if defined(ARDUINO_AVR_LEONARDO) + void setup_hardware(Serial_* myDebugSerial, HardwareSerial* myLoRaSerial); +#elif defined(ARDUINO_WIO_TERMINAL) +void setup_hardware(Serial_* myDebugSerial, Uart* myLoRaSerial); +#else + void setup_hardware(HardwareSerial* myDebugSerial, HardwareSerial* myLoRaSerial); +#endif + + void setup_lorawan(uint8_t band, bool mode, uint8_t devClass, uint8_t sf, bool adr, bool messageType, uint8_t portUp, bool SEND_BY_PUSH_BUTTON, uint32_t FRAME_DELAY); + void printInfo(void); + + bool join(void); + + void sendData(uint8_t* payloadUp, uint8_t sizePayloadUp); + uint8_t awaitForDownlinkClass_A(uint8_t* payloadDown, uint8_t* sizePayloadDown); + uint8_t awaitForDownlinkClass_C(uint8_t* payloadDown, uint8_t* sizePayloadDown); + uint8_t sleep(void); + +}; \ No newline at end of file diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/src/main.cpp b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ff91d9f1f443b146423f99ecd21c835cc60219e --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/src/main.cpp @@ -0,0 +1,101 @@ +#include <Arduino.h> +#include "lorae5.h" +#include "config_application.h" +#include "config_board.h" +#include <HardwareSerial.h> +#include "SdsDustSensor.h" + +// Définition des broches UART pour le SDS011 +#define RX_PIN_SDS 19 // GPIO19 pour RX du SDS011 +#define TX_PIN_SDS 18 // GPIO18 pour TX du SDS011 + +// Définition des broches UART pour le LoRa-E5 +#define RXD2 16 // GPIO16 pour RX du LoRa-E5 +#define TXD2 17 // GPIO17 pour TX du LoRa-E5 + +// Déclaration des objets Serial +HardwareSerial SDS_Serial(1); // UART1 pour SDS011 + +// Création des objets capteur et LoRa +SdsDustSensor sds(SDS_Serial); +LORAE5 lorae5(devEUI, appEUI, appKey, devAddr, nwkSKey, appSKey); + +// Buffer pour l'envoi des données LPP (2 channels x 4 octets) +uint8_t payloadUp[8]; + +void setup() { + Serial.begin(115200); + + // Initialisation du port série pour le SDS011 + SDS_Serial.begin(9600, SERIAL_8N1, RX_PIN_SDS, TX_PIN_SDS); + sds.begin(); + Serial.println("SDS011 Initialisé !"); + Serial.println(sds.queryFirmwareVersion().toString()); + Serial.println(sds.setActiveReportingMode().toString()); + Serial.println(sds.setContinuousWorkingPeriod().toString()); + + // Initialisation du module LoRa-E5 + lorae5.setup_hardware(&Debug_Serial, &Serial2); + lorae5.setup_lorawan(REGION, ACTIVATION_MODE, CLASS, SPREADING_FACTOR, ADAPTIVE_DR, CONFIRMED, PORT_UP, SEND_BY_PUSH_BUTTON, FRAME_DELAY); + lorae5.printInfo(); + + Debug_Serial.begin(115200); + Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); + + // Connexion OTAA si activée + if (ACTIVATION_MODE == OTAA) { + Debug_Serial.println("Join Procedure in progress..."); + while (!lorae5.join()); + delay(2000); + } +} + +void loop() { + PmResult pm = sds.readPm(); + + if (pm.isOk()) { + Serial.print("PM2.5 = "); + Serial.print(pm.pm25); + Serial.print(" µg/m³, PM10 = "); + Serial.print(pm.pm10); + Serial.println(" µg/m³"); + + // Conversion avec 1 chiffre après la virgule (x10) + uint16_t pm25 = static_cast<uint16_t>(pm.pm25 * 100); + uint16_t pm10 = static_cast<uint16_t>(pm.pm10 * 100); + + // Encodage Cayenne LPP + // Channel 1 - PM2.5 + payloadUp[0] = 0x01; // Channel + payloadUp[1] = 0x02; // Analog Input + payloadUp[2] = (pm25 >> 8) & 0xFF; // MSB + payloadUp[3] = pm25 & 0xFF; // LSB + + // Channel 2 - PM10 + payloadUp[4] = 0x02; // Channel + payloadUp[5] = 0x02; // Analog Input + payloadUp[6] = (pm10 >> 8) & 0xFF; // MSB + payloadUp[7] = pm10 & 0xFF; // LSB + + // Debug Payload + Serial.print("Payload LPP envoyé (HEX) : "); + for (int i = 0; i < sizeof(payloadUp); i++) { + Serial.print("0x"); + if (payloadUp[i] < 16) Serial.print("0"); + Serial.print(payloadUp[i], HEX); + Serial.print(" "); + } + Serial.println(); + + // Envoi LoRa + lorae5.sendData(payloadUp, sizeof(payloadUp)); + + delay(30000); // Attente 30s avant la prochaine mesure + } else { + Serial.print("Erreur de lecture SDS011 : "); + Serial.println(pm.statusToString()); + } + + // Mise en veille + lorae5.sleep(); +} diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/test/README b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_ET_SDS011_Cayenne/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/Test_Integration_TTN_SDS011_DHT22/.DS_Store b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d1db41456480cf17f050dd1c49735854df8bea08 Binary files /dev/null and b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/.DS_Store differ diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/.gitignore b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/.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/Test_Integration_TTN_SDS011_DHT22/.vscode/extensions.json b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/.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/Test_Integration_TTN_SDS011_DHT22/include/README b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/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/Test_Integration_TTN_SDS011_DHT22/include/config_application.h b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/include/config_application.h new file mode 100644 index 0000000000000000000000000000000000000000..125966d19d43afd93b13a5b591a5645a6e631c81 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/include/config_application.h @@ -0,0 +1,25 @@ +/***********************************************************************/ +/* Please see README page on https://github.com/SylvainMontagny/LoRaE5 */ +/***********************************************************************/ + +#define REGION EU868 +#define ACTIVATION_MODE OTAA +#define CLASS CLASS_A +#define SPREADING_FACTOR 12 +#define ADAPTIVE_DR false +#define CONFIRMED false +#define PORT_UP 15 + +#define SEND_BY_PUSH_BUTTON false +#define FRAME_DELAY 20000 + +String devEUI = "70B3D57ED00630C5"; + +// Configuration for ABP Activation Mode +String devAddr = "00000000"; +String nwkSKey = "00000000000000000000000000000000"; +String appSKey = "00000000000000000000000000000000"; + +// Configuration for OTAA Activation Mode +String appKey = "FFC756C21917F54E3993167F8E68AB22"; +String appEUI = "000000000000000"; diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/include/config_board.h b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/include/config_board.h new file mode 100644 index 0000000000000000000000000000000000000000..dab90556f3b544b86142ba434023865d04fb7fec --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/include/config_board.h @@ -0,0 +1,119 @@ +#ifndef CONFIG_BOARD_H +#define CONFIG_BOARD_H + +////////////////////////////////////////////// +// If you use an ARDUINO or ESP32 board +////////////////////////////////////////////// + #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_RENESAS) || defined(ARDUINO_WIO_TERMINAL) + #define Debug_Serial Serial // Select the right SerialX for Debug + + // Arduino's Serial Documentation : https://www.arduino.cc/reference/en/language/functions/communication/serial/ + // ESP32 Documentation : https://microcontrollerslab.com/esp32-uart-communication-pins-example/ + #define LoRa_Serial Serial2 // Select the right SerialX for the LoRaE5 connection + + + +// If you use a STM32 NUCLEO board +////////////////////////////////////////////// + #elif defined(ARDUINO_ARCH_STM32) + #define RX_PIN_DEBUG PA3 // Select UART RX Pin for Debug + #define TX_PIN_DEBUG PA2 // Select UART TX Pin for Debug + HardwareSerial Debug_Serial(RX_PIN_DEBUG, TX_PIN_DEBUG); + + + #define RX_PIN_LORA PA10 // Select UART RX Pin for the LoRaE5 connection + #define TX_PIN_LORA PA9 // Select UART TX Pin for the LoRaE5 connection + HardwareSerial LoRa_Serial(RX_PIN_LORA, TX_PIN_LORA); + + +////////////////////////////////////////////// +// /!\ Your card has never been tested yet ! +// Please let us know if you find the PIN configuration of your board +// So we can add it to the next version of this code +////////////////////////////////////////////// + + #else + #warning "Your board has never been tested with this lib" + #warning "Select the right Serial for Debug and LoRaE5 connection in config_board.h" + + #define Debug_Serial Serial + #define LoRa_Serial Serial1 + + #endif + +#endif //CONFIG_BOARD_H + +//////////////////////////////// +/* Uart Pin list for known board +//////////////////////////////// + +NUCLEO-F446RE : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +USART3_RX = PC5 Alternate PC11 +USART3_TX = PB10 Alternate PC10 + +// + +NUCLEO-L073RZ : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) Alternate PA15 +USART2_TX = PA2 (ST-Link) Alternate PA14 + +// + +NUCLEO-F411RE : +USART1_RX = PA10 Alternate PB7 Alternate PB3 +USART1_TX = PA9 Alternate PB6 Alternate PA15 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-WL55JC1 : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-G031K8 : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-L433RC-P : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) Alternate PA15 +USART2_TX = PA2 (ST-Link) + +// + +NUCLEO-F746ZG : +USART1_RX = PA10 Alternate PB7 +USART1_TX = PA9 Alternate PB6 + +USART2_RX = PA3 (ST-link) Alternate PD6 +USART2_TX = PA2 (ST-Link) Alternate PD5 + +USART3_RX = PD9 Alternate PB11 Alternate PC11 +USART3_TX = PD8 Alternate PB10 Alternate PC10 + +//////////////////////////////// +Uart Pin list for known board */ +//////////////////////////////// \ No newline at end of file diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/lib/README b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/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/Test_Integration_TTN_SDS011_DHT22/platformio.ini b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..66a8ee91ef7fac2858a7abc8ad0c066b5a1c6911 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/platformio.ini @@ -0,0 +1,20 @@ +; 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 +upload_speed = 115200 +monitor_speed = 115200 +build_flags = -Iinclude +lib_deps = + lewapek/Nova Fitness Sds dust sensors library@^1.5.1 + adafruit/DHT sensor library@^1.4.6 diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/src/.DS_Store b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/src/.DS_Store differ diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/src/lorae5.cpp b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/src/lorae5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d235c0cc0ae402586a1fd126176bbf6dbe645a53 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/src/lorae5.cpp @@ -0,0 +1,804 @@ +#include <Arduino.h> +#include <HardwareSerial.h> +#include "lorae5.h" + +LORAE5::LORAE5(String devEUI, String appEUI, String appKey, String devAddr, String nwkSKey, String appSKey){ + this->devEUI = devEUI; + this->appEUI = appEUI; + this->appKey = appKey; + this->devAddr = devAddr; + this->nwkSKey = nwkSKey; + this->appSKey = appSKey; +} + +#if defined(ARDUINO_AVR_LEONARDO) +/// @brief Allow to set the hardware setup for our transmission +/// @param myDebugSerial is the debug serial link +/// @param myLoRaSerial is the LoRaE5 serial link +void LORAE5::setup_hardware(Serial_* myDebugSerial, HardwareSerial* myLoRaSerial){ + unsigned long startTime = millis(); + + this->serialD = myDebugSerial; + this->serialL = myLoRaSerial; + + (this->serialD)->begin(115200); + (this->serialL)->begin(9600); + + while (millis() - startTime < 2000); +} +#elif defined(ARDUINO_WIO_TERMINAL) +/// @brief Allow to set the hardware setup for our transmission +/// @param myDebugSerial is the debug serial link +/// @param myLoRaSerial is the LoRaE5 serial link +void LORAE5::setup_hardware(Serial_* myDebugSerial, Uart* myLoRaSerial){ + unsigned long startTime = millis(); + + this->serialD = myDebugSerial; + this->serialL = myLoRaSerial; + + (this->serialD)->begin(115200); + (this->serialL)->begin(9600); + + while (millis() - startTime < 2000); +} +#else +/// @brief Allow to set the hardware setup for our transmission +/// @param myDebugSerial is the debug serial link +/// @param myLoRaSerial is the LoRaE5 serial link +void LORAE5::setup_hardware(HardwareSerial* myDebugSerial, HardwareSerial* myLoRaSerial){ + unsigned long startTime = millis(); + + this->serialD = myDebugSerial; + this->serialL = myLoRaSerial; + + (this->serialD)->begin(115200); + (this->serialL)->begin(9600); + + while (millis() - startTime < 2000); +} +#endif + +/// @brief Allow to set the LoRaWAN setup for our transmission +/// @param region is the region selected by the user +/// @param mode is the mode selected by the user +/// @param devClass is the class of the device selected by the user +/// @param sf is the Spreading Factor selected by the user +/// @param adr is to enable or disable the Adaptive Data Rate +/// @param confirmed is to enable or disable the confirmed message +/// @param portUp is the uplink port selected by the user +/// @param SEND_BY_PUSH_BUTTON is the push button condition selected by the user +/// @param FRAME_DELAY is the frame delay selected by the user +void LORAE5::setup_lorawan(uint8_t region, bool mode, uint8_t devClass, uint8_t sf, bool adr, bool confirmed, uint8_t portUp, bool SEND_BY_PUSH_BUTTON, uint32_t FRAME_DELAY){ + + while(!checkBoard()); + + setDevEUI(devEUI); + setAppEUI(appEUI); + setAppKey(appKey); + + setDevAddr(devAddr); + setNwkSKey(nwkSKey); + setAppSKey(appSKey); + + this->region = region; + + setRXDelay(); + setFrequencyBand(); + setMode(mode); + setClass(devClass); + setSF(sf); + setADR(adr); + setPortUp(portUp); + + this->confirmed = confirmed; + this->SEND_BY_PUSH_BUTTON = SEND_BY_PUSH_BUTTON; + this->FRAME_DELAY = FRAME_DELAY; +} + +/// @brief Allow to set the RX delay +void LORAE5::setRXDelay(){ + readResponse(1000,NO_DEBUG); + SERIAL_L("AT+DELAY=RX1,1000"); + readResponse(400,NO_DEBUG); + SERIAL_L("AT+DELAY=RX2,2000"); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the mode of the device +/// @param mode is the mode selected by the user +void LORAE5::setMode(bool mode){ + String strMode; + + this->mode = mode; + if (mode == OTAA) strMode = "LWOTAA"; + if (mode == ABP) strMode = "LWABP"; + + SERIAL_L("AT+MODE=" + strMode); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the class of the device +/// @param devClass is the the class of the device selected by the user +void LORAE5::setClass(uint8_t devClass){ + String strClass; + + this->devClass = devClass; + if (devClass == CLASS_A) strClass = "A"; + if (devClass == CLASS_C) strClass = "C"; + SERIAL_L("AT+CLASS=" + strClass); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the Spreading Factor (SF) of the device +/// @param sf is the Spreading factor selected by the user +void LORAE5::setSF(uint8_t sf){ + this->sf = sf; + if ((this->region) == EU868 || (this->region) == CN779 || (this->region) == EU433 || (this->region) == AU915 || (this->region) == CN470 || (this->region) == AS923 || (this->region) == KR920 || (this->region) == IN865 || (this->region) == RU864 || (this->region) == CN470PREQUEL || (this->region) == STE920){ + SERIAL_L("AT+DR=DR" + String(-sf+12)); + } + + if ((this->region) == US915 || (this->region) == US915HYBRID || (this->region) == AU915OLD){ + SERIAL_L("AT+DR=DR" + String(-sf+10)); + } + + String response = (this->serialL)->readStringUntil('\n'); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to process and set the Spreading Factor (SF) received from the downlink +void LORAE5::getSetSF(){ + String response; + (this->serialL)->setTimeout(500); + SERIAL_L("AT+DR"); + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 2 + SERIAL_D(response); // DEBUG + #endif + if (this->adr == true){ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 2 + SERIAL_D(response); // DEBUG + #endif + } + + if ((this->region) == EU868 || (this->region) == CN779 || (this->region) == EU433 || (this->region) == AU915 || (this->region) == CN470 || (this->region) == AS923 || (this->region) == KR920 || (this->region) == IN865 || (this->region) == RU864 || (this->region) == CN470PREQUEL || (this->region) == STE920){ + if ( response.indexOf("DR0") != -1 ) this->sf = 12; + else if ( response.indexOf("DR1") != -1 ) this->sf = 11; + else if ( response.indexOf("DR2") != -1 ) this->sf = 10; + else if ( response.indexOf("DR3") != -1 ) this->sf = 9; + else if ( response.indexOf("DR4") != -1 ) this->sf = 8; + else if ( response.indexOf("DR5") != -1 ) this->sf = 7; + } + + if ((this->region) == US915 || (this->region) == US915HYBRID || (this->region) == AU915OLD){ + if ( response.indexOf("DR0") != -1 ) this->sf = 10; + else if ( response.indexOf("DR1") != -1 ) this->sf = 9; + else if ( response.indexOf("DR2") != -1 ) this->sf = 8; + else if ( response.indexOf("DR3") != -1 ) this->sf = 7; + } + + readResponse(100,NO_DEBUG); +} + +/// @brief Allow to set Adaptative Data Rate (ADR) +/// @param adr is to enable or disable the Adaptive Data Rate +void LORAE5::setADR(bool adr){ + this->adr = adr; + SERIAL_L("AT+ADR=" + ((adr == true)? String("ON") : String("OFF"))); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Device EUI +/// @param deveui is the Device EUI +void LORAE5::setDevEUI(String deveui){ + SERIAL_L("AT+ID=DEVEUI," + devEUI); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Application EUI +/// @param appeui is the Application EUI +void LORAE5::setAppEUI(String appeui){ + SERIAL_L("AT+ID=APPEUI," + appeui); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Application Key +/// @param appkey is the Application Key +void LORAE5::setAppKey(String appkey){ + SERIAL_L("AT+KEY=APPKEY," + appkey); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Device Address +/// @param devaddr is the Device Address +void LORAE5::setDevAddr(String devaddr){ + SERIAL_L("AT+ID=DEVADDR," + devaddr); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Network Session Key +/// @param nwkskey is the Network Session Key +void LORAE5::setNwkSKey(String nwkskey){ + SERIAL_L("AT+KEY=NWKSKEY," + nwkskey); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set Application Session Key +/// @param appskey is the Application Session Key +void LORAE5::setAppSKey(String appskey){ + SERIAL_L("AT+KEY=APPSKEY," + appskey); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to set the uplink port +/// @param portUp is the uplink port selected by the user +void LORAE5::setPortUp(uint8_t portUp){ + this->portUp = portUp; + SERIAL_L("AT+PORT=" + String(portUp)); + readResponse(400,NO_DEBUG); +} + +/// @brief Allow to check if a board is connected +/// @return Board status +bool LORAE5::checkBoard(){ + bool success; + delay(500); + SERIAL_L("AT"); + SERIAL_D("\n\n\n\n"); + if ( (success = checkResponse(400,"+AT: OK\r\n",NO_DEBUG)) == false){ + SERIAL_D("> LoRa-E5 board not detected ..."); + } + else{ + SERIAL_D("> LoRa-E5 board detected ...\n"); + } + return success; +} + +/// @brief Allow to check if join procedure is successful +/// @return Joined status +bool LORAE5::join(){ + bool joined = false, failed = false, done = false; + String response, strDevAddr; + uint32_t index; + + delay(2000); + (this->serialL)->setTimeout(10000); + SERIAL_L("AT+JOIN"); + + do{ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 1 + SERIAL_D(response); // DEBUG + #endif + + // Analyse DevAddr response + if ( (index = response.indexOf("DevAddr")) != -1 ){ + index += strlen("DevAddr "); + strDevAddr += response.charAt(index + 0); + strDevAddr += response.charAt(index + 1); + strDevAddr += ":"; + strDevAddr += response.charAt(index + 3); + strDevAddr += response.charAt(index + 4); + strDevAddr += ":"; + strDevAddr += response.charAt(index + 6); + strDevAddr += response.charAt(index + 7); + strDevAddr += ":"; + strDevAddr += response.charAt(index + 9); + strDevAddr += response.charAt(index + 10); + SERIAL_D("> SUCCESS JOINED OTAA !!!"); + SERIAL_D("> DevAddr = " + strDevAddr); + joined = true; + } + // Analyse end of Join procedure + else if ( (index = response.indexOf("Done")) != -1 ){ + done = true; + } + // Analyse failed join + else if ( (index = response.indexOf("Join failed")) != -1 ){ + SERIAL_D("> JOIN FAILED ..."); + failed = true; + } + } while((!joined || !done) && !failed ); + SERIAL_D(""); + return joined; +} + +/// @brief Allow to send uplink data +/// @param payloadUp is the data to send +/// @param sizePayloadUp is the size of the data to send +void LORAE5::sendPayloadUp(uint8_t* payloadUp, uint8_t sizePayloadUp) { + SERIAL_D("Sending " + String(this->confirmed ? "Confirmed " : "Unconfirmed ") + String("Data Up")); + (this->serialD)->print("- Payload UP "); + + String stringPayloadUp = ""; + for (uint8_t i = 0; i < sizePayloadUp; i++) { + (this->serialD)->print(" 0x"); + if (payloadUp[i] < 0x10) { + (this->serialD)->print('0'); + stringPayloadUp += 0; + } + (this->serialD)->print(payloadUp[i], HEX); + (this->serialD)->print(" "); + stringPayloadUp += String(payloadUp[i], HEX); + } + + SERIAL_D(""); + SERIAL_D("- PortUp\t" + String(this->portUp)); + SERIAL_D("- SF\t\t" + String(this->sf)); + SERIAL_D(""); + + SERIAL_L("AT+" + String(this->confirmed ? "C" : "") + String("MSGHEX") + String("=") + stringPayloadUp); +} + +/// @brief Allow to manage the sending of uplink data +/// @param payloadUp is the data to send +/// @param sizePayloadUp is the size of the data to send +void LORAE5::sendData(uint8_t* payloadUp, uint8_t sizePayloadUp){ + bool transmissionDone = false; + String response; + uint32_t index; + (this->serialL)->setTimeout(500); + + getSetSF(); + sendPayloadUp(payloadUp, sizePayloadUp); + do{ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 3 + SERIAL_D(response); // DEBUG + #endif + + if ( (index = response.indexOf("Start")) != -1 ){ + SERIAL_D("Transmission Done\nWaiting for Downlink..."); + transmissionDone = true; + } + + if ( (index = response.indexOf("Length error 0")) != -1 ){ + SERIAL_L("AT+MSG"); + } + + if ( (index = response.indexOf("LoRaWAN modem is busy")) != -1 ){ + SERIAL_D("LoRaWAN modem is busy"); + } + + } while (!transmissionDone); + + if(confirmed == true){ + response = (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 3 + SERIAL_D(response); // DEBUG + #endif + if ( (index = response.indexOf("Wait ACK")) != -1 ){ + SERIAL_D("Waiting for ACK..."); + } + } + SERIAL_D(""); +} + +/// @brief Allow to debug or consume characters from the LoRaE5 module +/// @param timeOut is the maximum allocated time for the function +/// @param debug is to enable or disable the display +void LORAE5::readResponse(uint32_t timeOut, bool debug){ + uint32_t tstart = millis(); + + while (millis()-tstart < timeOut){ + if((this->serialL)->available() > 0) { + if (debug == DEBUG){ + (this->serialD)->print((char)(this->serialL)->read()); + } + else{ + (this->serialL)->read(); + } + } + } +} + +/// @brief Allow to check if a string is present in the log +/// @param timeOut is the maximum waiting time +/// @param strCheck is the String we are looking for +/// @param debug is to enable or disable the display +/// @return if the requested string is present or not +bool LORAE5::checkResponse(uint32_t timeOut, char *strCheck, bool debug){ + uint32_t tstart = millis(); + uint8_t i=0, checkSize = strlen(strCheck); + char c; + bool success = false; + + while (millis()-tstart < timeOut){ + if((this->serialL)->available() > 0) { + c = (this->serialL)->read(); + + if (debug == DEBUG){ + (this->serialD)->print(c); + } + + if( c == strCheck[i] ){ + if (i == (checkSize-1)){ + success = true; + } + else{ + i++; + } + } + else{ + i=0; + } + } + } +return success; +} + +/// @brief Displays the port up +/// @param string is the information received from the device and stored in a buffer memory +/// @return port up +uint8_t LORAE5::getPortUp(){ + return this->portUp; +} + +/// @brief Displays the port down +/// @param string is the information received from the device and stored in a buffer memory +/// @return port down +void LORAE5::getPortDown(String response){ + + String strPortDown; + uint32_t index; + uint32_t indexSemicolon = response.indexOf(';'); + index = response.indexOf("PORT: "); + index += strlen ("PORT: "); + + for (uint8_t i = 0 ; i < (indexSemicolon - index ) ; i++){ + strPortDown +=response.charAt(index + i); + } + uint8_t portDown = strPortDown.toInt(); + + SERIAL_D("- Port " + String(portDown)); + this->portDown = portDown; +} + +/// @brief Allow to process and display the payload down +/// @param payloadDown is downlink message +/// @param sizePayloadDown is size of the downlink message +/// @param response is the information received from the device and stored in a buffer memory +/// @return Payload down +void LORAE5::getPayloadDown(uint8_t* payloadDown, uint8_t* sizePayloadDown, String response){ + + // Get Downlink Payload + String strPayloadDown; + uint32_t index; + uint32_t indexQuote = response.lastIndexOf("\""); + index = response.indexOf("RX: \""); + index+= strlen("RX: \""); + for (uint8_t i = 0 ; i < (indexQuote - index ) ; i++){ + strPayloadDown +=response.charAt(index + i); + } + *sizePayloadDown = strPayloadDown.length() / 2 ; + for (uint8_t i = 0 ; i < *sizePayloadDown ; i++){ + String strByte =strPayloadDown.substring(i * 2, i * 2 + 2); + payloadDown[i] = strtoul(strByte.c_str(), nullptr, 16); + } + (this->serialD)->print("- Payload DOWN "); + for (uint8_t i = 0 ; i < *sizePayloadDown ; i++){ + (this->serialD)->print(" 0x"); + if (payloadDown[i] < 0x10){ + (this->serialD)->print('0'); + } + (this->serialD)->print(payloadDown[i], HEX); + (this->serialD)->print(" "); + } + SERIAL_D(""); +} + +/// @brief Displays downlink channel +/// @param response is the information received from the device and stored in a buffer memory +void LORAE5::getChannelDown(String response){ + + String strChannelDown; + uint32_t index; + uint32_t indexSemicolon = response.indexOf(", RSSI "); + index = response.indexOf(" RXWIN"); + index += strlen (" RXWIN"); + + for (uint8_t i = 0 ; i < (indexSemicolon - index ) ; i++){ + strChannelDown +=response.charAt(index + i); + } + uint8_t channelDown = strChannelDown.toInt(); + + if (channelDown == 0){ + SERIAL_D("- Slot RXC"); + } else { + SERIAL_D("- Slot RX" + String(channelDown)); + } + SERIAL_D(""); +} + +/// @brief Updates the Device Class +/// @return Device Class +uint8_t LORAE5::getClass(){ + return this->devClass; +} + +/// @brief Updates the Data Rate +/// @return Data rate +uint8_t LORAE5::getADR(){ + return this->adr; +} + +/// @brief Summarizes device parameters +void LORAE5::printInfo(){ +#ifndef ARDUINO_AVR_LEONARDO + SERIAL_D("# For more information visit : https://github.com/SylvainMontagny/LoRaE5"); + + if(this->mode == OTAA) {SERIAL_D("> OTAA");} + else {SERIAL_D("> ABP");} + + if(this->devClass == CLASS_A) {SERIAL_D("> CLASS_A");} + if(this->devClass == CLASS_C) {SERIAL_D("> CLASS_C");} + + SERIAL_D("> SF " + String(this->sf)); + + if(this->adr == true) {SERIAL_D("> ADR ON");} + else {SERIAL_D("> ADR OFF");} + + SERIAL_D(((this->confirmed == CONF)? String("> Confirmed") : String("> Unconfirmed"))); + SERIAL_D("> Port " + String(this->portUp)); + SERIAL_D(); + SERIAL_D( "* DevEUI 0x " + this->devEUI); + + if (this->mode == ABP) { + SERIAL_D("* DevAddr 0x " + this->devAddr); + SERIAL_D("* NwkSKey 0x " + this->nwkSKey); + SERIAL_D("* AppSKey 0x " + this->appSKey); + } + + if (this->mode == OTAA){ + SERIAL_D("* AppKey 0x " + this->appKey); + SERIAL_D("* AppEUI-JoinEUI 0x " + this->appEUI); + } + SERIAL_D(); +#endif +} + +/// @brief Waiting for Class_A downlink +/// @param payloadDown is downlink message +/// @param sizePayloadDown is size of the downlink message +/// @return Downlink data information (RET_T/RET_TIMEOUT/RET_DOWNLINK/RET_NO_DOWNLINK) +uint8_t LORAE5::awaitForDownlinkClass_A(uint8_t* payloadDown, uint8_t* sizePayloadDown){ + uint32_t index; + String response; + bool isDoneReceived = false; + bool isAckReceived = false; + + (this->serialL)->setTimeout(500); + + do{ + response += (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 4 + SERIAL_D(response); // DEBUG + #endif + + /****** Test Done *******/ + if ( (index = response.indexOf("Done")) != -1 ){ + isDoneReceived = true; + } + + }while(isDoneReceived == false); + + //Confirmed part + if ((confirmed == true)){ + if (index = response.indexOf("ACK Received") != -1 ){ + SERIAL_D("ACK received"); + isAckReceived = true; + } + else { + SERIAL_D("No ACK received\n"); + return RET_NO_ACK; + } + } + + //Processing data received + if ((index = response.indexOf("PORT: ")) != -1) { + SERIAL_D("Receiving Data Down:"); + getPortDown(response); + getPayloadDown(payloadDown, sizePayloadDown, response); + getChannelDown(response); + return RET_DOWNLINK; + } + + else if ( ((index = response.indexOf("RXWIN")) != -1) && (isAckReceived == false) ) { + SERIAL_D("MAC command received :"); + getChannelDown(response); + return RET_NO_DOWNLINK; + } + + else { + if(isAckReceived == false){ + SERIAL_D("No Class_A Data Received."); + } + SERIAL_D(); + return RET_NO_DOWNLINK; + } + +} + +/// @brief Waiting for Class_C downlink +/// @param payloadDown is downlink message +/// @param sizePayloadDown is size of the downlink message +/// @return Downlink data information (RET_T/RET_TIMEOUT/RET_DOWNLINK) +uint8_t LORAE5::awaitForDownlinkClass_C(uint8_t* payloadDown, uint8_t* sizePayloadDown){ + uint32_t index; + String response; + bool isDoneReceived = false; + uint32_t startTime = millis(); + + (this->serialL)->setTimeout(500); + +#ifndef ARDUINO_AVR_LEONARDO + if( this->SEND_BY_PUSH_BUTTON == true ){ + SERIAL_D("> Press 't' or receive a downlink.\n"); + }else if( this->SEND_BY_PUSH_BUTTON == false ){ + SERIAL_D("> Press 't', receive a downlink or wait " + String(this->FRAME_DELAY) + String(" ms.\n")); + } +#endif + + do{ + response += (this->serialL)->readStringUntil('\n'); + #if DEBUG_LEVEL == 4 + SERIAL_D(response); // DEBUG + #endif + + /****** Test Done *******/ + if ( (index = response.indexOf("Done")) != -1 ){ + isDoneReceived = true; + } + + /****** Test 't' : transmission request *******/ + if ( (this->serialD)->available() > 0 ) { + char userInput = (this->serialD)->read(); + if (userInput == 't' || userInput == 'T') { + SERIAL_D("# User input 't'.\n"); + return RET_T; + } + } + + /****** Test Timeout *******/ + if ((this->SEND_BY_PUSH_BUTTON == false) && ((millis() - startTime) >= (this->FRAME_DELAY))){ + SERIAL_D("No Class_C Data Received.\n"); + SERIAL_D("# Timeout reached.\n"); + return RET_TIMEOUT; + } + + }while(isDoneReceived == false); + + //Processing data received + if ((index = response.indexOf("PORT: ")) != -1) { + SERIAL_D("Receiving Data Down:"); + getPortDown(response); + getPayloadDown(payloadDown, sizePayloadDown, response); + getChannelDown(response); + return RET_DOWNLINK; + } +} + +/// @brief Allow to wait and put the LoRaE5 module in Low Power mode ON/OFF +/// @return Downlink data information (RET_T/RET_TIMEOUT) +uint8_t LORAE5::sleep(){ + uint32_t startTime = millis(); + SERIAL_L("AT+LOWPOWER"); + String response = (this->serialL)->readStringUntil('\n'); + readResponse(5, NO_DEBUG); + + if (this->SEND_BY_PUSH_BUTTON == false) { + while((millis() - startTime) < (this->FRAME_DELAY)){ + if ((this->serialD)->available() > 0) { + char userInput = (this->serialD)->read(); + if (userInput == 't' || userInput == 'T') { + SERIAL_L("WAKE-UP"); + response = (this->serialL)->readStringUntil('\n'); + readResponse(5,NO_DEBUG); + SERIAL_D("# User input 't'.\n"); + return RET_T; + } + } + } + SERIAL_L("WAKE-UP"); + response = (this->serialL)->readStringUntil('\n'); + readResponse(5,NO_DEBUG); + SERIAL_D("# Timeout reached.\n"); + return RET_TIMEOUT; + } else { + bool userTest = false; + while (userTest == false){ + if ((this->serialD)->available() > 0) { + char userInput = (this->serialD)->read(); + if (userInput == 't' || userInput == 'T') { + SERIAL_L("WAKE-UP"); + response = (this->serialL)->readStringUntil('\n'); + readResponse(5,NO_DEBUG); + userTest = true; + SERIAL_D("# User input 't'.\n"); + return RET_T; + } + } + } + } +} + +/// @brief Allow to change frequency band +void LORAE5::setFrequencyBand(){ + String response; + + if ((this->region) == EU868){ + SERIAL_L("AT+DR=EU868"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == US915){ + SERIAL_L("AT+DR=US915"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == US915HYBRID){ + SERIAL_L("AT+DR=US915HYBRID"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == CN779){ + SERIAL_L("AT+DR=CN779"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == EU433){ + SERIAL_L("AT+DR=EU433"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == AU915){ + SERIAL_L("AT+DR=AU915"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == AU915OLD){ + SERIAL_L("AT+DR=AU915OLD"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == CN470){ + SERIAL_L("AT+DR=CN470"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == CN470PREQUEL){ + SERIAL_L("AT+DR=CN470PREQUEL"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == AS923){ + SERIAL_L("AT+DR=AS923"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == KR920){ + SERIAL_L("AT+DR=KR920"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == IN865){ + SERIAL_L("AT+DR=IN865"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == RU864){ + SERIAL_L("AT+DR=RU864"); + response = (this->serialL)->readStringUntil('\n'); + } + + if ((this->region) == STE920){ + SERIAL_L("AT+DR=STE920"); + response = (this->serialL)->readStringUntil('\n'); + } + + #if DEBUG_LEVEL == 5 + SERIAL_L("AT+CH"); + response = (this->serialL)->readStringUntil('\n'); + SERIAL_D(response); + #endif +} \ No newline at end of file diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/src/lorae5.h b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/src/lorae5.h new file mode 100644 index 0000000000000000000000000000000000000000..cd5c3d63acf35ec3eb0f6e7a64df3e036cdb75a1 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/src/lorae5.h @@ -0,0 +1,133 @@ +#define SERIAL_D(x) this->serialD->println(x) +#define SERIAL_L(x) this->serialL->println(x) + +#define EU868 0 +#define US915 1 +#define CN779 2 +#define EU433 3 +#define AU915 4 +#define CN470 5 +#define AS923 6 +#define KR920 7 +#define IN865 8 +#define RU864 9 +#define CN470PREQUEL 10 +#define STE920 11 +#define US915HYBRID 12 +#define AU915OLD 13 + +#define ABP 0 +#define OTAA 1 + +#define CLASS_A 0 +#define CLASS_B 1 +#define CLASS_C 2 + +#define DEBUG true +#define NO_DEBUG false + +#define DEBUG_LEVEL 0 +/* list for DEBUG_LEVEL: +- join : 1; +- getSetSF : 2; +- sendData : 3; +- awaitForDownlink : 4; +- setFrequencyBand : 5; +*/ + +#define CONF true +#define UNCONF false + +#define _HEXA 0 +#define _STRING 1 + +enum RETURN { RET_DOWNLINK, RET_NO_DOWNLINK, RET_NO_ACK, RET_T, RET_TIMEOUT }; + +class LORAE5{ + +public: + // uint8_t downlink[50]; + +private: + uint8_t region; + bool mode; + uint8_t devClass; + uint8_t sf; + bool adr; + bool confirmed; + uint8_t portUp; + uint8_t portDown; + +#if defined(ARDUINO_AVR_LEONARDO) || defined(ARDUINO_WIO_TERMINAL) + Serial_* serialD; +#else + HardwareSerial* serialD; +#endif + +#if defined(ARDUINO_WIO_TERMINAL) + Uart* serialL; +#else + HardwareSerial* serialL; +#endif + + String devEUI; + String devAddr; + String nwkSKey; + String appSKey; + String appKey; + String appEUI; + + bool SEND_BY_PUSH_BUTTON; + uint32_t FRAME_DELAY; + + void readResponse(uint32_t timeOut, bool debug); + bool checkResponse(uint32_t timeOut, char *strCheck, bool debug); + bool checkBoard(void); + + void setDevEUI(String deveui); + void setAppKey(String appkey); + void setAppEUI(String appeui); + void setDevAddr(String devaddr); + void setNwkSKey(String nwkskey); + void setAppSKey(String appskey); + void setMode(bool mode); + void setClass(uint8_t devClass); + void setRXDelay(); + void setSF(uint8_t sf); + void setADR(bool adr); + void setPortUp(uint8_t portUp); + void setFrequencyBand(void); + + void getMode(void); + void getSetSF(void); + void getPortDown(String response); + void getPayloadDown(uint8_t* payloadDown, uint8_t* sizePayloadDown, String response); + void getChannelDown(String response); + uint8_t getADR(void); + uint8_t getPortUp(void); + uint8_t getClass(void); + + void sendPayloadUp(uint8_t* payloadUp, uint8_t sizePayloadUp); + +public: + LORAE5(String devEUI, String appEUI, String appKey, String devAddr, String nwkSKey, String appSKey); + +#if defined(ARDUINO_AVR_LEONARDO) + void setup_hardware(Serial_* myDebugSerial, HardwareSerial* myLoRaSerial); +#elif defined(ARDUINO_WIO_TERMINAL) +void setup_hardware(Serial_* myDebugSerial, Uart* myLoRaSerial); +#else + void setup_hardware(HardwareSerial* myDebugSerial, HardwareSerial* myLoRaSerial); +#endif + + void setup_lorawan(uint8_t band, bool mode, uint8_t devClass, uint8_t sf, bool adr, bool messageType, uint8_t portUp, bool SEND_BY_PUSH_BUTTON, uint32_t FRAME_DELAY); + void printInfo(void); + + bool join(void); + + void sendData(uint8_t* payloadUp, uint8_t sizePayloadUp); + uint8_t awaitForDownlinkClass_A(uint8_t* payloadDown, uint8_t* sizePayloadDown); + uint8_t awaitForDownlinkClass_C(uint8_t* payloadDown, uint8_t* sizePayloadDown); + uint8_t sleep(void); + +}; \ No newline at end of file diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/src/main.cpp b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63076f2b07165d77e8c3d7240f3515fae29d5b16 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/src/main.cpp @@ -0,0 +1,139 @@ +#include <Arduino.h> +#include "lorae5.h" +#include "config_application.h" +#include "config_board.h" +#include <HardwareSerial.h> +#include "SdsDustSensor.h" +#include "DHT.h" + + + +// Définition des broches UART pour le SDS011 +#define RX_PIN_SDS 19 // GPIO19 pour RX du SDS011 +#define TX_PIN_SDS 18 // GPIO18 pour TX du SDS011 + +// Définition des broches UART pour le LoRa-E5 +#define RXD2 16 // GPIO16 pour RX du LoRa-E5 +#define TXD2 17 // GPIO17 pour TX du LoRa-E5 + +// Définition des broches pour le DHT22 +#define DHTPIN 22 // GPIO22 pour le DHT22 +#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 + + +DHT dht(DHTPIN, DHTTYPE); + + + + + +// Déclaration des objets Serial +HardwareSerial SDS_Serial(1); // UART1 pour SDS011 + +// Création des objets capteur et LoRa +SdsDustSensor sds(SDS_Serial); +LORAE5 lorae5(devEUI, appEUI, appKey, devAddr, nwkSKey, appSKey); + +// Buffer pour l'envoi des données (4 octets, avec uint8_t pour bien gérer les octets) +uint8_t payloadUp[8]; + +void setup() { + Serial.begin(115200); + // Initialisation du capteur DHT + dht.begin(); + Serial.println(F("Capteur DHTxx initialisé.")); + + + // Initialisation du port série pour le SDS011 + SDS_Serial.begin(9600, SERIAL_8N1, RX_PIN_SDS, TX_PIN_SDS); + sds.begin(); + Serial.println("SDS011 Initialisé !"); + Serial.println(sds.queryFirmwareVersion().toString()); + Serial.println(sds.setActiveReportingMode().toString()); + Serial.println(sds.setContinuousWorkingPeriod().toString()); + + // Initialisation du module LoRa-E5 + lorae5.setup_hardware(&Debug_Serial, &Serial2); + lorae5.setup_lorawan(REGION, ACTIVATION_MODE, CLASS, SPREADING_FACTOR, ADAPTIVE_DR, CONFIRMED, PORT_UP, SEND_BY_PUSH_BUTTON, FRAME_DELAY); + lorae5.printInfo(); + + Debug_Serial.begin(115200); + Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); + + // Connexion OTAA si activée + if (ACTIVATION_MODE == OTAA) { + Debug_Serial.println("Join Procedure in progress..."); + while (!lorae5.join()); + delay(2000); + } +} + +void loop() { + PmResult pm = sds.readPm(); + + //On imprime les données sur le moniteur série + if (pm.isOk()) { + Serial.print("PM2.5 = "); + Serial.print(pm.pm25); + Serial.print(" µg/m³, PM10 = "); + Serial.print(pm.pm10); + Serial.println(" µg/m³"); + // Reading temperature or humidity takes about 250 milliseconds! + // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) + float h = dht.readHumidity(); + // Read temperature as Celsius (the default) + float t = dht.readTemperature(); + + // Check if any reads failed and exit early (to try again). + if (isnan(h) || isnan(t)) { + Serial.println(F("Failed to read from DHT sensor!")); + return; + } + Serial.print(F("Humidity: ")); + Serial.print(h); + Serial.print(F("% Temperature: ")); + Serial.print(t); + Serial.print(F("°C ")); + + + + // Conversion en uint16_t avec *10 pour garder 1 chiffre après la virgule + uint16_t pm25 = static_cast<uint16_t>(pm.pm25 * 10); // Valeurs de PM2.5 + uint16_t pm10 = static_cast<uint16_t>(pm.pm10 * 10); // Valeurs de PM10 + uint16_t temp = static_cast<uint16_t>(t * 10); // Température + uint16_t hum = static_cast<uint16_t>(h * 10); // Humidité + + // Stockage dans le payload en respectant l'ordre Little-Endian + payloadUp[0] = (pm25 >> 8) & 0xFF; // Octet fort en premier ✅ + payloadUp[1] = pm25 & 0xFF; // Octet faible en second ✅ + payloadUp[2] = (pm10 >> 8) & 0xFF; // Octet fort en premier ✅ + payloadUp[3] = pm10 & 0xFF; // Octet faible en second ✅ + + // Code pour la température et l'humidité + payloadUp[4] = (temp >> 8) & 0xFF; + payloadUp[5] = temp & 0xFF; + payloadUp[6] = (hum >> 8) & 0xFF; + payloadUp[7] = hum & 0xFF; + + // Affichage du payload avant envoi (debug) + Serial.print("Payload envoyé (HEX) : "); + for (int i = 0; i < sizeof(payloadUp); i++) { + Serial.print("0x"); + Serial.print(payloadUp[i], HEX); + Serial.print(" "); + } + Serial.println(); + + // Envoi via LoRa + lorae5.sendData(payloadUp, sizeof(payloadUp)); + + // Pause avant le prochain envoi (modifiable) + delay(30000); + } else { + Serial.print("Erreur de lecture SDS011 : "); + Serial.println(pm.statusToString()); + } + + // Mise en veille pour économie d'énergie + lorae5.sleep(); +} diff --git a/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/test/README b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/Test_Integration_TTN_SDS011_DHT22/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/Test_SHT30/.gitignore b/Capteur-autonome/Projects/Test_SHT30/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/Test_SHT30/.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/Test_SHT30/.vscode/extensions.json b/Capteur-autonome/Projects/Test_SHT30/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/Test_SHT30/.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/Test_SHT30/include/README b/Capteur-autonome/Projects/Test_SHT30/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/Test_SHT30/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/Test_SHT30/lib/README b/Capteur-autonome/Projects/Test_SHT30/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/Test_SHT30/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/Test_SHT30/platformio.ini b/Capteur-autonome/Projects/Test_SHT30/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..4ada557b45bc97f951ebcd9bde63db9efe5688b7 --- /dev/null +++ b/Capteur-autonome/Projects/Test_SHT30/platformio.ini @@ -0,0 +1,19 @@ +; 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 +monitor_speed = 115200 + +lib_deps = + adafruit/Adafruit SHT31 Library@^2.2.2 + SPI diff --git a/Capteur-autonome/Projects/Test_SHT30/src/main.cpp b/Capteur-autonome/Projects/Test_SHT30/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17a50b14b8e4c6f5dc1eca7c3fb03579291d8a98 --- /dev/null +++ b/Capteur-autonome/Projects/Test_SHT30/src/main.cpp @@ -0,0 +1,35 @@ +#include <Wire.h> +#include "Adafruit_SHT31.h" + +Adafruit_SHT31 sht31 = Adafruit_SHT31(); + +void setup() { + Serial.begin(115200); + Wire.begin(23, 22); // Initialisation I2C avec GPIO23 (SDA), GPIO22 (SCL) + + if (!sht31.begin(0x44)) { // Utilise le Wire par défaut déjà configuré + Serial.println("Couldn't find SHT30 sensor. Check wiring!"); + while (1); + } + + Serial.println("SHT30 initialized."); +} + +void loop() { + float temperature = sht31.readTemperature(); + float humidity = sht31.readHumidity(); + + if (!isnan(temperature) && !isnan(humidity)) { + Serial.print("Temperature: "); + Serial.print(temperature); + Serial.println(" °C"); + + Serial.print("Humidity: "); + Serial.print(humidity); + Serial.println(" %"); + } else { + Serial.println("Failed to read from SHT30 sensor!"); + } + + delay(2000); +} diff --git a/Capteur-autonome/Projects/Test_SHT30/test/README b/Capteur-autonome/Projects/Test_SHT30/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/Test_SHT30/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/Test_SI7021/.gitignore b/Capteur-autonome/Projects/Test_SI7021/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/Test_SI7021/.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/Test_SI7021/.vscode/extensions.json b/Capteur-autonome/Projects/Test_SI7021/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/Test_SI7021/.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/Test_SI7021/include/README b/Capteur-autonome/Projects/Test_SI7021/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/Test_SI7021/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/Test_SI7021/lib/README b/Capteur-autonome/Projects/Test_SI7021/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/Test_SI7021/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/Test_SI7021/platformio.ini b/Capteur-autonome/Projects/Test_SI7021/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..fc7886a9a4de122eb861935e5e4d7af2bfa0e94d --- /dev/null +++ b/Capteur-autonome/Projects/Test_SI7021/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 = adafruit/Adafruit Si7021 Library@^1.5.3 diff --git a/Capteur-autonome/Projects/Test_SI7021/src/main.cpp b/Capteur-autonome/Projects/Test_SI7021/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..750686f73299e9ee91ec0f9d993760d91163079b --- /dev/null +++ b/Capteur-autonome/Projects/Test_SI7021/src/main.cpp @@ -0,0 +1,34 @@ +#include <Wire.h> +#include <Adafruit_Si7021.h> + +Adafruit_Si7021 capteur = Adafruit_Si7021(); + +void setup() { + Serial.begin(115200); + + // Initialisation I2C avec SDA = GPIO19, SCL = GPIO18 + Wire.begin(19, 18); + + // Initialisation du capteur SI7021 + if (!capteur.begin()) { + Serial.println("Erreur : capteur SI7021 non détecté !"); + while (true); // Boucle infinie si le capteur est absent + } + + Serial.println("Capteur SI7021 détecté !"); +} + +void loop() { + float temperature = capteur.readTemperature(); + float humidite = capteur.readHumidity(); + + Serial.print("Température : "); + Serial.print(temperature); + Serial.println(" °C"); + + Serial.print("Humidité : "); + Serial.print(humidite); + Serial.println(" %"); + + delay(1000); // Attente d'une seconde entre chaque mesure +} diff --git a/Capteur-autonome/Projects/Test_SI7021/test/README b/Capteur-autonome/Projects/Test_SI7021/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/Test_SI7021/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/test.zip b/Capteur-autonome/Projects/test.zip new file mode 100644 index 0000000000000000000000000000000000000000..7bd54002c22ea47fb0ac62ba15d795d402371d0b Binary files /dev/null and b/Capteur-autonome/Projects/test.zip differ diff --git a/Capteur-autonome/Projects/test/.DS_Store b/Capteur-autonome/Projects/test/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..06a26f36c6d5b5e0e3761582df9766878cd3bc73 Binary files /dev/null and b/Capteur-autonome/Projects/test/.DS_Store differ diff --git a/Capteur-autonome/Projects/test/.gitignore b/Capteur-autonome/Projects/test/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/test/.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/test/.vscode/extensions.json b/Capteur-autonome/Projects/test/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/test/.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/test/include/README b/Capteur-autonome/Projects/test/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/test/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/test/lib/README b/Capteur-autonome/Projects/test/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/test/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/test/platformio.ini b/Capteur-autonome/Projects/test/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..72f0512a0d2ef50ad284df693d808ec4a97ebc5c --- /dev/null +++ b/Capteur-autonome/Projects/test/platformio.ini @@ -0,0 +1,18 @@ +; 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:freenove_esp32_dev] +platform = espressif32 +board = esp32dev +framework = arduino +lib_deps = + adafruit/DHT sensor library@^1.4.6 +upload_speed = 115200 +monitor_speed = 9200 diff --git a/Capteur-autonome/Projects/test/src/main.cpp b/Capteur-autonome/Projects/test/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f490d79663fa3423b6bb476866272b15a8b3e935 --- /dev/null +++ b/Capteur-autonome/Projects/test/src/main.cpp @@ -0,0 +1,71 @@ +// Example testing sketch for various DHT humidity/temperature sensors written by ladyada +// REQUIRES the following Arduino libraries: +// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library +// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor + +#include "DHT.h" + +#define DHTPIN 18 // Digital pin connected to the DHT sensor +// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 -- +// Pin 15 can work but DHT must be disconnected during program upload. + +// Uncomment whatever type you're using! +//#define DHTTYPE DHT11 // DHT 11 +#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 +//#define DHTTYPE DHT21 // DHT 21 (AM2301) + +// Connect pin 1 (on the left) of the sensor to +5V +// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 +// to 3.3V instead of 5V! +// Connect pin 2 of the sensor to whatever your DHTPIN is +// Connect pin 4 (on the right) of the sensor to GROUND +// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor + +// Initialize DHT sensor. +// Note that older versions of this library took an optional third parameter to +// tweak the timings for faster processors. This parameter is no longer needed +// as the current DHT reading algorithm adjusts itself to work on faster procs. +DHT dht(DHTPIN, DHTTYPE); + +void setup() { + Serial.begin(9600); + Serial.println(F("DHTxx test!")); + + dht.begin(); +} + +void loop() { + // Wait a few seconds between measurements. + delay(2000); + + // Reading temperature or humidity takes about 250 milliseconds! + // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) + float h = dht.readHumidity(); + // Read temperature as Celsius (the default) + float t = dht.readTemperature(); + // Read temperature as Fahrenheit (isFahrenheit = true) + float f = dht.readTemperature(true); + + // Check if any reads failed and exit early (to try again). + if (isnan(h) || isnan(t) || isnan(f)) { + Serial.println(F("Failed to read from DHT sensor!")); + return; + } + + // Compute heat index in Fahrenheit (the default) + float hif = dht.computeHeatIndex(f, h); + // Compute heat index in Celsius (isFahreheit = false) + float hic = dht.computeHeatIndex(t, h, false); + + Serial.print(F("Humidity: ")); + Serial.print(h); + Serial.print(F("% Temperature: ")); + Serial.print(t); + Serial.print(F("°C ")); + Serial.print(f); + Serial.print(F("°F Heat index: ")); + Serial.print(hic); + Serial.print(F("°C ")); + Serial.print(hif); + Serial.println(F("°F")); +} \ No newline at end of file diff --git a/Capteur-autonome/Projects/test/test/README b/Capteur-autonome/Projects/test/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/test/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/testesp8266/.gitignore b/Capteur-autonome/Projects/testesp8266/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..89cc49cbd652508924b868ea609fa8f6b758ec56 --- /dev/null +++ b/Capteur-autonome/Projects/testesp8266/.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/testesp8266/.vscode/extensions.json b/Capteur-autonome/Projects/testesp8266/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..080e70d08b9811fa743afe5094658dba0ed6b7c2 --- /dev/null +++ b/Capteur-autonome/Projects/testesp8266/.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/testesp8266/include/README b/Capteur-autonome/Projects/testesp8266/include/README new file mode 100644 index 0000000000000000000000000000000000000000..49819c0d54960b5848277cc68ab2d08169cecaf3 --- /dev/null +++ b/Capteur-autonome/Projects/testesp8266/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/testesp8266/lib/README b/Capteur-autonome/Projects/testesp8266/lib/README new file mode 100644 index 0000000000000000000000000000000000000000..93793971ffcaedf70668ba0d17d84315c350f9f7 --- /dev/null +++ b/Capteur-autonome/Projects/testesp8266/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/testesp8266/platformio.ini b/Capteur-autonome/Projects/testesp8266/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..6bb127a4f41b5a7b868a389ec7b6c499378d8350 --- /dev/null +++ b/Capteur-autonome/Projects/testesp8266/platformio.ini @@ -0,0 +1,14 @@ +; 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:nodemcuv2] +platform = espressif8266 +board = nodemcuv2 +framework = arduino diff --git a/Capteur-autonome/Projects/testesp8266/src/main.cpp b/Capteur-autonome/Projects/testesp8266/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb9fbba46e2fddd293e9056f65b63f69ce859b83 --- /dev/null +++ b/Capteur-autonome/Projects/testesp8266/src/main.cpp @@ -0,0 +1,18 @@ +#include <Arduino.h> + +// put function declarations here: +int myFunction(int, int); + +void setup() { + // put your setup code here, to run once: + int result = myFunction(2, 3); +} + +void loop() { + // put your main code here, to run repeatedly: +} + +// put function definitions here: +int myFunction(int x, int y) { + return x + y; +} \ No newline at end of file diff --git a/Capteur-autonome/Projects/testesp8266/test/README b/Capteur-autonome/Projects/testesp8266/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/Capteur-autonome/Projects/testesp8266/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