Commit b87383a2 authored by Eric Duminil's avatar Eric Duminil
Browse files

Trying mqtt conf

parent c860e02f
#include "mqtt.h"
#include "web_config.h"
#include "config.h"
#include "led_effects.h"
#include "sensor_console.h"
......@@ -18,10 +19,6 @@ namespace config {
uint16_t mqtt_sending_interval = MQTT_SENDING_INTERVAL; // [s]
//INFO: Listen to every CO2 sensor which is connected to the server:
// mosquitto_sub -h MQTT_SERVER -t 'CO2sensors/#' -p 443 --capath /etc/ssl/certs/ -u "MQTT_USER" -P "MQTT_PASSWORD" -v
const char *mqtt_server = MQTT_SERVER;
const uint16_t mqtt_port = MQTT_PORT;
const char *mqtt_user = MQTT_USER;
const char *mqtt_password = MQTT_PASSWORD;
const bool allow_mqtt_commands = ALLOW_MQTT_COMMANDS;
const unsigned long wait_after_fail = 900; // [s] Wait 15 minutes after an MQTT connection fail, before trying again.
}
......@@ -114,11 +111,18 @@ namespace mqtt {
}
Serial.print(F("MQTT - Attempting connection to "));
Serial.print(MQTT_SERVER);
Serial.print(config::mqtt_server);
Serial.print(MQTT_ENCRYPTED ? F(" (Encrypted") : F(" (Unencrypted"));
Serial.print(F(", port "));
Serial.print(MQTT_PORT);
Serial.print(F(") ..."));
Serial.print(config::mqtt_port);
Serial.print(F(") "));
Serial.print(F("User:'"));
Serial.print(config::mqtt_user);
Serial.print(F("' Password:'"));
for (int i = 0; i < strlen(config::mqtt_password); ++i) {
Serial.print("*");
}
Serial.print(F("' ..."));
led_effects::onBoardLEDOn();
// Wait for connection, at most 15s (default)
......
#include "ntp.h"
#include "sensor_console.h"
#include "config.h"
#include "web_config.h"
#include <WiFiUdp.h> // required for NTP
#include "src/lib/NTPClient/NTPClient.h" // NTP
namespace config {
const long utc_offset_in_seconds = UTC_OFFSET_IN_SECONDS; // UTC+1
}
//NOTE: ESP32 sometimes couldn't access the NTP server, and every loop would take +1000ms
// ifdefs could be used to define functions specific to ESP32, e.g. with configTime
namespace ntp {
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, config::ntp_server, config::utc_offset_in_seconds, 60000UL);
NTPClient timeClient(ntpUDP);
bool connected_at_least_once = false;
void setLocalTime(int32_t unix_seconds);
void initialize() {
timeClient.setPoolServerName(config::ntp_server);
timeClient.setTimeOffset((config::time_zone + config::daylight_saving_time) * 3600);
timeClient.setUpdateInterval(60000UL);
Serial.print("NTP - Trying to connect to : ");
Serial.println(config::ntp_server);
timeClient.begin();
......
......@@ -126,7 +126,7 @@ namespace web_config {
*/
ParameterGroup timeParams = ParameterGroup("NTP", "Time server");
TextTParameter<STRING_LEN> ntpParam =
TextTParameter<STRING_LEN> ntpServerParam =
Builder<TextTParameter<STRING_LEN>>("ntp_server").label("NTP Server").defaultValue(NTP_SERVER).build();
IntTParameter<int16_t> timeOffsetParam = Builder<IntTParameter<int16_t>>("timezone").label("Timezone").defaultValue(
(UTC_OFFSET_IN_SECONDS) / 3600).min(-23).max(23).step(1).placeholder("[h]").build();
......@@ -187,7 +187,7 @@ namespace web_config {
ledParams.addItem(&minBrightnessParam);
ledParams.addItem(&ledCountParam);
timeParams.addItem(&ntpParam);
timeParams.addItem(&ntpServerParam);
timeParams.addItem(&timeOffsetParam);
timeParams.addItem(&dstParam);
......@@ -285,6 +285,15 @@ namespace config {
uint8_t &min_brightness = web_config::minBrightnessParam.value();
uint16_t &led_count = web_config::ledCountParam.value();
char *ntp_server = web_config::ntpParam.value();
// Time server
char *ntp_server = web_config::ntpServerParam.value();
int16_t &time_zone = web_config::timeOffsetParam.value();
bool &daylight_saving_time = web_config::dstParam.value();
// MQTT
char *mqtt_server = web_config::mqttServerParam.value();
char *mqtt_user = web_config::mqttUserParam.value();
char *mqtt_password = web_config::mqttPasswordParam.value();
uint16_t &mqtt_port = web_config::mqttPortParam.value();
}
......@@ -24,8 +24,16 @@ namespace config {
extern uint8_t &min_brightness;
extern uint16_t &led_count;
// Time server
extern char *ntp_server;
extern int16_t &time_zone;
extern int16_t &time_zone; // [h]
extern bool &daylight_saving_time; // [true / false]
// MQTT
extern char *mqtt_server;
extern char *mqtt_user;
extern char *mqtt_password;
extern uint16_t &mqtt_port;
}
namespace web_config {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment