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/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/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/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/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/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..db4e977 --- /dev/null +++ b/IOT/test/main.cpp @@ -0,0 +1,23 @@ +#include +#include +#include "test.h" +void setup() { + delay(2000); + // start unit tests engine + UNITY_BEGIN(); + Serial.begin(115200); + RUN_TEST(TestWifiBeginConnected); + RUN_TEST(TestWifiBeginNotConnected); + RUN_TEST(TestConnectAPI); + // RUN_TEST(TestConnectAPIFailed); + RUN_TEST(TestSendValue); + + + 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..83f5454 --- /dev/null +++ b/IOT/test/test.h @@ -0,0 +1,10 @@ +#ifndef TEST_H +#define TEST_H + +void TestWifiBeginConnected(); +void TestWifiBeginNotConnected(); +void TestConnectAPI(); +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 new file mode 100644 index 0000000..19bb5d9 --- /dev/null +++ b/IOT/test/test_api.cpp @@ -0,0 +1,48 @@ +#include "test.h" +#include +#include "API.h" + + +//Testing WifiBegin function +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"); + +} + +//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"); +} + +//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"); +} + +//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"); +} \ No newline at end of file