diff --git a/.travis.yml b/.travis.yml new file mode 100755 index 0000000..cc6aecf --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +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" + - 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 old mode 100644 new mode 100755 index c7c90b9..f8c84ec --- 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 diff --git a/chapter1/.DS_Store b/chapter1/.DS_Store deleted file mode 100644 index befb8e4..0000000 Binary files a/chapter1/.DS_Store and /dev/null differ diff --git a/chapter1/cloud_data_logger/.DS_Store b/chapter1/cloud_data_logger/.DS_Store deleted file mode 100644 index 71856ec..0000000 Binary files a/chapter1/cloud_data_logger/.DS_Store and /dev/null differ diff --git a/chapter2/lelylan_lamp/lelylan_lamp.ino b/chapter2/lelylan_lamp/lelylan_lamp.ino deleted file mode 100644 index b9f0d51..0000000 --- a/chapter2/lelylan_lamp/lelylan_lamp.ino +++ /dev/null @@ -1,119 +0,0 @@ -// Libraries -#include -#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) - -// 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\"}]}"; - -byte server[] = { 178, 62, 108, 47 }; // MQTT server address - -// WiFi Client -WiFiClient wifiClient; - -// MQTT -void callback(char* topic, byte* payload, unsigned int length); // subscription callback -PubSubClient client(server, 1883, callback, wifiClient); // mqtt client - -// 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 - -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); - - 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(clientId, 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); -} - -/* 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/chapter3/lelylan_sensor/lelylan_sensor.ino b/chapter3/lelylan_sensor/lelylan_sensor.ino deleted file mode 100644 index c834ce0..0000000 --- a/chapter3/lelylan_sensor/lelylan_sensor.ino +++ /dev/null @@ -1,107 +0,0 @@ -// Libraries -#include -#include -#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) - -// WiFi name & password -const char* ssid = "your_WiFi_name"; -const char* password = "your_WiFi_password"; - -// MQTT server address -byte server[] = { 178, 62, 108, 47 }; - -// WiFi client -WiFiClient wifiClient; - -// Pin & type -#define DHTPIN 5 -#define DHTTYPE DHT11 - -// 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 - -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(clientId, 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) { - -} diff --git a/chapter1/dht_test/.DS_Store b/cloud_data_logger/.DS_Store similarity index 96% rename from chapter1/dht_test/.DS_Store rename to cloud_data_logger/.DS_Store index 88e75e9..2e0aaa5 100644 Binary files a/chapter1/dht_test/.DS_Store and b/cloud_data_logger/.DS_Store differ diff --git a/chapter1/cloud_data_logger/cloud_data_logger.ino b/cloud_data_logger/cloud_data_logger/cloud_data_logger.ino old mode 100644 new mode 100755 similarity index 73% rename from chapter1/cloud_data_logger/cloud_data_logger.ino rename to cloud_data_logger/cloud_data_logger/cloud_data_logger.ino index 74378c2..550253a --- a/chapter1/cloud_data_logger/cloud_data_logger.ino +++ b/cloud_data_logger/cloud_data_logger/cloud_data_logger.ino @@ -1,10 +1,20 @@ -// 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" // 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 @@ -16,15 +26,15 @@ 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() { - + // Start Serial Serial.begin(115200); delay(10); - - // Init DHT + + // Init DHT dht.begin(); // We start by connecting to a WiFi network @@ -39,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; @@ -56,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/chapter1/dht_test/dht_test.ino b/cloud_data_logger/dht_test/dht_test.ino old mode 100644 new mode 100755 similarity index 100% rename from chapter1/dht_test/dht_test.ino rename to cloud_data_logger/dht_test/dht_test.ino 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/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); + +}