diff --git a/README.md b/README.md index 041b98f..401b8b9 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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. @@ -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. diff --git a/aREST.h b/aREST.h index 68c4425..2e9a490 100644 --- a/aREST.h +++ b/aREST.h @@ -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 @@ -398,12 +399,14 @@ 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(); @@ -411,8 +414,10 @@ void handle_callback(PubSubClient& client, char* topic, byte* payload, unsigned 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(); } @@ -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);} @@ -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) diff --git a/examples/ESP8266/ESP8266.ino b/examples/ESP8266/ESP8266.ino index 8c7ad67..290ddaf 100644 --- a/examples/ESP8266/ESP8266.ino +++ b/examples/ESP8266/ESP8266.ino @@ -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 @@ -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 @@ -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; @@ -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) { @@ -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) { @@ -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; -} \ No newline at end of file +} diff --git a/examples/ESP8266_cloud/ESP8266_cloud.ino b/examples/ESP8266_cloud/ESP8266_cloud.ino new file mode 100644 index 0000000..8d847d3 --- /dev/null +++ b/examples/ESP8266_cloud/ESP8266_cloud.ino @@ -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 +#include +#include + +// 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); + +} diff --git a/examples/Serial/.DS_Store b/examples/Serial/.DS_Store deleted file mode 100644 index 5008ddf..0000000 Binary files a/examples/Serial/.DS_Store and /dev/null differ diff --git a/library.properties b/library.properties index b26217d..008fc0f 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=aREST -version=2.0.0 +version=2.0.1 author=Marco Schwartz maintainer=Marco Schwartz sentence=RESTful API for the Arduino platform.