Skip to content

Feature/littlefs update #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
84c0c2f
starts Webportal always
balu- Oct 22, 2021
d7cfa5e
extend possible characters for mqtt Password
balu- Oct 22, 2021
83f34bc
Merge branch 'balu--longerMqttPW' of https://github.com/balu-/esp8266…
Oct 29, 2021
2bd6ae6
rename src for Arduino IDE
Oct 29, 2021
840e86e
disabled subscribe to command
Oct 29, 2021
9bc23e1
add Topic param
Oct 29, 2021
be0ace4
disabled autoconf
Oct 29, 2021
122dcbd
disabled all mqtt stuff that i personaly do not need
Oct 29, 2021
7dfd85b
Using new name for config file to prefent bootloop
Oct 29, 2021
b3bd06a
set serial monitor speed correct
woutervantoledo Nov 1, 2021
6f49851
move to correct folder for vsc
woutervantoledo Nov 1, 2021
ddc4160
use wifimanager beta for always up config
woutervantoledo Nov 10, 2021
fedb1cf
Create platformio.yml
wvtbg Nov 25, 2021
054dfbf
dont test but run
woutervantoledo Nov 25, 2021
710198f
manually download wifimanager beta version
woutervantoledo Nov 25, 2021
e8249c7
lib no option on run
woutervantoledo Nov 25, 2021
dc8497d
call install lib to get wifimanager
woutervantoledo Nov 25, 2021
2f70519
i'm lost
woutervantoledo Nov 25, 2021
f46caa3
do check and install
woutervantoledo Nov 25, 2021
fbb76ed
skip check
woutervantoledo Nov 25, 2021
f790468
only build on linux as wget is used
woutervantoledo Nov 25, 2021
bea422e
woutervantoledo Nov 25, 2021
86ba248
disable some checks as not compliant yet
woutervantoledo Nov 25, 2021
f15b7e9
Merge pull request #1 from wvtbg/github-action
wvtbg Nov 25, 2021
bf772ef
fix storage and read of data after startup
woutervantoledo Nov 26, 2021
5119b01
update to littlefs filesystem
woutervantoledo Nov 26, 2021
b10fd3e
re enable home assistant topics and messages
woutervantoledo Dec 3, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/platformio.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: PlatformIO CI

on: [push]

jobs:
build:

runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v2
- name: Cache pip
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Cache PlatformIO
uses: actions/cache@v2
with:
path: ~/.platformio
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}

- name: Set up Python
uses: actions/setup-python@v2

- name: Install PlatformIO
run: |
python -m pip install --upgrade pip
pip install --upgrade platformio

- name: Install library dependencies
run: |
wget https://github.com/tzapu/WiFiManager/archive/refs/tags/2.0.5-beta.zip -O /tmp/WiFiManager.zip
pio lib install /tmp/WiFiManager.zip
pio lib install 1

- name: Run PlatformIO
run: pio run
env:
PLATFORMIO_CI_SRC: ${{ matrix.example }}
- name: Run Static Code Checks PlatformIO
run: pio check
env:
PLATFORMIO_CI_SRC: ${{ matrix.example }}
5 changes: 4 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
platform = espressif8266
board = d1_mini
framework = arduino
board_build.filesystem = littlefs
check_tool = clangtidy ;cppcheck, clangtidy, pvs-studio
lib_deps =
bblanchon/ArduinoJson @ ^6.18.3
knolleary/PubSubClient @ ^2.8
tzapu/WiFiManager @ ^0.16.0
tzapu/WiFiManager @ 2.0.5-beta
monitor_speed = 115200
35 changes: 26 additions & 9 deletions src/Config.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
#pragma once

#include <ArduinoJson.h>
#include <FS.h>
#include "LittleFS.h"

#define JSON_SIZE 512

