Skip to content

Commit

Permalink
added support for cloud.arest.io
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoschwartz committed Nov 25, 2015
1 parent ca661e0 commit af9a9eb
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 31 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# aREST [![Build Status](https://travis-ci.org/marcoschwartz/aREST.svg?branch=master)](https://travis-ci.org/marcoschwartz/aREST)

Version 2.0.0
Version 2.0.1

## Overview

Expand All @@ -10,6 +10,8 @@ It is designed to be universal and currently supports REST calls via HTTP (using

It also works with the ESP8266 WiFi chip using the ESP8266 processor, therefore working as an independent unit.

Boards running aREST can also be accessed from anywhere in the world via an API available at `cloud.arest.io`. Check the rest of this file and the examples ending with *_cloud* for more details. This currently only works with the ESP8266 WiFi chip.

If you want to know more about aREST, go over to [http://arest.io/](http://arest.io/).

## Contents
Expand Down Expand Up @@ -67,6 +69,10 @@ To use the library with the ESP8266 WiFi chip you will need to install the requi

- [Adafruit nRF8001 Library](https://github.com/adafruit/Adafruit_nRF8001)

### For Cloud Access

- [PubSub Library](https://github.com/knolleary/pubsubclient)

## Setup

To install the library, simply clone this repository in the /libraries folder of your Arduino folder.
Expand Down Expand Up @@ -114,6 +120,14 @@ To install the library, simply clone this repository in the /libraries folder of
5. Go to a web browser and type `192.168.1.103/mode/5/o` to set the pin as an output
6. Now type `192.168.1.103/digital/5/1` and the LED should turn on

## Cloud Access (ESP8266) (BETA)

1. Connect a LED & resistor to pin number 5 of your ESP8266 board
2. Open the ESP8266_cloud example sketch and modify the WiFi SSID & password, and also give a unique ID to your project
3. Upload the sketch to the board
5. Go to a web browser and type `cloud.arest.io/mode/5/o` to set the pin as an output
6. Now type `cloud.arest.io/digital/5/1` and the LED should turn on

## API documentation

The API currently supports five type of commands: digital, analog, and mode, variables, and user-defined functions.
Expand Down
33 changes: 20 additions & 13 deletions aREST.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Version 2.0.0
Changelog:
Version 2.0.1: Added beta support for cloud access via cloud.arest.io
Version 2.0.0: Added beta support for MQTT communications
Version 1.9.10: Added support for floats & Strings for Uno (without the CC3000 chip)
Version 1.9.8: Added support for ESP8266 chip
Expand Down Expand Up @@ -398,21 +399,25 @@ void handle_callback(PubSubClient& client, char* topic, byte* payload, unsigned

String msgString = String(mqtt_msg);

Serial.print("Received message via MQTT: ");
Serial.println(msgString);
if (DEBUG_MODE) {
Serial.print("Received message via MQTT: ");
Serial.println(msgString);
}

String modified_message = String(msgString) + " /";
char char_message[50];
modified_message.toCharArray(char_message, 50);
char char_message[100];
modified_message.toCharArray(char_message, 100);

handle(char_message);
char * answer = getBuffer();

String output = String(answer);
output.trim();

Serial.print("Sending message via MQTT: ");
Serial.println(output);
if (DEBUG_MODE) {
Serial.print("Sending message via MQTT: ");
Serial.println(output);
}
client.publish(out_topic, (char *) output.c_str());
resetBuffer();
}
Expand Down Expand Up @@ -905,9 +910,11 @@ bool send_command(bool headers) {
virtual void root_answer() {

#if defined(ADAFRUIT_CC3000_H) || defined(ESP8266) || defined(ethernet_h) || defined(WiFi_h)
if (command != 'u') {
addToBuffer(F("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: POST, GET, PUT, OPTIONS\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n"));
}
#if !defined(PubSubClient_h)
if (command != 'u') {
addToBuffer(F("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: POST, GET, PUT, OPTIONS\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n"));
}
#endif
#endif

if (LIGHTWEIGHT) {addToBuffer(id);}
Expand Down Expand Up @@ -1238,11 +1245,11 @@ void resetBuffer(){
// MQTT client
#if defined(PubSubClient_h)

char in_topic[50];
char out_topic[50];
char in_topic[100];
char out_topic[100];

const char* mqtt_server = "192.168.0.101";
//const char* mqtt_server = "45.55.79.41";
//const char* mqtt_server = "192.168.0.101";
const char* mqtt_server = "45.55.79.41";
#endif

// Float variables arrays (Mega & ESP8266 only)
Expand Down
32 changes: 16 additions & 16 deletions examples/ESP8266/ESP8266.ino
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
This a simple example of the aREST Library for the ESP8266 WiFi chip.
/*
This a simple example of the aREST Library for the ESP8266 WiFi chip.
See the README file for more details.
Written in 2015 by Marco Schwartz under a GPL license.
Written in 2015 by Marco Schwartz under a GPL license.
*/

// Import required libraries
Expand All @@ -16,7 +16,7 @@ aREST rest = aREST();
const char* ssid = "your_wifi_network_name";
const char* password = "your_wifi_network_password";

// The port to listen for incoming TCP connections
// The port to listen for incoming TCP connections
#define LISTEN_PORT 80

// Create an instance of the server
Expand All @@ -27,10 +27,10 @@ int temperature;
int humidity;

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

// Init variables and expose them to REST API
temperature = 24;
humidity = 40;
Expand All @@ -39,11 +39,11 @@ void setup(void)

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

// Give name and ID to device
rest.set_id("1");
rest.set_name("esp8266");

// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Expand All @@ -52,17 +52,17 @@ void setup(void)
}
Serial.println("");
Serial.println("WiFi connected");

// Start the server
server.begin();
Serial.println("Server started");

// Print the IP address
Serial.println(WiFi.localIP());
}

void loop() {

// Handle REST calls
WiFiClient client = server.available();
if (!client) {
Expand All @@ -72,15 +72,15 @@ void loop() {
delay(1);
}
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;
}
}
79 changes: 79 additions & 0 deletions examples/ESP8266_cloud/ESP8266_cloud.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
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 2015 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);

// Unique ID to identify the device for cloud.arest.io
char* device_id = "unique_device_id";

// 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 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 and ID to device
rest.set_id(device_id);
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.loop(client);

}

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

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

}
Binary file removed examples/Serial/.DS_Store
Binary file not shown.
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.0.0
version=2.0.1
author=Marco Schwartz
maintainer=Marco Schwartz <[email protected]>
sentence=RESTful API for the Arduino platform.
Expand Down

0 comments on commit af9a9eb

Please sign in to comment.