Skip to content

Commit

Permalink
fixes for new cloud server
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoschwartz committed May 25, 2019
1 parent 3d766c9 commit aed3cd0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 19 deletions.
82 changes: 65 additions & 17 deletions aREST.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -283,26 +285,68 @@ void subscribe(const String& device, const String& eventName) {
template <typename T>
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 <typename T>
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);

}

}

Expand Down Expand Up @@ -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];
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=aREST
version=2.7.4
version=2.8.0
author=Marco Schwartz
maintainer=Marco Schwartz <[email protected]>
sentence=RESTful API for the Arduino platform.
Expand Down
2 changes: 1 addition & 1 deletion license.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit aed3cd0

Please sign in to comment.