From ea7cb298aec3bebb532acea68adbd35822a77f58 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Wed, 26 Dec 2018 22:54:38 +0900 Subject: [PATCH] Having a std::vector problem. --- .travis.yml | 2 +- examples/mqttRSSI/data/param.json | 27 +++++++++++++++ examples/mqttRSSI/mqttRSSI.ino | 56 +++++++++++++++---------------- library.json | 2 +- src/AutoConnectElementBasis.h | 23 +++++++++---- src/AutoConnectElementBasisImpl.h | 6 ++-- src/AutoConnectElementJson.h | 3 ++ src/AutoConnectElementJsonImpl.h | 4 +++ 8 files changed, 85 insertions(+), 38 deletions(-) create mode 100644 examples/mqttRSSI/data/param.json diff --git a/.travis.yml b/.travis.yml index 1967126..e69ca76 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,7 @@ before_install: - if [[ "$BOARD" =~ "esp32:esp32:" ]]; then arduino --install-boards esp32:esp32; fi - - arduino --install-library PubSubClient,ArduinoJson:5.13.3,PageBuilder:1.2.1 + - arduino --install-library PubSubClient,ArduinoJson:5.13.3,PageBuilder:1.2.2 - buildExampleSketch() { arduino --verbose-build --verify --board $BOARD $PWD/examples/$1/$1.ino; } install: - mkdir -p ~/Arduino/libraries diff --git a/examples/mqttRSSI/data/param.json b/examples/mqttRSSI/data/param.json new file mode 100644 index 0000000..a73599b --- /dev/null +++ b/examples/mqttRSSI/data/param.json @@ -0,0 +1,27 @@ +[ + { + "name": "mqttserver", + "type": "ACInput", + "value": "mqtt.thingspeak.com", + "placeholder": "MQTT broker server", + "label": "Server" + }, + { + "name": "channelid", + "type": "ACInput", + "value": "454951", + "label": "Channel ID" + }, + { + "name": "userkey", + "type": "ACInput", + "value": "NRTFYGJ6TJFGX4RC", + "label": "User Key" + }, + { + "name": "apikey", + "type": "ACInput", + "value": "HBVQ2XV6VYBI4582", + "label": "API Key" + } +] diff --git a/examples/mqttRSSI/mqttRSSI.ino b/examples/mqttRSSI/mqttRSSI.ino index a388dab..766216a 100644 --- a/examples/mqttRSSI/mqttRSSI.ino +++ b/examples/mqttRSSI/mqttRSSI.ino @@ -1,16 +1,16 @@ /* - ESP8266/ESP32 publish the RSSI as the WiFi signal strength to ThingSpeak channel. - This example is for explaining how to use the AutoConnect library. - - In order to execute this example, the ThingSpeak account is needed. Sing up - for New User Account and create a New Channel via My Channels. - For details, please refer to the project page. - https://hieromon.github.io/AutoConnect/examples/index.html#used-with-mqtt-as-a-client-application - - This example is based on the environment as of March 20, 2018. - Copyright (c) 2018 Hieromon Ikasamo. - This software is released under the MIT License. - https://opensource.org/licenses/MIT +ESP8266/ESP32 publish the RSSI as the WiFi signal strength to ThingSpeak channel. +This example is for explaining how to use the AutoConnect library. + +In order to execute this example, the ThingSpeak account is needed. Sing up +for New User Account and create a New Channel via My Channels. +For details, please refer to the project page. +https://hieromon.github.io/AutoConnect/examples/index.html#used-with-mqtt-as-a-client-application + +This example is based on the environment as of March 20, 2018. +Copyright (c) 2018 Hieromon Ikasamo. +This software is released under the MIT License. +https://opensource.org/licenses/MIT */ #if defined(ARDUINO_ARCH_ESP8266) @@ -57,26 +57,22 @@ static const char AUX_mqtt_setting[] PROGMEM = R"raw( { "name": "mqttserver", "type": "ACInput", - "value": "mqtt.thingspeak.com", "placeholder": "MQTT broker server", "label": "Server" }, { "name": "channelid", "type": "ACInput", - "value": "454951", "label": "Channel ID" }, { "name": "userkey", "type": "ACInput", - "value": "NRTFYGJ6TJFGX4RC", "label": "User Key" }, { "name": "apikey", "type": "ACInput", - "value": "HBVQ2XV6VYBI4582", "label": "API Key" }, { @@ -85,8 +81,7 @@ static const char AUX_mqtt_setting[] PROGMEM = R"raw( "label": "Update period", "value": [ "30 sec.", - "60 sec.", - "180 sec." + "60 sec." ], "arrange": "vertical", "checked": 1 @@ -112,7 +107,7 @@ static const char AUX_mqtt_setting[] PROGMEM = R"raw( { "name": "save", "type": "ACSubmit", - "value": "Save&Start", + "value": "Save&Start", "uri": "/mqtt_save" }, { @@ -166,8 +161,8 @@ unsigned long lastPub = 0; bool mqttConnect() { static const char alphanum[] = "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; // For random generation of client ID. + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; // For random generation of client ID. char clientId[9]; uint8_t retry = 10; @@ -186,7 +181,8 @@ bool mqttConnect() { if (mqttClient.connect(clientId, MQTT_USER_ID, userKey.c_str())) { Serial.println("Established:" + String(clientId)); return true; - } else { + } + else { Serial.println("Connection failed:" + String(mqttClient.state())); if (!--retry) break; @@ -227,7 +223,10 @@ String loadParams(AutoConnectAux& aux, PageArgument& args) { SPIFFS.begin(); File param = SPIFFS.open(PARAM_FILE, "r"); if (param) { - aux.loadElement(param); + // if (aux.loadElement(param)) + // Serial.println(PARAM_FILE " loaded"); + // else + // Serial.println(PARAM_FILE " failed to load"); param.close(); } else @@ -249,13 +248,13 @@ String saveParams(AutoConnectAux& aux, PageArgument& args) { channelId = args.arg("channelid"); channelId.trim(); - + userKey = args.arg("userkey"); userKey.trim(); - + apiKey = args.arg("apikey"); apiKey.trim(); - + String upd = args.arg("period"); updateInterval = upd.substring(0, 2).toInt() * 1000; @@ -263,7 +262,7 @@ String saveParams(AutoConnectAux& aux, PageArgument& args) { hostName = args.arg("hostname"); hostName.trim(); - + // The entered value is owned by AutoConnectAux of /mqtt_setting. // In order to retrieve the elements of /mqtt_setting, // it is necessary to get the AutoConnectAux object of /mqtt_setting. @@ -360,7 +359,8 @@ void setup() { if (portal.begin()) { Serial.println("connected:" + WiFi.SSID()); Serial.println("IP:" + WiFi.localIP().toString()); - } else { + } + else { Serial.println("connection failed:" + String(WiFi.status())); while (1) { delay(100); diff --git a/library.json b/library.json index 78f8ca4..7104bb2 100644 --- a/library.json +++ b/library.json @@ -12,7 +12,7 @@ [ { "name": "PageBuilder", - "version": ">=1.2.1" + "version": ">=1.2.2" } ], "frameworks": "arduino", diff --git a/src/AutoConnectElementBasis.h b/src/AutoConnectElementBasis.h index ea1167a..5e3be3b 100644 --- a/src/AutoConnectElementBasis.h +++ b/src/AutoConnectElementBasis.h @@ -120,13 +120,23 @@ class AutoConnectInputBasis : virtual public AutoConnectElementBasis { */ class AutoConnectRadioBasis : virtual public AutoConnectElementBasis { public: - explicit AutoConnectRadioBasis(const char* name = "", std::vector values = {}, const char* label = "", const ACArrange_t order = AC_Vertical, const uint8_t checked = 0) : AutoConnectElementBasis(name, ""), label(label), order(order), checked(checked), _values(values) { + explicit AutoConnectRadioBasis(const char* name = "", std::vector const& values = {}, const char* label = "", const ACArrange_t order = AC_Vertical, const uint8_t checked = 0) : AutoConnectElementBasis(name, ""), label(label), order(order), checked(checked), _values(values) { _type = AC_Radio; +// _values = values; +// for (String v : values) { +// add(v); +// const std::string sv = v.c_str(); +// _values.push_back(sv); +// } } virtual ~AutoConnectRadioBasis() {} const String toHTML(void) const override; - void add(const String value) { _values.push_back(value); } - void empty(void) { _values.clear(); } + void add(const String& value) { _values.push_back(value); } + //void add(const String& value) { + // std::string sv = value.c_str(); + // _values.push_back(sv); + //} + void empty(const size_t reserve = 0) { _values.reserve(reserve); _values.clear(); std::vector().swap(_values); } void check(const String& value); String label; /**< A label for a subsequent radio buttons */ @@ -135,6 +145,7 @@ class AutoConnectRadioBasis : virtual public AutoConnectElementBasis { protected: std::vector _values; /**< Items in a group */ +// std::vector _values; /**< Items in a group */ }; /** @@ -147,13 +158,13 @@ class AutoConnectRadioBasis : virtual public AutoConnectElementBasis { */ class AutoConnectSelectBasis : virtual public AutoConnectElementBasis { public: - explicit AutoConnectSelectBasis(const char* name = "", std::vector options = {}, const char* label = "") : AutoConnectElementBasis(name, ""), label(String(label)), _options(options) { + explicit AutoConnectSelectBasis(const char* name = "", std::vector const& options = {}, const char* label = "") : AutoConnectElementBasis(name, ""), label(String(label)), _options(options) { _type = AC_Select; } virtual ~AutoConnectSelectBasis() {} const String toHTML(void) const override; - void add(const String option) { _options.push_back(option); } - void empty(void) { _options.clear(); } + void add(const String& option) { _options.push_back(option); } + void empty(const size_t reserve = 0) { _options.reserve(reserve); _options.clear(); std::vector().swap(_options); } String label; /**< A label for a subsequent input box */ diff --git a/src/AutoConnectElementBasisImpl.h b/src/AutoConnectElementBasisImpl.h index 6d32f29..a81172b 100644 --- a/src/AutoConnectElementBasisImpl.h +++ b/src/AutoConnectElementBasisImpl.h @@ -77,7 +77,8 @@ const String AutoConnectRadioBasis::toHTML(void) const { html += String("
"); } for (std::size_t n = 0; n < _values.size(); n++) { - String value = _values[n]; +// String value = _values[n]; + String value = String(_values[n].c_str()); html += String(FPSTR("