Skip to content

Commit

Permalink
v44 (#82)
Browse files Browse the repository at this point in the history
* Add config parameters

* rename filename setup http handler

* Add config And Create embedded file header script

* ログ表記揺れ修正

* ConfigNamesを使用するように変更

* ADD: config API INVALID KEY check

* Add configs to backup API

* ADD: Invalid key error to config set API

* Alerts sample

* move set_default_config to constructor

* ADD: Factory Reset API

* Update API document

* Change displaySkipReconfigure

* FIX:mDNS setInstanceName/AddService before begin

* FIX: mDNS Instance Name duplication

* finalize v44

* DEBUG_BUILD OFF
  • Loading branch information
yakumo-saki authored Mar 22, 2021
1 parent 0128245 commit 3778faf
Show file tree
Hide file tree
Showing 34 changed files with 1,133 additions and 690 deletions.
69 changes: 69 additions & 0 deletions build_script/create_embed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#Import("env", "projenv")

import os
import shutil
import glob
import pathlib

def readall(path):
f = open(path, 'r')
data = f.read()
f.close()
return data


def create_name_from_filename(filename):
ret = filename
ret = ret.replace(".", "_")
ret = ret.replace(" ", "_")
return ret


EMBED_DIR=os.path.join(os.getcwd(), "embed") # embedしたいファイルがおいてあるディレクトリ
OUT_DIR=os.path.join(os.getcwd(), "include", "embed") # 変換した .h ファイルの出力先
TEMPLATE_FILE=os.path.join(os.getcwd(), "build_script", "template.cpp")

print("=" * 78)
print("create_embed.py executing.")
print(f"EMBED_DIR = {EMBED_DIR}")
print(f"OUT_DIR = {OUT_DIR}")
print("=" * 78)

# access to global build environment
#print(env.Dump())

# access to project build environment (is used source files in "src" folder)
#print(projenv.Dump())

#
# Script to build cpp file from embed directory
#
# see: https://thingpulse.com/embed-binary-data-on-esp32/
# see: https://docs.platformio.org/en/latest/projectconf/advanced_scripting.html#before-pre-and-after-post-actions

if os.path.exists(OUT_DIR):
shutil.rmtree(OUT_DIR)

os.mkdir(OUT_DIR)
template = readall(TEMPLATE_FILE)

for file in pathlib.Path(EMBED_DIR).iterdir():
output = template
data = readall(file)

output_name = create_name_from_filename(file.name)

# 変数名部分を置換。 変数名は全部大文字とする
output = output.replace("$$REPLACE_NAME$$", output_name.upper()).replace("$$REPLACE_CONTENT$$", data)
output_cpp_path = os.path.join(OUT_DIR, output_name + ".h")

f = open(output_cpp_path, 'w')
data = f.write(output)
f.close()

print(f"generated {output_cpp_path}")


print("=" * 78)
print("DONE")
print("=" * 78)
5 changes: 5 additions & 0 deletions build_script/template.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <Arduino.h>

static const char $$REPLACE_NAME$$[] PROGMEM = R"=====(
$$REPLACE_CONTENT$$
)=====";
10 changes: 7 additions & 3 deletions create_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ rm -f ${MYDIR}/EnvBoyX*.tar.gz
# git ブランチからファイル名判定
branch=`git symbolic-ref --short HEAD`

if [ $branch = "master" ]; then
RELEASE_FILE_PATH=${MYDIR}/EnvBoyX.tar.gz
grep -i "DEBUG_BUILD.*TRUE" ${MYDIR}/src/global.cpp
if [ $? -eq 0 ] ; then
echo "** DEBUG_BUILD IS SET TO TRUE"
RELEASE_FILE_PATH=${MYDIR}/EnvBoyX-DEBUG.tar.gz
elif [ $branch = "main" ]; then
RELEASE_FILE_PATH=${MYDIR}/EnvBoyX.tar.gz
else
Expand All @@ -59,11 +61,13 @@ else
RELEASE_FILE_PATH=${MYDIR}/EnvBoyX-$branch.tar.gz
fi

rm -f ${RELEASE_FILE_PATH}
rm -f ${MYDIR}/EnvBoyX*.tar.gz
tar -C ${RELEASE_BASE_DIR} -zcvf ${RELEASE_FILE_PATH} .

