-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathESP8266_cloud.ino
84 lines (61 loc) · 1.88 KB
/
ESP8266_cloud.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
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 API 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 API key
rest.setKey(key);
// 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 ID to device (optional, if not set, a device ID will be auto-assigned to the device)
// rest.set_id("unique_device_id");
// 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");
}
void loop() {
// Connect to the cloud
rest.handle(client);
// Publish data on feed temperature, with value 10, every 5 seconds
rest.publish(client, "temperature", 10, 5000);
}
// Handles message arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {
rest.handle_callback(client, topic, payload, length);
}