Skip to content

Commit

Permalink
Merge logging to FFat to sensorState class. (#16)
Browse files Browse the repository at this point in the history
* Started merge of logging to sensorState class.

* Start sensors after setting the time with SNTP.

Some cleanup.

* Remove unused variables.

Save reset reason to file.

Fixed ntpTask stack overflow.

* Fixed getting SNTP sync.

logLineToFile() and resetString() are native again.

* Cleaned up.

* Only format FATFS on first run.

* Fixed fscking script problem. Took a long time to debug.

* Fix last commit.

* Log reset reason to FFat.

* Formatting.
  • Loading branch information
CelliesProjects authored Jul 13, 2019
1 parent 1016ca8 commit bbad87d
Show file tree
Hide file tree
Showing 15 changed files with 1,498 additions and 2,070 deletions.
130 changes: 0 additions & 130 deletions Task.cpp

This file was deleted.

67 changes: 0 additions & 67 deletions Task.h

This file was deleted.

95 changes: 67 additions & 28 deletions aquacontrol32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
#include <AsyncTCP.h> /* Reports as 1.0.3 https://github.com/me-no-dev/AsyncTCP */
#include <ESPAsyncWebServer.h> /* Reports as 1.2.2 https://github.com/me-no-dev/ESPAsyncWebServer */
#include <MoonPhase.h> /* https://github.com/CelliesProjects/MoonPhase */

#include "sensorState.h"
#include <sensorState.h> /* https://github.com/CelliesProjects/sensorState */
#include "ledState.h"

#include "deviceSetup.h"
#include "devicePinSetup.h"
#include "mapFloat.h"

#if GIT_TAG
#include "gitTagVersion.h"
Expand Down Expand Up @@ -144,7 +142,6 @@ const uint8_t tftTaskPriority = 6;
const uint8_t ntpTaskPriority = 5;
const uint8_t oledTaskPriority = 4;
const uint8_t wifiTaskPriority = 3;
const uint8_t loggerTaskPriority = 2;
const uint8_t webserverTaskPriority = 1;
const uint8_t moonSimtaskPriority = 0;

Expand All @@ -158,7 +155,6 @@ MoonPhase::moonData moonData;
TaskHandle_t xDimmerTaskHandle = NULL;
TaskHandle_t xTftTaskHandle = NULL;
TaskHandle_t xOledTaskHandle = NULL;
TaskHandle_t xLoggerTaskHandle = NULL;

//Boot time is saved
timeval systemStart;
Expand All @@ -178,21 +174,63 @@ uint8_t oledOrientation = OLED_ORIENTATION_NORMAL;
/*****************************************************************************************
end of global variables
*****************************************************************************************/

/* forward declarations */
void tftTask( void * pvParameters );
void oledTask( void * pvParameters );
void wifiTask( void * pvParameters );
void loggerTask( void * pvParameters );

BaseType_t startLogger()
/* global functions */
float mapFloat( const float &x, const float &in_min, const float &in_max, const float &out_min, const float &out_max) {
return ( x - in_min ) * ( out_max - out_min ) / ( in_max - in_min ) + out_min;
}

bool logLineToFile( fs::FS &fs, const char * path, const char * message ) {
File file = fs.open( path, FILE_APPEND );
if ( !file ) return false;

if ( !file.println( message ) ) {
file.close();
return false;
}
file.close();
return true;
}

const char * resetString( const uint8_t core ) {
const char * resetStr[] =
{
"",
"POWERON_RESET",
"",
"SW_RESET",
"OWDT_RESET",
"DEEPSLEEP_RESET",
"SDIO_RESET",
"TG0WDT_SYS_RESET",
"TG1WDT_SYS_RESET",
"RTCWDT_SYS_RESET",
"INTRUSION_RESET",
"TGWDT_CPU_RESET",
"SW_CPU_RESET",
"RTCWDT_CPU_RESET",
"EXT_CPU_RESET",
"RTCWDT_BROWN_OUT_RESET",
"RTCWDT_RTC_RESET"
};
return resetStr[rtc_get_reset_reason( core )];
}

void threeDigitPercentage( char *buffer, const uint8_t &bufferSize, const float &percentage, const bool &addPercentSign )
{
return xTaskCreatePinnedToCore(
loggerTask, /* Function to implement the task */
"loggerTask", /* Name of the task */
3000, /* Stack size in words */
NULL, /* Task input parameter */
loggerTaskPriority, /* Priority of the task */
&xLoggerTaskHandle, /* Task handle. */
1);
if ( percentage < 0.005 )
snprintf( buffer, bufferSize, addPercentSign ? " 0%% " : " 0 " );
else if ( percentage > 99.9 )
snprintf( buffer, bufferSize, addPercentSign ? " 100%% " : " 100 " );
else if ( percentage < 10 )
snprintf( buffer, bufferSize , addPercentSign ? " %1.2f%% " : " %1.2f ", percentage );
else
snprintf( buffer, bufferSize , addPercentSign ? " %2.1f%% " : " %2.1f ", percentage );
}

void setup()
Expand Down Expand Up @@ -220,24 +258,25 @@ void setup()
ESP_LOGI( TAG, "aquacontrol32 %s", sketchVersion );
ESP_LOGI( TAG, "ESP32 SDK: %s", ESP.getSdkVersion() );

sensor.startSensors();

preferences.begin( "aquacontrol32", false );

SPI.begin( SPI_SCK_PIN, SPI_MISO_PIN, SPI_MOSI_PIN );
//check if a token in NVS is set...
if ( !preferences.getString("firstrun", "true" ).equals( "false" ) ) {
ESP_LOGI( TAG, "Formatting FFat..." );
char label[5] = "ffat";
FFat.format( true, label );
preferences.putString( "firstrun", "false" ); //set token
}

tft.begin( TFT_SPI_CLOCK );
if ( FFat.begin() ) {
ESP_LOGI( TAG, "FFat mounted." );
} else {
ESP_LOGI( TAG, "Could not mount FFat." );
}

ESP_LOGI( TAG, "Starting FFat. (format on fail)" );
SPI.begin( SPI_SCK_PIN, SPI_MISO_PIN, SPI_MOSI_PIN );

if ( !FFat.begin( true ) )
{
ESP_LOGE( TAG, "FATAL ERROR! Could not mount FFat. Did you select the right partition scheme? (something with ffat)" );
}
else
{
ESP_LOGI( TAG, "FFat started. Total space: %lu kB. Free space: %lu kB.", FFat.totalBytes() / 1024, FFat.freeBytes() / 1024 );
}
tft.begin( TFT_SPI_CLOCK );

if ( TFT_HAS_NO_MISO || tft.readcommand8( ILI9341_RDSELFDIAG ) == 0xE0 )
{
Expand Down
2 changes: 1 addition & 1 deletion compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ else
fi
echo "const char * sketchVersion = \"$currentVersion\";" > gitTagVersion.h
echo -e Compiling aquacontrol32 version: '\e[36m'$currentVersion'\e[0m'
~/arduino-1.8.9/arduino --board espressif:esp32:mhetesp32minikit --pref custom_DebugLevel=esp32_none --port /dev/ttyUSB0 --pref upload.speed=921600 --preserve-temp-files --pref build.path=temp --pref build.partition=default_ffat --pref build.flash_freq=80m -v --verify aquacontrol32.ino | grep -e 'Using core' -e 'Using board' -e ' uses ' -e 'Using library' -e 'Compiling' -e 'Global'
~/arduino-1.8.9/arduino --board espressif:esp32:mhetesp32minikit --pref custom_DebugLevel=esp32_none --port /dev/ttyUSB0 --pref upload.speed=921600 --preserve-temp-files --pref build.path=temp --pref build.partitions=default_ffat --pref build.flash_freq=80m -v --verify aquacontrol32.ino | grep -e 'Using core' -e 'Using board' -e ' uses ' -e 'Using library' -e 'Compiling' -e 'Global'
rm gitTagVersion.h
2 changes: 1 addition & 1 deletion flash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ else
fi
echo "const char * sketchVersion = \"$currentVersion\";" > gitTagVersion.h
echo -e Compiling and flashing aquacontrol32 version: '\e[36m'$currentVersion'\e[0m'
~/arduino-1.8.9/arduino --board espressif:esp32:mhetesp32minikit --pref custom_DebugLevel=esp32_none --port /dev/ttyUSB0 --pref upload.speed=921600 --preserve-temp-files --pref build.path=temp --pref build.partition=default_ffat --pref build.flash_freq=80m -v --upload aquacontrol32.ino | grep -e 'Using core' -e 'Using board' -e ' uses ' -e 'Using library' -e 'Compiling' -e 'Global'
~/arduino-1.8.9/arduino --board espressif:esp32:mhetesp32minikit --pref custom_DebugLevel=esp32_none --port /dev/ttyUSB0 --pref upload.speed=921600 --preserve-temp-files --pref build.path=temp --pref build.partitions=default_ffat --pref build.flash_freq=80m -v --upload aquacontrol32.ino | grep -e 'Using core' -e 'Using board' -e ' uses ' -e 'Using library' -e 'Compiling' -e 'Global'
rm gitTagVersion.h
Loading

0 comments on commit bbad87d

Please sign in to comment.