banner "Release archive created ${RELEASE_FILE_PATH}"

# Warning

echo "To upload, run:"
echo "tar xvf `basename ${RELEASE_FILE_PATH}`"
echo "cd `basename ${RELEASE_FILE_PATH}`"
Expand Down
18 changes: 16 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ EnvBoyX has http web API.
| 42.0 | POST | /config/commit | JSON | Save config |
| 42.0 | POST | /config/revert | JSON | Revert to last saved config |
| 43.0 | GET | /config/backup | TEXT | Get current running config on curl command line |
| 44.0 | POST | /config/factory-reset | JSON | Delete ALL configs |

NOTE: There is no reboot API, because of security reason.

Expand All @@ -29,6 +30,9 @@ But some paramters are not changeable.

### parameters

names: `config_names.cpp`
values: `config_values.cpp`

| name | possible values | need restart | note |
|------|-----------------| -- | ------------------------ |
| mqttName | string | y | *1 mqttClientName. only affects in MQTT Mode |
Expand All @@ -40,6 +44,7 @@ But some paramters are not changeable.
| st7789 | "yes", "no" | y | Use ST7789 or not. only on ESP32 |
| st7789Mode | "st7789_BIG", "st7789_normal" | n | ST7789 display mode |
| displayFlip | "yes", "no" | n | Flip display or not |
| displayWaitForReconfigure | "skip", "on" | n | Skip on boot wait for reconfigure screen |
| opMode | "always", "mqtt" | y | Operation mode |
| mDNS | string | n | mDNS hostname |
| co2Alerts | {alerts} | n | See alerts section |
Expand All @@ -53,12 +58,21 @@ But some paramters are not changeable.

Alert config keys structure:

<kind>.<level_and_no>.<low_or_high>
`<kind>.<level>.<low_or_high>`

##### v44 and above

kind = [co2alerts | humiAlerts | luxAlerts | presAlerts]
level_and_no = [warn1 | warn2 | caut1 | caut2]
low_or_high = [L | H]

example: co2alerts.warn1.L

##### before v44

kind = [co2alerts | humiAlerts | luxAlerts | presAlerts]
level_and_no = [warning1 | warning2 | caution1 | caution2]
low_or_high = [low | high]

example: co2alerts.warning1.low
#### cURL example

Expand Down
10 changes: 9 additions & 1 deletion docs/history.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@

## v44.0: Untitled update

*
NOTE: Alerts settings are back to default on version up.

* ADD: MHZ-19B Auto Baseline Correction ON/OFF at boot time
* ADD: Skip wait for reconfigure
* ADD: Factory reset API
* CHANGE: Config set API key about alerts are chenged
* FIX: Internal code clean up
* FIX: Config backup API result contains gabage
* FIX: mDNS: Call setInstanceName/addService before begin() (this prevent mdns listing)

## v43.0: Delta all update

Expand Down
8 changes: 8 additions & 0 deletions embed/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ body {
background-color: aliceblue;
}

body.setup_done {
background-color: paleturquoise;
}

input[type='text'] {
width: 200px;
}
Expand All @@ -14,3 +18,7 @@ input[type='submit'] {
width: 200px;
height: 48px;
}

