@ -18,7 +18,8 @@
# include <limits>
# include <limits>
# define DUMMY_DATA 1
# define DUMMY_DATA 0
# define ENABLE_GPS_COORDINATES 1
AsyncWebServer server ( 80 ) ;
AsyncWebServer server ( 80 ) ;
@ -39,6 +40,10 @@ uint16_t batteryVoltage = 0; // in mV
uint16_t batteryOutputCurrent = 0 ; // in mV
uint16_t batteryOutputCurrent = 0 ; // in mV
int16_t temperature = 0 ; // in tenth of °C
int16_t temperature = 0 ; // in tenth of °C
int32_t altitude = 0 ; // in mm above sea level (can be negative if below sea level, or depending on atmospheric conditions)
int32_t altitude = 0 ; // in mm above sea level (can be negative if below sea level, or depending on atmospheric conditions)
float latitude = - 1000.0f ; // in decimal degrees
float longitude = - 1000.0f ; // in decimal degrees
float gpsAltitude = - 1000.0f ; // in meters, above sea level
char realtime [ 32 ] = { 0 } ; // UTC date and time, in format YYYY-MM-DDTHH:mm:ss.sssZ
// current trip
// current trip
uint32_t tripDistance = 0 ; // in meters
uint32_t tripDistance = 0 ; // in meters
@ -191,6 +196,10 @@ void setup()
OTA . begin ( ) ;
OTA . begin ( ) ;
# if ENABLE_GPS_COORDINATES
DataLogger : : get ( ) . enableGPSCoordinates ( true ) ;
# endif
Wire . begin ( I2C_SDA , I2C_SCL ) ;
Wire . begin ( I2C_SDA , I2C_SCL ) ;
pressureSensor . begin ( Wire ) ;
pressureSensor . begin ( Wire ) ;
@ -230,10 +239,54 @@ void setup()
int usedSize = ( int ) ( SPIFFS . usedBytes ( ) / 1000 ) ;
int usedSize = ( int ) ( SPIFFS . usedBytes ( ) / 1000 ) ;
char json [ 256 ] ;
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} " , v , c , s , td , ttt , tmt , tae , tme , temp , alt , logFileName , totalSize , usedSize ) ;
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 , realtim e ) ;
request - > send ( 200 , " text/json " , json ) ;
request - > send ( 200 , " text/json " , json ) ;
} ) ;
} ) ;
server . on ( " /api/info " , HTTP_POST , [ ] ( AsyncWebServerRequest * request ) {
//DebugLog.println("/api/info");
/*int params = request->params();
for ( int i = 0 ; i < params ; i + + ) {
AsyncWebParameter * p = request - > getParam ( i ) ;
DebugLog . print ( p - > name ( ) . c_str ( ) ) ;
DebugLog . print ( " = " ) ;
DebugLog . println ( p - > value ( ) . c_str ( ) ) ;
} */
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 ) {
server . on ( " /api/log/list " , HTTP_GET , [ ] ( AsyncWebServerRequest * request ) {
String json ;
String json ;
@ -435,13 +488,16 @@ void loop()
entry . temperature = ( float ) temperature / 10.0f ;
entry . temperature = ( float ) temperature / 10.0f ;
entry . altitude = ( float ) altitude / 1000.0f ;
entry . altitude = ( float ) altitude / 1000.0f ;
entry . latitude = latitude ;
entry . longitude = longitude ;
if ( entry . speed > 0.0f )
if ( entry . speed > 0.0f )
{
{
stoppedSince = - 1 ;
stoppedSince = - 1 ;
if ( ! DataLogger : : get ( ) . isOpen ( ) )
if ( ! DataLogger : : get ( ) . isOpen ( ) )
{
{
DebugLog . println ( " Starting DataLogger " ) ;
DebugLog . println ( " Starting DataLogger " ) ;
DataLogger : : get ( ) . open ( ) ;
DataLogger : : get ( ) . open ( realtime ) ;
tripDistance = 0 ;
tripDistance = 0 ;
tripMovingTime = 0 ;
tripMovingTime = 0 ;
tripTotalTime = 0 ;
tripTotalTime = 0 ;
@ -486,8 +542,16 @@ void loop()
uint32_t altitudeMillimeters = ( uint32_t ) ( entry . altitude * 1000.0f + 0.5f ) ;
uint32_t altitudeMillimeters = ( uint32_t ) ( entry . altitude * 1000.0f + 0.5f ) ;
static uint32_t lastLoopAltitude = altitudeMillimeters ;
static uint32_t lastLoopAltitude = altitudeMillimeters ;
uint32_t altitudeChange = altitudeMillimeters - lastLoopAltitude ;
uint32_t clampedPositiveAltitudeChange = 0 ;
lastLoopAltitude = altitudeMillimeters ;
if ( altitudeMillimeters > lastLoopAltitude + 300 )
{
clampedPositiveAltitudeChange = altitudeMillimeters - lastLoopAltitude ;
lastLoopAltitude = altitudeMillimeters ;
}
else if ( lastLoopAltitude > 300 & & altitudeMillimeters < lastLoopAltitude - 300 )
{
lastLoopAltitude = altitudeMillimeters ;
}
if ( isOnTrip )
if ( isOnTrip )
{
{
@ -495,8 +559,7 @@ void loop()
if ( isMoving ) tripMovingTime + = newSeconds ;
if ( isMoving ) tripMovingTime + = newSeconds ;
tripDistance + = newMeters ;
tripDistance + = newMeters ;
if ( altitudeChange > 0 )
tripAscendingElevation + = clampedPositiveAltitudeChange ;
tripAscendingElevation + = altitudeChange ;
static float remainingEnergy = 0.0f ;
static float remainingEnergy = 0.0f ;
float newEnergy = entry . batteryVoltage * entry . batteryOutputCurrent * ( ( float ) dt / 1000.0f ) + remainingEnergy ;
float newEnergy = entry . batteryVoltage * entry . batteryOutputCurrent * ( ( float ) dt / 1000.0f ) + remainingEnergy ;