1
- #pragma once
2
-
3
- #if defined(ESP8266)
4
- #include < ESP8266WiFi.h>
5
- #elif defined(ESP32)
6
- #include < WiFi.h>
7
- #endif
8
-
9
- #include < stdint.h>
10
- #include < string.h>
11
-
12
- #include " PubSubClient/PubSubClient.h"
13
- #include " ArduinoJson.h"
14
-
15
- #include " thingesp/Logger.cpp"
16
- #include " thingesp/Device.cpp"
17
- #include " thingesp/RateLimiter.cpp"
18
-
19
- String HandleResponse (String query) __attribute__((weak));
20
-
21
- class ThingESP32 : public DeviceData , public RateLimiter
22
- {
23
- public:
24
- ThingESP32 (const char * _username, const char * _projectName, const char * _credentials) : client(espClient)
25
- {
26
- username = _username;
27
- projectName = _projectName;
28
- credentials = _credentials;
29
-
30
- genMetaData ();
31
- };
32
-
33
-
34
- void sendMsg (String number, String msg)
35
- {
36
- if (is_rate_limited ()) return ;
37
-
38
- DynamicJsonDocument data_out (1024 );
39
- data_out[" action" ] = " device_call" ;
40
- data_out[" to_number" ] = number;
41
- data_out[" msg" ] = msg;
42
- String outdata;
43
- serializeJson (data_out, outdata);
44
- publishMSG (outdata.c_str ());
45
- }
46
-
47
- void initDevice ()
48
- {
49
- if (wifi_configured) {
50
-
51
- LOG_VALUE (" WiFi" , " Connecting to: " , ssid)
52
-
53
- WiFi.begin (ssid, ssid_password);
54
-
55
- while (WiFi.status () != WL_CONNECTED) {
56
- delay (500 );
57
- }
58
-
59
- LOG (" WiFi" , " Connected successfully" );
60
- LOG_VALUE (" WiFi" ," IP address: " , WiFi.localIP ());
61
-
62
-
63
- }
64
-
65
- randomSeed (micros ());
66
-
67
- client.setServer (MQTT_SERVER, MQTT_PORT);
68
- client.setCallback ([this ](char *topic, byte *payload, unsigned int length) {
69
- callback (topic, payload, length);
70
- });
71
- }
72
-
73
- void Handle ()
74
- {
75
- if (!client.connected ())
76
- {
77
- while (!client.connected ())
78
- {
79
- LOG (" SOCKET" , " Attempting connection to ThingESP" )
80
-
81
- if (client.connect (outName.c_str (), outName.c_str (), credentials))
82
- {
83
- LOG (" SOCKET" , " Connected to ThingESP successfully" )
84
- client.subscribe (topic.c_str ());
85
- publishMSG (get_rate_limits_msg ());
86
- }
87
- else
88
- {
89
- LOG_VALUE (" SOCKET" , " Error connecting to ThingESP! Error code: " , client.state ());
90
- if (client.state () == 5 )
91
- LOG (" SOCKET" ," Please check your username, project name or credentials! " )
92
- LOG (" SOCKET" , " Trying again in 10 seconds.." );
93
- delay (10000 );
94
- }
95
- }
96
- }
97
- client.loop ();
98
- }
99
-
100
-
101
- void setCallback ( String(*clbk)(String) ){
102
- this ->callbackFunction = clbk;
103
- }
104
-
105
- private:
106
-
107
- /*
108
- * the callback function
109
- */
110
- String (*callbackFunction)(String);
111
-
112
-
113
- /*
114
- * the WiFi Client
115
- */
116
- WiFiClient espClient;
117
-
118
-
119
-
120
- /*
121
- * PubSubClient for MQTT
122
- */
123
- PubSubClient client;
124
-
125
-
126
- void publishMSG (const char * _msg)
127
- {
128
- client.publish (topic.c_str (), _msg);
129
- }
130
-
131
- void callback (char *topic, byte *payload, unsigned int length)
132
- {
133
- String msg;
134
-
135
- for (int i = 0 ; i < length; i++)
136
- msg.concat ((char )payload[i]);
137
-
138
- onMessage (msg);
139
- }
140
-
141
-
142
- void onMessage (String& data)
143
- {
144
-
145
- DynamicJsonDocument data_in (1024 );
146
- DynamicJsonDocument data_out (1024 );
147
- deserializeJson (data_in, data);
148
-
149
- String incoming_action = data_in[" action" ];
150
-
151
- if (incoming_action == " query" )
152
- {
153
- data_out[" msg_id" ] = data_in[" msg_id" ];
154
- data_out[" action" ] = " returned_api_response" ;
155
- String query = data_in[" query" ];
156
-
157
- #ifndef _DISABLE_LOWER_CASE_
158
- query.toLowerCase ();
159
- #endif
160
-
161
- LOG_VALUE (" MSG" , " Query: " , query);
162
-
163
- String resp = !!HandleResponse ? HandleResponse (query) : this ->callbackFunction (query);
164
-
165
- LOG_VALUE (" MSG" , " Response: " , resp);
166
-
167
- data_out[" returned_api_response" ] = resp;
168
-
169
- String out_msg;
170
- serializeJson (data_out, out_msg);
171
- publishMSG (out_msg.c_str ());
172
-
173
- }
174
- else if (incoming_action == " RATE_LIMITS_INFO" ){
175
- set_rate_limit ((unsigned int )data_in[" delay" ]);
176
- }
177
- };
178
-
179
- };
1
+ // #pragma once
2
+ //
3
+ // #if defined(ESP8266)
4
+ // #include <ESP8266WiFi.h>
5
+ // #elif defined(ESP32)
6
+ // #include <WiFi.h>
7
+ // #endif
8
+ //
9
+ // #include <stdint.h>
10
+ // #include <string.h>
11
+ //
12
+ // #include "PubSubClient/PubSubClient.h"
13
+ // #include "ArduinoJson.h"
14
+ //
15
+ // #include "thingesp/Logger.cpp"
16
+ // #include "thingesp/Device.cpp"
17
+ // #include "thingesp/RateLimiter.cpp"
18
+ //
19
+ // String HandleResponse(String query) __attribute__((weak));
20
+ //
21
+ // class ThingESP32 : public DeviceData, public RateLimiter
22
+ // {
23
+ // public:
24
+ // ThingESP32(const char* _username, const char* _projectName, const char* _credentials) : client(espClient)
25
+ // {
26
+ // username = _username;
27
+ // projectName = _projectName;
28
+ // credentials = _credentials;
29
+ //
30
+ // genMetaData();
31
+ // };
32
+ //
33
+ //
34
+ //
35
+ //
36
+ // void initDevice()
37
+ // {
38
+ // if (wifi_configured) {
39
+ //
40
+ // LOG_VALUE("WiFi", "Connecting to: ", ssid)
41
+ //
42
+ // WiFi.begin(ssid, ssid_password);
43
+ //
44
+ // while (WiFi.status() != WL_CONNECTED) {
45
+ // delay(500);
46
+ // }
47
+ //
48
+ // LOG("WiFi", "Connected successfully");
49
+ // LOG_VALUE("WiFi","IP address: ", WiFi.localIP());
50
+ //
51
+ //
52
+ // }
53
+ //
54
+ // randomSeed(micros());
55
+ //
56
+ // client.setServer(MQTT_SERVER, MQTT_PORT);
57
+ // client.setCallback([this](char *topic, byte *payload, unsigned int length) {
58
+ // callback(topic, payload, length);
59
+ // });
60
+ // }
61
+ //
62
+ // void Handle()
63
+ // {
64
+ // if (!client.connected())
65
+ // {
66
+ // while (!client.connected())
67
+ // {
68
+ // LOG("SOCKET", "Attempting connection to ThingESP")
69
+ //
70
+ // if (client.connect(outName.c_str(), outName.c_str(), credentials))
71
+ // {
72
+ // LOG("SOCKET", "Connected to ThingESP successfully")
73
+ // client.subscribe(topic.c_str());
74
+ // publishMSG(get_rate_limits_msg());
75
+ // }
76
+ // else
77
+ // {
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
+ // LOG("SOCKET", "Trying again in 10 seconds..");
82
+ // delay(10000);
83
+ // }
84
+ // }
85
+ // }
86
+ // client.loop();
87
+ // }
88
+ //
89
+ //
90
+ // void setCallback( String(*clbk)(String) ){
91
+ // this->callbackFunction = clbk;
92
+ // }
93
+ //
94
+ // private:
95
+ //
96
+ // /*
97
+ // * the callback function
98
+ // */
99
+ // String (*callbackFunction)(String);
100
+ //
101
+ //
102
+ // /*
103
+ // * the WiFi Client
104
+ // */
105
+ // WiFiClient espClient;
106
+ //
107
+ //
108
+ //
109
+ // /*
110
+ // * PubSubClient for MQTT
111
+ // */
112
+ // PubSubClient client;
113
+ //
114
+ //
115
+ // void publishMSG(const char* _msg)
116
+ // {
117
+ // client.publish(topic.c_str(), _msg);
118
+ // }
119
+ //
120
+ // void callback(char *topic, byte *payload, unsigned int length)
121
+ // {
122
+ // String msg;
123
+ //
124
+ // for (int i = 0; i < length; i++)
125
+ // msg.concat((char)payload[i]);
126
+ //
127
+ // onMessage(msg);
128
+ // }
129
+ //
130
+ //
131
+ // void onMessage(String& data)
132
+ // {
133
+ //
134
+ // DynamicJsonDocument data_in(1024);
135
+ // DynamicJsonDocument data_out(1024);
136
+ // deserializeJson(data_in, data);
137
+ //
138
+ // String incoming_action = data_in["action"];
139
+ //
140
+ // if (incoming_action == "query")
141
+ // {
142
+ // data_out["msg_id"] = data_in["msg_id"];
143
+ // data_out["action"] = "returned_api_response";
144
+ // String query = data_in["query"];
145
+ //
146
+ // #ifndef _DISABLE_LOWER_CASE_
147
+ // query.toLowerCase();
148
+ // #endif
149
+ //
150
+ // LOG_VALUE("MSG", "Query: ", query);
151
+ //
152
+ // String resp = !!HandleResponse ? HandleResponse(query) : this->callbackFunction(query);
153
+ //
154
+ // LOG_VALUE("MSG", "Response: ", resp);
155
+ //
156
+ // data_out["returned_api_response"] = resp;
157
+ //
158
+ // String out_msg;
159
+ // serializeJson(data_out, out_msg);
160
+ // publishMSG(out_msg.c_str());
161
+ //
162
+ // }
163
+ // else if (incoming_action == "RATE_LIMITS_INFO"){
164
+ // set_rate_limit((unsigned int)data_in["delay"]);
165
+ // }
166
+ // };
167
+ //
168
+ // };
0 commit comments