Added sleepinterval + predef SSID

This commit is contained in:
Magnus 2021-04-03 22:22:18 +02:00
parent 87784ee609
commit 374341fede
18 changed files with 89 additions and 37 deletions

View File

@ -10,7 +10,6 @@ I started this project out of curiosity for how a motion sensor is working and s
* Add support for Blynk as endpoint * Add support for Blynk as endpoint
* Add support for https connections (push) * Add support for https connections (push)
* Add support for https web server (will require certificates to be created as part of build process) * Add support for https web server (will require certificates to be created as part of build process)
* Add option to enter SSID/Password as part of build proccess (since WifiManager does not support secure connections)
* Add iSpindle 3D print cradle + small PCB (what I use for my builds) * Add iSpindle 3D print cradle + small PCB (what I use for my builds)
# Functionallity # Functionallity
@ -124,3 +123,9 @@ Not yet complete.
I recommend that VSCODE with PlatformIO and Minfy extensions are used. Minify is used to reduce the size of the HTML files which are embedded into the firmware or uploaded to the file system. When using minify on a file, for example index.htm the output will be called index.min.htm. This is the file that will be used when buildning the image. I recommend that VSCODE with PlatformIO and Minfy extensions are used. Minify is used to reduce the size of the HTML files which are embedded into the firmware or uploaded to the file system. When using minify on a file, for example index.htm the output will be called index.min.htm. This is the file that will be used when buildning the image.
By default the html files are embedded but there are options to upload them to the file system to reduce the size of the image if the size becomes to large for OTA. By default the html files are embedded but there are options to upload them to the file system to reduce the size of the image if the size becomes to large for OTA.
You can set the SSID and PWD as presets through platformio.ini by adding the settings to the following definitions:
```
-D USER_SSID=\""\"" // =\""myssid\""
-D USER_SSID_PWD=\""\"" // =\""mypwd\""
```

File diff suppressed because one or more lines are too long

View File

@ -14,4 +14,4 @@
<button type="button" class="btn btn-warning" id="wifi-reset-btn">Factory default settings</button> <button type="button" class="btn btn-warning" id="wifi-reset-btn">Factory default settings</button>
</div> </div>
</div> </div>
--><hr class="my-4"></div><script type="text/javascript">function getConfig(){var n="/api/device";$("#spinner").show(),$.getJSON(n,function(n){console.log(n),$("#app-ver").text(n["app-ver"]+" (html 0.3.0)"),$("#mdns").text(n.mdns),$("#id").text(n.id)}).fail(function(){showError("Unable to get data from the device.")}).always(function(){$("#spinner").hide()})}window.onload=getConfig</script><!-- START FOOTER --><div class="container-fluid themed-container bg-primary text-light">(C) Copyright 2021 Magnus Persson</div></body></html> --><hr class="my-4"></div><script type="text/javascript">function getConfig(){var n="/api/device";$("#spinner").show(),$.getJSON(n,function(n){console.log(n),$("#app-ver").text(n["app-ver"]+" (html 0.3.2)"),$("#mdns").text(n.mdns),$("#id").text(n.id)}).fail(function(){showError("Unable to get data from the device.")}).always(function(){$("#spinner").hide()})}window.onload=getConfig</script><!-- START FOOTER --><div class="container-fluid themed-container bg-primary text-light">(C) Copyright 2021 Magnus Persson</div></body></html>

Binary file not shown.

Binary file not shown.

View File

@ -1 +1 @@
{ "project":"gravmon", "version":"0.3.1", "html": [ "index.min.htm", "device.min.htm", "config.min.htm", "about.min.htm" ] } { "project":"gravmon", "version":"0.3.2", "html": [ "index.min.htm", "device.min.htm", "config.min.htm", "about.min.htm" ] }

View File

