Skip to content

Commit

Permalink
#124 auto upgrade config (#126)
Browse files Browse the repository at this point in the history
* fix #124 auto upgrade
* fix #127 I2C Scan invalid log
  • Loading branch information
yakumo-saki authored Jan 23, 2023
1 parent 69e8cd6 commit 06165d4
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 49 deletions.
38 changes: 21 additions & 17 deletions _how-to-develop.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
# how to develop web ui
# how to develop

## copy files except config.html to static/
## arduino code

```
cd embed
rsync -v --exclude=config.html ./* static
```
1. git clone
2. open directory in VSCode
3. Fix includePath

## run python http.server or some simple web server on embed
### include path

Edit `.vscode/c_cpp_properties.json` file.

```
cd embed
python -m http.server
"includePath": [
"${workspaceFolder}/**",
"/Users/username/.platformio/packages/**" <-- Add this. path vary on your OS.
],
```

## Access web via web browser
## Web UI code

Use firefox or chrome newer version.
1. git clone
2. edit `src/global.cpp` `DEBUG_BUILD = true`. by change this, envboy returns CORS header.
3. Upload to board
4. exec `python -m http.server` In `embed` directory
5. access `localhost:8000` using web browser

# after development
### NOTE

Dont forget to write back all files in static/ to embed.
1. you cant use external JS libraries. because host has no internet access while in setup mode.

```
cd embed/static
cp -v ./* ../
```
NOTE: modernize JS/Web development is a task in near future.
2 changes: 2 additions & 0 deletions include/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "config_values.h"
#include "ConfigClass.h"

extern const String CFG_VERSION_INVALID;

enum struct CFG_VALIDATE_RESULT {
// 存在していて、バージョンも一致している
VALID,
Expand Down
6 changes: 4 additions & 2 deletions src/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "config_names.h"

const unsigned int CONF_JSON_SIZE = 4000;
extern const String CFG_VERSION_INVALID = "INVALID";


DynamicJsonDocument _create_config_json(bool save, const std::vector<String> &keyArray) {

Expand Down Expand Up @@ -119,12 +121,12 @@ String read_config_setting_id(File f) {
DeserializationError error = deserializeJson(doc, f);

if (error) {
return "INVALID";
return CFG_VERSION_INVALID;
}

JsonVariant value = doc[ConfigNames::SETTING_ID];
if (value.isNull()) {
return "INVALID"; // JSON not contains SETTING_ID
return CFG_VERSION_INVALID; // JSON not contains SETTING_ID
}

return value.as<String>();
Expand Down
8 changes: 6 additions & 2 deletions src/config/config_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ CFG_VALIDATE_RESULT has_valid_config_file() {
settingId = read_config_setting_id(f);
f.close();

if (String(SETTING_ID).equals(settingId)) {
if (settingId.equals(CFG_VERSION_INVALID)) {
cfglog("SETTING_ID not found. INVALID config.");
return CFG_VALIDATE_RESULT::ERROR;
} else if (String(SETTING_ID).equals(settingId)) {
cfglog("SETTING_ID verified. " + settingId);
return CFG_VALIDATE_RESULT::VALID;
} else {
Expand All @@ -80,7 +83,7 @@ CFG_VALIDATE_RESULT has_valid_config_file() {
}
}

cfglog("Unknown state. Assuming config not found");
cfglog("Unknown state. Assuming config not found. " + settingId);
return CFG_VALIDATE_RESULT::ERROR;
}

Expand All @@ -102,6 +105,7 @@ bool has_configured_file() {
return true;
}

/** ファイルシステムのマウント */
void config_setup() {

if (!LittleFS.begin()) {
Expand Down
35 changes: 12 additions & 23 deletions src/i2c_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,43 @@

String get_device_name_by_address(byte address) {
// ("0x39=TSL2561/0x3c=LCD/0x5c=AM2320/0x5d=LPS22HB/0x76=BME280"));
if (address == 0x23) return "BH1750 Lux sensor";
if (address == 0x39) return "TSL2561 Lux sensor";
if (address == 0x3c) return "SSD1306/SH1106 OLED display";
if (address == 0x5c) return "AM2320 Temperature and Humidity sensor";
if (address == 0x5d) return "LPS22HB High precision air pressure sensor";
if (address == 0x76) return "BME280 Temperature and Humidity and air pressure sensor";

if (address == 0x76)
return "BME280 Temperature and Humidity and air pressure sensor";
return "unknown device";
}

void i2c_scan()
{

void i2c_scan() {
unsigned long start = millis();

byte error, address;
int nDevices = 0;
for(address = 1; address < 127; address++ )
{

for (address = 1; address < 127; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();

if (error == 0)
{

if (error == 0) {
String msg = "Found at 0x";
if (address<16)
msg += "0";
msg += String(address,HEX);
if (address < 16) msg += "0";
msg += String(address, HEX);
msg += " " + String(address);
i2clog(msg + " " + get_device_name_by_address(address));
nDevices++;
}
else if (error==4)
{
} else if (error == 4) {
String msg = "Unknown error at address 0x";
if (address<16)
msg += "0";
msg += String(address,HEX);
if (address < 16) msg += "0";
msg += String(address, HEX);
i2clog(msg);
}
}

if (nDevices == 0) {
i2clog(F("No I2C devices found"));
} else {
i2clog(F("0x39=TSL2561/0x3c=LCD/0x5c=AM2320/0x5d=LPS22HB/0x76=BME280"));
}

i2clog("I2C scan done. time = " + String(millis() - start) + " ms");

}
5 changes: 4 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ void setup()
if (validateResult == CFG_VALIDATE_RESULT::VALID) {
mainlog(F("Config file is valid."));
} else if (validateResult == CFG_VALIDATE_RESULT::NEED_UPGRADE) {
mainlog(F("Config file needs upgrade."));
sectionlog(F("Config file needs upgrade. upgrading..."));
read_config();
save_config(); // upgrade means json + default, so only saving is needed.
sectionlog(F("Config file upgrade done!"));
} else {
mainlog(F("Config file validation error. dropping to setup mode."));
isNormal = false;
Expand Down
2 changes: 1 addition & 1 deletion src/main_normal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void setup_normal() {
sectionlog(F("Start watchdog"));
setup_watchdog();

// setupモードに入りやすくするための処理
// setupモードに入りやすくするための処理(deprecated)
if (config->get(ConfigNames::DISPLAY_RECONFIG) == ConfigValues::DISPLAY_RECONFIG_ON) {
sectionlog(F("Reset to reconfig start."));
remove_configure_flag_file();
Expand Down
6 changes: 3 additions & 3 deletions src/sensors/bh1750.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ bool bh_setup(void) {
byte error = Wire.endTransmission();

if (!(error == 0)) {
amlog(F("BH1750 disabled. "));
bhlog(F("BH1750 disabled. "));
return false;
}

if (!bh1750.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
amlog(F("BH1750 disabled: Device found but initialize error. "));
bhlog(F("BH1750 disabled: Device found but initialize error. "));
return false;
}

amlog(F("BH1750 Enabled"));
bhlog(F("BH1750 Enabled"));
use_bh1750 = true;
return true;
}
Expand Down

0 comments on commit 06165d4

Please sign in to comment.