@ -32,6 +32,7 @@ ADC batterySensor(39);
//Adafruit_ADS1015 currentSensor2;
Adafruit_DPS310 pressureSensor ;
const int8_t speedSensorPin = 13 ;
const int8_t logButtonPin = 12 ;
const int8_t debugLedPin = 2 ;
const int8_t I2C_SDA = 15 ;
const int8_t I2C_SCL = 4 ;
@ -102,7 +103,15 @@ void IRAM_ATTR onSpeedSensorChange(bool newState)
}
}
volatile bool logButtonPressDetected = false ;
void IRAM_ATTR onLogButtonChange ( bool logButtonPressed )
{
if ( logButtonPressed )
logButtonPressDetected = true ;
}
void IRAM_ATTR onSpeedSensorChange ( ) { onSpeedSensorChange ( digitalRead ( speedSensorPin ) = = HIGH ) ; }
void IRAM_ATTR onLogButtonChange ( ) { onLogButtonChange ( digitalRead ( logButtonPin ) = = LOW ) ; }
float getSpeed ( )
{
@ -168,6 +177,9 @@ void setup()
pinMode ( speedSensorPin , INPUT_PULLUP ) ;
attachInterrupt ( speedSensorPin , & onSpeedSensorChange , CHANGE ) ;
pinMode ( logButtonPin , INPUT_PULLUP ) ;
attachInterrupt ( logButtonPin , & onLogButtonChange , CHANGE ) ;
Serial . begin ( 115200 ) ;
if ( ! SPIFFS . begin ( false ) ) {
@ -210,124 +222,6 @@ void setup()
WebServer . setInfoReceivedCallback ( onInfoReceived ) ;
WebServer . begin ( ) ;
/*server.on("/api/debug/log", HTTP_GET, [](AsyncWebServerRequest *request){
AsyncResponseStream * response = request - > beginResponseStream ( " text/plain " ) ;
int logPartIdx = 0 ;
while ( true )
{
const char * text = DebugLog . get ( logPartIdx ) ;
if ( text [ 0 ] = = 0 ) break ;
response - > print ( text ) ;
+ + logPartIdx ;
}
request - > send ( response ) ;
} ) ;
server . on ( " /api/status " , HTTP_GET , [ ] ( AsyncWebServerRequest * request ) {
int v = batteryVoltage ;
int c = batteryOutputCurrent ;
int s = ( int ) ( getSpeed ( ) * 1000.0f + 0.5f ) ;
int temp = temperature ;
int alt = altitude ;
int td = tripDistance ;
int ttt = tripTotalTime ;
int tmt = tripMovingTime ;
int tae = tripAscendingElevation / 100 ; // convert mm to dm
int tme = tripMotorEnergy / 360 ; // convert Joules to dWh (tenth of Wh)
const char * logFileName = DataLogger : : get ( ) . currentLogFileName ( ) ;
if ( String ( logFileName ) . startsWith ( " /log/ " ) ) logFileName + = 5 ;
int totalSize = ( int ) ( SPIFFS . totalBytes ( ) / 1000 ) ;
int usedSize = ( int ) ( SPIFFS . usedBytes ( ) / 1000 ) ;
char json [ 256 ] ;
sprintf ( json , " { \" v \" :%d, \" c \" :%d, \" s \" :%d, \" td \" :%d, \" ttt \" :%d, \" tmt \" :%d, \" tae \" :%d, \" tme \" :%d, \" temp \" :%d, \" alt \" :%d, \" log \" : \" %s \" , \" tot \" :%d, \" used \" :%d, \" lat \" :%.5f, \" lng \" :%.5f, \" d \" : \" %s \" } " , v , c , s , td , ttt , tmt , tae , tme , temp , alt , logFileName , totalSize , usedSize , latitude , longitude , realtime ) ;
request - > send ( 200 , " text/json " , json ) ;
} ) ;
server . on ( " /api/info " , HTTP_POST , [ ] ( AsyncWebServerRequest * request ) {
//DebugLog.println("/api/info");
AsyncWebParameter * latitudeParam = request - > getParam ( " lat " , true ) ;
AsyncWebParameter * longitudeParam = request - > getParam ( " lng " , true ) ;
AsyncWebParameter * altitudeParam = request - > getParam ( " alt " , true ) ;
if ( latitudeParam ! = nullptr & & longitudeParam ! = nullptr )
{
char * ending = nullptr ;
latitude = strtof ( latitudeParam - > value ( ) . c_str ( ) , & ending ) ;
if ( * ending ! = 0 )
latitude = - 1000.0f ;
longitude = strtof ( longitudeParam - > value ( ) . c_str ( ) , & ending ) ;
if ( * ending ! = 0 )
longitude = - 1000.0f ;
//DebugLog.print("lat="); DebugLog.print(latitude); DebugLog.print(" lng="); DebugLog.println(longitude);
if ( altitudeParam ! = nullptr )
{
gpsAltitude = strtof ( altitudeParam - > value ( ) . c_str ( ) , & ending ) ;
if ( * ending ! = 0 )
gpsAltitude = - 1000.0f ;
//DebugLog.print("alt="); DebugLog.println(gpsAltitude);
}
}
AsyncWebParameter * timeParam = request - > getParam ( " time " , true ) ;
if ( timeParam ! = nullptr )
{
strcpy ( realtime , timeParam - > value ( ) . c_str ( ) ) ;
//DebugLog.print("time="); DebugLog.println(realtime);
}
request - > send ( 200 ) ;
} ) ;
server . on ( " /api/log/list " , HTTP_GET , [ ] ( AsyncWebServerRequest * request ) {
String json ;
json = " { \" files \" :[ " ;
auto logFolder = SPIFFS . open ( " /log " ) ;
auto file = logFolder . openNextFile ( ) ;
bool first = true ;
while ( file )
{
if ( ! first ) json + = " , " ;
json + = " { \" n \" : \" /api " ;
json + = file . name ( ) ;
json + = " \" , \" s \" : " ;
json + = file . size ( ) ;
json + = " } " ;
first = false ;
file = logFolder . openNextFile ( ) ;
}
json + = " ]} " ;
request - > send ( 200 , " text/json " , json . c_str ( ) ) ;
} ) ;
// Special case to send index.html without caching
server . on ( " / " , HTTP_GET , [ ] ( AsyncWebServerRequest * request ) { request - > send ( SPIFFS , " /www/index.html " , " text/html " ) ; } ) ;
server . serveStatic ( " /index.html " , SPIFFS , " /www/index.html " ) ;
server . on ( " /manifest.webmanifest " , HTTP_GET , [ ] ( AsyncWebServerRequest * request ) {
AsyncWebServerResponse * response = request - > beginResponse ( SPIFFS , " /www/manifest.webmanifest " , " application/manifest+json; charset=utf-8 " ) ;
//response->addHeader("Content-Disposition", "inline");
request - > send ( response ) ;
} ) ;
// Log files (not cached)
server . serveStatic ( " /api/log " , SPIFFS , " /log/ " ) ;
// Other static files are cached (index.html knows whether to ignore caching or not for each file)
server . serveStatic ( " / " , SPIFFS , " /www/ " ) . setCacheControl ( " max-age=5184000 " ) ;
server . begin ( ) ;
DebugLog . println ( " HTTP server started " ) ; */
digitalWrite ( debugLedPin , LOW ) ;
}
@ -479,17 +373,25 @@ void loop()
unsigned long dt = utils : : elapsed ( lastLoopMillis , now ) ;
lastLoopMillis = now ;
bool logButtonPressed = logButtonPressDetected | | ( digitalRead ( logButtonPin ) = = LOW ) ;
logButtonPressDetected = false ;
digitalWrite ( debugLedPin , logButtonPressed ) ;
static DataLogger : : Entry entry ;
entry . batteryVoltage = ( float ) status . batteryVoltage / 1000.0f ;
entry . batteryOutputCurrent = ( float ) status . batteryOutputCurrent / 1000.0f ;
entry . cumulatedDistance = speedSensorDistance ;
entry . speed = getSpeed ( ) ;
entry . temperature = ( float ) status . temperature / 10.0f ;
entry . altitude = ( float ) status . altitude / 1000.0f ;
entry . latitude = status . latitude ;
entry . longitude = status . longitude ;
entry . logButtonPressed = logButtonPressed ;
bool isMoving = entry . speed > 0.0f ;
bool needLogging = isMoving | | logButtonPressed ;
if ( entry . speed > 0.0f )
if ( needLogging )
{
stoppedSince = - 1 ;
if ( ! DataLogger : : get ( ) . isOpen ( ) )
@ -521,7 +423,6 @@ void loop()
DataLogger : : get ( ) . log ( now , entry ) ;
bool isOnTrip = DataLogger : : get ( ) . isOpen ( ) ;
bool isMoving = entry . speed > 0.0f ;
static unsigned long cumulatedMillis = 0 ;
cumulatedMillis + = dt ;