Skip to content

Commit

Permalink
Local develop (#3)
Browse files Browse the repository at this point in the history
* 📝 Add Gitter badge

* 📝 Add link to the Gitter in the ISSUE_TEMPLATE

* 🐛 Fix loopFunction being called too early before MQTT connection is established - closes homieiot#260 (homieiot#290)

* 👕 Fix lint

* ✨ Add destructor to HomieNode, that will abort() (homieiot#286)

* Added destructor to HomieNode, that will abort()

* 🎨 Log wifi client IP address upon connection. (homieiot#280)

This removes the need to hunt the device IP when debugging connectivity issues.

* 📝 Mention troubleshooting page in issue template

* 👕 Fix cpplint failure, causing travis CI to be red (homieiot#291)

E.g. https://travis-ci.org/marvinroger/homie-esp8266/jobs/199213998

Ran the following command successfully:
  cpplint --repository=. --recursive \
  --filter=-whitespace/line_length,-legal/copyright,-runtime/printf,-build/include,-build/namespace,-runtime/int \
  ./src

* 🔥 Remove MQTT ack logging

* 🐎 Make tiny optimizations

* 🎨 Add a comma to the wifi connected log

* 🎨 Reorganize next boot mode feature

* 🐎 Check length on input fields (homieiot#292)

* Fixing loopFunction being called too early before MQTT connection is established

* Started cleanup of strcpy/sprintf to include length check.

* Fixed warning under Interface.cpp

* 👕 Fix lint

* 🎨 Print firmware name and version at boot

* 🐛 Fix DeviceId incomplete MAC address (homieiot#296)

snprintf works with n-1 characters

expected behaviour: DeviceId is 12 characters
behaviour: only 11 characters are returned

(resulting in a "incomplete" mac address)

* :bug Fix millis() overflow in uptime - fix homieiot#299 (homieiot#302)

Corrected type declaration for correct overflow handling of millis
function in uptime calculation
  • Loading branch information
euphi authored Mar 6, 2017
1 parent 134844d commit eac8e2f
Show file tree
Hide file tree
Showing 24 changed files with 92 additions and 115 deletions.
8 changes: 5 additions & 3 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
The issue tracker is a great place to ask for enhancements and to report bugs.
If you have some questions or if you need help, some people might help you on the [Gitter room](https://gitter.im/homie-iot/ESP8266).

Before submitting your issue, make sure:

- [ ] You're using a [stable release](https://github.com/marvinroger/homie-esp8266/releases), not the Git development version which is, by definition, unstable
- [ ] You've read the documentation for *your* release, which is in the `docs/` folder of the `.zip` of the release you're using, especially the **Getting started** and **Troubleshooting** pages, which contain respectively the minimum required version of the dependencies, and some answsers to the most common problems
- [ ] You're using the examples bundled in *your* release, which are in the `examples/` folder of the `.zip` of the release you're using. Examples in the latest git revision might not be backward-compatible with your release
- [ ] You've read the documentation for *your* release (in the `docs/` folder for the v1, at https://homie-esp8266.readme.io) which contains some answsers to the most common problems (notably the `Limitations and know issues` and `Troubleshooting` pages)
- [ ] You're using the examples bundled in *your* release, which are in the `examples/` folder of the `.zip` of the release you're using. Examples might not be backward-compatible

Thanks!
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Homie for ESP8266
=================

[![Build Status](https://img.shields.io/travis/marvinroger/homie-esp8266/develop.svg?style=flat-square)](https://travis-ci.org/marvinroger/homie-esp8266) [![Latest Release](https://img.shields.io/badge/release-v2.0.0-yellow.svg?style=flat-square)](https://github.com/marvinroger/homie-esp8266/releases)
[![Build Status](https://img.shields.io/travis/marvinroger/homie-esp8266/develop.svg?style=flat-square)](https://travis-ci.org/marvinroger/homie-esp8266) [![Latest Release](https://img.shields.io/badge/release-v2.0.0-yellow.svg?style=flat-square)](https://github.com/marvinroger/homie-esp8266/releases) [![Gitter](https://img.shields.io/gitter/room/Homie/ESP8266.svg?style=flat-square)](https://gitter.im/homie-iot/ESP8266)

An Arduino for ESP8266 implementation of [Homie](https://github.com/marvinroger/homie), an MQTT convention for the IoT.

Expand Down
16 changes: 8 additions & 8 deletions src/Homie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ HomieClass::HomieClass()
: _setupCalled(false)
, _firmwareSet(false)
, __HOMIE_SIGNATURE("\x25\x48\x4f\x4d\x49\x45\x5f\x45\x53\x50\x38\x32\x36\x36\x5f\x46\x57\x25") {
strcpy(Interface::get().brand, DEFAULT_BRAND);
strlcpy(Interface::get().brand, DEFAULT_BRAND, MAX_BRAND_LENGTH);
Interface::get().bootMode = HomieBootMode::UNDEFINED;
Interface::get().configurationAp.secured = false;
Interface::get().led.enabled = true;
Expand Down Expand Up @@ -63,7 +63,7 @@ void HomieClass::setup() {
Interface::get().getConfig().setHomieBootModeOnNextBoot(HomieBootMode::UNDEFINED);
}

HomieBootMode _selectedHomieBootMode = HomieBootMode::CONFIG;
HomieBootMode _selectedHomieBootMode = HomieBootMode::CONFIGURATION;

// select boot mode source
if (_applicationHomieBootMode != HomieBootMode::UNDEFINED) {
Expand All @@ -77,7 +77,7 @@ void HomieClass::setup() {
// validate selected mode and fallback as needed
if (_selectedHomieBootMode == HomieBootMode::NORMAL && !Interface::get().getConfig().load()) {
Interface::get().getLogger() << F("Configuration invalid. Using CONFIG MODE") << endl;
_selectedHomieBootMode = HomieBootMode::CONFIG;
_selectedHomieBootMode = HomieBootMode::CONFIGURATION;
}

// run selected mode
Expand All @@ -86,7 +86,7 @@ void HomieClass::setup() {
Interface::get().event.type = HomieEventType::NORMAL_MODE;
Interface::get().eventHandler(Interface::get().event);

} else if (_selectedHomieBootMode == HomieBootMode::CONFIG) {
} else if (_selectedHomieBootMode == HomieBootMode::CONFIGURATION) {
_boot = &_bootConfig;
Interface::get().event.type = HomieEventType::CONFIGURATION_MODE;
Interface::get().eventHandler(Interface::get().event);
Expand Down Expand Up @@ -157,7 +157,7 @@ HomieClass& HomieClass::setConfigurationApPassword(const char* password) {
_checkBeforeSetup(F("setConfigurationApPassword"));

Interface::get().configurationAp.secured = true;
strcpy(Interface::get().configurationAp.password, password);
strlcpy(Interface::get().configurationAp.password, password, MAX_WIFI_PASSWORD_LENGTH);
}

void HomieClass::__setFirmware(const char* name, const char* version) {
Expand Down Expand Up @@ -242,11 +242,11 @@ HomieClass& HomieClass::setHomieBootModeOnNextBoot(HomieBootMode bootMode) {
return *this;
}

bool HomieClass::isConfigured() const {
bool HomieClass::isConfigured() {
return Interface::get().getConfig().load();
}

bool HomieClass::isConnected() const {
bool HomieClass::isConnected() {
return Interface::get().connected;
}

Expand Down Expand Up @@ -277,7 +277,7 @@ HomieClass& HomieClass::disableResetTrigger() {
return *this;
}

const ConfigStruct& HomieClass::getConfiguration() const {
const ConfigStruct& HomieClass::getConfiguration() {
return Interface::get().getConfig().get();
}

Expand Down
19 changes: 8 additions & 11 deletions src/Homie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "Homie/Blinker.hpp"

#include "SendingPromise.hpp"
#include "HomieBootMode.hpp"
#include "HomieEvent.hpp"
#include "HomieNode.hpp"
#include "HomieSetting.hpp"
#include "StreamingOperator.hpp"
Expand All @@ -37,11 +39,6 @@ class HomieClass {
void __setFirmware(const char* name, const char* version);
void __setBrand(const char* brand) const;

static const HomieBootMode MODE_UNDEFINED = HomieBootMode::UNDEFINED;
static const HomieBootMode MODE_STANDALONE = HomieBootMode::STANDALONE;
static const HomieBootMode MODE_CONFIG = HomieBootMode::CONFIG;
static const HomieBootMode MODE_NORMAL = HomieBootMode::NORMAL;

HomieClass& disableLogging();
HomieClass& setLoggingPrinter(Print* printer);
HomieClass& disableLedFeedback();
Expand All @@ -57,15 +54,15 @@ class HomieClass {
HomieClass& setHomieBootMode(HomieBootMode bootMode);
HomieClass& setHomieBootModeOnNextBoot(HomieBootMode bootMode);

void reset();
static void reset();
void reboot();
void setIdle(bool idle);
bool isConfigured() const;
bool isConnected() const;
const ConfigStruct& getConfiguration() const;
static void setIdle(bool idle);
static bool isConfigured();
static bool isConnected();
static const ConfigStruct& getConfiguration();
AsyncMqttClient& getMqttClient();
Logger& getLogger();
void prepareToSleep();
static void prepareToSleep();

private:
bool _setupCalled;
Expand Down
1 change: 1 addition & 0 deletions src/Homie/Boot/Boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void Boot::setup() {

WiFi.persistent(true); // Persist data on SDK as it seems Wi-Fi connection is faster

Interface::get().getLogger() << F("💡 Firmware ") << Interface::get().firmware.name << F(" (") << Interface::get().firmware.version << F(")") << endl;
Interface::get().getLogger() << F("🔌 Booting into ") << _name << F(" mode 🔌") << endl;
}

Expand Down
7 changes: 4 additions & 3 deletions src/Homie/Boot/BootConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ BootConfig::BootConfig()
, _jsonWifiNetworks()
, _flaggedForReboot(false)
, _flaggedForRebootAt(0)
, _proxyEnabled(false) {
, _proxyEnabled(false)
, _apIpStr({'\0'}) {
_wifiScanTimer.setInterval(CONFIG_SCAN_INTERVAL);
}

Expand All @@ -30,7 +31,7 @@ void BootConfig::setup() {
WiFi.mode(WIFI_AP_STA);

char apName[MAX_WIFI_SSID_LENGTH];
strcpy(apName, Interface::get().brand);
strlcpy(apName, Interface::get().brand, MAX_WIFI_SSID_LENGTH - 1 - MAX_MAC_STRING_LENGTH);
strcat_P(apName, PSTR("-"));
strcat(apName, DeviceId::get());

Expand All @@ -41,7 +42,7 @@ void BootConfig::setup() {
WiFi.softAP(apName);
}

sprintf(_apIpStr, "%d.%d.%d.%d", ACCESS_POINT_IP[0], ACCESS_POINT_IP[1], ACCESS_POINT_IP[2], ACCESS_POINT_IP[3]);
snprintf(_apIpStr, MAX_IP_STRING_LENGTH, "%d.%d.%d.%d", ACCESS_POINT_IP[0], ACCESS_POINT_IP[1], ACCESS_POINT_IP[2], ACCESS_POINT_IP[3]);

Interface::get().getLogger() << F("AP started as ") << apName << F(" with IP ") << _apIpStr << endl;
_dns.setTTL(30);
Expand Down
2 changes: 1 addition & 1 deletion src/Homie/Boot/BootConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class BootConfig : public Boot {
bool _flaggedForReboot;
uint32_t _flaggedForRebootAt;
bool _proxyEnabled;
char _apIpStr[15 + 1];
char _apIpStr[MAX_IP_STRING_LENGTH];

void _onCaptivePortal();
void _onDeviceInfoRequest();
Expand Down
31 changes: 10 additions & 21 deletions src/Homie/Boot/BootNormal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ using namespace HomieInternals;

BootNormal::BootNormal()
: Boot("normal")
, _mqttTimedRetry(MQTT_RECONNECT_STEP_INTERVAL, MQTT_RECONNECT_MAX_INTERVAL)
, _setupFunctionCalled(false)
, _mqttDisconnectNotified(true)
, _mqttTimedRetry(MQTT_RECONNECT_STEP_INTERVAL, MQTT_RECONNECT_MAX_INTERVAL)
, _flaggedForOta(false)
, _flaggedForReset(false)
, _flaggedForReboot(false)
Expand All @@ -20,7 +20,7 @@ BootNormal::BootNormal()
, _mqttWillTopic(nullptr)
, _mqttPayloadBuffer(nullptr) {
_statsTimer.setInterval(STATS_SEND_INTERVAL);
strncpy(_fwChecksum, ESP.getSketchMD5().c_str(), sizeof(_fwChecksum) - 1);
strlcpy(_fwChecksum, ESP.getSketchMD5().c_str(), sizeof(_fwChecksum));
_fwChecksum[sizeof(_fwChecksum) - 1] = '\0';
}

Expand Down Expand Up @@ -118,7 +118,7 @@ void BootNormal::_wifiConnect() {

void BootNormal::_onWifiGotIp(const WiFiEventStationModeGotIP& event) {
if (Interface::get().led.enabled) Interface::get().getBlinker().stop();
Interface::get().getLogger() << F("✔ Wi-Fi connected") << endl;
Interface::get().getLogger() << F("✔ Wi-Fi connected, IP: ") << event.ip << endl;
Interface::get().getLogger() << F("Triggering WIFI_CONNECTED event...") << endl;
Interface::get().event.type = HomieEventType::WIFI_CONNECTED;
Interface::get().event.ip = event.ip;
Expand Down Expand Up @@ -189,19 +189,9 @@ void BootNormal::_onMqttConnected() {
Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$name")), 1, true, Interface::get().getConfig().get().name);

IPAddress localIp = WiFi.localIP();
char localIpStr[15 + 1];
char localIpPartStr[3 + 1];
itoa(localIp[0], localIpPartStr, 10);
strcpy(localIpStr, localIpPartStr);
strcat_P(localIpStr, PSTR("."));
itoa(localIp[1], localIpPartStr, 10);
strcat(localIpStr, localIpPartStr);
strcat_P(localIpStr, PSTR("."));
itoa(localIp[2], localIpPartStr, 10);
strcat(localIpStr, localIpPartStr);
strcat_P(localIpStr, PSTR("."));
itoa(localIp[3], localIpPartStr, 10);
strcat(localIpStr, localIpPartStr);
char localIpStr[MAX_IP_STRING_LENGTH];
snprintf(localIpStr, MAX_IP_STRING_LENGTH - 1, "%d.%d.%d.%d", localIp[0], localIp[1], localIp[2], localIp[3]);

Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$localip")), 1, true, localIpStr);

char statsIntervalStr[3 + 1];
Expand Down Expand Up @@ -614,7 +604,6 @@ void BootNormal::_onMqttMessage(char* topic, char* payload, AsyncMqttClientMessa
}

void BootNormal::_onMqttPublish(uint16_t id) {
Interface::get().getLogger() << F("Triggering MQTT_PACKET_ACKNOWLEDGED event (packetId ") << id << F(")...") << endl;
Interface::get().event.type = HomieEventType::MQTT_PACKET_ACKNOWLEDGED;
Interface::get().event.packetId = id;
Interface::get().eventHandler(Interface::get().event);
Expand Down Expand Up @@ -755,11 +744,11 @@ void BootNormal::loop() {
Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$stats/uptime")), 1, true, uptimeStr);
_statsTimer.tick();
}
}

Interface::get().loopFunction();
Interface::get().loopFunction();

for (HomieNode* iNode : HomieNode::nodes) {
iNode->loop();
for (HomieNode* iNode : HomieNode::nodes) {
iNode->loop();
}
}
}
2 changes: 1 addition & 1 deletion src/Homie/Boot/BootStandalone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void BootStandalone::loop() {

if (_flaggedForConfig && Interface::get().reset.idle) {
Interface::get().getLogger() << F("Device is idle") << endl;
Interface::get().getConfig().setHomieBootModeOnNextBoot(HomieBootMode::CONFIG);
Interface::get().getConfig().setHomieBootModeOnNextBoot(HomieBootMode::CONFIGURATION);

Interface::get().getLogger() << F("Triggering ABOUT_TO_RESET event...") << endl;
Interface::get().event.type = HomieEventType::ABOUT_TO_RESET;
Expand Down
50 changes: 13 additions & 37 deletions src/Homie/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using namespace HomieInternals;

Config::Config()
: _valid(false)
, _configStruct()
, _spiffsBegan(false) {
: _configStruct()
, _spiffsBegan(false)
, _valid(false) {
}

bool Config::_spiffsBegin() {
Expand Down Expand Up @@ -93,16 +93,16 @@ bool Config::load() {
reqOtaEnabled = parsedJson["ota"]["enabled"];
}

strcpy(_configStruct.name, reqName);
strcpy(_configStruct.wifi.ssid, reqWifiSsid);
if (reqWifiPassword) strcpy(_configStruct.wifi.password, reqWifiPassword);
strcpy(_configStruct.deviceId, reqDeviceId);
strcpy(_configStruct.mqtt.server.host, reqMqttHost);
strlcpy(_configStruct.name, reqName, MAX_FRIENDLY_NAME_LENGTH);
strlcpy(_configStruct.wifi.ssid, reqWifiSsid, MAX_WIFI_SSID_LENGTH);
if (reqWifiPassword) strlcpy(_configStruct.wifi.password, reqWifiPassword, MAX_WIFI_PASSWORD_LENGTH);
strlcpy(_configStruct.deviceId, reqDeviceId, MAX_DEVICE_ID_LENGTH);
strlcpy(_configStruct.mqtt.server.host, reqMqttHost, MAX_HOSTNAME_LENGTH);
_configStruct.mqtt.server.port = reqMqttPort;
strcpy(_configStruct.mqtt.baseTopic, reqMqttBaseTopic);
strlcpy(_configStruct.mqtt.baseTopic, reqMqttBaseTopic, MAX_MQTT_BASE_TOPIC_LENGTH);
_configStruct.mqtt.auth = reqMqttAuth;
strcpy(_configStruct.mqtt.username, reqMqttUsername);
strcpy(_configStruct.mqtt.password, reqMqttPassword);
strlcpy(_configStruct.mqtt.username, reqMqttUsername, MAX_MQTT_CREDS_LENGTH);
strlcpy(_configStruct.mqtt.password, reqMqttPassword, MAX_MQTT_CREDS_LENGTH);
_configStruct.ota.enabled = reqOtaEnabled;

/* Parse the settings */
Expand Down Expand Up @@ -178,7 +178,7 @@ void Config::setHomieBootModeOnNextBoot(HomieBootMode bootMode) {
} else {
File bootModeFile = SPIFFS.open(CONFIG_NEXT_BOOT_MODE_FILE_PATH, "w");
if (!bootModeFile) {
Interface::get().getLogger() << F("✖ Cannot open BOOTMODE file") << endl;
Interface::get().getLogger() << F("✖ Cannot open NEXTMODE file") << endl;
return;
}

Expand All @@ -195,15 +195,7 @@ HomieBootMode Config::getHomieBootModeOnNextBoot() {
if (bootModeFile) {
int v = bootModeFile.parseInt();
bootModeFile.close();
if (v == 1) {
return HomieBootMode::STANDALONE;
} else if (v == 2) {
return HomieBootMode::CONFIG;
} else if (v == 3) {
return HomieBootMode::NORMAL;
} else {
return HomieBootMode::UNDEFINED;
}
return static_cast<HomieBootMode>(v);
} else {
return HomieBootMode::UNDEFINED;
}
Expand Down Expand Up @@ -289,22 +281,6 @@ void Config::log() const {
Interface::get().getLogger() << F("{} Stored configuration") << endl;
Interface::get().getLogger() << F(" • Hardware device ID: ") << DeviceId::get() << endl;
Interface::get().getLogger() << F(" • Device ID: ") << _configStruct.deviceId << endl;
Interface::get().getLogger() << F(" • Boot mode: ");
switch (_bootMode) {
case HomieBootMode::CONFIG:
Interface::get().getLogger() << F("configuration") << endl;
break;
case HomieBootMode::NORMAL:
Interface::get().getLogger() << F("normal") << endl;
break;
case HomieBootMode::STANDALONE:
Interface::get().getLogger() << F("standalone") << endl;
break;
default:
Interface::get().getLogger() << F("unknown") << endl;
break;
}

Interface::get().getLogger() << F(" • Name: ") << _configStruct.name << endl;

Interface::get().getLogger() << F(" • Wi-Fi: ") << endl;
Expand Down
2 changes: 1 addition & 1 deletion src/Homie/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Utils/Validation.hpp"
#include "Constants.hpp"
#include "Limits.hpp"
#include "../HomieBootMode.hpp"
#include "../HomieSetting.hpp"
#include "../StreamingOperator.hpp"

Expand All @@ -29,7 +30,6 @@ class Config {
bool isValid() const;

private:
HomieBootMode _bootMode;
ConfigStruct _configStruct;
bool _spiffsBegan;
bool _valid;
Expand Down
7 changes: 0 additions & 7 deletions src/Homie/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,4 @@ namespace HomieInternals {
const char CONFIG_UI_BUNDLE_PATH[] = "/homie/ui_bundle.gz";
const char CONFIG_NEXT_BOOT_MODE_FILE_PATH[] = "/homie/NEXTMODE";
const char CONFIG_FILE_PATH[] = "/homie/config.json";

enum class HomieBootMode : uint8_t {
UNDEFINED = 0,
STANDALONE = 1,
CONFIG = 2,
NORMAL = 3
};
} // namespace HomieInternals
Loading

0 comments on commit eac8e2f

Please sign in to comment.