Skip to content

Commit

Permalink
updated for new aREST cloud server
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoschwartz committed Jul 26, 2016
1 parent 3100a6c commit 26833d1
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 146 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ 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 (Ethernet) (BETA)
## Cloud Access (Ethernet)

1. Connect a LED & resistor to pin number 8 of your Arduino board
2. Open the Ethernet_cloud example sketch and modify the MAC address, and also give a unique ID to your project, for example 47fd9g
Expand All @@ -136,7 +136,7 @@ To install the library, simply clone this repository in the /libraries folder of
5. Go to a web browser and type `cloud.arest.io/47fd9g/mode/8/o` to set the pin as an output
6. Now type `cloud.arest.io/47fd9g/digital/8/1` and the LED should turn on

## Cloud Access (ESP8266) (BETA)
## Cloud Access (ESP8266)

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, for example 47fd9g
Expand Down
110 changes: 83 additions & 27 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.2.1
Version 2.3.0
Changelog:
Version 2.3.0: Implement required changes for the cloud server upgrade
Version 2.2.1: Added compatibility with the WINC1500 chip
Version 2.2.0: Added compatibility with the Arduino MKR1000 board
Version 2.1.2: Added data about hardware type in JSON answer
Expand Down Expand Up @@ -271,11 +272,14 @@ void send_http_headers(){
// Reset variables after a request
void reset_status() {

#if defined(ESP8266)
Serial.print("Memory loss before reset:");
Serial.println(freeMemory - ESP.getFreeHeap(),DEC);
freeMemory = ESP.getFreeHeap();
#endif
if (DEBUG_MODE) {
#if defined(ESP8266)
Serial.print("Memory loss before reset:");
Serial.println(freeMemory - ESP.getFreeHeap(),DEC);
freeMemory = ESP.getFreeHeap();
#endif
}


answer = "";
command = 'u';
Expand All @@ -286,13 +290,15 @@ void reset_status() {
index = 0;
//memset(&buffer[0], 0, sizeof(buffer));

#if defined(ESP8266)
Serial.print("Memory loss after reset:");
Serial.println(freeMemory - ESP.getFreeHeap(),DEC);
freeMemory = ESP.getFreeHeap();
Serial.print("Memory free:");
Serial.println(freeMemory, DEC);
#endif
if (DEBUG_MODE) {
#if defined(ESP8266)
Serial.print("Memory loss after reset:");
Serial.println(freeMemory - ESP.getFreeHeap(),DEC);
freeMemory = ESP.getFreeHeap();
Serial.print("Memory free:");
Serial.println(freeMemory, DEC);
#endif
}

}

Expand Down Expand Up @@ -702,12 +708,13 @@ void handle(PubSubClient& client){
}

void reconnect(PubSubClient& client) {

// Loop until we're reconnected
while (!client.connected()) {
Serial.print(F("Attempting MQTT connection..."));

// Attempt to connect
if (client.connect(id)) {
if (client.connect(client_id)) {
if (private_mqtt_server) {
Serial.println(F("Connected to MQTT server"));
}
Expand Down Expand Up @@ -840,7 +847,7 @@ void process(char c){
#if defined(ESP8266)
analogWriteRange(255);
#endif

}

// Variable or function request received ?
Expand Down Expand Up @@ -1173,7 +1180,7 @@ bool send_command(bool headers) {
if (command == 'f') {

// Execute function
uint8_t result = functions[value](arguments);
int result = functions[value](arguments);

// Send feedback to client
if (!LIGHTWEIGHT) {
Expand Down Expand Up @@ -1371,21 +1378,69 @@ void function(char * function_name, int (*f)(String)){
// Set device ID
void set_id(char *device_id){

strncpy(id,device_id, ID_SIZE);
strncpy(id, device_id, ID_SIZE);

#if defined(PubSubClient_h)
strcpy(in_topic, id);
strcat(in_topic, "_in");

strcpy(out_topic, id);
strcat(out_topic, "_out");
// Generate MQTT random ID
String randomId;
randomId = gen_random(6);

// Build topics IDs
String inTopic = randomId + String(id) + String("_in");
String outTopic = randomId + String(id) + String("_out");

// String inTopic = String(id) + String("_in");
// String outTopic = String(id) + String("_out");

strcpy(in_topic, inTopic.c_str());
strcpy(out_topic, outTopic.c_str());

// Build client ID
String clientId = randomId + String(id);
// String clientId = String(id);
strcpy(client_id, clientId.c_str());

if (DEBUG_MODE) {
Serial.print("Input MQTT topic: ");
Serial.println(in_topic);

Serial.print("Output MQTT topic: ");
Serial.println(out_topic);
}

strcpy(publish_topic, id);
strcat(publish_topic, "_publish");
#endif

}

#if defined(PubSubClient_h)
String gen_random(int length) {

String randomString;

#if defined(ESP8266)

randomString = String(ESP.getChipId());
randomString = randomString.substring(0, 6);

#else

String charset = "abcdefghijklmnopqrstuvwxyz0123456789";

// Generate
int l = charset.length();
int key;
for (int n = 0; n < length; n++) {
key = random(0, l - 1);
randomString += charset[key];
}

#endif

return randomString;
}
#endif

// Set device name
void set_name(char *device_name){

Expand Down Expand Up @@ -1633,16 +1688,17 @@ void setMQTTServer(char* new_mqtt_server){
#if defined(PubSubClient_h)

// Topics
char in_topic[ID_SIZE+5];
char out_topic[ID_SIZE+5];
char publish_topic[ID_SIZE+7];
char in_topic[ID_SIZE+10];
char out_topic[ID_SIZE+10];
char publish_topic[ID_SIZE+10];
char client_id[ID_SIZE+10];

// Subscribe topics & handlers
uint8_t subscriptions_index;
char * subscriptions_names[NUMBER_SUBSCRIPTIONS];

// aREST.io server
char* mqtt_server = "45.55.79.41";
char* mqtt_server = "45.55.196.201";
bool private_mqtt_server;
#endif

Expand Down
30 changes: 15 additions & 15 deletions examples/BLE/BLE.ino
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
/*
This a simple example of the aREST Library for Arduino (Uno/Mega/Due/Teensy)
using the Adafruit BLE UART library. See the README file for more details.
Written in 2014 by Marco Schwartz under a GPL license.
Written in 2014 by Marco Schwartz under a GPL license.
*/

// Libraries
Expand All @@ -26,13 +26,13 @@ int temperature;
int humidity;

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

// Start BLE
BTLEserial.begin();

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

// Function to be exposed
rest.function("led",ledControl);
// Give name and ID to device

// Give name & ID to the device (ID should be 6 characters long)
rest.set_id("008");
rest.set_name("ble_drake");
rest.set_name("ble_drake");
}

// Status message
aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;

void loop() {
void loop() {

// Tell the nRF8001 to do whatever it should be working on.
BTLEserial.pollACI();

// Ask what is our current status
aci_evt_opcode_t status = BTLEserial.getState();
// If the status changed....
Expand All @@ -72,7 +72,7 @@ void loop() {
// OK set the last status change to this one
laststatus = status;
}

// Handle REST calls
if (status == ACI_EVT_CONNECTED) {
rest.handle(BTLEserial);
Expand All @@ -81,10 +81,10 @@ void loop() {

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

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

digitalWrite(7,state);
return 1;
}
}
2 changes: 1 addition & 1 deletion examples/ESP8266/ESP8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void setup(void)
// Function to be exposed
rest.function("led",ledControl);

// Give name and ID to device
// Give name & ID to the device (ID should be 6 characters long)
rest.set_id("1");
rest.set_name("esp8266");

Expand Down
2 changes: 1 addition & 1 deletion examples/ESP8266_cloud/ESP8266_cloud.ino
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void setup(void)
rest.variable("temperature",&temperature);
rest.variable("humidity",&humidity);

// Give name and ID to device
// Give name & ID to the device (ID should be 6 characters long)
rest.set_id(device_id);
rest.set_name("esp8266");

Expand Down
2 changes: 1 addition & 1 deletion examples/ESP8266_softAP/ESP8266_softAP.ino
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void setup(void)
// Function to be exposed
rest.function("led",ledControl);

// Give name and ID to device
// Give name & ID to the device (ID should be 6 characters long)
rest.set_id("1");
rest.set_name("esp8266");

Expand Down
28 changes: 14 additions & 14 deletions examples/Ethernet/Ethernet.ino
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
/*
This a simple example of the aREST Library for Arduino (Uno/Mega/Due/Teensy)
using the Ethernet library (for example to be used with the Ethernet shield).
using the Ethernet library (for example to be used with the Ethernet shield).
See the README file for more details.
Written in 2014 by Marco Schwartz under a GPL license.
Written in 2014 by Marco Schwartz under a GPL license.
*/

// Libraries
Expand All @@ -29,10 +29,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 @@ -41,8 +41,8 @@ void setup(void)

// Function to be exposed
rest.function("led",ledControl);
// Give name and ID to device

// Give name & ID to the device (ID should be 6 characters long)
rest.set_id("008");
rest.set_name("dapper_drake");

Expand All @@ -61,21 +61,21 @@ void setup(void)
wdt_enable(WDTO_4S);
}

void loop() {
void loop() {

// listen for incoming clients
EthernetClient client = server.available();
rest.handle(client);
wdt_reset();

}

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

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

digitalWrite(6,state);
return 1;
}
}
4 changes: 2 additions & 2 deletions examples/Ethernet_cloud/Ethernet_cloud.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ aREST rest = aREST(client);
int temperature;
int humidity;

// Unique ID to identify the device for cloud.arest.io
// ID to identify the device on the cloud (should be 6 characters long)
char* device_id = "unique_device_id";

void setup(void)
Expand All @@ -44,7 +44,7 @@ void setup(void)
rest.variable("temperature",&temperature);
rest.variable("humidity",&humidity);

// Give name and ID to device
// Give name & ID to the device
rest.set_id(device_id);
rest.set_name("ethernet");

Expand Down
Loading

0 comments on commit 26833d1

Please sign in to comment.