#ifndef SensorBase_h #define SensorBase_h #include // https://github.com/esp8266/Arduino #include // https://github.com/tzapu/WiFiManager #include // https://github.com/datacute/DoubleResetDetector // Support for real-time clock #include #include #include #include #define MAX_SUBSCRIPTIONS 25 #define DRD_TIMEOUT 10 #define DRD_ADDRESS 0 struct subscription { bool isUsed = false; int qos = 1; String topic; subscription() {} }; struct mqtt { String server; int port = 1883; String username; String password; String locationID; String deviceID; bool enableHeartbeat; String DEVICE; String ALIVE; String HEARRTBEAT; String HEARRTBEAT_STATUS; String REFRESH; String RESETWIFI; struct subscription subscriptions[MAX_SUBSCRIPTIONS]; mqtt(){} }; class SensorBase { private: String _wifiSSID = ""; String _wifiPassword = ""; int _wifiLED = -1; int _wifiAPLED = -1; // Mosquitto Broker mqtt _mqtt; int mqttConnectFails = 0; char clientID[25]; bool _statusAsJSON = false; bool _publishAlives = true; bool _suspend = false; WiFiClient _wiFiClient; PubSubClient _pubSubClient = PubSubClient(_wiFiClient); WiFiUDP _ntpUDP; NTPClient _timeClient = NTPClient(_ntpUDP); Metro everyTenSeconds = Metro(1000*10); DoubleResetDetector _drd = DoubleResetDetector(DRD_TIMEOUT, DRD_ADDRESS); void mqttConnect(); bool publishMQTTHeartbeat(); public: SensorBase(); virtual void begin(); virtual void loop(); virtual bool refreshSensor() {}; char* getClientID(); WiFiClient getWiFiClient(); bool setWifiSettings(String SSID, String password); bool setWifiLED(int wifiLED); bool setWifiAPLED(int wifiAPLED); // Mosquitto Broker void setMQTT(String server, int port, String username, String password, String locationID, String deviceID); void setMQTT(mqtt mqtt); bool setMQTTCallback(MQTT_CALLBACK_SIGNATURE); bool setMQTTSuspendPublish(bool suspend); bool setMQTTHeartbeat(bool heartbeat); bool setMQTTStatusAsJSON(bool statusAsJSON); bool setMQTTPublishAlives(bool publishAlives); bool addMQTTSubscription(String topic, const int qos); char* mqtt_callback(char* topic, byte* payload, unsigned int length); // catch what we need PubSubClient getPubSubClient(); bool publishMQTT(String topic, String payload, boolean retained); bool publishMQTT(String topic, String payload); bool publishMQTTStatus(String status); bool publishMQTTError(String status); }; #endif