mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-10 15:36:38 +02:00
* added mqtt
This commit is contained in:
parent
8bfbd8d45b
commit
c00be7bb35
10 changed files with 299 additions and 38 deletions
90
tools/esp8266/mqtt.h
Normal file
90
tools/esp8266/mqtt.h
Normal file
|
@ -0,0 +1,90 @@
|
|||
#ifndef __MQTT_H__
|
||||
#define __MQTT_H__
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include "defines.h"
|
||||
|
||||
class mqtt {
|
||||
public:
|
||||
mqtt() {
|
||||
mClient = new PubSubClient(mEspClient);
|
||||
mAddressSet = false;
|
||||
|
||||
memset(mUser, 0, MQTT_USER_LEN);
|
||||
memset(mPwd, 0, MQTT_PWD_LEN);
|
||||
memset(mTopic, 0, MQTT_TOPIC_LEN);
|
||||
}
|
||||
|
||||
~mqtt() {
|
||||
delete mClient;
|
||||
}
|
||||
|
||||
void setup(const char *broker, const char *topic, const char *user, const char *pwd) {
|
||||
mAddressSet = true;
|
||||
mClient->setServer(broker, 1883);
|
||||
|
||||
snprintf(mUser, MQTT_USER_LEN, "%s", user);
|
||||
snprintf(mPwd, MQTT_PWD_LEN, "%s", pwd);
|
||||
snprintf(mTopic, MQTT_TOPIC_LEN, "%s", topic);
|
||||
}
|
||||
|
||||
void sendMsg(const char *topic, const char *msg) {
|
||||
if(mAddressSet) {
|
||||
uint8_t len = MQTT_TOPIC_LEN + strlen(msg);
|
||||
char top[len];
|
||||
snprintf(top, len, "%s/%s", mTopic, topic);
|
||||
|
||||
if(!mClient->connected())
|
||||
reconnect();
|
||||
mClient->publish(top, msg);
|
||||
}
|
||||
}
|
||||
|
||||
bool isConnected(bool doRecon = false) {
|
||||
if(doRecon)
|
||||
reconnect();
|
||||
return mClient->connected();
|
||||
}
|
||||
|
||||
char *getUser(void) {
|
||||
return mUser;
|
||||
}
|
||||
|
||||
char *getPwd(void) {
|
||||
return mUser;
|
||||
}
|
||||
|
||||
char *getTopic(void) {
|
||||
return mTopic;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
//if(!mClient->connected())
|
||||
// reconnect();
|
||||
mClient->loop();
|
||||
}
|
||||
|
||||
private:
|
||||
void reconnect(void) {
|
||||
if(!mClient->connected()) {
|
||||
String mqttId = "ESP-" + String(random(0xffff), HEX);
|
||||
if((strlen(mUser) > 0) && (strlen(mPwd) > 0)) {
|
||||
mClient->connect(mqttId.c_str(), mUser, mPwd);
|
||||
}
|
||||
else {
|
||||
mClient->connect(mqttId.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WiFiClient mEspClient;
|
||||
PubSubClient *mClient;
|
||||
|
||||
bool mAddressSet;
|
||||
char mUser[MQTT_USER_LEN];
|
||||
char mPwd[MQTT_PWD_LEN];
|
||||
char mTopic[MQTT_TOPIC_LEN];
|
||||
};
|
||||
|
||||
#endif /*__MQTT_H_*/
|
Loading…
Add table
Add a link
Reference in a new issue