Factored out MQTT stuff
This commit is contained in:
62
.vscode/settings.json
vendored
Normal file
62
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"*.module": "php",
|
||||||
|
"*.html": "html",
|
||||||
|
"cctype": "cpp",
|
||||||
|
"clocale": "cpp",
|
||||||
|
"cmath": "cpp",
|
||||||
|
"cstdarg": "cpp",
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"cstdio": "cpp",
|
||||||
|
"cstdlib": "cpp",
|
||||||
|
"cstring": "cpp",
|
||||||
|
"ctime": "cpp",
|
||||||
|
"cwchar": "cpp",
|
||||||
|
"cwctype": "cpp",
|
||||||
|
"array": "cpp",
|
||||||
|
"atomic": "cpp",
|
||||||
|
"strstream": "cpp",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"bitset": "cpp",
|
||||||
|
"chrono": "cpp",
|
||||||
|
"complex": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"deque": "cpp",
|
||||||
|
"list": "cpp",
|
||||||
|
"unordered_map": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"algorithm": "cpp",
|
||||||
|
"functional": "cpp",
|
||||||
|
"iterator": "cpp",
|
||||||
|
"map": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"memory_resource": "cpp",
|
||||||
|
"numeric": "cpp",
|
||||||
|
"random": "cpp",
|
||||||
|
"ratio": "cpp",
|
||||||
|
"set": "cpp",
|
||||||
|
"string": "cpp",
|
||||||
|
"system_error": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"type_traits": "cpp",
|
||||||
|
"utility": "cpp",
|
||||||
|
"fstream": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iomanip": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"iostream": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"new": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"sstream": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"thread": "cpp",
|
||||||
|
"cfenv": "cpp",
|
||||||
|
"cinttypes": "cpp",
|
||||||
|
"typeindex": "cpp",
|
||||||
|
"typeinfo": "cpp"
|
||||||
|
}
|
||||||
|
}
|
||||||
113
include/MQTTHelper.h
Normal file
113
include/MQTTHelper.h
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
#include <PubSubClient.h>
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
|
class MQTTHelper {
|
||||||
|
private:
|
||||||
|
String _group = "homedevice";
|
||||||
|
String _identifier = "garagesensor";
|
||||||
|
const char* _user = "homedevice";
|
||||||
|
const char* _password = "WNzAb4VazNFUPlpkn0ED";
|
||||||
|
|
||||||
|
WiFiClient _espClient;
|
||||||
|
PubSubClient _mqttClient;
|
||||||
|
String _will_topic;
|
||||||
|
|
||||||
|
void reconnect(int delayMillis = 5000);
|
||||||
|
bool connect();
|
||||||
|
void announce();
|
||||||
|
void callback(char* topic, byte* payload, unsigned int length);
|
||||||
|
public:
|
||||||
|
MQTTHelper() : _espClient(), _mqttClient(_espClient) {
|
||||||
|
auto deviceMac = WiFi.macAddress();
|
||||||
|
deviceMac.replace(":","");
|
||||||
|
_identifier = "etxean-"+deviceMac.substring(6);
|
||||||
|
_will_topic = getTopic("online");
|
||||||
|
_mqttClient.setCallback([this] (char* topic, byte* payload, unsigned int length) { this->callback(topic, payload, length); });
|
||||||
|
}
|
||||||
|
|
||||||
|
String getIdentifier() { return _identifier; }
|
||||||
|
|
||||||
|
String getTopic(String sensor) {
|
||||||
|
return _group + "/" + _identifier + "/" + sensor;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Configure(IPAddress server, uint16 port) {
|
||||||
|
Serial.println("Configuring MQTT connection to: "+server.toString()+":"+String(port));
|
||||||
|
_mqttClient.setServer(server, port);
|
||||||
|
if (_mqttClient.connected()) {
|
||||||
|
connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
static unsigned long lastWill;
|
||||||
|
if (!_mqttClient.connected()) {
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
|
_mqttClient.loop();
|
||||||
|
|
||||||
|
auto now = millis();
|
||||||
|
if ((now - lastWill) > 1000)
|
||||||
|
{
|
||||||
|
lastWill = now;
|
||||||
|
_mqttClient.publish(_will_topic.c_str(), "true");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void publish(String sensor, T payload, bool retained = false) {
|
||||||
|
_mqttClient.publish(getTopic(sensor).c_str(), String(payload).c_str(), retained);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void MQTTHelper::callback(char* topic, byte* payload, unsigned int length) {
|
||||||
|
Serial.print("Message arrived:");
|
||||||
|
Serial.print(topic);
|
||||||
|
Serial.print(" = ");
|
||||||
|
for (unsigned int i=0; i<length; i++) {
|
||||||
|
Serial.print((char)payload[i]);
|
||||||
|
}
|
||||||
|
Serial.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MQTTHelper::reconnect(int delayMillis) {
|
||||||
|
// Loop until we're reconnected
|
||||||
|
while (!_mqttClient.connected()) {
|
||||||
|
Serial.print("Attempting MQTT connection...");
|
||||||
|
if (connect()) {
|
||||||
|
Serial.println("connected");
|
||||||
|
} else {
|
||||||
|
auto reconnectSeconds = (int)round(delayMillis/1000.0);
|
||||||
|
Serial.print("failed, rc=");
|
||||||
|
Serial.print(_mqttClient.state());
|
||||||
|
Serial.println(" try again in "+String(reconnectSeconds)+" seconds");
|
||||||
|
// Wait 5 seconds before retrying
|
||||||
|
delay(delayMillis);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MQTTHelper::announce() {
|
||||||
|
DynamicJsonDocument doc(1024);
|
||||||
|
doc["id"] = _identifier;
|
||||||
|
doc["ip"] = WiFi.localIP().toString();
|
||||||
|
doc["mac"] = WiFi.macAddress();
|
||||||
|
String payload;
|
||||||
|
serializeJson(doc,payload);
|
||||||
|
publish("settings",payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MQTTHelper::connect() {
|
||||||
|
// If you do not want to use a username and password, change next line to
|
||||||
|
// if (_mqttClient.connect(_identifier.c_str()) {
|
||||||
|
if (_mqttClient.connect(_identifier.c_str(), _user, _password, _will_topic.c_str(), 2, false, "false")) {
|
||||||
|
_mqttClient.subscribe((getTopic("settings")+"/get").c_str());
|
||||||
|
announce();
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
159
src/main.cpp
159
src/main.cpp
@@ -1,51 +1,71 @@
|
|||||||
#include <Arduino.h>
|
#include <FS.h> //this needs to be first, or it all crashes and burns...
|
||||||
|
|
||||||
|
#include "MQTTHelper.h"
|
||||||
|
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <PubSubClient.h>
|
|
||||||
#include <DNSServer.h>
|
#include <DNSServer.h>
|
||||||
#include <ESP8266WebServer.h>
|
#include <ESP8266WebServer.h>
|
||||||
#include <WiFiManager.h>
|
#include <WiFiManager.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
#define TURN_ON LOW
|
#define TURN_ON LOW
|
||||||
#define TURN_OFF HIGH
|
#define TURN_OFF HIGH
|
||||||
|
|
||||||
#define mqtt_server "192.168.249.5"
|
struct Config {
|
||||||
#define mqtt_port 1883
|
IPAddress mqtt_server = {192, 168, 249, 5};
|
||||||
#define mqtt_user "homedevice"
|
uint16 mqtt_port = 1883;
|
||||||
#define mqtt_password "WNzAb4VazNFUPlpkn0ED"
|
} config;
|
||||||
#define mqtt_client_id "home_DistanceSensor"
|
|
||||||
|
MQTTHelper mqtt;
|
||||||
|
|
||||||
#define distance_topic "garagesensor/distance"
|
|
||||||
#define heartbeatMillis 10000
|
#define heartbeatMillis 10000
|
||||||
#define messageMillis 1000
|
#define messageMillis 1000
|
||||||
#define will_topic "garagesensor/online"
|
|
||||||
#define will_message "false"
|
|
||||||
|
|
||||||
WiFiClient espClient;
|
//flag for saving data
|
||||||
PubSubClient mpttClient(espClient);
|
bool shouldSaveConfig = false;
|
||||||
|
//callback notifying us of the need to save config
|
||||||
|
void saveConfigCallback () {
|
||||||
|
Serial.println("Should save config");
|
||||||
|
shouldSaveConfig = true;
|
||||||
|
}
|
||||||
|
|
||||||
void heartbeat();
|
void heartbeat(int times = 3);
|
||||||
void longbeat();
|
void longbeat();
|
||||||
void setup_wifi(String ssid, String password);
|
void setup_wifi(String ssid, String password);
|
||||||
void reconnect();
|
void reconnect();
|
||||||
|
void readConfig();
|
||||||
|
void saveConfig();
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
Serial.println();
|
||||||
randomSeed(analogRead(0));
|
randomSeed(analogRead(0));
|
||||||
pinMode(LED_BUILTIN, OUTPUT);
|
pinMode(LED_BUILTIN, OUTPUT);
|
||||||
pinMode(LED_BUILTIN, TURN_OFF);
|
pinMode(LED_BUILTIN, TURN_OFF);
|
||||||
|
|
||||||
|
//clean FS, for testing
|
||||||
|
//SPIFFS.format();
|
||||||
|
|
||||||
|
readConfig();
|
||||||
//WiFiManager
|
//WiFiManager
|
||||||
WiFiManager wifiManager;
|
WiFiManager wifiManager;
|
||||||
//wifiManager.resetSettings(); //reset saved settings
|
//wifiManager.resetSettings(); //reset saved settings
|
||||||
wifiManager.autoConnect();
|
|
||||||
|
WiFiManagerParameter custom_mqtt_server("MQTT server", "mqtt server", config.mqtt_server.toString().c_str(), 40);
|
||||||
|
WiFiManagerParameter custom_mqtt_port("MQTT port", "mqtt port", String(config.mqtt_port).c_str(), 6);
|
||||||
|
wifiManager.setSaveConfigCallback(saveConfigCallback);
|
||||||
|
wifiManager.addParameter(&custom_mqtt_server);
|
||||||
|
wifiManager.addParameter(&custom_mqtt_port);
|
||||||
|
wifiManager.autoConnect(mqtt.getIdentifier().c_str());
|
||||||
|
|
||||||
//if you get here you have connected to the WiFi
|
//if you get here you have connected to the WiFi
|
||||||
Serial.print("WiFi connected to: ");
|
Serial.println("WiFi connected to: "+WiFi.SSID()+", IP address: "+WiFi.localIP().toString());
|
||||||
Serial.println(WiFi.SSID());
|
|
||||||
Serial.print("IP address: ");
|
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
|
|
||||||
mpttClient.setServer(mqtt_server, mqtt_port);
|
//read updated parameters
|
||||||
|
config.mqtt_server.fromString(custom_mqtt_server.getValue());
|
||||||
|
config.mqtt_port = String(custom_mqtt_port.getValue()).toInt();
|
||||||
|
saveConfig();
|
||||||
|
mqtt.Configure(config.mqtt_server,config.mqtt_port);
|
||||||
}
|
}
|
||||||
|
|
||||||
long lastMessage = 0;
|
long lastMessage = 0;
|
||||||
@@ -59,11 +79,7 @@ bool checkBound(float newValue, float prevValue, float maxDiff) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (!mpttClient.connected()) {
|
mqtt.loop();
|
||||||
reconnect();
|
|
||||||
}
|
|
||||||
mpttClient.loop();
|
|
||||||
mpttClient.publish(will_topic, "true");
|
|
||||||
|
|
||||||
long now = millis();
|
long now = millis();
|
||||||
if (now - lastHeartbeat > heartbeatMillis) {
|
if (now - lastHeartbeat > heartbeatMillis) {
|
||||||
@@ -75,66 +91,81 @@ void loop() {
|
|||||||
lastMessage = now;
|
lastMessage = now;
|
||||||
|
|
||||||
float newDistance = random(5,400)/100.0;
|
float newDistance = random(5,400)/100.0;
|
||||||
Serial.print("New distance:");
|
// Serial.print("New distance:");
|
||||||
Serial.println(String(newDistance).c_str());
|
// Serial.println(String(newDistance).c_str());
|
||||||
|
|
||||||
if (checkBound(newDistance, distance, diff)) {
|
if (checkBound(newDistance, distance, diff)) {
|
||||||
distance = newDistance;
|
distance = newDistance;
|
||||||
Serial.print(distance_topic);
|
auto sensorName = "distance";
|
||||||
Serial.print(" = ");
|
Serial.print(mqtt.getTopic(sensorName));
|
||||||
Serial.println(String(distance).c_str());
|
Serial.println(" = "+String(distance));
|
||||||
mpttClient.publish(distance_topic, String(distance).c_str());
|
mqtt.publish("distance", distance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup_wifi(String ssid, String password) {
|
void readConfig() {
|
||||||
delay(10);
|
//read configuration from FS json
|
||||||
// We start by connecting to a WiFi network
|
Serial.println("mounting FS...");
|
||||||
|
if (SPIFFS.begin()) {
|
||||||
|
Serial.println("mounted file system");
|
||||||
|
if (SPIFFS.exists("/config.json")) {
|
||||||
|
//file exists, reading and loading
|
||||||
|
Serial.println("reading config.json");
|
||||||
|
File configFile = SPIFFS.open("/config.json", "r");
|
||||||
|
if (configFile) {
|
||||||
|
Serial.println("opened config.json");
|
||||||
|
DynamicJsonDocument doc(1024);
|
||||||
|
DeserializationError error = deserializeJson(doc,configFile);
|
||||||
|
serializeJsonPretty(doc,Serial);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.print("Connecting to ");
|
if (error) {
|
||||||
Serial.println(ssid);
|
Serial.print("failed to load config.json: ");
|
||||||
|
Serial.println(error.c_str());
|
||||||
WiFi.begin(ssid, password);
|
return;
|
||||||
|
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
|
||||||
delay(500);
|
|
||||||
Serial.print(".");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("");
|
Serial.println("\nparsed config.json");
|
||||||
Serial.println("WiFi connected");
|
IPAddress addr;
|
||||||
Serial.print("IP address: ");
|
if (addr.fromString(doc["mqtt_server"].as<char*>())) {
|
||||||
Serial.println(WiFi.localIP());
|
config.mqtt_server = addr;
|
||||||
|
}
|
||||||
|
config.mqtt_port = doc["mqtt_port"];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void reconnect() {
|
|
||||||
// Loop until we're reconnected
|
|
||||||
while (!mpttClient.connected()) {
|
|
||||||
Serial.print("Attempting MQTT connection...");
|
|
||||||
// Attempt to connect
|
|
||||||
// If you do not want to use a username and password, change next line to
|
|
||||||
// if (mpttClient.connect(mqtt_client_id)) {
|
|
||||||
if (mpttClient.connect(mqtt_client_id, mqtt_user, mqtt_password, will_topic, 2, false, will_message)) {
|
|
||||||
Serial.println("connected");
|
|
||||||
} else {
|
} else {
|
||||||
Serial.print("failed, rc=");
|
Serial.println("failed to mount FS");
|
||||||
Serial.print(mpttClient.state());
|
|
||||||
Serial.println(" try again in 5 seconds");
|
|
||||||
// Wait 5 seconds before retrying
|
|
||||||
delay(5000);
|
|
||||||
}
|
}
|
||||||
|
//end read
|
||||||
|
}
|
||||||
|
|
||||||
|
void saveConfig() {
|
||||||
|
//save the custom parameters to FS
|
||||||
|
if (shouldSaveConfig) {
|
||||||
|
Serial.println("saving config.json");
|
||||||
|
DynamicJsonDocument doc(1024);
|
||||||
|
doc["mqtt_server"] = config.mqtt_server.toString();
|
||||||
|
doc["mqtt_port"] = String(config.mqtt_port);
|
||||||
|
|
||||||
|
File configFile = SPIFFS.open("/config.json", "w");
|
||||||
|
if (!configFile) {
|
||||||
|
Serial.println("failed to open config.json for writing");
|
||||||
|
}
|
||||||
|
|
||||||
|
serializeJsonPretty(doc,Serial);
|
||||||
|
serializeJson(doc,configFile);
|
||||||
|
configFile.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void heartbeat()
|
void heartbeat(int times)
|
||||||
{
|
{
|
||||||
for (int i=0; i<2; i++)
|
for (int blink=0; blink<times; blink++)
|
||||||
{
|
{
|
||||||
digitalWrite(LED_BUILTIN, TURN_ON);
|
digitalWrite(LED_BUILTIN, TURN_ON);
|
||||||
delay(50);
|
delay(20);
|
||||||
digitalWrite(LED_BUILTIN, TURN_OFF);
|
digitalWrite(LED_BUILTIN, TURN_OFF);
|
||||||
delay(100);
|
delay(200);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user