Skip to content

Commit 8854244

Browse files
committed
initial commit
1 parent 7b055ae commit 8854244

File tree

6 files changed

+297
-1
lines changed

6 files changed

+297
-1
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
# EasyMqtt
2-
Easy setup of Mqtt on the esp8266
2+
Easy handling of Mqtt on esp6288
3+
4+
This library is currentlly under heavy development.
5+
And shoud not be used at the moment.
6+
7+
## Examble usage EasyMqtt
8+
```C++
9+
#include <EasyMqtt.h>
10+
11+
EasyMqtt mqtt;
12+
13+
void setup() {
14+
Serial.begin(115200);
15+
Serial.println();
16+
17+
// Setup wifi
18+
mqtt.wifi("ssid", "password");
19+
mqtt.mqtt("host", 1883, "user", "pass");
20+
21+
22+
mqtt["wifi"]["rssi"] << [](){ return String(WiFi.RSSI()); };
23+
}
24+
25+
void loop() {
26+
mqtt.loop();
27+
}
28+
29+
```

keywords.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#######################################
2+
# Syntax Coloring Map For PubSubClient
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
EasyMqtt KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
16+
#######################################
17+
# Constants (LITERAL1)
18+
#######################################

library.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "EasyMqtt",
3+
"keywords": "ethernet, mqtt, m2m, iot",
4+
"description": "Makes it easier to get up and runnging with mqtt on the esp8266",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/bloft/EasyMqtt.git"
8+
},
9+
"version": "2.6",
10+
"exclude": "tests",
11+
"examples": "examples/*/*.ino",
12+
"frameworks": "arduino",
13+
"platforms": [
14+
"espressif"
15+
]
16+
}

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=EasyMqtt
2+
version=0.1
3+
author=Bjarne Loft <[email protected]>
4+
maintainer=Bjarne Loft <[email protected]>
5+
sentence=A wrapper library for using mqtt on the ESP8266
6+
paragraph=Makes it easyer to get up and running with Mqtt on the esp8266
7+
category=Communication
8+
url=...
9+
architectures=*

src/EasyMqtt.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#ifndef EasyMqtt_h
2+
#define EasyMqtt_h
3+
4+
#include <ESP8266WiFi.h>
5+
#include <PubSubClient.h>
6+
#include "MqttMap.h"
7+
8+
class EasyMqtt : public MqttMap {
9+
private:
10+
WiFiClient wifiClient;
11+
PubSubClient mqttClient;
12+
13+
String deviceId = "EasyMqtt";
14+
const char* mqtt_username = "N/A";
15+
const char* mqtt_password = "N/A";
16+
17+
protected:
18+
virtual String getTopic() {
19+
return String("easyMqtt/") + deviceId;
20+
}
21+
22+
MqttMap & getDiag() {
23+
return operator[]("diag");
24+
}
25+
26+
/**
27+
* Handle connections to mqtt
28+
*/
29+
void mqttReconnect() {
30+
while (!mqttClient.connected()) {
31+
if (mqttClient.connect(deviceId.c_str(), mqtt_username, mqtt_password)) {
32+
Serial.println("MQTT connected");
33+
subscribe(mqttClient);
34+
} else {
35+
Serial.print("failed, rc=");
36+
Serial.print(mqttClient.state());
37+
Serial.println(" try again in 5 seconds");
38+
delay(5000);
39+
}
40+
}
41+
}
42+
43+
public:
44+
EasyMqtt() : MqttMap("easyMqtt") {
45+
deviceId = String(ESP.getChipId());
46+
47+
getDiag().setInterval(30l);
48+
getDiag()["mem"]["heap"] << [](){ return String(ESP.getFreeHeap()); };
49+
}
50+
51+
/**
52+
* Setup connection to wifi
53+
*/
54+
void wifi(const char* ssid, const char* password) {
55+
WiFi.mode(WIFI_STA);
56+
WiFi.begin(ssid, password);
57+
58+
while (WiFi.status() != WL_CONNECTED) {
59+
delay(500);
60+
Serial.print(".");
61+
}
62+
Serial.println("WiFi connected");
63+
Serial.println("IP address: ");
64+
Serial.println(WiFi.localIP());
65+
66+
Serial.print("Chip ID : ");
67+
Serial.println(ESP.getChipId());
68+
69+
// Setup wifi diag
70+
getDiag()["wifi"]["rssi"] << [](){ return String(WiFi.RSSI()); };
71+
getDiag()["wifi"]["ssid"] << [](){ return WiFi.SSID(); };
72+
}
73+
74+
/**
75+
* Setup connection to mqtt
76+
*/
77+
void mqtt(const char* host, int port, const char* username, const char* password) {
78+
mqttClient.setClient(wifiClient);
79+
mqttClient.setCallback([&](const char* topic, uint8_t* payload, unsigned int length) { callback(topic, payload, length); });
80+
mqttClient.setServer(host, port);
81+
mqtt_username = username;
82+
mqtt_password = password;
83+
}
84+
85+
/**
86+
* Handle the normal loop
87+
*/
88+
void loop() {
89+
mqttReconnect();
90+
mqttClient.loop();
91+
MqttMap::loop(mqttClient);
92+
}
93+
};
94+
95+
#endif

