1+ #define MQTT_MAX_PACKET_SIZE 1024
12#if defined(ESP8266)
23#include < ESP8266WiFi.h>
3- #elif defined (ESP32)
4+ #elif defined(ESP32)
45#include < WiFi.h>
56#endif
67#include " PubSubClient/PubSubClient.h"
78#include " ArduinoJson.h"
89
9-
1010String HandleResponse (String query);
1111
1212class ThingESP32
1313{
1414public:
15- ThingESP32 (String username, String deviceName, String password)
15+ ThingESP32 (String username, String deviceName, String password) : g_client(espClient)
1616 {
17- WiFiClient espClient;
18- PubSubClient client (espClient);
19- this ->client = client;
2017 this ->Username = username;
2118 this ->DeviceName = deviceName;
2219 this ->Password = password;
2320 };
2421
25-
2622 void SetWiFi (const char *ssID, const char *ssID_password)
2723 {
2824 this ->ssid = ssID;
2925 this ->ssid_password = ssID_password;
3026 }
3127
32- void logic (String data)
33- {
34- DynamicJsonDocument data_in (1024 );
35- DynamicJsonDocument data_out (1024 );
36- deserializeJson (data_in, data);
37-
38- if (data_in[" action" ] == " query" )
39- {
40- data_out[" msg_id" ] = data_in[" msg_id" ];
41- data_out[" action" ] = " returned_api_response" ;
42- String query = data_in[" query" ];
43- query.toLowerCase ();
44- data_out[" returned_api_response" ] = HandleResponse (query);
45- String outdata;
46- serializeJson (data_out, outdata);
47- publishMSG (outdata.c_str ());
48- }
49- };
50-
51- void sendMsg (String number, String msg)
52- {
53- DynamicJsonDocument data_out (1024 );
54- data_out[" action" ] = " device_call" ;
55- data_out[" to_number" ] = number;
56- data_out[" msg" ] = msg;
57- String outdata;
58- serializeJson (data_out, outdata);
59- publishMSG (outdata.c_str ());
60- }
61-
6228 void initDevice ()
6329 {
64- // this->client = client;
6530 this ->topic = this ->DeviceName + " /" + this ->Username ;
6631 this ->outname = this ->DeviceName + " @" + this ->Username ;
6732
@@ -71,41 +36,66 @@ class ThingESP32
7136 this ->char_topic = this ->topic .c_str ();
7237 this ->initiated = true ;
7338
74- this ->setupIT ();
39+ Serial.println ();
40+ Serial.print (" Connecting to " );
41+ Serial.println (ssid);
42+ Serial.println (mqttServer);
43+
44+ WiFi.begin (ssid, ssid_password);
45+
46+ while (WiFi.status () != WL_CONNECTED)
47+ {
48+ delay (500 );
49+ Serial.print (" ." );
50+ }
51+
52+ randomSeed (micros ());
53+
54+ Serial.println (" " );
55+ Serial.println (" WiFi connected" );
56+ Serial.println (" IP address: " );
57+ Serial.println (WiFi.localIP ());
58+
59+ g_client.setServer (this ->mqttServer , this ->mqttPort );
60+ g_client.setCallback ([this ](char *topic, byte *payload, unsigned int length) {
61+ callback (topic, payload, length);
62+ });
7563 }
7664
7765 void Handle ()
7866 {
79- if (!client .connected ())
67+ if (!g_client .connected ())
8068 {
81- while (!client .connected ())
69+ while (!g_client .connected ())
8270 {
71+ delay (10 );
8372 Serial.print (" Attempting connection..." );
84- if (client .connect (this ->char_outname , this ->char_outname , this ->char_Password ))
73+ if (g_client .connect (this ->char_outname , this ->char_outname , this ->char_Password ))
8574 {
8675 Serial.println (" connected" );
87- client .subscribe (this ->char_topic );
76+ g_client .subscribe (this ->char_topic );
8877 }
8978 else
9079 {
9180 Serial.print (" failed, rc=" );
92- Serial.print (this -> client .state ());
81+ Serial.print (g_client .state ());
9382 Serial.println (" try again in 5 seconds" );
9483 delay (5000 );
9584 }
9685 }
9786 }
98- this -> client .loop ();
87+ g_client .loop ();
9988 }
10089
101- void publishMSG (const char *info)
102- {
103- client.publish (this ->char_topic , info);
104- }
105-
106- void SetHost (const char *host)
90+ void sendMsg (String number, String msg)
10791 {
108- this ->mqttServer = host;
92+ DynamicJsonDocument data_out (1024 );
93+ data_out[" action" ] = " device_call" ;
94+ data_out[" to_number" ] = number;
95+ data_out[" msg" ] = msg;
96+ String outdata;
97+ serializeJson (data_out, outdata);
98+ publishMSG (outdata.c_str ());
10999 }
110100
111101private:
@@ -119,17 +109,19 @@ class ThingESP32
119109 const char *ssid_password;
120110
121111 const char *mqttServer = " thingesp.siddhesh.me" ;
122-
112+
123113 int mqttPort = 1893 ;
124114
125115 String topic;
126116 String outname;
117+
127118 const char *char_DeviceName;
128119 const char *char_Password;
129120 const char *char_outname;
130121 const char *char_topic;
131122
132- PubSubClient client;
123+ WiFiClient espClient;
124+ PubSubClient g_client;
133125
134126 void callback (char *topic, byte *payload, unsigned int length)
135127 {
@@ -144,35 +136,30 @@ class ThingESP32
144136 srr.concat ((char )payload[i]);
145137 }
146138 Serial.print (srr);
147- this -> logic (srr);
139+ onMessage (srr);
148140 }
149141
150- void setupIT ( )
142+ void onMessage (String data )
151143 {
144+ DynamicJsonDocument data_in (1024 );
145+ DynamicJsonDocument data_out (1024 );
146+ deserializeJson (data_in, data);
152147
153- Serial.println ();
154- Serial.print (" Connecting to " );
155- Serial.println (ssid);
156- Serial.println (mqttServer);
157-
158- WiFi.begin (ssid, ssid_password);
159-
160- while (WiFi.status () != WL_CONNECTED)
148+ if (data_in[" action" ] == " query" )
161149 {
162- delay (500 );
163- Serial.print (" ." );
150+ data_out[" msg_id" ] = data_in[" msg_id" ];
151+ data_out[" action" ] = " returned_api_response" ;
152+ String query = data_in[" query" ];
153+ query.toLowerCase ();
154+ data_out[" returned_api_response" ] = HandleResponse (query);
155+ String outdata;
156+ serializeJson (data_out, outdata);
157+ publishMSG (outdata.c_str ());
164158 }
159+ }
165160
166- randomSeed (micros ());
167-
168- Serial.println (" " );
169- Serial.println (" WiFi connected" );
170- Serial.println (" IP address: " );
171- Serial.println (WiFi.localIP ());
172-
173- this ->client .setServer (this ->mqttServer , this ->mqttPort );
174- this ->client .setCallback ([this ](char *topic, byte *payload, unsigned int length) {
175- callback (topic, payload, length);
176- });
161+ void publishMSG (const char *info)
162+ {
163+ g_client.publish (this ->char_topic , info);
177164 }
178165};
0 commit comments