diff --git a/01_log_data/log_data/log_data.ino b/02_data_logging/01_log_data/log_data/log_data.ino similarity index 92% rename from 01_log_data/log_data/log_data.ino rename to 02_data_logging/01_log_data/log_data/log_data.ino index 0aaff08..db47e64 100644 --- a/01_log_data/log_data/log_data.ino +++ b/02_data_logging/01_log_data/log_data/log_data.ino @@ -3,8 +3,8 @@ #include "DHT.h" // WiFi credientials -const char* ssid = "UPC2A55751"; -const char* password = "sBfj3wfEurnh"; +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; // Status int status = WL_IDLE_STATUS; @@ -57,7 +57,7 @@ void loop() { float t = dht.readTemperature(); // Build request - String request = "GET /dweet/for/myesp8266?temperature="; + String request = "GET /dweet/for/65ftiv?temperature="; request += String(t); request += "&humidity="; request += String(h); diff --git a/02_data_logging/04_alerts/04_alerts.ino b/02_data_logging/04_alerts/04_alerts.ino new file mode 100644 index 0000000..41f9118 --- /dev/null +++ b/02_data_logging/04_alerts/04_alerts.ino @@ -0,0 +1,106 @@ +// Libraries +#include +#include "DHT.h" + +// WiFi settings +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; + +// DHT11 sensor pins +#define DHTPIN 5 +#define DHTTYPE DHT11 + +// Initialize DHT sensor +DHT dht(DHTPIN, DHTTYPE, 15); + +// IFTTT settings +const char* host = "maker.ifttt.com"; +const char* eventName = "alert"; +const char* key = "your-ifttt-key"; + +void setup() { + + Serial.begin(115200); + delay(10); + + // Init DHT + dht.begin(); + + // We start by connecting to a WiFi network + Serial.println(); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + + +void loop() { + + // Reading temperature and humidity + float h = dht.readHumidity(); + float t = dht.readTemperature(); + + // Check temperature data + if (t > 30.00) { + + Serial.print("connecting to "); + Serial.println(host); + + // Use WiFiClient class to create TCP connections + WiFiClient client; + const int httpPort = 80; + if (!client.connect(host, httpPort)) { + Serial.println("connection failed"); + return; + } + + // We now create a URI for the request + String url = "/trigger/"; + url += eventName; + url += "/with/key/"; + url += key; + + Serial.print("Requesting URL: "); + Serial.println(url); + + // This will send the request to the server + client.print(String("GET ") + url + " HTTP/1.1\r\n" + + "Host: " + host + "\r\n" + + "Connection: close\r\n\r\n"); + int timeout = millis() + 5000; + while (client.available() == 0) { + if (timeout - millis() < 0) { + Serial.println(">>> Client Timeout !"); + client.stop(); + return; + } + } + + // Read all the lines of the reply from server and print them to Serial + while(client.available()){ + String line = client.readStringUntil('\r'); + Serial.print(line); + } + + Serial.println(); + Serial.println("closing connection"); + + // Wait a long time before new alert + delay(10 * 60 * 1000); + + } + +} + diff --git a/02_data_logging/05_data_monitoring/log_data_one/log_data_one.ino b/02_data_logging/05_data_monitoring/log_data_one/log_data_one.ino new file mode 100644 index 0000000..db47e64 --- /dev/null +++ b/02_data_logging/05_data_monitoring/log_data_one/log_data_one.ino @@ -0,0 +1,95 @@ +// Libraries +#include +#include "DHT.h" + +// WiFi credientials +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; + +// Status +int status = WL_IDLE_STATUS; + +// Pin +#define DHTPIN 5 + +// Use DHT11 +#define DHTTYPE DHT11 + +// Create instance +DHT dht(DHTPIN, DHTTYPE); + +// Server +char server[] = "dweet.io"; + +// Client instance +WiFiClient client; + +void setup() { + + // Initialize serial and wait for port to open: + Serial.begin(115200); + + // Init DHT + dht.begin(); + + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + // Connect to WiFi + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected"); + + // Print the IP address + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +void loop() { + + // Reading temperature & humidity + float h = dht.readHumidity(); + float t = dht.readTemperature(); + + // Build request + String request = "GET /dweet/for/65ftiv?temperature="; + request += String(t); + request += "&humidity="; + request += String(h); + request += " HTTP/1.1"; + + Serial.println("\nStarting connection to server..."); + // if you get a connection, report back via serial: + if (client.connect(server, 80)) { + Serial.println("connected to server"); + // Make a HTTP request: + Serial.println(request); + client.println(request); + client.println("Host: dweet.io"); + client.println("Connection: close"); + client.println(); + } + + // if there are incoming bytes available + // from the server, read them and print them: + while (client.connected()) { + while (client.available()) { + char c = client.read(); + Serial.print(c); + } + } + + // if the server's disconnected, stop the client: + Serial.println(); + Serial.println("disconnecting from server."); + client.stop(); + + delay(10000); +} + + diff --git a/02_data_logging/05_data_monitoring/log_data_two/log_data_two.ino b/02_data_logging/05_data_monitoring/log_data_two/log_data_two.ino new file mode 100644 index 0000000..de259af --- /dev/null +++ b/02_data_logging/05_data_monitoring/log_data_two/log_data_two.ino @@ -0,0 +1,95 @@ +// Libraries +#include +#include "DHT.h" + +// WiFi credientials +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; + +// Status +int status = WL_IDLE_STATUS; + +// Pin +#define DHTPIN 5 + +// Use DHT11 +#define DHTTYPE DHT11 + +// Create instance +DHT dht(DHTPIN, DHTTYPE); + +// Server +char server[] = "dweet.io"; + +// Client instance +WiFiClient client; + +void setup() { + + // Initialize serial and wait for port to open: + Serial.begin(115200); + + // Init DHT + dht.begin(); + + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + // Connect to WiFi + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected"); + + // Print the IP address + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +void loop() { + + // Reading temperature & humidity + float h = dht.readHumidity(); + float t = dht.readTemperature(); + + // Build request + String request = "GET /dweet/for/7fcfiv?temperature="; + request += String(t); + request += "&humidity="; + request += String(h); + request += " HTTP/1.1"; + + Serial.println("\nStarting connection to server..."); + // if you get a connection, report back via serial: + if (client.connect(server, 80)) { + Serial.println("connected to server"); + // Make a HTTP request: + Serial.println(request); + client.println(request); + client.println("Host: dweet.io"); + client.println("Connection: close"); + client.println(); + } + + // if there are incoming bytes available + // from the server, read them and print them: + while (client.connected()) { + while (client.available()) { + char c = client.read(); + Serial.print(c); + } + } + + // if the server's disconnected, stop the client: + Serial.println(); + Serial.println("disconnecting from server."); + client.stop(); + + delay(10000); +} + + diff --git a/03_cloud_control/02_led_control/02_led_control.ino b/03_cloud_control/02_led_control/02_led_control.ino new file mode 100644 index 0000000..4f4ea85 --- /dev/null +++ b/03_cloud_control/02_led_control/02_led_control.ino @@ -0,0 +1,58 @@ +// 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 = "87fcff"; + +// WiFi parameters +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; + +// Functions +void callback(char* topic, byte* payload, unsigned int length); + +void setup(void) +{ + // Start Serial + Serial.begin(115200); + + // Set callback + client.setCallback(callback); + + // Give name & ID to the device (ID should be 6 characters long) + 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"); + +} + +void loop() { + + // Connect to the cloud + rest.handle(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/03_cloud_control/03_dashboard/03_dashboard.ino b/03_cloud_control/03_dashboard/03_dashboard.ino new file mode 100644 index 0000000..f79aaeb --- /dev/null +++ b/03_cloud_control/03_dashboard/03_dashboard.ino @@ -0,0 +1,82 @@ +// Import required libraries +#include +#include +#include +#include "DHT.h" + +// Clients +WiFiClient espClient; +PubSubClient client(espClient); + +// Create aREST instance +aREST rest = aREST(client); + +// Pin +#define DHTPIN 4 + +// Use DHT11 +#define DHTTYPE DHT11 + +// Create instance +DHT dht(DHTPIN, DHTTYPE); + +// Unique ID to identify the device for cloud.arest.io +char* device_id = "87fcff"; + +// WiFi parameters +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; + +// Functions +void callback(char* topic, byte* payload, unsigned int length); + +// Variables to be exposed to the API +float temperature; +float humidity; + +void setup(void) +{ + // Start Serial + Serial.begin(115200); + + // Init DHT + dht.begin(); + + // Set callback + client.setCallback(callback); + + // Give name & ID to the device (ID should be 6 characters long) + rest.set_id(device_id); + rest.set_name("esp8266"); + + rest.variable("temperature", &temperature); + rest.variable("humidity", &humidity); + + // Connect to WiFi + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected"); + +} + +void loop() { + + // Reading temperature & humidity + humidity = dht.readHumidity(); + temperature = dht.readTemperature(); + + // Connect to the cloud + rest.handle(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/03_cloud_control/04_power_switch/04_power_switch.ino b/03_cloud_control/04_power_switch/04_power_switch.ino new file mode 100644 index 0000000..4f4ea85 --- /dev/null +++ b/03_cloud_control/04_power_switch/04_power_switch.ino @@ -0,0 +1,58 @@ +// 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 = "87fcff"; + +// WiFi parameters +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; + +// Functions +void callback(char* topic, byte* payload, unsigned int length); + +void setup(void) +{ + // Start Serial + Serial.begin(115200); + + // Set callback + client.setCallback(callback); + + // Give name & ID to the device (ID should be 6 characters long) + 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"); + +} + +void loop() { + + // Connect to the cloud + rest.handle(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/04_web_services/01_drive/01_drive.ino b/04_web_services/01_drive/01_drive.ino new file mode 100644 index 0000000..2ea4e86 --- /dev/null +++ b/04_web_services/01_drive/01_drive.ino @@ -0,0 +1,103 @@ +// Libraries +#include +#include "DHT.h" + +// WiFi settings +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; + +// DHT11 sensor pins +#define DHTPIN 5 +#define DHTTYPE DHT11 + +// Initialize DHT sensor +DHT dht(DHTPIN, DHTTYPE, 15); + +// IFTTT settings +const char* host = "maker.ifttt.com"; +const char* eventName = "google"; +const char* key = "your-ifttt-key"; + +void setup() { + + Serial.begin(115200); + delay(10); + + // Init DHT + dht.begin(); + + // We start by connecting to a WiFi network + Serial.println(); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + + +void loop() { + + // Reading temperature and humidity + float h = dht.readHumidity(); + float t = dht.readTemperature(); + + Serial.print("connecting to "); + Serial.println(host); + + // Use WiFiClient class to create TCP connections + WiFiClient client; + const int httpPort = 80; + if (!client.connect(host, httpPort)) { + Serial.println("connection failed"); + return; + } + + // We now create a URI for the request + String url = "/trigger/"; + url += eventName; + url += "/with/key/"; + url += key; + url += "?value1=" + String(t); + url += "&value2=" + String(h); + + Serial.print("Requesting URL: "); + Serial.println(url); + + // This will send the request to the server + client.print(String("GET ") + url + " HTTP/1.1\r\n" + + "Host: " + host + "\r\n" + + "Connection: close\r\n\r\n"); + int timeout = millis() + 5000; + while (client.available() == 0) { + if (timeout - millis() < 0) { + Serial.println(">>> Client Timeout !"); + client.stop(); + return; + } + } + + // Read all the lines of the reply from server and print them to Serial + while(client.available()){ + String line = client.readStringUntil('\r'); + Serial.print(line); + } + + Serial.println(); + Serial.println("closing connection"); + + // Wait 10 seconds + delay(10 * 1000); + +} + diff --git a/04_web_services/02_twitter/02_twitter.ino b/04_web_services/02_twitter/02_twitter.ino new file mode 100644 index 0000000..188920e --- /dev/null +++ b/04_web_services/02_twitter/02_twitter.ino @@ -0,0 +1,103 @@ +// Libraries +#include +#include "DHT.h" + +// WiFi settings +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; + +// DHT11 sensor pins +#define DHTPIN 5 +#define DHTTYPE DHT11 + +// Initialize DHT sensor +DHT dht(DHTPIN, DHTTYPE, 15); + +// IFTTT settings +const char* host = "maker.ifttt.com"; +const char* eventName = "twitter"; +const char* key = "your-ifttt-key"; + +void setup() { + + Serial.begin(115200); + delay(10); + + // Init DHT + dht.begin(); + + // We start by connecting to a WiFi network + Serial.println(); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + + +void loop() { + + // Reading temperature and humidity + float h = dht.readHumidity(); + float t = dht.readTemperature(); + + Serial.print("connecting to "); + Serial.println(host); + + // Use WiFiClient class to create TCP connections + WiFiClient client; + const int httpPort = 80; + if (!client.connect(host, httpPort)) { + Serial.println("connection failed"); + return; + } + + // We now create a URI for the request + String url = "/trigger/"; + url += eventName; + url += "/with/key/"; + url += key; + url += "?value1=" + String(t); + url += "&value2=" + String(h); + + Serial.print("Requesting URL: "); + Serial.println(url); + + // This will send the request to the server + client.print(String("GET ") + url + " HTTP/1.1\r\n" + + "Host: " + host + "\r\n" + + "Connection: close\r\n\r\n"); + int timeout = millis() + 5000; + while (client.available() == 0) { + if (timeout - millis() < 0) { + Serial.println(">>> Client Timeout !"); + client.stop(); + return; + } + } + + // Read all the lines of the reply from server and print them to Serial + while(client.available()){ + String line = client.readStringUntil('\r'); + Serial.print(line); + } + + Serial.println(); + Serial.println("closing connection"); + + // Wait 10 minutes + delay(10 * 60 * 1000); + +} + diff --git a/04_web_services/03_notifications/03_notifications.ino b/04_web_services/03_notifications/03_notifications.ino new file mode 100644 index 0000000..31a40a5 --- /dev/null +++ b/04_web_services/03_notifications/03_notifications.ino @@ -0,0 +1,103 @@ +// Libraries +#include +#include "DHT.h" + +// WiFi settings +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; + +// DHT11 sensor pins +#define DHTPIN 5 +#define DHTTYPE DHT11 + +// Initialize DHT sensor +DHT dht(DHTPIN, DHTTYPE, 15); + +// IFTTT settings +const char* host = "maker.ifttt.com"; +const char* eventName = "notification"; +const char* key = "your-ifttt-key"; + +void setup() { + + Serial.begin(115200); + delay(10); + + // Init DHT + dht.begin(); + + // We start by connecting to a WiFi network + Serial.println(); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + + +void loop() { + + // Reading temperature and humidity + float h = dht.readHumidity(); + float t = dht.readTemperature(); + + Serial.print("connecting to "); + Serial.println(host); + + // Use WiFiClient class to create TCP connections + WiFiClient client; + const int httpPort = 80; + if (!client.connect(host, httpPort)) { + Serial.println("connection failed"); + return; + } + + // We now create a URI for the request + String url = "/trigger/"; + url += eventName; + url += "/with/key/"; + url += key; + url += "?value1=" + String(t); + url += "&value2=" + String(h); + + Serial.print("Requesting URL: "); + Serial.println(url); + + // This will send the request to the server + client.print(String("GET ") + url + " HTTP/1.1\r\n" + + "Host: " + host + "\r\n" + + "Connection: close\r\n\r\n"); + int timeout = millis() + 5000; + while (client.available() == 0) { + if (timeout - millis() < 0) { + Serial.println(">>> Client Timeout !"); + client.stop(); + return; + } + } + + // Read all the lines of the reply from server and print them to Serial + while(client.available()){ + String line = client.readStringUntil('\r'); + Serial.print(line); + } + + Serial.println(); + Serial.println("closing connection"); + + // Wait 10 minutes + delay(10 * 60 * 1000); + +} + diff --git a/04_web_services/04_gardening/alerts/alerts.ino b/04_web_services/04_gardening/alerts/alerts.ino new file mode 100644 index 0000000..5c0d52e --- /dev/null +++ b/04_web_services/04_gardening/alerts/alerts.ino @@ -0,0 +1,110 @@ +// Libraries +#include +#include + +// Humidity threshold +float threshold = 60.00; + +// Pins +#define dataPin 4 +#define clockPin 5 + +// WiFi settings +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; + +// Variables to be measured +float temperature; +float humidity; + +// Create instance for the sensor +SHT1x sht1x(dataPin, clockPin); + +// IFTTT settings +const char* host = "maker.ifttt.com"; +const char* eventName = "gardening"; +const char* key = "your-ifttt-key"; + +void setup() { + + Serial.begin(115200); + delay(10); + + // We start by connecting to a WiFi network + Serial.println(); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + + +void loop() { + + // Read values from the sensor + temperature = sht1x.readTemperatureC(); + humidity = sht1x.readHumidity(); + + // Check humidity data + if (humidity < threshold) { + + Serial.print("connecting to "); + Serial.println(host); + + // Use WiFiClient class to create TCP connections + WiFiClient client; + const int httpPort = 80; + if (!client.connect(host, httpPort)) { + Serial.println("connection failed"); + return; + } + + // We now create a URI for the request + String url = "/trigger/"; + url += eventName; + url += "/with/key/"; + url += key; + + Serial.print("Requesting URL: "); + Serial.println(url); + + // This will send the request to the server + client.print(String("GET ") + url + " HTTP/1.1\r\n" + + "Host: " + host + "\r\n" + + "Connection: close\r\n\r\n"); + int timeout = millis() + 5000; + while (client.available() == 0) { + if (timeout - millis() < 0) { + Serial.println(">>> Client Timeout !"); + client.stop(); + return; + } + } + + // Read all the lines of the reply from server and print them to Serial + while(client.available()){ + String line = client.readStringUntil('\r'); + Serial.print(line); + } + + Serial.println(); + Serial.println("closing connection"); + + // Wait a long time before new alert + delay(10 * 60 * 1000); + + } + +} + diff --git a/04_web_services/04_gardening/soil_sensor_test/soil_sensor_test.ino b/04_web_services/04_gardening/soil_sensor_test/soil_sensor_test.ino new file mode 100755 index 0000000..56494ea --- /dev/null +++ b/04_web_services/04_gardening/soil_sensor_test/soil_sensor_test.ino @@ -0,0 +1,40 @@ +// Library +#include + +// Pins +#define dataPin 4 +#define clockPin 5 + +// Create instance for the sensor +SHT1x sht1x(dataPin, clockPin); + +void setup() +{ + Serial.begin(115200); // Open serial connection to report values to host + Serial.println("Starting up"); +} + +void loop() +{ + // Variables + float temp_c; + float temp_f; + float humidity; + + // Read values from the sensor + temp_c = sht1x.readTemperatureC(); + temp_f = sht1x.readTemperatureF(); + humidity = sht1x.readHumidity(); + + // Print the values to the serial port + Serial.print("Temperature: "); + Serial.print(temp_c, DEC); + Serial.print("C / "); + Serial.print(temp_f, DEC); + Serial.print("F. Humidity: "); + Serial.print(humidity); + Serial.println("%"); + + // Wait 2 seconds + delay(2000); +} diff --git a/05_m2m/02_simple_m2m/esp8266_led/esp8266_led.ino b/05_m2m/02_simple_m2m/esp8266_led/esp8266_led.ino new file mode 100644 index 0000000..92ecf2b --- /dev/null +++ b/05_m2m/02_simple_m2m/esp8266_led/esp8266_led.ino @@ -0,0 +1,81 @@ +// 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 = "23ddcc"; + +// WiFi parameters +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; + +// LED state +bool ledState; + +// Functions +void callback(char* topic, byte* payload, unsigned int length); +int toggle(String command); + +void setup(void) +{ + + // Start Serial + Serial.begin(115200); + + // Set callback + client.setCallback(callback); + + // Give name and ID to device + rest.set_id(device_id); + rest.set_name("led"); + + // Function + rest.function("toggle", toggle); + + // LED state + ledState = false; + + // Connect to WiFi + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected"); + + // Pin 5 as output + pinMode(D5, OUTPUT); + +} + +void loop() { + + // Connect to the cloud + rest.handle(client); + +} + +// Handles message arrived on subscribed topic(s) +void callback(char* topic, byte* payload, unsigned int length) { + + // Handle + rest.handle_callback(client, topic, payload, length); + +} + +// Toggle LED +int toggle(String command) { + + ledState = !ledState; + digitalWrite(D5, ledState); + return 1; +} diff --git a/05_m2m/02_simple_m2m/trigger_button/trigger_button.ino b/05_m2m/02_simple_m2m/trigger_button/trigger_button.ino new file mode 100644 index 0000000..a95413a --- /dev/null +++ b/05_m2m/02_simple_m2m/trigger_button/trigger_button.ino @@ -0,0 +1,88 @@ +// Libraries +#include + +// WiFi setting +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; + +// IFTTT settings +const char* host = "maker.ifttt.com"; +const char* eventName = "button_pressed"; +const char* key = "your-ifttt-key"; + +void setup() { + Serial.begin(115200); + delay(10); + + // We start by connecting to a WiFi network + + Serial.println(); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + + // Pin 5 as input + pinMode(D5, INPUT); +} + +void loop() { + + if (digitalRead(D5)) { + Serial.print("connecting to "); + Serial.println(host); + + // Use WiFiClient class to create TCP connections + WiFiClient client; + const int httpPort = 80; + if (!client.connect(host, httpPort)) { + Serial.println("connection failed"); + return; + } + + // We now create a URI for the request + String url = "/trigger/"; + url += eventName; + url += "/with/key/"; + url += key; + + Serial.print("Requesting URL: "); + Serial.println(url); + + // This will send the request to the server + client.print(String("GET ") + url + " HTTP/1.1\r\n" + + "Host: " + host + "\r\n" + + "Connection: close\r\n\r\n"); + int timeout = millis() + 5000; + while (client.available() == 0) { + if (timeout - millis() < 0) { + Serial.println(">>> Client Timeout !"); + client.stop(); + return; + } + } + + // Read all the lines of the reply from server and print them to Serial + while(client.available()){ + String line = client.readStringUntil('\r'); + Serial.print(line); + } + + Serial.println(); + Serial.println("closing connection"); + } + + +} + diff --git a/05_m2m/03_alarm_m2m/alarm/alarm.ino b/05_m2m/03_alarm_m2m/alarm/alarm.ino new file mode 100644 index 0000000..09ed530 --- /dev/null +++ b/05_m2m/03_alarm_m2m/alarm/alarm.ino @@ -0,0 +1,104 @@ +// 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 = "25tyif"; + +// WiFi parameters +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; + +// Alarm +bool alarmState; +int ledState = LOW; +unsigned long previousMillis = 0; +const long interval = 1000; + +// Functions +void callback(char* topic, byte* payload, unsigned int length); +int toggle(String command); + +void setup(void) +{ + + // Start Serial + Serial.begin(115200); + + // Set callback + client.setCallback(callback); + + // Give name and ID to device + rest.set_id(device_id); + rest.set_name("alarm"); + + // Function + rest.function("alarm", alarm); + + // Alarm state + alarmState = false; + + // Connect to WiFi + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected"); + + // Pin 5 as output + pinMode(D5, OUTPUT); + +} + +void loop() { + + unsigned long currentMillis = millis(); + + if (currentMillis - previousMillis >= interval) { + + // save the last time you blinked the LED + previousMillis = currentMillis; + + // if the LED is off turn it on and vice-versa: + if (ledState == LOW) { + ledState = HIGH; + } else { + ledState = LOW; + } + + // set the LED with the ledState of the variable: + if (alarmState == true) { + digitalWrite(D5, ledState); + } + + } + + // Connect to the cloud + rest.handle(client); + +} + +// Handles message arrived on subscribed topic(s) +void callback(char* topic, byte* payload, unsigned int length) { + + // Handle + rest.handle_callback(client, topic, payload, length); + +} + +// Activate alarm +int alarm(String command) { + + alarmState = true; + return 1; +} diff --git a/05_m2m/03_alarm_m2m/motion_sensor/motion_sensor.ino b/05_m2m/03_alarm_m2m/motion_sensor/motion_sensor.ino new file mode 100644 index 0000000..d1f54d3 --- /dev/null +++ b/05_m2m/03_alarm_m2m/motion_sensor/motion_sensor.ino @@ -0,0 +1,88 @@ +// Libraries +#include + +// WiFi setting +const char* ssid = "your_wifi_ssid"; +const char* password = "your_wifi_password"; + +// IFTTT settings +const char* host = "maker.ifttt.com"; +const char* eventName = "alarm_activated"; +const char* key = "your-ifttt-key"; + +void setup() { + Serial.begin(115200); + delay(10); + + // We start by connecting to a WiFi network + + Serial.println(); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + + // Pin 5 as input + pinMode(D5, INPUT); +} + +void loop() { + + if (digitalRead(D5)) { + Serial.print("connecting to "); + Serial.println(host); + + // Use WiFiClient class to create TCP connections + WiFiClient client; + const int httpPort = 80; + if (!client.connect(host, httpPort)) { + Serial.println("connection failed"); + return; + } + + // We now create a URI for the request + String url = "/trigger/"; + url += eventName; + url += "/with/key/"; + url += key; + + Serial.print("Requesting URL: "); + Serial.println(url); + + // This will send the request to the server + client.print(String("GET ") + url + " HTTP/1.1\r\n" + + "Host: " + host + "\r\n" + + "Connection: close\r\n\r\n"); + int timeout = millis() + 5000; + while (client.available() == 0) { + if (timeout - millis() < 0) { + Serial.println(">>> Client Timeout !"); + client.stop(); + return; + } + } + + // Read all the lines of the reply from server and print them to Serial + while(client.available()){ + String line = client.readStringUntil('\r'); + Serial.print(line); + } + + Serial.println(); + Serial.println("closing connection"); + } + + +} +