-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added MQTT Support and enabled 2xBME/P280 Sensors at same time (using…
… different adresses)
- Loading branch information
1 parent
9909248
commit 22e1cad
Showing
30 changed files
with
744 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/**************************************************************************/ | ||
/*! | ||
@file MQTT.cpp | ||
@author M. Fegerl (Sensate Digital Solutions GmbH) | ||
@license GPL (see LICENSE file) | ||
The Sensate ESP8266 firmware is used to connect ESP8266 based hardware | ||
with the Sensate Cloud and the Sensate apps. | ||
----> https://www.sensate.io | ||
SOURCE: https://github.com/sensate-io/firmware-esp8266.git | ||
@section HISTORY | ||
v32 - Added MQTT Support! | ||
*/ | ||
/**************************************************************************/ | ||
|
||
#include "MQTT.h" | ||
|
||
extern unsigned long currentMillis; | ||
boolean enableMQTT; | ||
MQTT *mqtt; | ||
|
||
MQTT::MQTT (char* brokerUrl, long brokerPort, String username, String password) | ||
{ | ||
_username = username; | ||
_password = password; | ||
_brokerPort = brokerPort; | ||
_brokerUrl = String(brokerUrl); | ||
|
||
espClient = WiFiClient(); | ||
pubSubClient = new PubSubClient(espClient); | ||
|
||
clientId = "Sensate-"+getUUID().substring(0,7); | ||
|
||
pubSubClient->setServer(_brokerUrl.c_str(), _brokerPort); | ||
|
||
lastMillis = 0; | ||
} | ||
|
||
MQTT::MQTT (char* brokerUrl, long brokerPort) | ||
{ | ||
_username = "NULL"; | ||
_password = "NULL"; | ||
|
||
clientId = "Sensate-"+getUUID().substring(0,7); | ||
|
||
pubSubClient = new PubSubClient(espClient); | ||
pubSubClient->setServer(brokerUrl, brokerPort); | ||
|
||
} | ||
|
||
bool MQTT::connect() | ||
{ | ||
Serial.println("Connecting to broker with clientId: "+clientId); | ||
|
||
if(_username=="NULL" && _password=="NULL") | ||
{ | ||
Serial.println("Connecting to MQTT broker..."); | ||
|
||
if (pubSubClient->connect(clientId.c_str())) | ||
{ | ||
Serial.println("Connected to MQTT broker"); | ||
return true; | ||
} | ||
else | ||
{ | ||
Serial.print("Connection to MQTT Broker failed with state "); | ||
Serial.print(pubSubClient->state()); | ||
return false; | ||
} | ||
} | ||
else | ||
{ | ||
Serial.println("Connecting to MQTT broker with credentials..."); | ||
|
||
if (pubSubClient->connect(clientId.c_str(), _username.c_str(), _password.c_str())) | ||
{ | ||
Serial.println("Connected to MQTT broker"); | ||
return true; | ||
} | ||
else | ||
{ | ||
Serial.print("Connection to MQTT Broker failed with state "); | ||
Serial.print(pubSubClient->state()); | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
void MQTT::publishForAutoDiscovery(Sensor* sensor) | ||
{ | ||
String pTopic = "homeassistant/sensor/"+clientId+"/"+String(sensor->getId())+"/config"; | ||
String category = sensor->getCategory(); | ||
String pPayload; | ||
|
||
if(category==NULL) | ||
category = "Unnamed"; | ||
|
||
if(sensor->getMqttClass()=="resistance" || sensor->getMqttClass()=="altitude" || sensor->getMqttClass()=="flux" || sensor->getMqttClass()=="") | ||
pPayload = "{\"name\": \""+sensor->getName()+"\", \"state_topic\": \"Sensate/"+category+"/"+sensor->getName()+"/value\", \"unit_of_measurement\": \""+sensor->getMqttUnit()+"\"}"; | ||
else | ||
pPayload = "{\"name\": \""+sensor->getName()+"\", \"device_class\": \""+sensor->getMqttClass()+"\", \"state_topic\": \"Sensate/"+category+"/"+sensor->getName()+"/value\", \"unit_of_measurement\": \""+sensor->getMqttUnit()+"\"}"; | ||
|
||
pubSubClient->publish(pTopic.c_str(), pPayload.c_str()); | ||
} | ||
|
||
void MQTT::loop() | ||
{ | ||
pubSubClient->loop(); | ||
} | ||
|
||
void MQTT::publishSensorData(Data* data[], int dataCount) | ||
{ | ||
boolean republish = false; | ||
|
||
if (!pubSubClient->connected()) { | ||
Serial.println("Trying reconnecting to MQTT broker..."); | ||
republish=connect(); | ||
} | ||
|
||
if((lastMillis == 0) || (currentMillis>lastMillis+300000)) | ||
{ | ||
Serial.println("Republish MQTT Sensors (once every 5min)"); | ||
republish=true; | ||
lastMillis = currentMillis; | ||
} | ||
|
||
for (int i = 0; i < dataCount; i++) | ||
{ | ||
if(republish) | ||
{ | ||
publishForAutoDiscovery(data[i]->getSensor()); | ||
delay(250); | ||
} | ||
|
||
String cat = data[i]->getSensor()->getCategory(); | ||
if(cat.equals("")) | ||
cat="Unnamed"; | ||
String name = data[i]->getSensor()->getName(); | ||
String vTopic = "Sensate/"+cat+"/"+name+"/value"; | ||
pubSubClient->publish(vTopic.c_str(), data[i]->getValueString().c_str()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/**************************************************************************/ | ||
/*! | ||
@file MQTT.h | ||
@author M. Fegerl (Sensate Digital Solutions GmbH) | ||
@license GPL (see LICENSE file) | ||
The Sensate ESP8266 firmware is used to connect ESP8266 based hardware | ||
with the Sensate Cloud and the Sensate apps. | ||
----> https://www.sensate.io | ||
SOURCE: https://github.com/sensate-io/firmware-esp8266.git | ||
@section HISTORY | ||
v32 - Added MQTT Support! | ||
*/ | ||
/**************************************************************************/ | ||
|
||
#include <Arduino.h> | ||
#include <PubSubClient.h> | ||
#include <ESP8266WiFi.h> | ||
|
||
#ifndef _MQTT_h_ | ||
#define _MQTT_h_ | ||
|
||
#include "../controller/UUID.h" | ||
#include "Data.h" | ||
|
||
class MQTT { | ||
private: | ||
unsigned long lastMillis; | ||
String _brokerUrl; | ||
long _brokerPort; | ||
String _username; | ||
String _password; | ||
WiFiClient espClient; | ||
PubSubClient* pubSubClient; | ||
String clientId; | ||
|
||
public: | ||
MQTT (char*, long); | ||
MQTT (char*, long, String, String); | ||
bool connect(void); | ||
void loop(void); | ||
void publishSensorData(Data* data[], int); | ||
void publishForAutoDiscovery(Sensor*); | ||
}; | ||
|
||
#endif |
Oops, something went wrong.