diff --git a/client/client/lib/deep_sleep/f_deep_sleep.cpp b/client/client/lib/deep_sleep/f_deep_sleep.cpp index 60d1ac0b44d850f809d44be2b2fd609536fb54a0..c434f53c5728c4bb840c8ec19087f802f9c63ffe 100644 --- a/client/client/lib/deep_sleep/f_deep_sleep.cpp +++ b/client/client/lib/deep_sleep/f_deep_sleep.cpp @@ -1,23 +1,43 @@ #include "f_deep_sleep.hpp" +#include "esp32-hal-log.h" +#include "esp_log.h" -void print_wakeup_reason(){ - esp_sleep_wakeup_cause_t wakeup_reason; +namespace DeepSleep { +static const std::string TAG = "DEEP_SLEEP"; - wakeup_reason = esp_sleep_get_wakeup_cause(); - switch(wakeup_reason) - { - case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break; - case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break; - case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break; - case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break; - case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break; - default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break; - } -} +void print_wakeup_reason() +{ + esp_sleep_wakeup_cause_t wakeup_reason; + + wakeup_reason = esp_sleep_get_wakeup_cause(); + switch (wakeup_reason) { + case ESP_SLEEP_WAKEUP_EXT0: + ESP_LOGD(TAG.c_str(), "Wakeup caused by external signal using RTC_IO"); + break; + case ESP_SLEEP_WAKEUP_EXT1: + ESP_LOGD(TAG.c_str(), "Wakeup caused by external signal using RTC_CNTL"); + break; + case ESP_SLEEP_WAKEUP_TIMER: + ESP_LOGD(TAG.c_str(), "Wakeup caused by timer"); + break; + case ESP_SLEEP_WAKEUP_TOUCHPAD: + ESP_LOGD(TAG.c_str(), "Wakeup caused by touchpad"); + break; + case ESP_SLEEP_WAKEUP_ULP: + ESP_LOGD(TAG.c_str(), "Wakeup caused by ULP program"); + break; + default: + ESP_LOGD(TAG.c_str(), "Wakeup was not caused by deep sleep: %d\n", wakeup_reason); + break; + } +} -void deep_sleep(int time_in_sec){ - esp_sleep_enable_timer_wakeup(time_in_sec * 1000000); - esp_deep_sleep_start(); +void deep_sleep(int time_in_sec) +{ + esp_sleep_enable_timer_wakeup(time_in_sec * 1000000); + esp_deep_sleep_start(); } + +}; // namespace DeepSleep diff --git a/client/client/lib/deep_sleep/f_deep_sleep.hpp b/client/client/lib/deep_sleep/f_deep_sleep.hpp index b0c91129d025c7cd48f0350a269a765c7c8e0f5b..7a528c840b8dab363a1f1b65a917f007294317c7 100644 --- a/client/client/lib/deep_sleep/f_deep_sleep.hpp +++ b/client/client/lib/deep_sleep/f_deep_sleep.hpp @@ -3,7 +3,16 @@ #include <Arduino.h> -void deep_sleep(int time_to_sleep); +namespace DeepSleep { +// https://en.cppreference.com/w/cpp/language/storage_duration +// When used in a declaration at namespace scope, it specifies internal linkage. +// internal linkage. The variable can be referred to from all scopes in the current translation unit. All variables +// which are declared at file scope have this linkage, including variables declared static at file scope. +static RTC_DATA_ATTR int bootCount = 0; + +void deep_sleep(int time_to_sleep_in_seconds); void print_wakeup_reason(); +} // namespace DeepSleep + #endif diff --git a/client/client/src/main.cpp b/client/client/src/main.cpp index bf964c0c5c0280b41dc8e0a514f76408276314d0..179aff942a17df7532af4e52d517e93f2d1e81cc 100644 --- a/client/client/src/main.cpp +++ b/client/client/src/main.cpp @@ -1,6 +1,7 @@ #include "../lib/dr26_analogue/dr26.hpp" #include "NoDataAvailableException.hpp" #include "esp_log.h" +#include "f_deep_sleep.hpp" #include <Arduino.h> #include <drs26.hpp> #include <ina219.hpp> @@ -13,6 +14,11 @@ ForteDRS26 drs26; void setup() { Serial.begin(115200); + + DeepSleep::print_wakeup_reason(); + DeepSleep::bootCount++; + ESP_LOGD(TAG.c_str(), "Boot number: %d", DeepSleep::bootCount); + drs26.setup(); espnow_setup(); @@ -22,10 +28,7 @@ void setup() void loop() { - out_data_drs26 data{}; - try { - // data = drs26.readData(); auto messages = drs26.buildMessages(); for (const Message &message : messages) { @@ -36,9 +39,5 @@ void loop() std::cerr << e.what() << '\n'; } - ESP_LOGE(TAG.c_str(), "Sensor Circumference: "); - // log_e("Temperature: "); - // log_e("Id: "); - - delay(5000); + DeepSleep::deep_sleep(5); } diff --git a/host/arduino/.gitignore b/host/arduino/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..3fe18ad4795165c312b8998befee223818bd4b33 --- /dev/null +++ b/host/arduino/.gitignore @@ -0,0 +1,3 @@ +.pio +CMakeListsPrivate.txt +cmake-build-*/ diff --git a/host/arduino/CMakeLists.txt b/host/arduino/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..c357da067b7d61fada76d400d8906af6d1be73dd --- /dev/null +++ b/host/arduino/CMakeLists.txt @@ -0,0 +1,33 @@ +# !!! WARNING !!! AUTO-GENERATED FILE, PLEASE DO NOT MODIFY IT AND USE +# https://docs.platformio.org/page/projectconf/section_env_build.html#build-flags +# +# If you need to override existing CMake configuration or add extra, +# please create `CMakeListsUser.txt` in the root of project. +# The `CMakeListsUser.txt` will not be overwritten by PlatformIO. + +cmake_minimum_required(VERSION 3.13) +set(CMAKE_SYSTEM_NAME Generic) +set(CMAKE_C_COMPILER_WORKS 1) +set(CMAKE_CXX_COMPILER_WORKS 1) + +project("arduino" C CXX) + +include(CMakeListsPrivate.txt) + +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/CMakeListsUser.txt) +include(CMakeListsUser.txt) +endif() + +add_custom_target( + Production ALL + COMMAND platformio -c clion run "$<$<NOT:$<CONFIG:All>>:-e${CMAKE_BUILD_TYPE}>" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) + +add_custom_target( + Debug ALL + COMMAND platformio -c clion debug "$<$<NOT:$<CONFIG:All>>:-e${CMAKE_BUILD_TYPE}>" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) + +add_executable(Z_DUMMY_TARGET ${SRC_LIST}) diff --git a/host/arduino/include/README b/host/arduino/include/README new file mode 100644 index 0000000000000000000000000000000000000000..194dcd43252dcbeb2044ee38510415041a0e7b47 --- /dev/null +++ b/host/arduino/include/README @@ -0,0 +1,39 @@ + +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 usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +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/host/arduino/platformio.ini b/host/arduino/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..58dec1ab8899033f0d5b4dac328bbc79b74213e0 --- /dev/null +++ b/host/arduino/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:mkrnb1500] +platform = atmelsam +board = mkrnb1500 +framework = arduino +lib_deps = + arduino-libraries/MKRNB@^1.5.1 + bblanchon/ArduinoJson@^6.19.4 + arduino-libraries/Arduino Low Power@^1.2.2 diff --git a/host/arduino/src/main.cpp b/host/arduino/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d15df627d863b11e55b619b11cf981e2b4c1f8f --- /dev/null +++ b/host/arduino/src/main.cpp @@ -0,0 +1,169 @@ +/* +Web client + +This sketch connects to a website through a MKR NB 1500 board. Specifically, +this example downloads the URL "http://example.org/" and +prints it to the Serial monitor. + +Circuit: +- MKR NB 1500 board +- Antenna +- SIM card with a data plan + +created 8 Mar 2012 +by Tom Igoe +*/ + +// libraries +#include "ArduinoLowPower.h" +#include <ArduinoJson.h> +#include <MKRNB.h> +#include <string> + +// initialize the library instance +GPRS gprs; +NB nbAccess; + +void attachToNetwork(); + +std::string getResponse(NBClient &client); +std::string getResponseBody(const std::string &response); +std::string getResponse(std::string server, std::string path, int port) +{ + NBClient client; + // if you get a connection, report back via serial: + if (client.connect(server.c_str(), port)) { + Serial.println("connected to server"); + // Make a HTTP request: + client.print("GET "); + client.print(path.c_str()); + client.println(" HTTP/1.1"); + client.print("Host: "); + client.println(server.c_str()); + client.println("Connection: close"); + client.println(); + } else { + // if you didn't get a connection to the server: + Serial.println("connection failed"); + } + + std::string response = getResponse(client); + + std::string token = getResponseBody(response); + Serial.println(token.c_str()); + client.stop(); + return token; +} +std::string getResponseBody(const std::string &response) +{ + std::string delimiter = "\r\n\r\n"; + std::string token = response.substr(response.find(delimiter)).replace(0, delimiter.length(), ""); + return token; +} + +std::string getResponse(NBClient &client) +{ + String status = client.readStringUntil('\r'); + Serial.println(status); + + std::string response; + while (client.connected()) { + if (client.available()) { + char c = client.read(); + response += c; + } + } + return response; +} + +std::string postRequest(std::string server, std::string path, int port, std::string body) +{ + NBClient client; + // if you get a connection, report back via serial: + if (client.connect(server.c_str(), port)) { + Serial.println("connected to server"); + // Make a HTTP request: + client.print("POST "); + client.print(path.c_str()); + client.println(" HTTP/1.1"); + client.print("Host: "); + client.println(server.c_str()); + client.println("Connection: close"); + client.println("Content-Type: application/json"); + client.print("Content-Length: "); + client.println(body.length()); + client.println(); + client.println(body.c_str()); + } else { + // if you didn't get a connection to the server: + Serial.println("connection failed"); + } + + std::string response = getResponse(client); + + std::string token = getResponseBody(response); + + Serial.println(token.c_str()); + client.stop(); + return token; +} + +void setup() +{ + // initialize serial communications and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } +} + +void attachToNetwork() +{ + Serial.println("Starting Arduino web client."); + // connection state + boolean connected = false; + + // After starting the modem with NB.begin() + // attach to the GPRS network with the APN, login and password + while (!connected) { + if ((nbAccess.begin("", "m2m.public.at") == NB_READY) && (gprs.attachGPRS() == GPRS_READY)) { + connected = true; + } else { + Serial.println("Not connected"); + exit(1); + delay(1000); + } + } +} + +void loop() +{ + attachToNetwork(); + + Serial.println("connecting..."); + + auto response = getResponse("date.jsontest.com", "/", 80); + // convert response to json object + DynamicJsonDocument doc(1024); + auto error = deserializeJson(doc, response); + if (error) { + Serial.print(F("deserializeJson() failed: ")); + Serial.println(error.f_str()); + return; + } + const char *date = doc["date"]; + long long milliseconds_since_epoch = doc["milliseconds_since_epoch"].as<long long>(); + const char *time = doc["time"]; + Serial.println(date); + Serial.println(milliseconds_since_epoch); + Serial.println(time); + nbAccess.shutdown(); + + // TODO: WAKE UP FROM WATCHDOG (PIN) + LowPower.deepSleep(10000); + + // TODO: SAVE TO EEPROM OR SD CARD VIA SPI + // TODO: REFACTOR CODE + // TODO: TEST CODE + +} diff --git a/host/arduino/test/README b/host/arduino/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/host/arduino/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