Skip to content

Commit

Permalink
split sensor code + LittleFS
Browse files Browse the repository at this point in the history
  • Loading branch information
pilotak committed May 25, 2021
1 parent 6f6d343 commit efd54d5
Show file tree
Hide file tree
Showing 14 changed files with 443 additions and 378 deletions.
42 changes: 42 additions & 0 deletions code/.astylerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Don't create backup files, let git handle it
suffix=none

# K&R style
style=google

# 4 spaces, convert tabs to spaces
indent=spaces=4
convert-tabs

# Indent
indent-switches
indent-classes
indent-preproc-block
indent-preproc-define

# Brackets
add-brackets

# Remove spaces in and around parentheses
unpad-paren

# Insert a space after if, while, for, and around operators
pad-header
pad-oper

# Pointer/reference operators go next to the name (on the right)
align-pointer=name
align-reference=name

# Attach { for classes and namespaces
attach-namespaces
attach-classes

# Extend longer lines, define maximum 120 value. This results in aligned code,
# otherwise the lines are broken and not consistent
max-continuation-indent=120
max-code-length=120

# Breaks
break-after-logical
break-blocks=all
2 changes: 1 addition & 1 deletion code/WemosWeatherStation/const.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define DEVICE_NAME "meteo" // used for MQTT, OTA, AP
#define FW_VERSION "1.0.1"
#define FW_VERSION "2.0.0"

#define CONFIG_PATH "/config.json"
#define CONFIG_AP_SSID DEVICE_NAME "_config"
Expand Down
44 changes: 34 additions & 10 deletions code/WemosWeatherStation/load.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// wifi.h
#include <FS.h>
#include "LittleFS.h"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266httpUpdate.h>
Expand All @@ -13,9 +13,10 @@ WiFiEventHandler wifiDisconnectHandler;

class IntParameter : public WiFiManagerParameter {
public:
IntParameter(const char *id, const char *placeholder, int32_t value, const uint8_t length = 10, const char *custom = NULL)
IntParameter(const char *id, const char *placeholder, int32_t value, const uint8_t length = 10,
const char *custom = NULL)
: WiFiManagerParameter("") {
const char * type_number = "type=\"number\" ";
const char *type_number = "type=\"number\" ";
uint16_t custom_len = 0;

if (custom) {
Expand Down Expand Up @@ -81,22 +82,23 @@ IntParameter custom_mqtt_port("mqtt_port", "MQTT port", mqtt_port, 4, "required"
WiFiManagerParameter custom_mqtt_user("mqtt_user", "MQTT user", mqtt_user, sizeof(mqtt_user),
"placeholder=\"Leave blank if not aplicable\"");
WiFiManagerParameter custom_mqtt_password("mqtt_password", "MQTT password", mqtt_password, sizeof(mqtt_password),
"placeholder=\"Leave blank if not aplicable\" type=\"password\"");
"placeholder=\"Leave blank if not aplicable\" type=\"password\"");


// ota.h
#if defined(NOFUSS_OTA)
#include "NoFUSSClient.h"
char nofuss_server[40];
WiFiManagerParameter custom_nofuss_server("nofuss_server", "NoFUSS server", nofuss_server, sizeof(nofuss_server), "required");
#include "NoFUSSClient.h"
char nofuss_server[40];
WiFiManagerParameter custom_nofuss_server("nofuss_server", "NoFUSS server", nofuss_server, sizeof(nofuss_server),
"required");
#endif

#if defined(HTTP_OTA)
#include <ESP8266HTTPClient.h>
bool do_http_update = false;
char http_ota_url[100];

void httpUpdate(const char* url);
void httpUpdate(const char *url);
#endif

#if defined(ARDUINO_OTA)
Expand All @@ -113,7 +115,29 @@ bool ota_in_progess = false;

MeteoFunctions meteoFunctions;

struct sensorData {
float humidity[2] = {NAN, NAN}; // rel, abs
float temp[3] = {NAN, NAN, NAN}; // from baro, temp, humidity
float pressure[2] = {NAN, NAN}; // rel, abs
};

#if defined(SENSOR_BMP280) || defined(SENSOR_BME280) || defined(SENSOR_LPS35HW)
#define HAS_BARO
#endif

#if defined(SENSOR_MCP9808)
#define HAS_TEMP
#endif

#if defined(SENSOR_HTU21D) || defined(SENSOR_SHT31)
#define HAS_HUMIDITY
#endif

float round2(float value) {
return round(value * 100.0) / 100.0;
}

#if defined(HAS_BARO)
MovingAverageFloat <READ_SAMPLES> baro_filter[2]; // rel pressure, abs pressure
MovingAverageFloat <READ_SAMPLES> baro_temp_filter;

Expand All @@ -122,12 +146,12 @@ MeteoFunctions meteoFunctions;
IntParameter custom_height_above_sea("height_above_sea", "Height above sea (m)", height_above_sea, 4, "required");
#endif

#if defined(SENSOR_BME280) || defined(SENSOR_HTU21D) || defined(SENSOR_SHT31)
#if defined(SENSOR_BME280) || defined(HAS_HUMIDITY)
MovingAverageFloat <READ_SAMPLES> humidity_filter[2]; // rel humidity, abs humidity
MovingAverageFloat <READ_SAMPLES> humidity_temp_filter;
#endif

#if defined(SENSOR_MCP9808)
#if defined(HAS_TEMP)
MovingAverageFloat <READ_SAMPLES> temp_filter;
#endif

Expand Down
6 changes: 3 additions & 3 deletions code/WemosWeatherStation/meters.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,20 @@ void metersLoop() {
float temp = NAN;
float humidity = NAN;

#if defined(SENSOR_HTU21D) || defined(SENSOR_SHT31)
#if defined(HAS_HUMIDITY)

if (sensor_state & 0b100) {
temp = humidity_temp_filter.get();
humidity = humidity_filter[0].get();
}

#elif defined(SENSOR_MCP9808)
#elif defined(HAS_TEMP)

if (sensor_state & 0b010) {
temp = temp_filter.get();
}

#elif defined(SENSOR_BMP280) || defined(SENSOR_BME280) || defined(SENSOR_LPS35HW)
#elif defined(HAS_BARO)

if (sensor_state & 0b001) {
temp = baro_temp_filter.get();
Expand Down
2 changes: 1 addition & 1 deletion code/WemosWeatherStation/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
#endif

if (strcmp(topic, MQTT_HEIGHT_UPDATE_TOPIC) == 0 && len > 0) {
#if defined(SENSOR_BMP280) || defined(SENSOR_BME280) || defined(SENSOR_LPS35HW)
#if defined(HAS_BARO)

if (atoi(payload) > 0) {
height_above_sea = atoi(payload);
Expand Down
3 changes: 1 addition & 2 deletions code/WemosWeatherStation/ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,13 @@ void httpUpdate() {
WiFiClient client;

ESPhttpUpdate.rebootOnUpdate(false);
ESPhttpUpdate.followRedirects(true);

#if defined(DEBUG)
Serial.print("[OTA] Starting HTTP update from: ");
Serial.println(http_ota_url);
#endif

t_httpUpdate_return ret = ESPhttpUpdate.update(client, http_ota_url, FW_VERSION);
t_httpUpdate_return ret = ESPhttpUpdate.update(client, http_ota_url);

switch (ret) {
case HTTP_UPDATE_FAILED:
Expand Down
Loading

0 comments on commit efd54d5

Please sign in to comment.