From af9a9eb874f7fae06f17d9e2d37591ca2fd6e70f Mon Sep 17 00:00:00 2001 From: marcoschwartz Date: Wed, 25 Nov 2015 11:23:01 +0100 Subject: [PATCH] added support for cloud.arest.io --- README.md | 16 ++++- aREST.h | 33 ++++++---- examples/ESP8266/ESP8266.ino | 32 ++++----- examples/ESP8266_cloud/ESP8266_cloud.ino | 79 +++++++++++++++++++++++ examples/Serial/.DS_Store | Bin 6148 -> 0 bytes library.properties | 2 +- 6 files changed, 131 insertions(+), 31 deletions(-) create mode 100644 examples/ESP8266_cloud/ESP8266_cloud.ino delete mode 100644 examples/Serial/.DS_Store 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 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 sentence=RESTful API for the Arduino platform.