Skip to content

Commit

Permalink
fixes for aREST pro
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoschwartz committed Aug 31, 2016
1 parent ba43c2e commit 685bc0b
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 3 deletions.
9 changes: 7 additions & 2 deletions aREST.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License:
http://creativecommons.org/licenses/by-sa/4.0/
Version 2.4.0
Version 2.4.1
Changelog:
Version 2.4.1: Additional fixes for Pro plans
Version 2.4.0: Added support for aREST Pro & several fixes
Version 2.3.1: Fixed pin mapping for NodeMCU/Wemos boards
Version 2.3.0: Implement required changes for the cloud server upgrade
Expand Down Expand Up @@ -241,7 +242,11 @@ void publish(PubSubClient& client, String eventName, T data) {

}

void setKey(char* proKey) {
void setKey(char* proKey, PubSubClient& client) {

// Assign MQTT server
mqtt_server = "104.131.78.157";
client.setServer(mqtt_server, 1883);

// Set key
proKey = proKey;
Expand Down
81 changes: 81 additions & 0 deletions examples/ESP8266_cloud_pro/ESP8266_cloud_pro.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
This a simple example of the aREST Library for the ESP8266 WiFi chip.
This example illustrate the cloud part of aREST that makes the board accessible from anywhere
See the README file for more details.
Written in 2016 by Marco Schwartz under a GPL license.
*/

// Import required libraries
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>

// Clients
WiFiClient espClient;
PubSubClient client(espClient);

// Create aREST instance
aREST rest = aREST(client);

// aREST Pro key (that you can get at dashboard.arest.io)
char * key = "your_arest_key";

// WiFi parameters
const char* ssid = "your_wifi_network_name";
const char* password = "your_wifi_network_password";

// Variables to be exposed to the API
int temperature;
int humidity;

// Functions
void callback(char* topic, byte* payload, unsigned int length);

void setup(void)
{
// Start Serial
Serial.begin(115200);

// Set aREST key
rest.setKey(key, client);

// Set callback
client.setCallback(callback);

// Init variables and expose them to REST API
temperature = 24;
humidity = 40;
rest.variable("temperature",&temperature);
rest.variable("humidity",&humidity);

// Give name to device
rest.set_name("esp8266");

// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

// Set output topic
char* out_topic = rest.get_topic();

}

void loop() {

// Connect to the cloud
rest.handle(client);

}

// Handles message arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {

rest.handle_callback(client, topic, payload, length);

}
101 changes: 101 additions & 0 deletions examples/MKR1000_cloud_pro/MKR1000_cloud_pro.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
This a simple example of the aREST Library for the Arduino/Genuino MKR1000 board.
See the README file for more details.
Written in 2016 by Marco Schwartz under a GPL license.
*/

// Import required libraries
#include <SPI.h>
#include <WiFi101.h>
#include <PubSubClient.h>
#include <aREST.h>

// Status
int status = WL_IDLE_STATUS;

// Clients
WiFiClient wifiClient;
PubSubClient client(wifiClient);

// Create aREST instance
aREST rest = aREST(client);

// aREST Pro key (that you can get at dashboard.arest.io)
char * key = "your_arest_key";

// WiFi parameters
char ssid[] = "your_wifi_network_name";
char password[] = "your_wifi_network_password";

// Variables to be exposed to the API
int temperature;
int humidity;

// Declare functions to be exposed to the API
int ledControl(String command);

// Callback function for the cloud connection
void callback(char* topic, byte* payload, unsigned int length);

void setup(void)
{
// Start Serial
Serial.begin(115200);

// Set aREST key
rest.setKey(key, client);

// Set callback
client.setCallback(callback);

// Init variables and expose them to REST API
temperature = 24;
humidity = 40;
rest.variable("temperature",&temperature);
rest.variable("humidity",&humidity);

// Function to be exposed
rest.function("led",ledControl);

// Give name to device
rest.set_name("mkr1000");

// Connect to WiFi
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, password);

// Wait 10 seconds for connection:
delay(10000);
}
Serial.println("WiFi connected");

}

void loop() {

// Connect to the cloud
rest.handle(client);

}

// Custom function accessible by the API
int ledControl(String command) {

// Get state from command
int state = command.toInt();

digitalWrite(6,state);
return 1;
}


// Handles message arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {

// Handle
rest.handle_callback(client, topic, payload, length);

}
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.4.0
version=2.4.1
author=Marco Schwartz
maintainer=Marco Schwartz <[email protected]>
sentence=RESTful API for the Arduino platform.
Expand Down

0 comments on commit 685bc0b

Please sign in to comment.