namespace Config {
char mqtt_server[80] = "example.tld";
char mqtt_topic[80] = "esp8266-vindriktning-particle-sensor/%s";

char username[24] = "";
char password[24] = "";
char password[72] = "";


void save() {
DynamicJsonDocument json(512);
Serial.println("save config\n");
DynamicJsonDocument json(JSON_SIZE);
json["mqtt_server"] = mqtt_server;
json["mqtt_topic"] = mqtt_topic;
json["username"] = username;
json["password"] = password;

File configFile = SPIFFS.open("/config.json", "w");
Serial.println("== Save == \n");
Serial.printf("mqtt_server: %s\n", mqtt_server);
Serial.printf("mqtt_topic: %s\n", mqtt_topic);
Serial.printf("mqtt_user: %s\n", username);


File configFile = LittleFS.open("/conf.json", "w");
if (!configFile) {
return;
}
Expand All @@ -25,22 +37,27 @@ namespace Config {
}

void load() {
if (SPIFFS.begin()) {

if (SPIFFS.exists("/config.json")) {
File configFile = SPIFFS.open("/config.json", "r");
Serial.println("load config\n");
if (LittleFS.begin()) {
if (LittleFS.exists("/conf.json")) {
File configFile = LittleFS.open("/conf.json", "r");

if (configFile) {
const size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]);

configFile.readBytes(buf.get(), size);
DynamicJsonDocument json(512);
DynamicJsonDocument json(JSON_SIZE);

if (DeserializationError::Ok == deserializeJson(json, buf.get())) {
strcpy(mqtt_server, json["mqtt_server"]);
strcpy(mqtt_topic, json["mqtt_topic"]);
strcpy(username, json["username"]);
strcpy(password, json["password"]);
Serial.println("== Load == \n");
Serial.printf("mqtt_server: %s\n", mqtt_server);
Serial.printf("mqtt_topic: %s\n", mqtt_topic);
Serial.printf("mqtt_user: %s\n", username);
}
}
}
Expand Down
45 changes: 39 additions & 6 deletions src/esp8266-vindriktning-particle-sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ PubSubClient mqttClient;
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", Config::mqtt_server, sizeof(Config::mqtt_server));
WiFiManagerParameter custom_mqtt_user("user", "MQTT username", Config::username, sizeof(Config::username));
WiFiManagerParameter custom_mqtt_pass("pass", "MQTT password", Config::password, sizeof(Config::password));
WiFiManagerParameter custom_mqtt_topic("topic", "MQTT Topic", Config::mqtt_topic, sizeof(Config::mqtt_topic));

uint32_t lastMqttConnectionAttempt = 0;
const uint16_t mqttConnectionInterval = 60000; // 1 minute = 60 seconds = 60000 milliseconds
Expand All @@ -30,8 +31,8 @@ const uint16_t statusPublishInterval = 30000; // 30 seconds = 30000 milliseconds

char identifier[24];
#define FIRMWARE_PREFIX "esp8266-vindriktning-particle-sensor"
#define AVAILABILITY_ONLINE "online"
#define AVAILABILITY_OFFLINE "offline"
#define AVAILABILITY_ONLINE "{ \"state\" : \"online\" }"
#define AVAILABILITY_OFFLINE "{ \"state\" : \"offline\" }"
char MQTT_TOPIC_AVAILABILITY[128];
char MQTT_TOPIC_STATE[128];
char MQTT_TOPIC_COMMAND[128];
Expand Down Expand Up @@ -61,18 +62,26 @@ void setup() {

snprintf(identifier, sizeof(identifier), "VINDRIKTNING-%X", ESP.getChipId());
snprintf(MQTT_TOPIC_AVAILABILITY, 127, "%s/%s/status", FIRMWARE_PREFIX, identifier);
snprintf(MQTT_TOPIC_STATE, 127, "%s/%s/state", FIRMWARE_PREFIX, identifier);
snprintf(MQTT_TOPIC_COMMAND, 127, "%s/%s/command", FIRMWARE_PREFIX, identifier);

snprintf(MQTT_TOPIC_AUTOCONF_PM25_SENSOR, 127, "homeassistant/sensor/%s/%s_pm25/config", FIRMWARE_PREFIX, identifier);
snprintf(MQTT_TOPIC_AUTOCONF_WIFI_SENSOR, 127, "homeassistant/sensor/%s/%s_wifi/config", FIRMWARE_PREFIX, identifier);

WiFi.hostname(identifier);

Config::load();

if (strlen(Config::mqtt_server) > 0 ) custom_mqtt_server.setValue(Config::mqtt_server, strlen(Config::mqtt_server));
if (strlen(Config::username) > 0 ) custom_mqtt_user.setValue(Config::username, strlen(Config::username));
if (strlen(Config::password) > 0 ) custom_mqtt_pass.setValue(Config::password, strlen(Config::password));
if (strlen(Config::mqtt_topic) > 0 ) custom_mqtt_topic.setValue(Config::mqtt_topic, strlen(Config::mqtt_topic));

setupWifi();
setupOTA();

snprintf(MQTT_TOPIC_STATE, 127, Config::mqtt_topic, identifier);
Serial.printf("MQTT server: %s\n", Config::mqtt_server);
Serial.printf("MQTT Topic State: %s\n", MQTT_TOPIC_STATE);

mqttClient.setServer(Config::mqtt_server, 1883);
mqttClient.setKeepAlive(10);
mqttClient.setBufferSize(2048);
Expand Down Expand Up @@ -119,6 +128,21 @@ void loop() {
ArduinoOTA.handle();
SerialCom::handleUart(state);
mqttClient.loop();
wifiManager.process();
if(shouldSaveConfig){
//save if needed
Serial.println("Get Values from web and save\n");
strcpy(Config::mqtt_server, custom_mqtt_server.getValue());
strcpy(Config::username, custom_mqtt_user.getValue());
strcpy(Config::password, custom_mqtt_pass.getValue());
strcpy(Config::mqtt_topic, custom_mqtt_topic.getValue());
Config::save();
snprintf(MQTT_TOPIC_STATE, 127, Config::mqtt_topic, identifier);
Serial.printf("MQTT Topic State: %s\n", MQTT_TOPIC_STATE);
//reset save flag
shouldSaveConfig = false;

}

const uint32_t currentMillis = millis();
if (currentMillis - statusPublishPreviousMillis >= statusPublishInterval) {
Expand All @@ -144,6 +168,10 @@ void setupWifi() {
wifiManager.addParameter(&custom_mqtt_server);
wifiManager.addParameter(&custom_mqtt_user);
wifiManager.addParameter(&custom_mqtt_pass);
wifiManager.addParameter(&custom_mqtt_topic);

wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.setPreSaveConfigCallback(saveConfigCallback);

WiFi.hostname(identifier);
wifiManager.autoConnect(identifier);
Expand All @@ -152,15 +180,20 @@ void setupWifi() {
strcpy(Config::mqtt_server, custom_mqtt_server.getValue());
strcpy(Config::username, custom_mqtt_user.getValue());
strcpy(Config::password, custom_mqtt_pass.getValue());
strcpy(Config::mqtt_topic, custom_mqtt_topic.getValue());

if (shouldSaveConfig) {
Config::save();
//reset save flag
shouldSaveConfig = false;
} else {
// For some reason, the read values get overwritten in this function
// To combat this, we just reload the config
// This is most likely a logic error which could be fixed otherwise
Config::load();
}

wifiManager.startWebPortal();
}

void resetWifiSettingsAndReboot() {
Expand All @@ -171,8 +204,8 @@ void resetWifiSettingsAndReboot() {

void mqttReconnect() {
for (uint8_t attempt = 0; attempt < 3; ++attempt) {
if (mqttClient.connect(identifier, Config::username, Config::password, MQTT_TOPIC_AVAILABILITY, 1, true, AVAILABILITY_OFFLINE)) {
mqttClient.publish(MQTT_TOPIC_AVAILABILITY, AVAILABILITY_ONLINE, true);
if (mqttClient.connect(identifier, Config::username, Config::password, MQTT_TOPIC_STATE, 1, true, AVAILABILITY_OFFLINE)) {
mqttClient.publish(MQTT_TOPIC_STATE, AVAILABILITY_ONLINE, true);
publishAutoConfig();

// Make sure to subscribe after polling the status so that we never execute commands with the default data
Expand Down