diff --git a/aREST.h b/aREST.h index 9773eb9..cc8c620 100644 --- a/aREST.h +++ b/aREST.h @@ -7,9 +7,11 @@ This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License: http://creativecommons.org/licenses/by-sa/4.0/ - Version 2.7.4 + Version 2.8.0 Changelog: + Version 2.8.0: Compatibility with the new aREST cloud server + Version 2.7.5: Added rate-limitation for publish() Version 2.7.4: Fix for the Arduino Ethernet 2.0 library Version 2.7.3: Added support to set your own ID when using API key Version 2.7.2: Bug fixes for aREST.io @@ -283,26 +285,68 @@ void subscribe(const String& device, const String& eventName) { template void publish(PubSubClient& client, const String& eventName, T data) { - // Get event data - if (DEBUG_MODE) { - Serial.print("Publishing event " + eventName + " with data: "); - Serial.println(data); - } + uint32_t currentMillis = millis(); - // Build message - String message = "{\"client_id\": \"" + id + "\", \"event_name\": \"" + eventName + "\", \"data\": \"" + String(data) + "\"}"; + if (currentMillis - previousMillis >= interval) { + + previousMillis = currentMillis; + + // Get event data + if (DEBUG_MODE) { + Serial.print("Publishing event " + eventName + " with data: "); + Serial.println(data); + } + + // Build message + String message = "{\"client_id\": \"" + id + "\", \"event_name\": \"" + eventName + "\", \"data\": \"" + String(data) + "\"}"; + + if (DEBUG_MODE) { + Serial.print("Sending message via MQTT: "); + Serial.println(message); + } + + // Convert + char charBuf[100]; + message.toCharArray(charBuf, 100); + + // Publish + client.publish(publish_topic, charBuf); - if (DEBUG_MODE) { - Serial.print("Sending message via MQTT: "); - Serial.println(message); } - // Convert - char charBuf[100]; - message.toCharArray(charBuf, 100); +} + +template +void publish(PubSubClient& client, const String& eventName, T data, uint32_t customInterval) { + + uint32_t currentMillis = millis(); + + if (currentMillis - previousMillis >= customInterval) { + + previousMillis = currentMillis; + + // Get event data + if (DEBUG_MODE) { + Serial.print("Publishing event " + eventName + " with data: "); + Serial.println(data); + } + + // Build message + String message = "{\"client_id\": \"" + id + "\", \"event_name\": \"" + eventName + "\", \"data\": \"" + String(data) + "\"}"; - // Publish - client.publish(publish_topic, charBuf); + if (DEBUG_MODE) { + Serial.print("Sending message via MQTT: "); + Serial.println(message); + } + + // Convert + char charBuf[100]; + message.toCharArray(charBuf, 100); + + // Publish + client.publish(publish_topic, charBuf); + + } } @@ -1937,6 +1981,10 @@ void setMQTTServer(char* new_mqtt_server){ // Status LED uint8_t status_led_pin; + // Interval + uint32_t previousMillis; + uint32_t interval = 1000; + // Int variables arrays uint8_t variables_index; Variable* variables[NUMBER_VARIABLES]; @@ -1956,7 +2004,7 @@ void setMQTTServer(char* new_mqtt_server){ char * subscriptions_names[NUMBER_SUBSCRIPTIONS]; // aREST.io server - char* mqtt_server = "104.131.78.157"; + char* mqtt_server = "104.248.48.85"; bool private_mqtt_server; #endif diff --git a/library.properties b/library.properties index 29916b1..3975b9f 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=aREST -version=2.7.4 +version=2.8.0 author=Marco Schwartz maintainer=Marco Schwartz sentence=RESTful API for the Arduino platform. diff --git a/license.md b/license.md index 59f80d3..fb45f85 100644 --- a/license.md +++ b/license.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014-2018 Marco Schwartz +Copyright (c) 2014-2019 Marco Schwartz Authors: Marco Schwartz Permission is hereby granted, free of charge, to any person obtaining a copy