Skip to content
Snippets Groups Projects
Verified Commit 70fc7c08 authored by Zoe Michaela Dietmar Pfister's avatar Zoe Michaela Dietmar Pfister :gay_pride_flag:
Browse files

Merge branch 'develop' into serial-comm-and-sd-write

parents 19dc4ebe 6558cf96
No related branches found
No related tags found
4 merge requests!39Merge Develop into Main,!19development into master,!17Inital Host, initial Client,!14Merge gsm host into development
#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
......@@ -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
#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);
}
.pio
CMakeListsPrivate.txt
cmake-build-*/
# !!! 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})
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
; 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
/*
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
}
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment