From 9811022b7663dafe4d46c876d48dad835ca7d0a6 Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 24 Apr 2023 11:17:36 +0200 Subject: [PATCH 1/7] Create Branche Test_IOT --- .vscode/c_cpp_properties.json | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..834113c --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,19 @@ +{ + "configurations": [ + { + "name": "Mac", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "macFrameworkPath": [ + "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks" + ], + "compilerPath": "/usr/bin/clang", + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "macos-clang-arm64" + } + ], + "version": 4 +} \ No newline at end of file From 5784e38fbbb804a25643a6523fb36090ebff6b81 Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 24 Apr 2023 12:22:52 +0200 Subject: [PATCH 2/7] feat: add Test for WifiConnect API --- .vscode/settings.json | 13 +++++++++++++ IOT/envs.ini | 6 ++++++ IOT/secrets.ini.example | 12 ------------ IOT/src/main.cpp | 8 ++++++-- IOT/test/main.cpp | 20 ++++++++++++++++++++ IOT/test/test.h | 7 +++++++ IOT/test/test_api.cpp | 19 +++++++++++++++++++ 7 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 .vscode/settings.json delete mode 100644 IOT/secrets.ini.example create mode 100644 IOT/test/main.cpp create mode 100644 IOT/test/test.h create mode 100644 IOT/test/test_api.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a341e93 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,13 @@ +{ + "files.associations": { + "__bit_reference": "cpp", + "__hash_table": "cpp", + "__split_buffer": "cpp", + "array": "cpp", + "initializer_list": "cpp", + "string": "cpp", + "string_view": "cpp", + "unordered_map": "cpp", + "vector": "cpp" + } +} \ No newline at end of file diff --git a/IOT/envs.ini b/IOT/envs.ini index b3db1a7..1bbf9b6 100644 --- a/IOT/envs.ini +++ b/IOT/envs.ini @@ -1,4 +1,10 @@ ; Add additionnal environments in this file +; Debug environemnt +[env:test] +test_build_src = true +build_type = debug +build_flags = ${env.build_flags} + -D TESTING ; Default production environment [env:prod] diff --git a/IOT/secrets.ini.example b/IOT/secrets.ini.example deleted file mode 100644 index b517f84..0000000 --- a/IOT/secrets.ini.example +++ /dev/null @@ -1,12 +0,0 @@ -; Add secrets token/logins/etc `secrets.ini` -; Add the sames values as blank in `secrets.ini.example -; To be able to reproduce it - -[secrets] -build_flags = - - -D WIFI_SSID=\"...\" - -D WIFI_PASSWORD=\"...\"4 - - -D USER_NAME=\"...\" - -D USER_PASSWORD=\"...\" \ No newline at end of file diff --git a/IOT/src/main.cpp b/IOT/src/main.cpp index 14f34bd..4f7861c 100644 --- a/IOT/src/main.cpp +++ b/IOT/src/main.cpp @@ -1,12 +1,16 @@ +#ifndef TESTING + #include #include "Program.h" Program* program; void setup() { - program = new Program(); + program = new Program(); } void loop() { - program->loop(); + program->loop(); } + +#endif \ No newline at end of file diff --git a/IOT/test/main.cpp b/IOT/test/main.cpp new file mode 100644 index 0000000..6683380 --- /dev/null +++ b/IOT/test/main.cpp @@ -0,0 +1,20 @@ +#include +#include +#include "test.h" +void setup() { + delay(2000); + // start unit tests engine + UNITY_BEGIN(); + Serial.begin(115200); + RUN_TEST(TestWifiBeginConnected); + RUN_TEST(TestWifiBeginNotConnected); + + + UNITY_END(); +} + + + +void loop() { + +} \ No newline at end of file diff --git a/IOT/test/test.h b/IOT/test/test.h new file mode 100644 index 0000000..6795825 --- /dev/null +++ b/IOT/test/test.h @@ -0,0 +1,7 @@ +#ifndef TEST_H +#define TEST_H + +void TestWifiBeginConnected(); +void TestWifiBeginNotConnected(); + +#endif \ No newline at end of file diff --git a/IOT/test/test_api.cpp b/IOT/test/test_api.cpp new file mode 100644 index 0000000..0f08f49 --- /dev/null +++ b/IOT/test/test_api.cpp @@ -0,0 +1,19 @@ +#include "test.h" +#include +#include "API.h" + +void TestWifiBeginConnected() { + API* api = new API(USER_NAME, USER_PASSWORD, API_HOST); + + Serial1.begin(MONITOR_SPEED); + TEST_ASSERT_EQUAL_MESSAGE(true, api->wifiBegin(WIFI_SSID, WIFI_PASSWORD, &Serial1), "Wifi not connected"); + +} + +void TestWifiBeginNotConnected() { + API* api = new API(USER_NAME, USER_PASSWORD, API_HOST); + + Serial2.begin(MONITOR_SPEED); + TEST_ASSERT_EQUAL_MESSAGE(false, api->wifiBegin(WIFI_SSID, WIFI_PASSWORD, &Serial2), "Wifi connected"); + +} \ No newline at end of file From 36bdb8634346308f6bf0cd6a2b6d021865cb284e Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 24 Apr 2023 14:54:26 +0200 Subject: [PATCH 3/7] feat: Add Test for Connect API --- IOT/lib/API/include/API.h | 9 ++++++--- IOT/lib/API/src/API.cpp | 8 ++++++-- IOT/test/main.cpp | 2 ++ IOT/test/test.h | 2 ++ IOT/test/test_api.cpp | 24 +++++++++++++++++++++++- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/IOT/lib/API/include/API.h b/IOT/lib/API/include/API.h index 27eda6b..463f6f6 100644 --- a/IOT/lib/API/include/API.h +++ b/IOT/lib/API/include/API.h @@ -40,9 +40,7 @@ public: * @return false il y a une erreur durran l'envoie */ bool sendValue(String val, String poubelleID, String unit, bool full); - - -private: + /** * @brief connect l'utilisateur a l'api et met a jour le token * @@ -51,6 +49,11 @@ private: */ bool connect(); + //TODO :: Check wifibegin avant + +private: + + /** * @brief ID de l'utilisateur * diff --git a/IOT/lib/API/src/API.cpp b/IOT/lib/API/src/API.cpp index 89427f5..5af9675 100644 --- a/IOT/lib/API/src/API.cpp +++ b/IOT/lib/API/src/API.cpp @@ -59,7 +59,8 @@ bool API::connect(){ this->client->println(); - while (!client->available()) {} + while (!client->available()) { + } String responce = ""; while (client->available()) { @@ -77,7 +78,10 @@ bool API::connect(){ this->client->stop(); sortie = true; - + if (JSONVar::stringify(JSONVar::parse(str)["code"]) == "400") { + Serial.println("Failed to authenticate"); + return false; + } return sortie; } diff --git a/IOT/test/main.cpp b/IOT/test/main.cpp index 6683380..8fe4dc6 100644 --- a/IOT/test/main.cpp +++ b/IOT/test/main.cpp @@ -8,6 +8,8 @@ void setup() { Serial.begin(115200); RUN_TEST(TestWifiBeginConnected); RUN_TEST(TestWifiBeginNotConnected); + RUN_TEST(TestConnectAPI); + // RUN_TEST(TestConnectAPIFailed); UNITY_END(); diff --git a/IOT/test/test.h b/IOT/test/test.h index 6795825..2ef0746 100644 --- a/IOT/test/test.h +++ b/IOT/test/test.h @@ -3,5 +3,7 @@ void TestWifiBeginConnected(); void TestWifiBeginNotConnected(); +void TestConnectAPI(); +// void TestConnectAPIFailed(); #endif \ No newline at end of file diff --git a/IOT/test/test_api.cpp b/IOT/test/test_api.cpp index 0f08f49..98fc292 100644 --- a/IOT/test/test_api.cpp +++ b/IOT/test/test_api.cpp @@ -2,6 +2,8 @@ #include #include "API.h" + +//Testing WifiBegin function void TestWifiBeginConnected() { API* api = new API(USER_NAME, USER_PASSWORD, API_HOST); @@ -16,4 +18,24 @@ void TestWifiBeginNotConnected() { Serial2.begin(MONITOR_SPEED); TEST_ASSERT_EQUAL_MESSAGE(false, api->wifiBegin(WIFI_SSID, WIFI_PASSWORD, &Serial2), "Wifi connected"); -} \ No newline at end of file +} + +//Testing Connect function +void TestConnectAPI() { + API* api = new API(USER_NAME, USER_PASSWORD, API_HOST); + + Serial1.begin(MONITOR_SPEED); + api->wifiBegin(WIFI_SSID, WIFI_PASSWORD, &Serial1); + TEST_ASSERT_EQUAL_MESSAGE(true, api->connect(), "Not Connected"); +} + +// void TestConnectAPIFailed() { +// API* api = new API("Carl", "toto", API_HOST); + +// Serial1.begin(MONITOR_SPEED); +// api->wifiBegin(WIFI_SSID, WIFI_PASSWORD, &Serial1); +// TEST_ASSERT_EQUAL_MESSAGE(false, api->connect(), "Connected"); +// } + +//Testing SendValue function + From 884169e7457549cefdf29d8cdf2b96231c574656 Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 24 Apr 2023 15:49:16 +0200 Subject: [PATCH 4/7] feat: add Test for sendValue --- IOT/test/main.cpp | 1 + IOT/test/test.h | 3 ++- IOT/test/test_api.cpp | 20 ++++++++++++++------ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/IOT/test/main.cpp b/IOT/test/main.cpp index 8fe4dc6..db4e977 100644 --- a/IOT/test/main.cpp +++ b/IOT/test/main.cpp @@ -10,6 +10,7 @@ void setup() { RUN_TEST(TestWifiBeginNotConnected); RUN_TEST(TestConnectAPI); // RUN_TEST(TestConnectAPIFailed); + RUN_TEST(TestSendValue); UNITY_END(); diff --git a/IOT/test/test.h b/IOT/test/test.h index 2ef0746..83f5454 100644 --- a/IOT/test/test.h +++ b/IOT/test/test.h @@ -4,6 +4,7 @@ void TestWifiBeginConnected(); void TestWifiBeginNotConnected(); void TestConnectAPI(); -// void TestConnectAPIFailed(); +void TestConnectAPIFailed(); +void TestSendValue(); #endif \ No newline at end of file diff --git a/IOT/test/test_api.cpp b/IOT/test/test_api.cpp index 98fc292..7b0cd6b 100644 --- a/IOT/test/test_api.cpp +++ b/IOT/test/test_api.cpp @@ -29,13 +29,21 @@ void TestConnectAPI() { TEST_ASSERT_EQUAL_MESSAGE(true, api->connect(), "Not Connected"); } -// void TestConnectAPIFailed() { -// API* api = new API("Carl", "toto", API_HOST); +//FIXME: boucle inf when connection failed +void TestConnectAPIFailed() { + API* api = new API("Carl", "toto", API_HOST); -// Serial1.begin(MONITOR_SPEED); -// api->wifiBegin(WIFI_SSID, WIFI_PASSWORD, &Serial1); -// TEST_ASSERT_EQUAL_MESSAGE(false, api->connect(), "Connected"); -// } + Serial1.begin(MONITOR_SPEED); + api->wifiBegin(WIFI_SSID, WIFI_PASSWORD, &Serial1); + TEST_ASSERT_EQUAL_MESSAGE(false, api->connect(), "Connected"); +} //Testing SendValue function +void TestSendValue() { + API* api = new API(USER_NAME, USER_PASSWORD, API_HOST); + + Serial1.begin(MONITOR_SPEED); + api->wifiBegin(WIFI_SSID, WIFI_PASSWORD, &Serial1); + TEST_ASSERT_EQUAL_MESSAGE(true, api->sendValue("30", "gdnuxl0wlgurtj3", "W", true), "Not Connected to server"); +} From b3da67f31eb39a9294a6c62a2cf131e75975ae85 Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 24 Apr 2023 16:05:50 +0200 Subject: [PATCH 5/7] feat: Add end to end test for ultrasonic capteur --- IOT/config.ini | 2 ++ IOT/include/Program.h | 7 +++++++ IOT/src/Program.cpp | 8 ++++++-- IOT/test/test_api.cpp | 3 +-- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/IOT/config.ini b/IOT/config.ini index 17d9e3f..8a28ef4 100644 --- a/IOT/config.ini +++ b/IOT/config.ini @@ -21,3 +21,5 @@ build_flags = -D EXAMPLE_NUMBER=69 + -D TRASHCAN_ONE=\"gdnuxl0wlgurtj3\" + diff --git a/IOT/include/Program.h b/IOT/include/Program.h index 03e3115..549809b 100644 --- a/IOT/include/Program.h +++ b/IOT/include/Program.h @@ -3,6 +3,7 @@ #include #include +#include "API.h" class Program{ public: @@ -26,5 +27,11 @@ private: * */ Ultrasonic *ultrasonic; + + /** + * @brief Réference de l'API pour les calls + * + */ + API *api; }; #endif \ No newline at end of file diff --git a/IOT/src/Program.cpp b/IOT/src/Program.cpp index 1f87003..710d302 100644 --- a/IOT/src/Program.cpp +++ b/IOT/src/Program.cpp @@ -1,10 +1,13 @@ #include "Program.h" - int distance; Program::Program(){ + this->api = new API(USER_NAME, USER_PASSWORD, API_HOST); + Serial1.begin(MONITOR_SPEED); + this->api->wifiBegin(WIFI_SSID, WIFI_PASSWORD, &Serial1); + Serial.begin(MONITOR_SPEED); this->ultrasonic = new Ultrasonic(ULTRA_SOUND_TRIGD, ULTRA_SOUND_ECHO); } @@ -12,7 +15,8 @@ Program::Program(){ void Program::loop(){ distance = this->ultrasonic->read(); + this->api->sendValue(JSONVar::stringify(distance), TRASHCAN_ONE, "W", false); Serial.print("Distance in CM: "); Serial.println(distance); - delay(1000); + delay(10000); } \ No newline at end of file diff --git a/IOT/test/test_api.cpp b/IOT/test/test_api.cpp index 7b0cd6b..19bb5d9 100644 --- a/IOT/test/test_api.cpp +++ b/IOT/test/test_api.cpp @@ -45,5 +45,4 @@ void TestSendValue() { Serial1.begin(MONITOR_SPEED); api->wifiBegin(WIFI_SSID, WIFI_PASSWORD, &Serial1); TEST_ASSERT_EQUAL_MESSAGE(true, api->sendValue("30", "gdnuxl0wlgurtj3", "W", true), "Not Connected to server"); -} - +} \ No newline at end of file From e088b4e304c42114a000599e461b8b123b1aae55 Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 24 Apr 2023 16:49:54 +0200 Subject: [PATCH 6/7] Fix: remove useless file --- .vscode/c_cpp_properties.json | 19 ------------------- .vscode/settings.json | 13 ------------- 2 files changed, 32 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json delete mode 100644 .vscode/settings.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 834113c..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "configurations": [ - { - "name": "Mac", - "includePath": [ - "${workspaceFolder}/**" - ], - "defines": [], - "macFrameworkPath": [ - "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks" - ], - "compilerPath": "/usr/bin/clang", - "cStandard": "c17", - "cppStandard": "c++17", - "intelliSenseMode": "macos-clang-arm64" - } - ], - "version": 4 -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index a341e93..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "files.associations": { - "__bit_reference": "cpp", - "__hash_table": "cpp", - "__split_buffer": "cpp", - "array": "cpp", - "initializer_list": "cpp", - "string": "cpp", - "string_view": "cpp", - "unordered_map": "cpp", - "vector": "cpp" - } -} \ No newline at end of file From 9b0b5bedd8528d22f6452752cf86d405d3ffd6f6 Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 24 Apr 2023 16:52:08 +0200 Subject: [PATCH 7/7] feat: add doc for secret --- IOT/secrets.ini.example | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 IOT/secrets.ini.example diff --git a/IOT/secrets.ini.example b/IOT/secrets.ini.example new file mode 100644 index 0000000..b517f84 --- /dev/null +++ b/IOT/secrets.ini.example @@ -0,0 +1,12 @@ +; Add secrets token/logins/etc `secrets.ini` +; Add the sames values as blank in `secrets.ini.example +; To be able to reproduce it + +[secrets] +build_flags = + + -D WIFI_SSID=\"...\" + -D WIFI_PASSWORD=\"...\"4 + + -D USER_NAME=\"...\" + -D USER_PASSWORD=\"...\" \ No newline at end of file