Skip to content

Commit

Permalink
new Offset Calibration method and Filesystem config improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
universam1 committed Nov 17, 2019
1 parent 2dcd60c commit 6d55d57
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 50 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Check out [IOT DEVICE PULLS ITS WEIGHT IN HOME BREWING](http://hackaday.com/2017

| Date | Note |
| :--------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| 17.11.19 | Firmware 6.2.0: New Calibration routine: The calibration procedure takes now less than 5s only to determine offsets that set the current position of the Accelerometer close as possible to level! Also this new implementation solves a few issues reported where the values have not been saved reliably. Also, the configuration file routine has been improved to support larger strings that were clipped at some point |
| 10.11.19 | Firmware 6.1.3: Extending the length of URL parameter to support API Gateway URLs. Fixes of incompatibilities from #308. Support for latest ESP8266 SDK solving naming collisions |
| 8.11.19 | Firmware 6.1.2: hardening SPIFFS config save routine |
| 27.10.19 | Firmware 6.1.1: Added support for ThingSpeak (#305) (#308), improving documentation, fix for prometheus sender data (#303), updating to new PIO output path |
Expand Down
7 changes: 4 additions & 3 deletions pio/lib/Globals/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
extern Ticker flasher;

// defines go here
#define FIRMWAREVERSION "6.1.4"
#define FIRMWAREVERSION "6.2.0"

#define API_FHEM true
#define API_UBIDOTS true
Expand Down Expand Up @@ -107,10 +107,11 @@ extern Ticker flasher;
extern int16_t ax, ay, az;
extern float Volt, Temperatur, Tilt, Gravity;

extern MPU6050_Base accelgyro;
extern MPU6050 accelgyro;
extern bool saveConfig();
extern bool saveConfig(int16_t, int16_t, int16_t);
extern bool saveConfig(int16_t Offset[6]);
extern bool formatSpiffs();
extern void flash();

float scaleTemperature(float t);
String tempScaleLabel(void);
Expand Down
20 changes: 8 additions & 12 deletions pio/lib/WiFiManagerKT/WiFiManagerKT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,23 +1047,19 @@ void WiFiManager::handleOffset()
page += FPSTR(HTTP_STYLE);
page += _customHeadElement;

if (offset.getStatus() != "done!")
page += F("<META HTTP-EQUIV=\"refresh\" CONTENT=\"120;url=http://192.168.4.1/\">");
page += FPSTR(HTTP_HEADER_END);
page += F("<h1>calibrate Offset</h1><hr>");
page += F("<table>");
page += F("<tr><td>");
page += F("...calibration in progress...<br><h2>DO NOT MOVE OR SHAKE!</h2><br>It takes ~2min to complete. Once complete the iSpindel will restart and the blue LED will switch from continous to blinking");
// page += offset.getStatus();
page += F("</td></tr>");
page += F("</table>");
page += F("<META HTTP-EQUIV=\"refresh\" CONTENT=\"6;url=/iSpindel\"> \
<h1>calibrate Offset</h1><hr> \
<table><tr><td> \
...calibration in progress...<br><h2>DO NOT MOVE OR SHAKE!</h2><br> \
It takes a few seconds to complete. Once complete the blue LED will switch from continous to blinking \
</td></tr></table>");

page += FPSTR(HTTP_END);

server->send(200, "text/html", page);
if (offset.getStatus() == "idle")
offset.calibrate();
ESP.reset();
offset.calibrate();
flasher.attach(1, flash);
}

/** Handle the state page */
Expand Down
74 changes: 39 additions & 35 deletions pio/src/iSpindel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ All rights reserverd by S.Lang <universam@web.de>
// !DEBUG 1

// definitions go here
MPU6050_Base accelgyro;
MPU6050 accelgyro;
OneWire *oneWire;
DallasTemperature DS18B20;
DeviceAddress tempDeviceAddress;
Expand Down Expand Up @@ -84,7 +84,7 @@ uint32_t my_sleeptime = 15 * 60;
uint16_t my_port = 80;
uint32_t my_channel;
float my_vfact = ADCDIVISOR;
int16_t my_aX = UNINIT, my_aY = UNINIT, my_aZ = UNINIT;
int16_t my_Offset[6];
uint8_t my_tempscale = TEMP_CELSIUS;
int8_t my_OWpin = -1;

Expand Down Expand Up @@ -126,12 +126,16 @@ void saveConfigCallback()

void applyOffset()
{
if (my_aX != UNINIT && my_aY != UNINIT && my_aZ != UNINIT)
if (my_Offset[0] != UNINIT && my_Offset[1] != UNINIT && my_Offset[2] != UNINIT)
{
CONSOLELN(String("applying offsets: ") + my_aX + ":" + my_aY + ":" + my_aZ);
accelgyro.setXAccelOffset(my_aX);
accelgyro.setYAccelOffset(my_aY);
accelgyro.setZAccelOffset(my_aZ);
CONSOLELN(String("applying offsets: ") + my_Offset[0] + ":" + my_Offset[1] + ":" + my_Offset[2]);
accelgyro.setXAccelOffset(my_Offset[0]);
accelgyro.setYAccelOffset(my_Offset[1]);
accelgyro.setZAccelOffset(my_Offset[2]);
accelgyro.setXGyroOffset(my_Offset[3]);
accelgyro.setYGyroOffset(my_Offset[4]);
accelgyro.setZGyroOffset(my_Offset[5]);
delay(1);

CONSOLELN(String("confirming offsets: ") + accelgyro.getXAccelOffset() + ":" + accelgyro.getYAccelOffset() + ":" + accelgyro.getZAccelOffset());
}
Expand Down Expand Up @@ -168,14 +172,14 @@ bool readConfig()
else
{
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);

configFile.readBytes(buf.get(), size);
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, buf.get());

if (!error)
DynamicJsonDocument doc(size * 2);
DeserializationError error = deserializeJson(doc, configFile);
if (error)
{
CONSOLE(F("deserializeJson() failed: "));
CONSOLELN(error.c_str());
}
else
{
if (doc.containsKey("Name"))
strcpy(my_name, doc["Name"]);
Expand Down Expand Up @@ -215,14 +219,14 @@ bool readConfig()
my_psk = (const char *)doc["PSK"];
if (doc.containsKey("POLY"))
strcpy(my_polynominal, doc["POLY"]);
if (doc.containsKey("aX"))
my_aX = doc["aX"];
if (doc.containsKey("aY"))
my_aY = doc["aY"];
if (doc.containsKey("aZ"))
my_aZ = doc["aZ"];

applyOffset();
if (doc.containsKey("Offset"))
{
for (size_t i = 0; i < (sizeof(my_Offset) / sizeof(*my_Offset)); i++)
{
my_Offset[i] = doc["Offset"][i];
}
}

CONSOLELN(F("parsed config:"));
#ifdef DEBUG
Expand Down Expand Up @@ -439,12 +443,11 @@ bool formatSpiffs()
return SPIFFS.begin();
}

bool saveConfig(int16_t aX, int16_t aY, int16_t aZ)
bool saveConfig(int16_t Offset[6])
{
my_aX = aX;
my_aY = aY;
my_aZ = aZ;
CONSOLELN(String("new offsets: ") + my_aX + ":" + my_aY + ":" + my_aZ);
std::copy(Offset, Offset + 6, my_Offset);
CONSOLELN(String("new offsets: ") + Offset[0] + ":" + Offset[1] + ":" + Offset[2]);
CONSOLELN(String("confirming offsets: ") + accelgyro.getXAccelOffset() + ":" + accelgyro.getYAccelOffset() + ":" + accelgyro.getZAccelOffset());

return saveConfig();
}
Expand All @@ -464,7 +467,7 @@ bool saveConfig()
}
}

