7
7
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License:
8
8
http://creativecommons.org/licenses/by-sa/4.0/
9
9
10
- Version 2.7.4
10
+ Version 2.8.0
11
11
Changelog:
12
12
13
+ Version 2.8.0: Compatibility with the new aREST cloud server
14
+ Version 2.7.5: Added rate-limitation for publish()
13
15
Version 2.7.4: Fix for the Arduino Ethernet 2.0 library
14
16
Version 2.7.3: Added support to set your own ID when using API key
15
17
Version 2.7.2: Bug fixes for aREST.io
@@ -283,26 +285,68 @@ void subscribe(const String& device, const String& eventName) {
283
285
template <typename T>
284
286
void publish (PubSubClient& client, const String& eventName, T data) {
285
287
286
- // Get event data
287
- if (DEBUG_MODE) {
288
- Serial.print (" Publishing event " + eventName + " with data: " );
289
- Serial.println (data);
290
- }
288
+ uint32_t currentMillis = millis ();
291
289
292
- // Build message
293
- String message = " {\" client_id\" : \" " + id + " \" , \" event_name\" : \" " + eventName + " \" , \" data\" : \" " + String (data) + " \" }" ;
290
+ if (currentMillis - previousMillis >= interval) {
291
+
292
+ previousMillis = currentMillis;
293
+
294
+ // Get event data
295
+ if (DEBUG_MODE) {
296
+ Serial.print (" Publishing event " + eventName + " with data: " );
297
+ Serial.println (data);
298
+ }
299
+
300
+ // Build message
301
+ String message = " {\" client_id\" : \" " + id + " \" , \" event_name\" : \" " + eventName + " \" , \" data\" : \" " + String (data) + " \" }" ;
302
+
303
+ if (DEBUG_MODE) {
304
+ Serial.print (" Sending message via MQTT: " );
305
+ Serial.println (message);
306
+ }
307
+
308
+ // Convert
309
+ char charBuf[100 ];
310
+ message.toCharArray (charBuf, 100 );
311
+
312
+ // Publish
313
+ client.publish (publish_topic, charBuf);
294
314
295
- if (DEBUG_MODE) {
296
- Serial.print (" Sending message via MQTT: " );
297
- Serial.println (message);
298
315
}
299
316
300
- // Convert
301
- char charBuf[100 ];
302
- message.toCharArray (charBuf, 100 );
317
+ }
318
+
319
+ template <typename T>
320
+ void publish (PubSubClient& client, const String& eventName, T data, uint32_t customInterval) {
321
+
322
+ uint32_t currentMillis = millis ();
323
+
324
+ if (currentMillis - previousMillis >= customInterval) {
325
+
326
+ previousMillis = currentMillis;
327
+
328
+ // Get event data
329
+ if (DEBUG_MODE) {
330
+ Serial.print (" Publishing event " + eventName + " with data: " );
331
+ Serial.println (data);
332
+ }
333
+
334
+ // Build message
335
+ String message = " {\" client_id\" : \" " + id + " \" , \" event_name\" : \" " + eventName + " \" , \" data\" : \" " + String (data) + " \" }" ;
303
336
304
- // Publish
305
- client.publish (publish_topic, charBuf);
337
+ if (DEBUG_MODE) {
338
+ Serial.print (" Sending message via MQTT: " );
339
+ Serial.println (message);
340
+ }
341
+
342
+ // Convert
343
+ char charBuf[100 ];
344
+ message.toCharArray (charBuf, 100 );
345
+
346
+ // Publish
347
+ client.publish (publish_topic, charBuf);
348
+
349
+ }
306
350
307
351
}
308
352
@@ -1937,6 +1981,10 @@ void setMQTTServer(char* new_mqtt_server){
1937
1981
// Status LED
1938
1982
uint8_t status_led_pin;
1939
1983
1984
+ // Interval
1985
+ uint32_t previousMillis;
1986
+ uint32_t interval = 1000 ;
1987
+
1940
1988
// Int variables arrays
1941
1989
uint8_t variables_index;
1942
1990
Variable* variables[NUMBER_VARIABLES];
@@ -1956,7 +2004,7 @@ void setMQTTServer(char* new_mqtt_server){
1956
2004
char * subscriptions_names[NUMBER_SUBSCRIPTIONS];
1957
2005
1958
2006
// aREST.io server
1959
- char * mqtt_server = " 104.131.78.157 " ;
2007
+ char * mqtt_server = " 104.248.48.85 " ;
1960
2008
bool private_mqtt_server;
1961
2009
1962
2010
#endif
0 commit comments