@ -105,6 +105,13 @@
</div> </div>
</div> </div>
</fieldset> </fieldset>
<div class="form-group row">
<label for="sleep-interval" class="col-sm-4 col-form-label">Interval (seconds):</label>
<div class="col-sm-4">
<input type="number" min="10" max="10000" class="form-control" name="sleep-interval" id="sleep-interval">
</div>
<label for="sleep-interval" class="col-sm-4 col-form-label" id="sleep-interval-info"></label>
</div>
<div class="form-group row"> <div class="form-group row">
<div class="col-sm-8 offset-sm-4"> <div class="col-sm-8 offset-sm-4">
<button type="submit" class="btn btn-primary" id="device-btn">Save</button> <button type="submit" class="btn btn-primary" id="device-btn">Save</button>
@ -155,13 +162,6 @@
<input type="url" maxlength="90" class="form-control" name="brewfather-push" id="brewfather-push"> <input type="url" maxlength="90" class="form-control" name="brewfather-push" id="brewfather-push">
</div> </div>
</div> </div>
<div class="form-group row">
<label for="push-interval" class="col-sm-4 col-form-label">Interval (seconds):</label>
<div class="col-sm-4">
<input type="number" min="10" max="10000" class="form-control" name="push-interval" id="push-interval">
</div>
<label for="push-interval" class="col-sm-4 col-form-label" id="push-interval-info"></label>
</div>
<div class="form-group row"> <div class="form-group row">
<div class="col-sm-8 offset-sm-4"> <div class="col-sm-8 offset-sm-4">
<button type="submit" class="btn btn-primary" id="push-btn">Save</button> <button type="submit" class="btn btn-primary" id="push-btn">Save</button>
@ -281,13 +281,13 @@
} ); } );
}); });
function updatePushInfo() { function updateSleepInfo() {
var i = $("#push-interval").val() var i = $("#sleep-interval").val()
$("#push-interval-info").text( Math.floor(i/60) + " m " + (i%60) + " s" ) $("#sleep-interval-info").text( Math.floor(i/60) + " m " + (i%60) + " s" )
} }
// Trigger the calibration // Trigger the calibration
$("#push-interval").keyup(updatePushInfo); $("#sleep-interval").keyup(updateSleepInfo);
function setButtonDisabled( b ) { function setButtonDisabled( b ) {
$("#device-btn").prop("disabled", b); $("#device-btn").prop("disabled", b);
@ -302,7 +302,7 @@
setButtonDisabled( true ); setButtonDisabled( true );
var url = "/api/config"; var url = "/api/config";
var url = "/test/config.json"; //var url = "/test/config.json";
$('#spinner').show(); $('#spinner').show();
$.getJSON(url, function (cfg) { $.getJSON(url, function (cfg) {
console.log( cfg ); console.log( cfg );
@ -319,7 +319,7 @@
$("#http-push").val(cfg["http-push"]); $("#http-push").val(cfg["http-push"]);
$("#http-push2").val(cfg["http-push2"]); $("#http-push2").val(cfg["http-push2"]);
$("#brewfather-push").val(cfg["brewfather-push"]); $("#brewfather-push").val(cfg["brewfather-push"]);
$("#push-interval").val(cfg["push-interval"]); $("#sleep-interval").val(cfg["sleep-interval"]);
$("#voltage-factor").val(cfg["voltage-factor"]); $("#voltage-factor").val(cfg["voltage-factor"]);
$("#gravity-formula").val(cfg["gravity-formula"]); $("#gravity-formula").val(cfg["gravity-formula"]);
$("#temp-adjustment-value").val(cfg["temp-adjustment-value"]); $("#temp-adjustment-value").val(cfg["temp-adjustment-value"]);
@ -335,7 +335,7 @@
.always(function() { .always(function() {
$('#spinner').hide(); $('#spinner').hide();
setButtonDisabled( false ); setButtonDisabled( false );
updatePushInfo(); updateSleepInfo();
}); });
} }
</script> </script>

File diff suppressed because one or more lines are too long

View File

@ -25,6 +25,8 @@ build_flags = #-O0 -Wl,-Map,output.map
#-D SKIP_SLEEPMODE #-D SKIP_SLEEPMODE
-D USE_LITTLEFS=true -D USE_LITTLEFS=true
-D EMBED_HTML -D EMBED_HTML
-D USER_SSID=\""\"" # =\""myssid\""
-D USER_SSID_PWD=\""\"" # =\""mypwd\""
-D CFG_APPVER="\"0.3.2\"" -D CFG_APPVER="\"0.3.2\""
lib_deps = lib_deps =
# https://github.com/jrowberg/i2cdevlib.git # Using local copy of this library # https://github.com/jrowberg/i2cdevlib.git # Using local copy of this library

View File

@ -38,7 +38,7 @@ Config::Config() {
sprintf(&buf[0], "" WIFI_MDNS "%s", getID() ); sprintf(&buf[0], "" WIFI_MDNS "%s", getID() );
mDNS = &buf[0]; mDNS = &buf[0];
setTempFormat('C'); setTempFormat('C');
setPushInterval(900); // 15 minutes setSleepInterval(900); // 15 minutes
setVoltageFactor(1.59); // Conversion factor for battery setVoltageFactor(1.59); // Conversion factor for battery
setTempSensorAdj(0.0); setTempSensorAdj(0.0);
setGravityTempAdj(false); setGravityTempAdj(false);
@ -57,7 +57,8 @@ void Config::createJson(DynamicJsonDocument& doc) {
doc[ CFG_PARAM_PUSH_BREWFATHER ] = getBrewfatherPushTarget(); doc[ CFG_PARAM_PUSH_BREWFATHER ] = getBrewfatherPushTarget();
doc[ CFG_PARAM_PUSH_HTTP ] = getHttpPushTarget(); doc[ CFG_PARAM_PUSH_HTTP ] = getHttpPushTarget();
doc[ CFG_PARAM_PUSH_HTTP2 ] = getHttpPushTarget2(); doc[ CFG_PARAM_PUSH_HTTP2 ] = getHttpPushTarget2();
doc[ CFG_PARAM_PUSH_INTERVAL ] = getPushInterval(); doc[ CFG_PARAM_SLEEP_INTERVAL ] = getSleepInterval();
doc[ CFG_PARAM_PUSH_INTERVAL ] = getSleepInterval(); // TODO: @deprecated
doc[ CFG_PARAM_VOLTAGEFACTOR ] = getVoltageFactor(); doc[ CFG_PARAM_VOLTAGEFACTOR ] = getVoltageFactor();
doc[ CFG_PARAM_GRAVITY_FORMULA ] = getGravityFormula(); doc[ CFG_PARAM_GRAVITY_FORMULA ] = getGravityFormula();
doc[ CFG_PARAM_TEMP_ADJ ] = getTempSensorAdj(); doc[ CFG_PARAM_TEMP_ADJ ] = getTempSensorAdj();
@ -160,8 +161,10 @@ bool Config::loadFile() {
setHttpPushTarget( doc[ CFG_PARAM_PUSH_HTTP ] ); setHttpPushTarget( doc[ CFG_PARAM_PUSH_HTTP ] );
if( !doc[ CFG_PARAM_PUSH_HTTP2 ].isNull() ) if( !doc[ CFG_PARAM_PUSH_HTTP2 ].isNull() )
setHttpPushTarget2( doc[ CFG_PARAM_PUSH_HTTP2 ] ); setHttpPushTarget2( doc[ CFG_PARAM_PUSH_HTTP2 ] );
if( !doc[ CFG_PARAM_PUSH_INTERVAL ].isNull() ) if( !doc[ CFG_PARAM_SLEEP_INTERVAL ].isNull() )
setPushInterval( doc[ CFG_PARAM_PUSH_INTERVAL ].as<int>() ); setSleepInterval( doc[ CFG_PARAM_SLEEP_INTERVAL ].as<int>() );
if( !doc[ CFG_PARAM_PUSH_INTERVAL ].isNull() ) // TODO: @deprecated
setSleepInterval( doc[ CFG_PARAM_PUSH_INTERVAL ].as<int>() ); // TODO: @deprecated
if( !doc[ CFG_PARAM_VOLTAGEFACTOR ].isNull() ) if( !doc[ CFG_PARAM_VOLTAGEFACTOR ].isNull() )
setVoltageFactor( doc[ CFG_PARAM_VOLTAGEFACTOR ].as<float>() ); setVoltageFactor( doc[ CFG_PARAM_VOLTAGEFACTOR ].as<float>() );
if( !doc[ CFG_PARAM_GRAVITY_FORMULA ].isNull() ) if( !doc[ CFG_PARAM_GRAVITY_FORMULA ].isNull() )
@ -230,7 +233,7 @@ void Config::debug() {
Log.verbose(F("CFG : Push brewfather; '%s'." CR), getBrewfatherPushTarget() ); Log.verbose(F("CFG : Push brewfather; '%s'." CR), getBrewfatherPushTarget() );
Log.verbose(F("CFG : Push http; '%s'." CR), getHttpPushTarget() ); Log.verbose(F("CFG : Push http; '%s'." CR), getHttpPushTarget() );
Log.verbose(F("CFG : Push http2; '%s'." CR), getHttpPushTarget2() ); Log.verbose(F("CFG : Push http2; '%s'." CR), getHttpPushTarget2() );
Log.verbose(F("CFG : Push interval; %d." CR), getPushInterval() ); Log.verbose(F("CFG : Sleep interval; %d." CR), getSleepInterval() );
// Log.verbose(F("CFG : Accel offset\t%d\t%d\t%d" CR), gyroCalibration.ax, gyroCalibration.ay, gyroCalibration.az ); // Log.verbose(F("CFG : Accel offset\t%d\t%d\t%d" CR), gyroCalibration.ax, gyroCalibration.ay, gyroCalibration.az );
// Log.verbose(F("CFG : Gyro offset \t%d\t%d\t%d" CR), gyroCalibration.gx, gyroCalibration.gy, gyroCalibration.gz ); // Log.verbose(F("CFG : Gyro offset \t%d\t%d\t%d" CR), gyroCalibration.gx, gyroCalibration.gy, gyroCalibration.gz );
#endif #endif

View File

@ -48,6 +48,8 @@ SOFTWARE.
#define CFG_PARAM_PUSH_BREWFATHER "brewfather-push" // URL (brewfather format) #define CFG_PARAM_PUSH_BREWFATHER "brewfather-push" // URL (brewfather format)
#define CFG_PARAM_PUSH_HTTP "http-push" // URL (iSpindle format) #define CFG_PARAM_PUSH_HTTP "http-push" // URL (iSpindle format)
#define CFG_PARAM_PUSH_HTTP2 "http-push2" // URL (iSpindle format) #define CFG_PARAM_PUSH_HTTP2 "http-push2" // URL (iSpindle format)
#define CFG_PARAM_SLEEP_INTERVAL "sleep-interval" // Sleep interval
// TODO: @deprecated setting
#define CFG_PARAM_PUSH_INTERVAL "push-interval" // Time between push #define CFG_PARAM_PUSH_INTERVAL "push-interval" // Time between push
#define CFG_PARAM_TEMPFORMAT "temp-format" // C or F #define CFG_PARAM_TEMPFORMAT "temp-format" // C or F
#define CFG_PARAM_VOLTAGEFACTOR "voltage-factor" // Factor to calculate the battery voltage #define CFG_PARAM_VOLTAGEFACTOR "voltage-factor" // Factor to calculate the battery voltage
@ -92,12 +94,12 @@ class Config {
char tempFormat; // C, F char tempFormat; // C, F
float voltageFactor; float voltageFactor;
float tempSensorAdj; // This value will be added to the read sensor value float tempSensorAdj; // This value will be added to the read sensor value
int sleepInterval;
// Push target settings // Push target settings
String brewfatherPushTarget; String brewfatherPushTarget;
String httpPushTarget; String httpPushTarget;
String httpPushTarget2; String httpPushTarget2;
int pushInterval;
// Gravity and temperature calculations // Gravity and temperature calculations
String gravityFormula; String gravityFormula;
@ -133,9 +135,9 @@ class Config {
void setHttpPushTarget2( String s ) { httpPushTarget2 = s; saveNeeded = true; } void setHttpPushTarget2( String s ) { httpPushTarget2 = s; saveNeeded = true; }
bool isHttpActive2() { return httpPushTarget2.length()>0?true:false; } bool isHttpActive2() { return httpPushTarget2.length()>0?true:false; }
int getPushInterval() { return pushInterval; } int getSleepInterval() { return sleepInterval; }
void setPushInterval( int v ) { pushInterval = v; saveNeeded = true; } void setSleepInterval( int v ) { sleepInterval = v; saveNeeded = true; }
void setPushInterval( String s ) { pushInterval = s.toInt(); saveNeeded = true; } void setSleepInterval( String s ) { sleepInterval = s.toInt(); saveNeeded = true; }
char getTempFormat() { return tempFormat; } char getTempFormat() { return tempFormat; }
void setTempFormat( char c ) { tempFormat = c; saveNeeded = true; } void setTempFormat( char c ) { tempFormat = c; saveNeeded = true; }

View File

@ -187,12 +187,12 @@ void loop() {
unsigned long runTime = millis() - startMillis; unsigned long runTime = millis() - startMillis;
// Enter sleep mode... // Enter sleep mode...
Log.notice(F("MAIN: Entering deep sleep for %d s, run time %l s, battery=%F V." CR), myConfig.getPushInterval(), runTime/1000, volt ); Log.notice(F("MAIN: Entering deep sleep for %d s, run time %l s, battery=%F V." CR), myConfig.getSleepInterval(), runTime/1000, volt );
LittleFS.end(); LittleFS.end();
myGyro.enterSleep(); myGyro.enterSleep();
drd->stop(); drd->stop();
delay(100); delay(100);
deepSleep( myConfig.getPushInterval() ); deepSleep( myConfig.getSleepInterval() );
} }
#if LOG_LEVEL==6 #if LOG_LEVEL==6

View File

@ -31,7 +31,7 @@ PushTarget myPushTarget;
// //
void PushTarget::send(float angle, float gravity, float temp, float runTime, bool force ) { void PushTarget::send(float angle, float gravity, float temp, float runTime, bool force ) {
unsigned long timePassed = abs( millis() - ms ); unsigned long timePassed = abs( millis() - ms );
unsigned long interval = myConfig.getPushInterval()*1000; unsigned long interval = myConfig.getSleepInterval()*1000;
if( ( timePassed < interval ) && !force) { if( ( timePassed < interval ) && !force) {
#if LOG_LEVEL==6 #if LOG_LEVEL==6
@ -133,7 +133,7 @@ void PushTarget::sendHttp( String serverPath, float angle, float gravity, float
doc["name"] = myConfig.getMDNS(); doc["name"] = myConfig.getMDNS();
doc["ID"] = myConfig.getID(); doc["ID"] = myConfig.getID();
doc["token"] = "gravmon"; doc["token"] = "gravmon";
doc["interval"] = myConfig.getPushInterval(); doc["interval"] = myConfig.getSleepInterval();
doc["temperature"] = reduceFloatPrecision( temp, 1 ); doc["temperature"] = reduceFloatPrecision( temp, 1 );
doc["temp-units"] = String( myConfig.getTempFormat() ); doc["temp-units"] = String( myConfig.getTempFormat() );
doc["gravity"] = reduceFloatPrecision( gravity, 4 ); doc["gravity"] = reduceFloatPrecision( gravity, 4 );

View File

@ -36,6 +36,7 @@ INCBIN(AboutHtm, "data/about.min.htm" );
// Minium web interface for uploading htm files // Minium web interface for uploading htm files
INCBIN(UploadHtm, "data/upload.min.htm" ); INCBIN(UploadHtm, "data/upload.min.htm" );
#endif #endif
// EOF // EOF

View File

@ -292,6 +292,7 @@ void webHandleConfigDevice() {
#endif #endif
myConfig.setMDNS( server.arg( CFG_PARAM_MDNS ).c_str() ); myConfig.setMDNS( server.arg( CFG_PARAM_MDNS ).c_str() );
myConfig.setTempFormat( server.arg( CFG_PARAM_TEMPFORMAT ).charAt(0) ); myConfig.setTempFormat( server.arg( CFG_PARAM_TEMPFORMAT ).charAt(0) );
myConfig.setSleepInterval( server.arg( CFG_PARAM_SLEEP_INTERVAL ).c_str() );
myConfig.saveFile(); myConfig.saveFile();
server.sendHeader("Location", "/config.htm#collapseOne", true); server.sendHeader("Location", "/config.htm#collapseOne", true);
server.send(302, "text/plain", "Device config updated" ); server.send(302, "text/plain", "Device config updated" );
@ -316,7 +317,6 @@ void webHandleConfigPush() {
myConfig.setHttpPushTarget( server.arg( CFG_PARAM_PUSH_HTTP ).c_str() ); myConfig.setHttpPushTarget( server.arg( CFG_PARAM_PUSH_HTTP ).c_str() );
myConfig.setHttpPushTarget2( server.arg( CFG_PARAM_PUSH_HTTP2 ).c_str() ); myConfig.setHttpPushTarget2( server.arg( CFG_PARAM_PUSH_HTTP2 ).c_str() );
myConfig.setBrewfatherPushTarget( server.arg( CFG_PARAM_PUSH_BREWFATHER ).c_str() ); myConfig.setBrewfatherPushTarget( server.arg( CFG_PARAM_PUSH_BREWFATHER ).c_str() );
myConfig.setPushInterval( server.arg( CFG_PARAM_PUSH_INTERVAL ).c_str() );
myConfig.saveFile(); myConfig.saveFile();
server.sendHeader("Location", "/config.htm#collapseTwo", true); server.sendHeader("Location", "/config.htm#collapseTwo", true);
server.send(302, "text/plain", "Push config updated" ); server.send(302, "text/plain", "Push config updated" );

View File

@ -39,6 +39,9 @@ Wifi myWifi;
WiFiManager myWifiManager; WiFiManager myWifiManager;
bool shouldSaveConfig = false; bool shouldSaveConfig = false;
const char* userSSID= USER_SSID;
const char* userPWD = USER_SSID_PWD;
// //
// Callback notifying us of the need to save config // Callback notifying us of the need to save config
// //
@ -56,7 +59,7 @@ bool Wifi::connect( bool showPortal ) {
#endif #endif
unsigned long startMillis = millis(); unsigned long startMillis = millis();
if( showPortal ) { if( strlen(userSSID)==0 && showPortal ) {
Log.notice(F("WIFI: Starting wifi portal." CR)); Log.notice(F("WIFI: Starting wifi portal." CR));
myWifiManager.setBreakAfterConfig( true ); myWifiManager.setBreakAfterConfig( true );
@ -79,7 +82,13 @@ bool Wifi::connect( bool showPortal ) {
// Connect to wifi // Connect to wifi
int i = 0; int i = 0;
if( strlen(userSSID) ) {
Log.notice(F("WIFI: connecting to wifi using predefined settings %s." CR), userSSID);
WiFi.begin( userSSID, userPWD );
} else {
WiFi.begin(); WiFi.begin();
}
while( WiFi.status() != WL_CONNECTED ) { while( WiFi.status() != WL_CONNECTED ) {
delay(100); delay(100);
Serial.print( "." ); Serial.print( "." );

View File

@ -1 +1,26 @@
{"mdns":"gravitymon2","id":"ee1bfc","ota-url":"","temp-format":"C","brewfather-push":"","http-push":"http://192.168.1.16:9090/api/v1/V7s7vRXLqnsW3HdxxRuD/telemetry","http-push2":"http://192.168.1.16:9090/test", "push-interval":30,"voltage-factor":1.59,"gravity-formula":"0.0*tilt^3+0.0*tilt^2+0.0017978*tilt+0.9436","temp-adjustment-value":0,"gravity-temp-adjustment":false,"gyro-calibration-data":{"ax":-4814,"ay":1143,"az":2270,"gx":47,"gy":-25,"gz":47},"angle":90.38,"gravity":1.1106,"battery":4.24} {
"mdns": "gravitymon2",
"id": "ee1bfc",
"ota-url": "",
"temp-format": "C",
"brewfather-push": "",
"http-push": "http://192.168.1.16:9090/api/v1/V7s7vRXLqnsW3HdxxRuD/telemetry",
"http-push2": "http://192.168.1.16:9090/test",
"sleep-interval": 30,
"push-interval": 30,
"voltage-factor": 1.59,
"gravity-formula": "0.0*tilt^3+0.0*tilt^2+0.0017978*tilt+0.9436",
"temp-adjustment-value": 0,
"gravity-temp-adjustment": false,
"gyro-calibration-data": {
"ax": -4814,
"ay": 1143,
"az": 2270,
"gx": 47,
"gy": -25,
"gz": 47
},
"angle": 90.38,
"gravity": 1.1106,
"battery": 4.24
}

View File

@ -1 +1,6 @@
{"app-name":"GravityMon ","app-ver":"0.0.0","id":"7376ef","mdns":"gravmon"} {
"app-name": "GravityMon ",
"app-ver": "0.0.0",
"id": "7376ef",
"mdns": "gravmon"
}