From 9f10b484b0bb629e4be7f676bdf8f3ffeaa9b800 Mon Sep 17 00:00:00 2001 From: marcoschwartz Date: Tue, 28 Apr 2015 12:13:18 +0200 Subject: [PATCH 1/6] updated licenses --- chapter1/cloud_data_logger/cloud_data_logger.ino | 12 +++++++++++- chapter2/lelylan_lamp/lelylan_lamp.ino | 10 ++++++++++ chapter3/lelylan_sensor/lelylan_sensor.ino | 10 ++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/chapter1/cloud_data_logger/cloud_data_logger.ino b/chapter1/cloud_data_logger/cloud_data_logger.ino index 74378c2..a1c3d9f 100644 --- a/chapter1/cloud_data_logger/cloud_data_logger.ino +++ b/chapter1/cloud_data_logger/cloud_data_logger.ino @@ -1,4 +1,14 @@ -// Import required libraries +/*************************************************** + +Written by Marco Schwartz for Open Home Automation. +BSD license, all text above must be included in any redistribution + +Based on the original sketches supplied with the ESP8266/Arduino +implementation written by Ivan Grokhotkov + +****************************************************/ + +// Libraries #include #include "DHT.h" diff --git a/chapter2/lelylan_lamp/lelylan_lamp.ino b/chapter2/lelylan_lamp/lelylan_lamp.ino index b9f0d51..720ab53 100644 --- a/chapter2/lelylan_lamp/lelylan_lamp.ino +++ b/chapter2/lelylan_lamp/lelylan_lamp.ino @@ -1,3 +1,13 @@ +/*************************************************** + +Written by Marco Schwartz for Open Home Automation. +BSD license, all text above must be included in any redistribution + +Based on the original sketches supplied with the ESP8266/Arduino +implementation written by Ivan Grokhotkov + +****************************************************/ + // Libraries #include #include diff --git a/chapter3/lelylan_sensor/lelylan_sensor.ino b/chapter3/lelylan_sensor/lelylan_sensor.ino index c834ce0..c3e1b75 100644 --- a/chapter3/lelylan_sensor/lelylan_sensor.ino +++ b/chapter3/lelylan_sensor/lelylan_sensor.ino @@ -1,3 +1,13 @@ +/*************************************************** + +Written by Marco Schwartz for Open Home Automation. +BSD license, all text above must be included in any redistribution + +Based on the original sketches supplied with the ESP8266/Arduino +implementation written by Ivan Grokhotkov + +****************************************************/ + // Libraries #include #include From 6d98f6472631953c6fa4b2454ab794eb93843e7f Mon Sep 17 00:00:00 2001 From: marcoschwartz Date: Tue, 12 May 2015 19:31:07 +0200 Subject: [PATCH 2/6] fix for chapter 1 --- chapter1/cloud_data_logger/cloud_data_logger.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter1/cloud_data_logger/cloud_data_logger.ino b/chapter1/cloud_data_logger/cloud_data_logger.ino index a1c3d9f..41b20e8 100644 --- a/chapter1/cloud_data_logger/cloud_data_logger.ino +++ b/chapter1/cloud_data_logger/cloud_data_logger.ino @@ -26,7 +26,7 @@ const char* password = "your_wifi_network_password"; DHT dht(DHTPIN, DHTTYPE, 15); // Host -const char* host = "54.84.241.136"; +const char* host = "dweet.io"; void setup() { From db00c1cd0306f84f5d4fbdb230edca418c98918e Mon Sep 17 00:00:00 2001 From: marcoschwartz Date: Wed, 20 May 2015 08:57:00 +0200 Subject: [PATCH 3/6] changed chapters names --- chapter1/.DS_Store | Bin 6148 -> 0 bytes chapter1/cloud_data_logger/.DS_Store | Bin 6148 -> 0 bytes chapter1/dht_test/.DS_Store | Bin 6148 -> 0 bytes .../cloud_data_logger/cloud_data_logger.ino | 0 .../dht_test/dht_test.ino | 0 .../lelylan_lamp/lelylan_lamp.ino | 0 .../lelylan_sensor/lelylan_sensor.ino | 0 7 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 chapter1/.DS_Store delete mode 100644 chapter1/cloud_data_logger/.DS_Store delete mode 100644 chapter1/dht_test/.DS_Store rename {chapter1 => cloud_data_logger}/cloud_data_logger/cloud_data_logger.ino (100%) rename {chapter1 => cloud_data_logger}/dht_test/dht_test.ino (100%) rename {chapter2 => control_lamp_anywhere}/lelylan_lamp/lelylan_lamp.ino (100%) rename {chapter3 => real_time_iot_dashboard}/lelylan_sensor/lelylan_sensor.ino (100%) diff --git a/chapter1/.DS_Store b/chapter1/.DS_Store deleted file mode 100644 index befb8e4618434db03d25d23f82cfb3068d5c4b02..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~&2G~`5XWcR0B(A51xS2c_KK8)T}VG}CT)*Mk&Id)ij0k|WF^av>`fySMe>;w zhdu#N;6eC$1zrU%%)oBT{jQR!H3HJV; zjB-&+&-=<&R$G@YUvbvj>vXsq?I+=|oP;u#mE$6;zN$agiO0S|lPD^0~7{ zN95j`PW@f~ZioBc)~v&)zPIaj`1V$JHgiuM=k}fM{RfXmvlp*kzd3vR?)}Hl`fV|s z%#>XQ`V6@SyrVb2h3xL|f zYGqiay#(cOqk+*zE4H99org-KTgQqLqcr!tC7J zdvh;0#cdXV%@5mKUZdx;HNbT|z3tEla; z!!urSgBR@4-M8Il-F9oL{3qWL*Myx;%nCUm2jqYpkOOkyX9v9ZGNu>F%5p#s$bnA> z{QFP{=caO>9G?!R7y-x=mLqr_vjn+#f!tK?lUZR{O35j;I$~JLSub^8Q@Kx0IV?`* zbLz=fClrg*Sue32)+8&-0XZ;q;5?Tz@Bd5UoB4kz(n=15Ukb<4m7yXa6%)&KNyZvF!lqK^8|&%4b*%izso<<>IcHIng}LR4c#@< z+qJV>!S*%)Tfgqkffaxy-4Smd=H}PkXLe8-Bhq<~7u@5S@eGf%>hBZIJ;n|XWZg{o zPZ+ND!`*g2Y+2(Ecu7C~2;)j71*Cu!kOERb3j9U^@4d9;QKF(0kOETRO9B5rG`eFi z924Wy!4M+=alv#L*D*^Fn0a|r rcjG!J4AG8>(T=(Cc6<^=S=W5c>%DMH3_9~cC+cUwb&*MdzgFNAY+M{- diff --git a/chapter1/cloud_data_logger/cloud_data_logger.ino b/cloud_data_logger/cloud_data_logger/cloud_data_logger.ino similarity index 100% rename from chapter1/cloud_data_logger/cloud_data_logger.ino rename to cloud_data_logger/cloud_data_logger/cloud_data_logger.ino diff --git a/chapter1/dht_test/dht_test.ino b/cloud_data_logger/dht_test/dht_test.ino similarity index 100% rename from chapter1/dht_test/dht_test.ino rename to cloud_data_logger/dht_test/dht_test.ino diff --git a/chapter2/lelylan_lamp/lelylan_lamp.ino b/control_lamp_anywhere/lelylan_lamp/lelylan_lamp.ino similarity index 100% rename from chapter2/lelylan_lamp/lelylan_lamp.ino rename to control_lamp_anywhere/lelylan_lamp/lelylan_lamp.ino diff --git a/chapter3/lelylan_sensor/lelylan_sensor.ino b/real_time_iot_dashboard/lelylan_sensor/lelylan_sensor.ino similarity index 100% rename from chapter3/lelylan_sensor/lelylan_sensor.ino rename to real_time_iot_dashboard/lelylan_sensor/lelylan_sensor.ino From 8c966fb07034114672d9b2e91de65ec5b943de56 Mon Sep 17 00:00:00 2001 From: marcoschwartz Date: Sat, 30 May 2015 16:29:49 +0200 Subject: [PATCH 4/6] fix for MQTT --- .../lelylan_lamp/lelylan_lamp.ino | 76 +++++++++++-------- .../lelylan_sensor/lelylan_sensor.ino | 25 +++--- 2 files changed, 57 insertions(+), 44 deletions(-) diff --git a/control_lamp_anywhere/lelylan_lamp/lelylan_lamp.ino b/control_lamp_anywhere/lelylan_lamp/lelylan_lamp.ino index 720ab53..2651850 100644 --- a/control_lamp_anywhere/lelylan_lamp/lelylan_lamp.ino +++ b/control_lamp_anywhere/lelylan_lamp/lelylan_lamp.ino @@ -13,11 +13,9 @@ implementation written by Ivan Grokhotkov #include // Credentials -char* deviceId = "device_id"; // * set your device id (will be the MQTT client username) -char* deviceSecret = "device_secret"; // * set your device secret (will be the MQTT client password) -char* outTopic = "devices/device_id/set"; // * MQTT channel where physical updates are published -char* inTopic = "devices/device_id/get"; // * MQTT channel where lelylan updates are received -char* clientId = "7dslk2dfksd"; // * set a random string (max 23 chars, will be the MQTT client id) +String deviceId = "device_id"; // * set your device id (will be the MQTT client username) +String deviceSecret = "device_secret"; // * set your device secret (will be the MQTT client password) +String clientId = "7dslk2dfksd"; // * set a random string (max 23 chars, will be the MQTT client id) // WiFi name & password const char* ssid = "your_WiFi_name"; @@ -27,15 +25,19 @@ const char* password = "your_WiFi_password"; char* payloadOn = "{\"properties\":[{\"id\":\"518be5a700045e1521000001\",\"value\":\"on\"}]}"; char* payloadOff = "{\"properties\":[{\"id\":\"518be5a700045e1521000001\",\"value\":\"off\"}]}"; -byte server[] = { 178, 62, 108, 47 }; // MQTT server address +// Topics +String outTopic = "devices/" + deviceId + "/set"; // * MQTT channel where physical updates are published +String inTopic = "devices/" + deviceId + "/get"; // * MQTT channel where lelylan updates are received + +// MQTT server address +IPAddress server(178, 62, 108, 47); // WiFi Client WiFiClient wifiClient; // MQTT -void callback(char* topic, byte* payload, unsigned int length); // subscription callback -PubSubClient client(server, 1883, callback, wifiClient); // mqtt client - +PubSubClient client(server); + // Pins int outPin = 5; // led @@ -45,6 +47,31 @@ int reading; // current reading from the input pin int previous = LOW; // previous reading from the input pin long time = 0; // the last time the output pin was toggled long debounce = 200; // the debounce time, increase if the output flickers + +// Callback +void callback(const MQTT::Publish& pub) { + + // Copy the payload content into a char* + char* json; + json = (char*) malloc(pub.payload_len() + 1); + memcpy(json, pub.payload(), pub.payload_len()); + json[pub.payload_len()] = '\0'; + + // Update the physical status and confirm the executed update + boolean state; + if (String(payloadOn) == String(json)) { + Serial.println("[LELYLAN] Led turned on"); + lelylanPublish("on"); + state = HIGH; + } else { + Serial.println("[LELYLAN] Led turned off"); + lelylanPublish("off"); + state = LOW; + } + + digitalWrite(outPin, state); + free(json); +} void setup() { Serial.begin(115200); @@ -58,6 +85,9 @@ void setup() { WiFi.begin(ssid, password); + // Set callback + client.set_callback(callback); + while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); @@ -84,7 +114,8 @@ void lelylanConnection() { // add reconnection logics if (!client.connected()) { // connection to MQTT server - if (client.connect(clientId, deviceId, deviceSecret)) { + if (client.connect(MQTT::Connect(clientId) + .set_auth(deviceId, deviceSecret))) { Serial.println("[PHYSICAL] Successfully connected with MQTT"); lelylanSubscribe(); // topic subscription } @@ -104,26 +135,5 @@ void lelylanPublish(char* value) { void lelylanSubscribe() { client.subscribe(inTopic); } - -/* Receive Lelylan message and confirm the physical change */ -void callback(char* topic, byte* payload, unsigned int length) { - // copu the payload content into a char* - char* json; - json = (char*) malloc(length + 1); - memcpy(json, payload, length); - json[length] = '\0'; - - // update the physical status and confirm the executed update - if (String(payloadOn) == String(json)) { - Serial.println("[LELYLAN] Led turned on"); - lelylanPublish("on"); - state = HIGH; - } else { - Serial.println("[LELYLAN] Led turned off"); - lelylanPublish("off"); - state = LOW; - } - - digitalWrite(outPin, state); - free(json); -} + +// diff --git a/real_time_iot_dashboard/lelylan_sensor/lelylan_sensor.ino b/real_time_iot_dashboard/lelylan_sensor/lelylan_sensor.ino index c3e1b75..6c752fa 100644 --- a/real_time_iot_dashboard/lelylan_sensor/lelylan_sensor.ino +++ b/real_time_iot_dashboard/lelylan_sensor/lelylan_sensor.ino @@ -14,18 +14,17 @@ implementation written by Ivan Grokhotkov #include "DHT.h" // Credentials -char* deviceId = "device_id"; // * set your device id (will be the MQTT client username) -char* deviceSecret = "device_secret"; // * set your device secret (will be the MQTT client password) -char* outTopic = "devices/device_id/set"; // * MQTT channel where physical updates are published -char* inTopic = "devices/device_id/get"; // * MQTT channel where lelylan updates are received -char* clientId = "7dslk2dfksd"; // * set a random string (max 23 chars, will be the MQTT client id) +String deviceId = "device_id"; // * set your device id (will be the MQTT client username) +String deviceSecret = "device_secret"; // * set your device secret (will be the MQTT client password) +String clientId = "7dslk2dfksd"; // * set a random string (max 23 chars, will be the MQTT client id) // WiFi name & password const char* ssid = "your_WiFi_name"; const char* password = "your_WiFi_password"; -// MQTT server address -byte server[] = { 178, 62, 108, 47 }; +// Topics +String outTopic = "devices/" + deviceId + "/set"; // * MQTT channel where physical updates are published +String inTopic = "devices/" + deviceId + "/get"; // * MQTT channel where lelylan updates are received // WiFi client WiFiClient wifiClient; @@ -37,9 +36,12 @@ WiFiClient wifiClient; // Initialize DHT sensor DHT dht(DHTPIN, DHTTYPE, 15); -// MQTT communication -void callback(char* topic, byte* payload, unsigned int length); // subscription callback -PubSubClient client(server, 1883, callback, wifiClient); // mqtt client +// MQTT server address +IPAddress server(178, 62, 108, 47); + +// MQTT +PubSubClient client(server); + void setup() { Serial.begin(115200); @@ -98,7 +100,8 @@ void lelylanConnection() { // add reconnection logics if (!client.connected()) { // connection to MQTT server - if (client.connect(clientId, deviceId, deviceSecret)) { + if (client.connect(MQTT::Connect(clientId) + .set_auth(deviceId, deviceSecret))) { Serial.println("[PHYSICAL] Successfully connected with MQTT"); lelylanSubscribe(); // topic subscription } From b096e74fc0da84feb8fe84aefa4b96db9c747c15 Mon Sep 17 00:00:00 2001 From: marcoschwartz Date: Wed, 12 Aug 2015 13:29:15 +0200 Subject: [PATCH 5/6] added Travis CI --- .travis.yml | 13 +++++++++++++ README.md | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..456fe66 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: c +sudo: false +before_install: + - source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh) +install: + - arduino --install-library "aREST" + - arduino --install-library "DHT sensor library" +script: + - build_platform esp8266 +notifications: + email: + on_success: change + on_failure: change \ No newline at end of file diff --git a/README.md b/README.md index c7c90b9..f8c84ec 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ -# iot-esp8266 +# Internet of Things with the ESP8266 [![Build Status](https://travis-ci.org/openhomeautomation/iot-esp8266.svg)](https://travis-ci.org/openhomeautomation/iot-esp8266) + Code for the Internet of Things with the ESP8266 book From 8082f906fc86d241989723b80327e4420315667b Mon Sep 17 00:00:00 2001 From: marcoschwartz Date: Tue, 28 Jun 2016 12:12:58 +0200 Subject: [PATCH 6/6] updated for new book version --- .travis.yml | 1 + README.md | 0 cloud_data_logger/.DS_Store | Bin 0 -> 6148 bytes .../cloud_data_logger/cloud_data_logger.ino | 37 +++-- cloud_data_logger/dht_test/dht_test.ino | 0 .../lamp_anywhere/lamp_anywhere.ino | 62 ++++++++ .../lelylan_lamp/lelylan_lamp.ino | 139 ------------------ .../cloud_sensor/cloud_sensor.ino | 87 +++++++++++ .../lelylan_sensor/lelylan_sensor.ino | 120 --------------- 9 files changed, 168 insertions(+), 278 deletions(-) mode change 100644 => 100755 .travis.yml mode change 100644 => 100755 README.md create mode 100644 cloud_data_logger/.DS_Store mode change 100644 => 100755 cloud_data_logger/cloud_data_logger/cloud_data_logger.ino mode change 100644 => 100755 cloud_data_logger/dht_test/dht_test.ino create mode 100644 control_lamp_anywhere/lamp_anywhere/lamp_anywhere.ino delete mode 100644 control_lamp_anywhere/lelylan_lamp/lelylan_lamp.ino create mode 100644 real_time_iot_dashboard/cloud_sensor/cloud_sensor.ino delete mode 100644 real_time_iot_dashboard/lelylan_sensor/lelylan_sensor.ino diff --git a/.travis.yml b/.travis.yml old mode 100644 new mode 100755 index 456fe66..cc6aecf --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ before_install: install: - arduino --install-library "aREST" - arduino --install-library "DHT sensor library" + - arduino --install-library "DHT sensor library" script: - build_platform esp8266 notifications: diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/cloud_data_logger/.DS_Store b/cloud_data_logger/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..2e0aaa5c167b549a572cbfffebc15d1431b0b8e6 GIT binary patch literal 6148 zcmeHKJ8HvF5S&e17}7Xh%6A3dKp5u)K7bSApvKr>;?$~qt{g419|A%NxJhGXVRznJ z?YxJk&}sqL>bzS53jlMvBQ74M=FizE~o%@f3~a7bi^W=SO`)oR4Bq%+^Dt}7f8lMbul!|G*VFU*EaeK-D|$+Zd?b2 nA=)u9+A%lYj-R3^>zc3myek|MgU)==iTW9EU1U<=zZLifw6_>V literal 0 HcmV?d00001 diff --git a/cloud_data_logger/cloud_data_logger/cloud_data_logger.ino b/cloud_data_logger/cloud_data_logger/cloud_data_logger.ino old mode 100644 new mode 100755 index 41b20e8..550253a --- a/cloud_data_logger/cloud_data_logger/cloud_data_logger.ino +++ b/cloud_data_logger/cloud_data_logger/cloud_data_logger.ino @@ -1,10 +1,10 @@ /*************************************************** -Written by Marco Schwartz for Open Home Automation. +Written by Marco Schwartz for Open Home Automation. BSD license, all text above must be included in any redistribution -Based on the original sketches supplied with the ESP8266/Arduino -implementation written by Ivan Grokhotkov +Based on the original sketches supplied with the ESP8266/Arduino +implementation written by Ivan Grokhotkov ****************************************************/ @@ -13,8 +13,8 @@ implementation written by Ivan Grokhotkov #include "DHT.h" // WiFi parameters -const char* ssid = "your_wifi_network_name"; -const char* password = "your_wifi_network_password"; +const char* ssid = "wifi-name"; +const char* password = "wifi-pass"; // Pin #define DHTPIN 5 @@ -29,12 +29,12 @@ DHT dht(DHTPIN, DHTTYPE, 15); const char* host = "dweet.io"; void setup() { - + // Start Serial Serial.begin(115200); delay(10); - - // Init DHT + + // Init DHT dht.begin(); // We start by connecting to a WiFi network @@ -49,16 +49,16 @@ void setup() { } Serial.println(""); - Serial.println("WiFi connected"); + Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { - + Serial.print("Connecting to "); Serial.println(host); - + // Use WiFiClient class to create TCP connections WiFiClient client; const int httpPort = 80; @@ -66,29 +66,28 @@ void loop() { Serial.println("connection failed"); return; } - + // Reading temperature and humidity int h = dht.readHumidity(); // Read temperature as Celsius int t = dht.readTemperature(); - + // This will send the request to the server client.print(String("GET /dweet/for/myesp8266?temperature=") + String(t) + "&humidity=" + String(h) + " HTTP/1.1\r\n" + - "Host: " + host + "\r\n" + + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(10); - + // 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"); - + // Repeat every 10 seconds delay(10000); - -} +} diff --git a/cloud_data_logger/dht_test/dht_test.ino b/cloud_data_logger/dht_test/dht_test.ino old mode 100644 new mode 100755 diff --git a/control_lamp_anywhere/lamp_anywhere/lamp_anywhere.ino b/control_lamp_anywhere/lamp_anywhere/lamp_anywhere.ino new file mode 100644 index 0000000..5e9e387 --- /dev/null +++ b/control_lamp_anywhere/lamp_anywhere/lamp_anywhere.ino @@ -0,0 +1,62 @@ +// Control a lamp from anywhere using the ESP8266 + +// 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 = "40g3cs"; + +// WiFi parameters +const char* ssid = "wifi-name"; +const char* password = "wifi-pass"; + +// 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 and ID to device + rest.set_id(device_id); + rest.set_name("lamp"); + + // 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) { + + // Handle + rest.handle_callback(client, topic, payload, length); + +} diff --git a/control_lamp_anywhere/lelylan_lamp/lelylan_lamp.ino b/control_lamp_anywhere/lelylan_lamp/lelylan_lamp.ino deleted file mode 100644 index 2651850..0000000 --- a/control_lamp_anywhere/lelylan_lamp/lelylan_lamp.ino +++ /dev/null @@ -1,139 +0,0 @@ -/*************************************************** - -Written by Marco Schwartz for Open Home Automation. -BSD license, all text above must be included in any redistribution - -Based on the original sketches supplied with the ESP8266/Arduino -implementation written by Ivan Grokhotkov - -****************************************************/ - -// Libraries -#include -#include - -// Credentials -String deviceId = "device_id"; // * set your device id (will be the MQTT client username) -String deviceSecret = "device_secret"; // * set your device secret (will be the MQTT client password) -String clientId = "7dslk2dfksd"; // * set a random string (max 23 chars, will be the MQTT client id) - -// WiFi name & password -const char* ssid = "your_WiFi_name"; -const char* password = "your_WiFi_password"; - -// Sketch logic -char* payloadOn = "{\"properties\":[{\"id\":\"518be5a700045e1521000001\",\"value\":\"on\"}]}"; -char* payloadOff = "{\"properties\":[{\"id\":\"518be5a700045e1521000001\",\"value\":\"off\"}]}"; - -// Topics -String outTopic = "devices/" + deviceId + "/set"; // * MQTT channel where physical updates are published -String inTopic = "devices/" + deviceId + "/get"; // * MQTT channel where lelylan updates are received - -// MQTT server address -IPAddress server(178, 62, 108, 47); - -// WiFi Client -WiFiClient wifiClient; - -// MQTT -PubSubClient client(server); - -// Pins -int outPin = 5; // led - -// Logic -int state = HIGH; // current state of the output pin -int reading; // current reading from the input pin -int previous = LOW; // previous reading from the input pin -long time = 0; // the last time the output pin was toggled -long debounce = 200; // the debounce time, increase if the output flickers - -// Callback -void callback(const MQTT::Publish& pub) { - - // Copy the payload content into a char* - char* json; - json = (char*) malloc(pub.payload_len() + 1); - memcpy(json, pub.payload(), pub.payload_len()); - json[pub.payload_len()] = '\0'; - - // Update the physical status and confirm the executed update - boolean state; - if (String(payloadOn) == String(json)) { - Serial.println("[LELYLAN] Led turned on"); - lelylanPublish("on"); - state = HIGH; - } else { - Serial.println("[LELYLAN] Led turned off"); - lelylanPublish("off"); - state = LOW; - } - - digitalWrite(outPin, state); - free(json); -} - -void setup() { - Serial.begin(115200); - delay(500); - - // Connect to WiFi - Serial.println(); - Serial.println(); - Serial.print("Connecting to "); - Serial.println(ssid); - - WiFi.begin(ssid, password); - - // Set callback - client.set_callback(callback); - - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - - // MQTT server connection - lelylanConnection(); - pinMode(outPin, OUTPUT); // led pin setup -} - -void loop() { - - // Keep connection open - lelylanConnection(); - -} - -/* MQTT server connection */ -void lelylanConnection() { - // add reconnection logics - if (!client.connected()) { - // connection to MQTT server - if (client.connect(MQTT::Connect(clientId) - .set_auth(deviceId, deviceSecret))) { - Serial.println("[PHYSICAL] Successfully connected with MQTT"); - lelylanSubscribe(); // topic subscription - } - } - client.loop(); -} - -/* MQTT publish */ -void lelylanPublish(char* value) { - if (value == "on") - client.publish(outTopic, payloadOn); // light on - else - client.publish(outTopic, payloadOff); // light off -} - -/* MQTT subscribe */ -void lelylanSubscribe() { - client.subscribe(inTopic); -} - -// diff --git a/real_time_iot_dashboard/cloud_sensor/cloud_sensor.ino b/real_time_iot_dashboard/cloud_sensor/cloud_sensor.ino new file mode 100644 index 0000000..3e4261f --- /dev/null +++ b/real_time_iot_dashboard/cloud_sensor/cloud_sensor.ino @@ -0,0 +1,87 @@ +// Cloud sensor with the ESP8266 & aREST + +// Import required libraries +#include +#include +#include +#include "DHT.h" + +// DHT11 sensor pins +#define DHTPIN 5 +#define DHTTYPE DHT11 + +// Initialize DHT sensor +DHT dht(DHTPIN, DHTTYPE, 15); + +// 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 = "42gcfs"; + +// WiFi parameters +const char* ssid = "wifi-name"; +const char* password = "wifi-pass"; + +// Variables to be exposed to the API +float temperature; +float humidity; + +// Functions +void callback(char* topic, byte* payload, unsigned int length); + +void setup(void) +{ + + // Start Serial + Serial.begin(115200); + + // Init DHT + dht.begin(); + + // Set callback + client.setCallback(callback); + + // Give name and ID to device + rest.set_id(device_id); + rest.set_name("sensor"); + + // Init variables and expose them to REST API + 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 and humidity + humidity = dht.readHumidity(); + + // Read temperature as Celsius + 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) { + + // Handle + rest.handle_callback(client, topic, payload, length); + +} diff --git a/real_time_iot_dashboard/lelylan_sensor/lelylan_sensor.ino b/real_time_iot_dashboard/lelylan_sensor/lelylan_sensor.ino deleted file mode 100644 index 6c752fa..0000000 --- a/real_time_iot_dashboard/lelylan_sensor/lelylan_sensor.ino +++ /dev/null @@ -1,120 +0,0 @@ -/*************************************************** - -Written by Marco Schwartz for Open Home Automation. -BSD license, all text above must be included in any redistribution - -Based on the original sketches supplied with the ESP8266/Arduino -implementation written by Ivan Grokhotkov - -****************************************************/ - -// Libraries -#include -#include -#include "DHT.h" - -// Credentials -String deviceId = "device_id"; // * set your device id (will be the MQTT client username) -String deviceSecret = "device_secret"; // * set your device secret (will be the MQTT client password) -String clientId = "7dslk2dfksd"; // * set a random string (max 23 chars, will be the MQTT client id) - -// WiFi name & password -const char* ssid = "your_WiFi_name"; -const char* password = "your_WiFi_password"; - -// Topics -String outTopic = "devices/" + deviceId + "/set"; // * MQTT channel where physical updates are published -String inTopic = "devices/" + deviceId + "/get"; // * MQTT channel where lelylan updates are received - -// WiFi client -WiFiClient wifiClient; - -// Pin & type -#define DHTPIN 5 -#define DHTTYPE DHT11 - -// Initialize DHT sensor -DHT dht(DHTPIN, DHTTYPE, 15); - -// MQTT server address -IPAddress server(178, 62, 108, 47); - -// MQTT -PubSubClient client(server); - - -void setup() { - Serial.begin(115200); - delay(500); - - // Init DHT - dht.begin(); - - // Connect to WiFi - 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()); - - lelylanConnection(); // MQTT server connection -} - -void loop() { - - // Keep connection open - lelylanConnection(); - - // Reading temperature and humidity - int h = dht.readHumidity(); - - // Read temperature as Celsius - int t = dht.readTemperature(); - - // Messages for MQTT - String temperature = "{\"properties\":[{\"id\":\"552b72f1c70d1fd785000003\",\"value\":\"" + String(t) + "\"}]}"; - String humidity = "{\"properties\":[{\"id\":\"552b7315c70d1f4ee0000003\",\"value\":\"" + String(h) + "\"}]}"; - - // Publish temperature - client.publish(outTopic, (char *) temperature.c_str()); - delay(100); - - // Publish humidity - client.publish(outTopic, (char *) humidity.c_str()); - delay(10000); - -} - -/* MQTT server connection */ -void lelylanConnection() { - // add reconnection logics - if (!client.connected()) { - // connection to MQTT server - if (client.connect(MQTT::Connect(clientId) - .set_auth(deviceId, deviceSecret))) { - Serial.println("[PHYSICAL] Successfully connected with MQTT"); - lelylanSubscribe(); // topic subscription - } - } - client.loop(); -} - -/* MQTT subscribe */ -void lelylanSubscribe() { - client.subscribe(inTopic); -} - -/* Receive Lelylan message and confirm the physical change */ -void callback(char* topic, byte* payload, unsigned int length) { - -}