DynamicJsonDocument doc(1024);
DynamicJsonDocument doc(2048);

doc["Name"] = my_name;
doc["Token"] = my_token;
Expand All @@ -484,15 +487,15 @@ bool saveConfig()
doc["Vfact"] = my_vfact;
doc["TS"] = my_tempscale;
doc["OWpin"] = my_OWpin;

// Store current Wifi credentials
doc["POLY"] = my_polynominal;
doc["SSID"] = WiFi.SSID();
doc["PSK"] = WiFi.psk();

doc["POLY"] = my_polynominal;
doc["aX"] = my_aX;
doc["aY"] = my_aY;
doc["aZ"] = my_aZ;
JsonArray array = doc.createNestedArray("Offset");
for (auto &&i : my_Offset)
{
array.add(i);
}

File configFile = SPIFFS.open(CFGFILE, "w");
if (!configFile)
Expand Down Expand Up @@ -798,6 +801,7 @@ void initAccel()
Wire.setClock(100000);
Wire.setClockStretchLimit(2 * 230);

testAccel();
// init the Accel
accelgyro.initialize();
accelgyro.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
Expand All @@ -813,7 +817,7 @@ void initAccel()
accelgyro.setInterruptDrive(1); // Open drain
accelgyro.setRate(17);
accelgyro.setIntDataReadyEnabled(true);
testAccel();
applyOffset();
}

float calculateTilt()
Expand Down

0 comments on commit 6d55d57

Please sign in to comment.