a.setup_again {
font-size: 150%;
}
3 changes: 3 additions & 0 deletions examples/curl-example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
curl -X POST -d "mDNS=ebx32&opMode=always&displayFlip=yes&displayBrightness=50&oledType=SSD&st7789=yes&st7789Mode=st7789_normal&mhz19b=no&mhz19bPwmPin=14&mhz19bRxPin=32&mhz19bTxPin=33&mqttBroker=&mqttName=&tempAlerts=&tempAlerts.warn1.L=-99&tempAlerts.warn1.H=10&tempAlerts.warn2.L=10&tempAlerts.warn2.H=40&tempAlerts.caut1.L=10&tempAlerts.caut1.H=15&tempAlerts.caut2.L=28&tempAlerts.caut2.H=30&humiAlerts.warn1.L=0&humiAlerts.warn1.H=20&humiAlerts.warn2.L=75&humiAlerts.warn2.H=100&humiAlerts.caut1.L=20&humiAlerts.caut1.H=35&humiAlerts.caut2.L=65&humiAlerts.caut2.H=75&luxAlerts.warn1.L=0&luxAlerts.warn1.H=1&luxAlerts.warn2.L=2000&luxAlerts.warn2.H=99999&luxAlerts.caut1.L=-1&luxAlerts.caut1.H=-1&luxAlerts.caut2.L=-1&luxAlerts.caut2.H=-1&presAlerts.warn1.L=0&presAlerts.warn1.H=995&presAlerts.warn2.L=1025&presAlerts.warn2.H=3000&presAlerts.caut1.L=995&presAlerts.caut1.H=1000&presAlerts.caut2.L=3000&presAlerts.caut2.H=3000&co2Alerts.warn1.L=0&co2Alerts.warn1.H=200&co2Alerts.warn2.L=1000&co2Alerts.warn2.H=9999&co2Alerts.caut1.L=200&co2Alerts.caut1.H=300&co2Alerts.caut2.L=800&co2Alerts.caut2.H=1000" http://ebx32.local/config

curl -X POST -d "tempAlerts.warn1.L=-99&tempAlerts.warn1.H=10&tempAlerts.warn2.L=10&tempAlerts.warn2.H=40&tempAlerts.caut1.L=10&tempAlerts.caut1.H=15&tempAlerts.caut2.L=28&tempAlerts.caut2.H=30&humiAlerts.warn1.L=0&humiAlerts.warn1.H=20&humiAlerts.warn2.L=75&humiAlerts.warn2.H=100&humiAlerts.caut1.L=20&humiAlerts.caut1.H=35&humiAlerts.caut2.L=65&humiAlerts.caut2.H=75&luxAlerts.warn1.L=0&luxAlerts.warn1.H=1&luxAlerts.warn2.L=2000&luxAlerts.warn2.H=99999&luxAlerts.caut1.L=-1&luxAlerts.caut1.H=-1&luxAlerts.caut2.L=-1&luxAlerts.caut2.H=-1&presAlerts.warn1.L=0&presAlerts.warn1.H=995&presAlerts.warn2.L=1025&presAlerts.warn2.H=3000&presAlerts.caut1.L=995&presAlerts.caut1.H=1000&presAlerts.caut2.L=3000&presAlerts.caut2.H=3000&co2Alerts.warn1.L=0&co2Alerts.warn1.H=200&co2Alerts.warn2.L=1000&co2Alerts.warn2.H=9999&co2Alerts.caut1.L=200&co2Alerts.caut1.H=300&co2Alerts.caut2.L=800&co2Alerts.caut2.H=1000" http://ebx32.local/config
38 changes: 3 additions & 35 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,6 @@
#include <ArduinoJson.h>
#include "FS.h"

const String CFG_SETTING_ID = "settingId";
const String CFG_SSID = "ssid";
const String CFG_PASSWORD = "password";
const String CFG_MDNS = "mDNS";
const String CFG_OPMODE = "opMode";
const String CFG_DISPLAY_FLIP = "displayFlip";
const String CFG_DISPLAY_BRIGHTNESS = "displayBrightness";
const String CFG_OLED_TYPE = "oledType";
const String CFG_ST7789 = "st7789";
const String CFG_ST7789_MODE = "st7789Mode";
const String CFG_MHZ19B = "mhz19b";
const String CFG_MHZ19B_PWM = "mhz19bPwmPin";
const String CFG_MHZ19B_RX = "mhz19bRxPin";
const String CFG_MHZ19B_TX = "mhz19bTxPin";
const String CFG_MQTT_BROKER = "mqttBroker";
const String CFG_MQTT_NAME = "mqttName";

const String CFG_TEMP_ALERT = "tempAlerts";
const String CFG_HUMI_ALERT = "humiAlerts";
const String CFG_LUX_ALERT = "luxAlerts";
const String CFG_PRES_ALERT = "presAlerts";
const String CFG_CO2_ALERT = "co2Alerts";

const String CFG_ALERT_WARN1_LO = "warn1.L";
const String CFG_ALERT_WARN1_HI = "warn1.H";
const String CFG_ALERT_WARN2_LO = "warn2.L";
const String CFG_ALERT_WARN2_HI = "warn2.H";
const String CFG_ALERT_CAUTION1_LO = "caut1.L";
const String CFG_ALERT_CAUTION1_HI = "caut1.H";
const String CFG_ALERT_CAUTION2_LO = "caut2.L";
const String CFG_ALERT_CAUTION2_HI = "caut2.H";

