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

AMPEL_WIFI

parent e2bb5beb
......@@ -12,18 +12,22 @@
#ifdef AMPEL_CSV
# include "csv_writer.h"
#endif
#ifdef AMPEL_MQTT
# include "mqtt.h"
#ifdef AMPEL_WIFI
# include "wifi_util.h"
# ifdef AMPEL_MQTT
# include "mqtt.h"
# endif
# ifdef AMPEL_HTTP
# include "web_server.h"
# endif
#endif
#ifdef AMPEL_LORAWAN
# include "lorawan.h"
#endif
#ifdef AMPEL_HTTP
# include "web_server.h"
#endif
#include "util.h"
#include "wifi_util.h"
#include "co2_sensor.h"
#include "led_effects.h"
......
......@@ -75,6 +75,7 @@ void setup() {
Serial.print(F("Board : "));
Serial.println(BOARD);
#ifdef AMPEL_WIFI
// Try to connect to Wi-Fi
WiFiConnect(SENSOR_ID);
......@@ -82,9 +83,9 @@ void setup() {
Serial.println(WiFi.status());
if (WiFi.status() == WL_CONNECTED) {
#ifdef AMPEL_HTTP
# ifdef AMPEL_HTTP
web_server::initialize();
#endif
# endif
ntp::initialize();
......@@ -95,10 +96,11 @@ void setup() {
Serial.println(F("Error setting up MDNS responder!"));
}
#ifdef AMPEL_MQTT
# ifdef AMPEL_MQTT
mqtt::initialize("CO2sensors/" + SENSOR_ID);
#endif
# endif
}
#endif
#ifdef AMPEL_CSV
csv_writer::initialize();
......@@ -137,7 +139,7 @@ void loop() {
csv_writer::logIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity);
#endif
#ifdef AMPEL_MQTT
#if defined(AMPEL_WIFI) && defined(AMPEL_MQTT)
mqtt::publishIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity);
#endif
......@@ -179,18 +181,20 @@ void checkFlashButton() {
}
void keepServicesAlive() {
#ifdef AMPEL_WIFI
if (WiFi.status() == WL_CONNECTED) {
#if defined(ESP8266)
# if defined(ESP8266)
//NOTE: Sadly, there seems to be a bug in the current MDNS implementation.
// It stops working after 2 minutes. And forcing a restart leads to a memory leak.
MDNS.update();
#endif
# endif
ntp::update(); // NTP client has its own timer. It will connect to NTP server every 60s.
#ifdef AMPEL_HTTP
# ifdef AMPEL_HTTP
web_server::update();
#endif
#ifdef AMPEL_MQTT
# endif
# ifdef AMPEL_MQTT
mqtt::keepConnection(); // MQTT client has its own timer. It will keep alive every 15s.
#endif
# endif
}
#endif
}
......@@ -2,16 +2,11 @@
namespace config {
// WiFi config. See 'config.h' if you want to modify those values.
#ifdef WIFI_SSID
const char *wifi_ssid = WIFI_SSID;
const char *wifi_password = WIFI_PASSWORD;
#else
const char *wifi_ssid = "NO_WIFI";
const char *wifi_password = "";
#endif
#ifdef WIFI_TIMEOUT
const uint8_t wifi_timeout = WIFI_TIMEOUT; // [s] Will try to connect during wifi_timeout seconds before failing.
const uint8_t wifi_timeout = WIFI_TIMEOUT; // [s] Will try to connect during wifi_timeout seconds before failing.
#else
const uint8_t wifi_timeout = 60; // [s] Will try to connect during wifi_timeout seconds before failing.
#endif
......@@ -20,14 +15,8 @@ namespace config {
// Initialize Wi-Fi
void WiFiConnect(const String &hostname) {
//NOTE: WiFi Multi could allow multiple SSID and passwords.
if (strcmp(config::wifi_ssid, "NO_WIFI") == 0) {
Serial.println("Please change WIFI_SSID in config.h if you want to connect.");
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
return;
}
WiFi.persistent(false); // Don't write user & password to Flash.
WiFi.mode(WIFI_STA); // Set ESP8266 to be a WiFi-client only
WiFi.mode(WIFI_STA); // Set ESP8266 to be a WiFi-client only
#if defined(ESP8266)
WiFi.hostname(hostname);
#elif defined(ESP32)
......
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