src/MqttMap.h

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#ifndef MqttMap_h
2+
#define MqttMap_h
3+
4+
#include <Arduino.h>
5+
#include <PubSubClient.h>
6+
7+
class MqttMap {
8+
private:
9+
void (*outFunction)(String payload) = NULL;
10+
String (*inFunction)() = NULL;
11+
12+
const char* name = "N/A";
13+
int interval = 0;
14+
unsigned long nextUpdate = 0; // Last read of data
15+
16+
MqttMap* parent = NULL;
17+
MqttMap* next = NULL;
18+
MqttMap* children = NULL;
19+
20+
protected:
21+
MqttMap(const char* _name, MqttMap& _parent) {
22+
name = _name;
23+
parent = &_parent;
24+
interval = 0;
25+
}
26+
27+
virtual String getTopic() {
28+
if(parent) {
29+
return parent->getTopic() + "/" + name;
30+
} else {
31+
return String(name);
32+
}
33+
}
34+
35+
public:
36+
MqttMap(const char* _name) {
37+
name = _name;
38+
interval = 5;
39+
}
40+
41+
int getInterval() {
42+
if(interval == 0) {
43+
return parent->getInterval();
44+
}
45+
return interval;
46+
}
47+
48+
void setInterval(int _interval) {
49+
interval = _interval;
50+
}
51+
52+
void callback(const char* _topic, uint8_t* payload, unsigned int length) {
53+
if(strcmp(getTopic().c_str(), _topic) == 0) {
54+
if(outFunction != NULL) {
55+
char _payload[length];
56+
for (int i=0;i<length;i++) {
57+
_payload[i] = (char)payload[i];
58+
}
59+
printf("Found callback, %s: %s\n", _topic, _payload);
60+
outFunction(String(_payload));
61+
}
62+
} else {
63+
MqttMap* child = children;
64+
while(child != NULL) {
65+
child->callback(_topic, payload, length);
66+
child = child->next;
67+
}
68+
}
69+
}
70+
71+
void subscribe(PubSubClient& client) {
72+
if(outFunction != NULL) {
73+
String topic = getTopic();
74+
printf("Subscribe: %s\n", topic.c_str());
75+
client.subscribe(topic.c_str());
76+
}
77+
MqttMap* child = children;
78+
while(child != NULL) {
79+
child->subscribe(client);
80+
child = child->next;
81+
}
82+
}
83+
84+
void loop(PubSubClient& client) {
85+
if(inFunction != NULL) {
86+
unsigned long time = millis();
87+
if(time >= nextUpdate) {
88+
nextUpdate = time + (getInterval() * 1000);
89+
String topic = getTopic();
90+
String value = inFunction();
91+
printf("Value(%s): %s\n", topic.c_str(), value.c_str());
92+
client.publish(topic.c_str(), value.c_str());
93+
}
94+
}
95+
MqttMap* child = children;
96+
while(child != NULL) {
97+
child->loop(client);;
98+
child = child->next;
99+
}
100+
}
101+
102+
MqttMap & operator[](const char* name) {
103+
MqttMap * child = children;
104+
while(child != NULL) {
105+
if(strcmp(child->name, name) == 0) {
106+
return *child;
107+
}
108+
child = child->next;
109+
}
110+
MqttMap * oldChild = children;
111+
children = new MqttMap(name, *this);
112+
children->next = oldChild;
113+
return *children;
114+
}
115+
116+
/**
117+
* Read data from function and send it to mqtt
118+
*/
119+
void operator<<(String (*inFunction)()) {
120+
MqttMap::inFunction = inFunction;
121+
}
122+
123+
/**
124+
* Handle data comming from mqtt
125+
*/
126+
void operator>>(void (*outFunction)(String payload)) {
127+
MqttMap::outFunction = outFunction;
128+
}
129+
};
130+
131+
#endif

0 commit comments

Comments
 (0)