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+ }
0 commit comments