// --------------------------------------------------------------------
// ボード依存あり
// --------------------------------------------------------------------
Expand All @@ -58,13 +26,13 @@ bool has_valid_config();

void config_setup();

/** ボード依存:設定ファイルを削除 */
void config_factory_reset();

// --------------------------------------------------------------------
// ボード依存なし
// --------------------------------------------------------------------

/** デフォルト値をセット */
void set_default_config_value();

void print_config();

void trim_config();
Expand Down
50 changes: 50 additions & 0 deletions include/config_names.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <Arduino.h>

/**
* Configで使用される、名称の定数クラス。
* 実際の値は ConfigValue クラスにある
*/
class ConfigNames {
public:
static const String SETTING_ID;
static const String SSID;
static const String PASSWORD;
static const String MDNS;

static const String OPMODE;

static const String DISPLAY_FLIP;
static const String DISPLAY_BRIGHTNESS;
static const String DISPLAY_RECONFIG;

static const String OLED_TYPE;

static const String ST7789;
static const String ST7789_MODE;

static const String MHZ19B;
static const String MHZ19B_PWM;
static const String MHZ19B_RX;
static const String MHZ19B_TX;
static const String MHZ19B_ABC;

static const String MQTT_BROKER;
static const String MQTT_NAME;

static const String TEMP_ALERT;
static const String HUMI_ALERT;
static const String LUX_ALERT;
static const String PRES_ALERT;
static const String CO2_ALERT;

static const String ALERT_WARN1_LO;
static const String ALERT_WARN1_HI;
static const String ALERT_WARN2_LO;
static const String ALERT_WARN2_HI;
static const String ALERT_CAUTION1_LO;
static const String ALERT_CAUTION1_HI;
static const String ALERT_CAUTION2_LO;
static const String ALERT_CAUTION2_HI;
};
15 changes: 14 additions & 1 deletion include/global_ConfigValues.h → include/config_values.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

#include <Arduino.h>

/**
* Configで使用する値の定数クラス。
* HTML上の name要素だったり、JSONのキー(=APIのキー)に使われる名前は ConfigNamesにある
*/
class ConfigValues {
public:
static const String settings;
static const String configured_file;

static const String SETTING_ID;

static const String OPMODE_NAME;
static const String OPMODE_MQTT;
static const String OPMODE_DISPLAY;

static const String ST7789_USE;
static const String ST7789_NOUSE;

static const String ST7789_MODE_BIG;
static const String ST7789_MODE_NORMAL;

Expand All @@ -22,7 +29,13 @@ class ConfigValues {
static const String DISPLAY_FLIP_ON;
static const String DISPLAY_FLIP_OFF;

static const String DISPLAY_RECONFIG_SKIP;
static const String DISPLAY_RECONFIG_ON;

static const String MHZ_USE_PWM;
static const String MHZ_USE_UART;
static const String MHZ_NOUSE;
};

static const String MHZ_ABC_ON;
static const String MHZ_ABC_OFF;
};
28 changes: 28 additions & 0 deletions include/embed/style_css.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <Arduino.h>

static const char STYLE_CSS[] PROGMEM = R"=====(
body {
background-color: aliceblue;
}

body.setup_done {
background-color: paleturquoise;
}

input[type='text'] {
width: 200px;
}

input.num {
width: 50px;
}

input[type='submit'] {
width: 200px;
height: 48px;
}

a.setup_again {
font-size: 150%;
}
)=====";
6 changes: 5 additions & 1 deletion include/global.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <Arduino.h>
#include <WiFiClient.h>

#include "global_ConfigValues.h"
#include "global_MimeType.h"

#include "config_values.h"
#include "config_names.h"

#include "structs.h"

// --------------------------------------------------------------------
Expand All @@ -15,6 +17,8 @@ extern const String ver;
extern const String minorVer;
extern const String product_long;

extern const String SETTING_ID;

extern const bool DEBUG_BUILD;

// --------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 3778faf

Please sign in to comment.