Skip to content

Commit 0ca73bf

Browse files
committed
refactored bunch of code, & added local-limiting
1 parent 9f19fec commit 0ca73bf

File tree

10 files changed

+310
-461
lines changed

10 files changed

+310
-461
lines changed

keywords.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
#######################################
44

55
#######################################
6-
# Datatypes (KEYWORD1) Classes
6+
# Datatypes (KEYWORD1)
77
#######################################
88

9-
WH_ESP8266 KEYWORD1
9+
ThingESP32 KEYWORD1
10+
ThingESP8266 KEYWORD1
1011

1112
#######################################
12-
# Datatypes (KEYWORD2) Methods
13+
# Methods and Functions (KEYWORD2)
1314
#######################################
1415

15-
DeviceClient KEYWORD2
16+
SetWiFi KEYWORD2
17+
initDevice KEYWORD2
18+
setCallback KEYWORD2
19+
Handle KEYWORD2
20+
sendMsg KEYWORD2
1621

22+
######################################
23+
# Constants (LITERAL1)
1724
#######################################
18-
# Datatypes (KEYWORD3) Setup & Loop
19-
#######################################
20-
21-
SetDevice KEYWORD3
22-
SetWiFi KEYWORD3
23-
initDevice KEYWORD3
24-
Handle KEYWORD3
25-
HandleResponse KEYWORD3
25+
HandleResponse LITERAL1

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ThingESP
2-
version=1.2.2
2+
version=1.3.0
33
author=SiddheshNan <hello@siddhesh.me>
44
maintainer=SiddheshNan <hello@siddhesh.me>
55
sentence=Arduino library for the ThingsESP Platform.

src/ThingESP.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
#define MQTT_MAX_PACKET_SIZE 1024
2+
#include "./ThingESPClient.cpp"
23

34
#if defined(ESP8266)
4-
#include "ThingESP_8266.cpp"
5+
class ThingESP8266 : public thing_esp::ThingESPClient {
6+
public:
7+
ThingESP8266(const char* _username, const char* _projectName, const char* _credentials) : ThingESPClient(_username, _projectName, _credentials)
8+
{}
9+
};
510
#elif defined(ESP32)
6-
#include "ThingESP_32.cpp"
11+
class ThingESP32 : public thing_esp::ThingESPClient {
12+
public:
13+
ThingESP32(const char* _username, const char* _projectName, const char* _credentials) : ThingESPClient(_username, _projectName, _credentials)
14+
{}
15+
};
716
#endif
17+
18+
19+

src/ThingESPClient.cpp

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#pragma once
2+
3+
#if defined(ESP8266)
4+
#include <ESP8266WiFi.h>
5+
#elif defined(ESP32)
6+
#include <WiFi.h>
7+
#endif
8+
9+
#ifndef _DISABLE_TLS_
10+
11+
#include <WiFiClientSecure.h>
12+
13+
#endif
14+
15+
#include <stdint.h>
16+
#include <string.h>
17+
18+
#include "PubSubClient/PubSubClient.h"
19+
#include "ArduinoJson.h"
20+
21+
#include "thingesp/Logger.cpp"
22+
#include "thingesp/Device.cpp"
23+
#include "thingesp/RateLimiter.cpp"
24+
#include "thingesp/Message.cpp"
25+
26+
namespace thing_esp {
27+
class ThingESPClient : public DeviceData, public Message {
28+
public:
29+
ThingESPClient(const char *_username, const char *_projectName, const char *_credentials) : client(espClient) {
30+
31+
#if !defined(_DISABLE_TLS_) && !defined(ESP32)
32+
espClient.setInsecure();
33+
delay(2);
34+
#endif
35+
36+
username = _username;
37+
projectName = _projectName;
38+
credentials = _credentials;
39+
40+
genMetaData();
41+
};
42+
43+
void initDevice() {
44+
if (wifi_configured) {
45+
46+
LOG_VALUE("WiFi", "Connecting to: ", ssid)
47+
48+
WiFi.begin(ssid, ssid_password);
49+
50+
while (WiFi.status() != WL_CONNECTED) {
51+
delay(500);
52+
}
53+
54+
LOG("WiFi", "Connected successfully");
55+
LOG_VALUE("WiFi", "IP address: ", WiFi.localIP());
56+
57+
58+
}
59+
60+
randomSeed(micros());
61+
62+
client.setServer(MQTT_SERVER, MQTT_PORT);
63+
client.setCallback([this](char *topic, byte *payload, unsigned int length) {
64+
callback(topic, payload, length);
65+
});
66+
}
67+
68+
void Handle() {
69+
if (!client.connected()) {
70+
while (!client.connected()) {
71+
LOG("SOCKET", "Attempting connection to ThingESP")
72+
73+
if (client.connect(outName.c_str(), outName.c_str(), credentials)) {
74+
LOG("SOCKET", "Connected to ThingESP successfully")
75+
client.subscribe(topic.c_str());
76+
publishMSG(get_rate_limits_msg());
77+
} else {
78+
LOG_VALUE("SOCKET", "Error connecting to ThingESP! Error code: ", client.state());
79+
if (client.state() == 5) {
80+
LOG("SOCKET", "Please check your username, project name or credentials! ");
81+
}
82+
LOG("SOCKET", "Trying again in 10 seconds..");
83+
delay(10000);
84+
}
85+
}
86+
}
87+
client.loop();
88+
}
89+
90+
91+
protected:
92+
93+
/*
94+
* the callback function
95+
*/
96+
String (*callbackFunction)(String)
97+
98+
override;
99+
100+
101+
/*
102+
* the WiFi Client
103+
*/
104+
#if !defined(_DISABLE_TLS_) && !defined(ESP32)
105+
WiFiClientSecure espClient;
106+
#else
107+
WiFiClient espClient;
108+
#endif
109+
110+
111+
/*
112+
* PubSubClient for MQTT
113+
*/
114+
PubSubClient client;
115+
116+
117+
void publishMSG(const char *_msg) override {
118+
client.publish(topic.c_str(), _msg);
119+
}
120+
121+
void callback(char *topic, byte *payload, unsigned int length) {
122+
String msg;
123+
124+
for (int i = 0; i < length; i++)
125+
msg.concat((char) payload[i]);
126+
127+
this->onMessage(msg);
128+
}
129+
130+
131+
};
132+
133+
}

src/ThingESP_32.cpp

Lines changed: 0 additions & 168 deletions
This file was deleted.

0 commit comments

Comments
 (0)