Skip to content

Commit aed3cd0

Browse files
committed
fixes for new cloud server
1 parent 3d766c9 commit aed3cd0

File tree

3 files changed

+67
-19
lines changed

3 files changed

+67
-19
lines changed

aREST.h

+65-17
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License:
88
http://creativecommons.org/licenses/by-sa/4.0/
99
10-
Version 2.7.4
10+
Version 2.8.0
1111
Changelog:
1212
13+
Version 2.8.0: Compatibility with the new aREST cloud server
14+
Version 2.7.5: Added rate-limitation for publish()
1315
Version 2.7.4: Fix for the Arduino Ethernet 2.0 library
1416
Version 2.7.3: Added support to set your own ID when using API key
1517
Version 2.7.2: Bug fixes for aREST.io
@@ -283,26 +285,68 @@ void subscribe(const String& device, const String& eventName) {
283285
template <typename T>
284286
void publish(PubSubClient& client, const String& eventName, T data) {
285287

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();
291289

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);
294314

295-
if (DEBUG_MODE) {
296-
Serial.print("Sending message via MQTT: ");
297-
Serial.println(message);
298315
}
299316

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) + "\"}";
303336

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+
}
306350

307351
}
308352

@@ -1937,6 +1981,10 @@ void setMQTTServer(char* new_mqtt_server){
19371981
// Status LED
19381982
uint8_t status_led_pin;
19391983

1984+
// Interval
1985+
uint32_t previousMillis;
1986+
uint32_t interval = 1000;
1987+
19401988
// Int variables arrays
19411989
uint8_t variables_index;
19421990
Variable* variables[NUMBER_VARIABLES];
@@ -1956,7 +2004,7 @@ void setMQTTServer(char* new_mqtt_server){
19562004
char * subscriptions_names[NUMBER_SUBSCRIPTIONS];
19572005

19582006
// aREST.io server
1959-
char* mqtt_server = "104.131.78.157";
2007+
char* mqtt_server = "104.248.48.85";
19602008
bool private_mqtt_server;
19612009

19622010
#endif

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=aREST
2-
version=2.7.4
2+
version=2.8.0
33
author=Marco Schwartz
44
maintainer=Marco Schwartz <marcolivier.schwartz@gmail.com>
55
sentence=RESTful API for the Arduino platform.

license.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014-2018 Marco Schwartz
3+
Copyright (c) 2014-2019 Marco Schwartz
44
Authors: Marco Schwartz
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy

0 commit comments

Comments
 (0)