├── README.md ├── chapter1 ├── basic_actuator │ └── basic_actuator.ino ├── basic_sensor │ └── basic_sensor.ino ├── grab_page │ └── grab_page.ino ├── send_data │ └── send_data.ino └── wifi_test │ └── wifi_test.ino ├── chapter2 ├── .DS_Store ├── posting_data │ └── posting_data.ino └── sensors_test │ ├── .DS_Store │ └── sensors_test.ino ├── chapter3 ├── .DS_Store ├── email_notifications │ └── email_notifications.ino ├── facebook │ ├── TembooAccount.h │ └── facebook.ino ├── google_sheet │ └── google_sheet.ino ├── push_alerts │ └── push_alerts.ino ├── sms_data │ └── sms_data.ino └── tweeting │ ├── TembooAccount.h │ └── tweeting.ino └── chapter4 ├── .DS_Store ├── automated_light ├── .DS_Store ├── automated_light_lamp │ └── automated_light_lamp.ino └── automated_light_sensor │ └── automated_light_sensor.ino ├── automated_sprinkler ├── .DS_Store ├── sensor_test │ └── sensor_test.ino ├── sprinkler_relay │ └── sprinkler_relay.ino └── sprinkler_sensor │ ├── .DS_Store │ └── sprinkler_sensor.ino ├── cloud_m2m ├── cloud_m2m_button │ └── cloud_m2m_button.ino └── cloud_m2m_led │ └── cloud_m2m_led.ino ├── local_m2m ├── local_m2m_button │ └── local_m2m_button.ino └── local_m2m_led │ └── local_m2m_led.ino └── m2m_alarm ├── alarm_led └── alarm_led.ino └── alarm_sensor └── alarm_sensor.ino /README.md: -------------------------------------------------------------------------------- 1 | # iot-arduino-cookbook 2 | Code for the Internet of Things Cookbook 3 | -------------------------------------------------------------------------------- /chapter1/basic_actuator/basic_actuator.ino: -------------------------------------------------------------------------------- 1 | // Pins 2 | int relayPin = 5; 3 | 4 | void setup() { 5 | 6 | // Set pin as output 7 | pinMode(relayPin, OUTPUT); 8 | 9 | } 10 | 11 | void loop() { 12 | 13 | // Set relay ON 14 | digitalWrite(relayPin, HIGH); 15 | 16 | // Wait 17 | delay(1000); 18 | 19 | // Set relay OFF 20 | digitalWrite(relayPin, LOW); 21 | 22 | // Wait 23 | delay(1000); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /chapter1/basic_sensor/basic_sensor.ino: -------------------------------------------------------------------------------- 1 | // Pins 2 | int sensorPin = A0; 3 | 4 | void setup() { 5 | 6 | // Serial 7 | Serial.begin(115200); 8 | 9 | } 10 | 11 | void loop() { 12 | 13 | // Reading 14 | int sensorValue = analogRead(sensorPin); 15 | 16 | // Display 17 | Serial.print("Sensor reading: "); 18 | Serial.println(sensorValue); 19 | 20 | // Wait 21 | delay(500); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /chapter1/grab_page/grab_page.ino: -------------------------------------------------------------------------------- 1 | // Library 2 | #include 3 | #include 4 | 5 | // Credentials 6 | char ssid[] = "wifi-name"; // your network SSID (name) 7 | char pass[] = "wifi-pass"; // your network password 8 | int status = WL_IDLE_STATUS; // the Wifi radio's status 9 | 10 | // Server 11 | char server[] = "www.example.com"; 12 | 13 | // WiFi client 14 | WiFiClient client; 15 | 16 | void setup() { 17 | 18 | // Initialize serial 19 | Serial.begin(115200); 20 | while (!Serial) { 21 | ; // wait for serial port to connect. Needed for native USB port only 22 | } 23 | 24 | // Check for the presence of the shield: 25 | if (WiFi.status() == WL_NO_SHIELD) { 26 | Serial.println("WiFi shield not present"); 27 | // don't continue: 28 | while (true); 29 | } 30 | 31 | // Attempt to connect to Wifi network: 32 | while (status != WL_CONNECTED) { 33 | Serial.print("Attempting to connect to SSID: "); 34 | Serial.println(ssid); 35 | // Connect to WPA/WPA2 network. Change this line if using open or WEP network: 36 | status = WiFi.begin(ssid, pass); 37 | 38 | // wait 10 seconds for connection: 39 | delay(10000); 40 | } 41 | Serial.println("Connected to wifi"); 42 | printWifiStatus(); 43 | 44 | Serial.println("\nStarting connection to server..."); 45 | 46 | // Connect to server 47 | if (client.connect(server, 80)) { 48 | Serial.println("connected to server"); 49 | 50 | // Make a request: 51 | client.println("GET / HTTP/1.1"); 52 | client.println("Host: www.example.com"); 53 | client.println("Connection: close"); 54 | client.println(); 55 | } 56 | } 57 | 58 | void loop() { 59 | 60 | // Read data 61 | while (client.available()) { 62 | char c = client.read(); 63 | Serial.write(c); 64 | } 65 | 66 | // Stop the connection 67 | if (!client.connected()) { 68 | Serial.println(); 69 | Serial.println("disconnecting from server."); 70 | client.stop(); 71 | 72 | // do nothing forevermore: 73 | while (true); 74 | } 75 | } 76 | 77 | 78 | void printWifiStatus() { 79 | // print the SSID of the network you're attached to: 80 | Serial.print("SSID: "); 81 | Serial.println(WiFi.SSID()); 82 | 83 | // print your WiFi shield's IP address: 84 | IPAddress ip = WiFi.localIP(); 85 | Serial.print("IP Address: "); 86 | Serial.println(ip); 87 | 88 | // print the received signal strength: 89 | long rssi = WiFi.RSSI(); 90 | Serial.print("signal strength (RSSI):"); 91 | Serial.print(rssi); 92 | Serial.println(" dBm"); 93 | } 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /chapter1/send_data/send_data.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include 3 | #include 4 | 5 | // WiFi credentials 6 | char ssid[] = "wifi-name"; // your network SSID (name) 7 | char pass[] = "wifi-pass"; // your network password 8 | int status = WL_IDLE_STATUS; 9 | 10 | // Initialize the Wifi client library 11 | WiFiClient client; 12 | 13 | // Server 14 | char server[] = "dweet.io"; 15 | 16 | // Connection interval 17 | unsigned long lastConnectionTime = 0; 18 | const unsigned long postingInterval = 10L * 1000L; 19 | 20 | void setup() { 21 | 22 | // Initialize serial and wait for port to open: 23 | Serial.begin(115200); 24 | while (!Serial) { 25 | ; // wait for serial port to connect. Needed for native USB port only 26 | } 27 | 28 | // Check for the presence of the shield: 29 | if (WiFi.status() == WL_NO_SHIELD) { 30 | Serial.println("WiFi shield not present"); 31 | // don't continue: 32 | while (true); 33 | } 34 | 35 | // Attempt to connect to Wifi network: 36 | while ( status != WL_CONNECTED) { 37 | Serial.print("Attempting to connect to SSID: "); 38 | Serial.println(ssid); 39 | // Connect to WPA/WPA2 network. Change this line if using open or WEP network: 40 | status = WiFi.begin(ssid, pass); 41 | 42 | // wait 10 seconds for connection: 43 | delay(10000); 44 | } 45 | 46 | // Print connection status 47 | printWifiStatus(); 48 | } 49 | 50 | void loop() { 51 | 52 | // Read incoming data 53 | while (client.available()) { 54 | char c = client.read(); 55 | Serial.write(c); 56 | } 57 | 58 | // Send request at regular intervals 59 | if (millis() - lastConnectionTime > postingInterval) { 60 | 61 | // Measure light level 62 | int sensorData = analogRead(A0); 63 | 64 | // Send request 65 | httpRequest(sensorData); 66 | } 67 | 68 | } 69 | 70 | // Make HTTP request 71 | void httpRequest(int sensorData) { 72 | 73 | // Close existing connection 74 | client.stop(); 75 | 76 | // Connect & send request 77 | if (client.connect(server, 80)) { 78 | 79 | Serial.println("connecting..."); 80 | 81 | // Send the HTTP PUT request: 82 | client.println("GET /dweet/for/myarduino?light=" + String(sensorData) + " HTTP/1.1"); 83 | client.println("Host: dweet.io"); 84 | client.println("User-Agent: ArduinoWiFi/1.1"); 85 | client.println("Connection: close"); 86 | client.println(); 87 | 88 | // Note the time that the connection was made: 89 | lastConnectionTime = millis(); 90 | } 91 | else { 92 | // if you couldn't make a connection: 93 | Serial.println("connection failed"); 94 | } 95 | } 96 | 97 | 98 | void printWifiStatus() { 99 | // print the SSID of the network you're attached to: 100 | Serial.print("SSID: "); 101 | Serial.println(WiFi.SSID()); 102 | 103 | // print your WiFi shield's IP address: 104 | IPAddress ip = WiFi.localIP(); 105 | Serial.print("IP Address: "); 106 | Serial.println(ip); 107 | 108 | // print the received signal strength: 109 | long rssi = WiFi.RSSI(); 110 | Serial.print("signal strength (RSSI):"); 111 | Serial.print(rssi); 112 | Serial.println(" dBm"); 113 | } 114 | 115 | 116 | -------------------------------------------------------------------------------- /chapter1/wifi_test/wifi_test.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include 3 | #include 4 | 5 | // Credentials 6 | char ssid[] = "wifi-name"; // your network SSID (name) 7 | char pass[] = "wifi-pass"; // your network password 8 | int status = WL_IDLE_STATUS; // the Wifi radio's status 9 | 10 | void setup() { 11 | 12 | // Serial 13 | Serial.begin(115200); 14 | 15 | // Attempt to connect to Wifi network: 16 | while ( status != WL_CONNECTED) { 17 | Serial.print("Attempting to connect to WPA SSID: "); 18 | Serial.println(ssid); 19 | 20 | // Connect to WPA/WPA2 network: 21 | status = WiFi.begin(ssid, pass); 22 | 23 | // Wait 10 seconds for connection: 24 | delay(10000); 25 | } 26 | 27 | // you're connected now, so print out the data: 28 | Serial.print("You're connected to the network"); 29 | printCurrentNet(); 30 | printWifiData(); 31 | 32 | } 33 | 34 | void loop() { 35 | 36 | // Check the network connection once every 10 seconds: 37 | delay(10000); 38 | printCurrentNet(); 39 | } 40 | 41 | void printWifiData() { 42 | // print your WiFi shield's IP address: 43 | IPAddress ip = WiFi.localIP(); 44 | Serial.print("IP Address: "); 45 | Serial.println(ip); 46 | Serial.println(ip); 47 | 48 | // print your MAC address: 49 | byte mac[6]; 50 | WiFi.macAddress(mac); 51 | Serial.print("MAC address: "); 52 | Serial.print(mac[5], HEX); 53 | Serial.print(":"); 54 | Serial.print(mac[4], HEX); 55 | Serial.print(":"); 56 | Serial.print(mac[3], HEX); 57 | Serial.print(":"); 58 | Serial.print(mac[2], HEX); 59 | Serial.print(":"); 60 | Serial.print(mac[1], HEX); 61 | Serial.print(":"); 62 | Serial.println(mac[0], HEX); 63 | 64 | } 65 | 66 | void printCurrentNet() { 67 | // print the SSID of the network you're attached to: 68 | Serial.print("SSID: "); 69 | Serial.println(WiFi.SSID()); 70 | 71 | // print the MAC address of the router you're attached to: 72 | byte bssid[6]; 73 | WiFi.BSSID(bssid); 74 | Serial.print("BSSID: "); 75 | Serial.print(bssid[5], HEX); 76 | Serial.print(":"); 77 | Serial.print(bssid[4], HEX); 78 | Serial.print(":"); 79 | Serial.print(bssid[3], HEX); 80 | Serial.print(":"); 81 | Serial.print(bssid[2], HEX); 82 | Serial.print(":"); 83 | Serial.print(bssid[1], HEX); 84 | Serial.print(":"); 85 | Serial.println(bssid[0], HEX); 86 | 87 | // print the received signal strength: 88 | long rssi = WiFi.RSSI(); 89 | Serial.print("signal strength (RSSI):"); 90 | Serial.println(rssi); 91 | 92 | // print the encryption type: 93 | byte encryption = WiFi.encryptionType(); 94 | Serial.print("Encryption Type:"); 95 | Serial.println(encryption, HEX); 96 | Serial.println(); 97 | } 98 | 99 | -------------------------------------------------------------------------------- /chapter2/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcoschwartz/iot-arduino-cookbook/e120c3a32a2765331638cf15a1ad0e78a5d7b73b/chapter2/.DS_Store -------------------------------------------------------------------------------- /chapter2/posting_data/posting_data.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include 3 | #include 4 | #include "DHT.h" 5 | 6 | // Pins 7 | int sensorPin = A0; 8 | #define DHTPIN 5 9 | #define DHTTYPE DHT11 10 | 11 | // WiFi credentials 12 | char ssid[] = "wifi-name"; // your network SSID (name) 13 | char pass[] = "wifi-pass"; // your network password 14 | int status = WL_IDLE_STATUS; 15 | 16 | // Thing name 17 | char * thingName = "mymkr1000"; 18 | 19 | // Server 20 | char server[] = "dweet.io"; 21 | 22 | // Connection interval 23 | unsigned long lastConnectionTime = 0; 24 | const unsigned long postingInterval = 10L * 1000L; 25 | 26 | // Initialize the Wifi client library 27 | WiFiClient client; 28 | 29 | // Initialize DHT sensor 30 | DHT dht(DHTPIN, DHTTYPE, 15); 31 | 32 | void setup() { 33 | 34 | // Serial 35 | Serial.begin(115200); 36 | 37 | // Init DHT 38 | dht.begin(); 39 | 40 | // Check for the presence of the shield: 41 | if (WiFi.status() == WL_NO_SHIELD) { 42 | Serial.println("WiFi shield not present"); 43 | // don't continue: 44 | while (true); 45 | } 46 | 47 | // Attempt to connect to Wifi network: 48 | while ( status != WL_CONNECTED) { 49 | Serial.print("Attempting to connect to SSID: "); 50 | Serial.println(ssid); 51 | // Connect to WPA/WPA2 network. Change this line if using open or WEP network: 52 | status = WiFi.begin(ssid, pass); 53 | 54 | // wait 10 seconds for connection: 55 | delay(10000); 56 | } 57 | 58 | } 59 | 60 | void loop() { 61 | 62 | // Read incoming data 63 | while (client.available()) { 64 | char c = client.read(); 65 | Serial.write(c); 66 | } 67 | 68 | // Send request at regular intervals 69 | if (millis() - lastConnectionTime > postingInterval) { 70 | 71 | // Reading temperature and humidity 72 | float humidity = dht.readHumidity(); 73 | 74 | // Read temperature as Celsius 75 | float temperature = dht.readTemperature(); 76 | 77 | // Reading from analog sensor 78 | int sensorValue = analogRead(sensorPin); 79 | float lightLevel = sensorValue/1024.*100; 80 | 81 | // Send request 82 | httpRequest(temperature, humidity, lightLevel); 83 | } 84 | 85 | } 86 | 87 | // Make HTTP request 88 | void httpRequest(float temperature, float humidity, float lightLevel) { 89 | 90 | // Close existing connection 91 | client.stop(); 92 | 93 | // Connect & send request 94 | if (client.connect(server, 80)) { 95 | 96 | Serial.println("connecting..."); 97 | 98 | // Prepare request 99 | String request = "GET /dweet/for/"; 100 | request += String(thingName); 101 | request += "?temperature=" + String(temperature); 102 | request += "&humidity=" + String(humidity); 103 | request += "&light=" + String(lightLevel); 104 | request += " HTTP/1.1"; 105 | 106 | // Send the HTTP request: 107 | client.println(request); 108 | client.println("Host: dweet.io"); 109 | client.println("User-Agent: ArduinoWiFi/1.1"); 110 | client.println("Connection: close"); 111 | client.println(); 112 | 113 | // Note the time that the connection was made: 114 | lastConnectionTime = millis(); 115 | } 116 | else { 117 | // if you couldn't make a connection: 118 | Serial.println("connection failed"); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /chapter2/sensors_test/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcoschwartz/iot-arduino-cookbook/e120c3a32a2765331638cf15a1ad0e78a5d7b73b/chapter2/sensors_test/.DS_Store -------------------------------------------------------------------------------- /chapter2/sensors_test/sensors_test.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include "DHT.h" 3 | 4 | // Pins 5 | int sensorPin = A0; 6 | #define DHTPIN 5 7 | #define DHTTYPE DHT11 8 | 9 | // Initialize DHT sensor 10 | DHT dht(DHTPIN, DHTTYPE, 15); 11 | 12 | void setup() { 13 | 14 | // Serial 15 | Serial.begin(115200); 16 | 17 | // Init DHT 18 | dht.begin(); 19 | 20 | } 21 | 22 | void loop() { 23 | 24 | // Reading temperature and humidity 25 | float humidity = dht.readHumidity(); 26 | // Read temperature as Celsius 27 | float temperature = dht.readTemperature(); 28 | 29 | // Display 30 | Serial.print("Temperature: "); 31 | Serial.print(temperature); 32 | Serial.println(" C"); 33 | 34 | Serial.print("Humidity: "); 35 | Serial.print(humidity); 36 | Serial.println(" %"); 37 | 38 | // Reading from analog sensor 39 | int sensorValue = analogRead(sensorPin); 40 | float lightLevel = sensorValue/1024.*100; 41 | 42 | // Display 43 | Serial.print("Light level: "); 44 | Serial.print(lightLevel); 45 | Serial.println(" %"); 46 | Serial.println(); 47 | 48 | // Wait 49 | delay(500); 50 | 51 | } 52 | -------------------------------------------------------------------------------- /chapter3/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcoschwartz/iot-arduino-cookbook/e120c3a32a2765331638cf15a1ad0e78a5d7b73b/chapter3/.DS_Store -------------------------------------------------------------------------------- /chapter3/email_notifications/email_notifications.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include 3 | #include 4 | #include "DHT.h" 5 | 6 | // WiFi parameters 7 | char ssid[] = "wifi-name"; 8 | char password[] = "wifi-pass"; 9 | 10 | // IFTTT settings 11 | const char* host = "maker.ifttt.com"; 12 | const char* eventName = "alert_light_level"; 13 | const char* key = "key"; 14 | 15 | // Status 16 | int status = WL_IDLE_STATUS; 17 | 18 | void setup() { 19 | 20 | Serial.begin(115200); 21 | delay(10); 22 | 23 | // Connect to WiFi 24 | while (status != WL_CONNECTED) { 25 | Serial.print("Attempting to connect to SSID: "); 26 | Serial.println(ssid); 27 | status = WiFi.begin(ssid, password); 28 | 29 | // Wait 10 seconds for connection: 30 | delay(10000); 31 | } 32 | Serial.println("WiFi connected"); 33 | } 34 | 35 | 36 | void loop() { 37 | 38 | // Measure light level 39 | int sensorData = analogRead(0); 40 | float lightLevel = (sensorData/1024.)*100; 41 | 42 | // Check humidity data 43 | if (lightLevel < 50.00) { 44 | 45 | Serial.print("Connecting to "); 46 | Serial.println(host); 47 | 48 | // Use WiFiClient class to create TCP connections 49 | WiFiClient client; 50 | const int httpPort = 80; 51 | if (!client.connect(host, httpPort)) { 52 | Serial.println("connection failed"); 53 | return; 54 | } 55 | 56 | // We now create a URI for the request 57 | String url = "/trigger/"; 58 | url += eventName; 59 | url += "/with/key/"; 60 | url += key; 61 | 62 | Serial.print("Requesting URL: "); 63 | Serial.println(url); 64 | 65 | // This will send the request to the server 66 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 67 | "Host: " + host + "\r\n" + 68 | "Connection: close\r\n\r\n"); 69 | int timeout = millis() + 5000; 70 | while (client.available() == 0) { 71 | if (timeout - millis() < 0) { 72 | Serial.println(">>> Client Timeout !"); 73 | client.stop(); 74 | return; 75 | } 76 | } 77 | 78 | // Read all the lines of the reply from server and print them to Serial 79 | while(client.available()){ 80 | String line = client.readStringUntil('\r'); 81 | Serial.print(line); 82 | } 83 | 84 | Serial.println(); 85 | Serial.println("closing connection"); 86 | 87 | // Wait a long time before new alert 88 | delay(10 * 60 * 1000); 89 | 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /chapter3/facebook/TembooAccount.h: -------------------------------------------------------------------------------- 1 | /* 2 | IMPORTANT NOTE about TembooAccount.h 3 | 4 | TembooAccount.h contains your Temboo account information and must be included 5 | alongside your sketch. To do so, make a new tab in Arduino, call it TembooAccount.h, 6 | and copy this content into it. 7 | */ 8 | 9 | #define TEMBOO_ACCOUNT "account" // Your Temboo account name 10 | #define TEMBOO_APP_KEY_NAME "app" // Your Temboo app key name 11 | #define TEMBOO_APP_KEY "key" // Your Temboo app key 12 | 13 | /* 14 | The same TembooAccount.h file settings can be used for all Temboo SDK sketches. 15 | Keeping your account information in a separate file means you can share the 16 | main .ino file without worrying that you forgot to delete your credentials. 17 | */ 18 | -------------------------------------------------------------------------------- /chapter3/facebook/facebook.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include 3 | #include 4 | #include 5 | #include "TembooAccount.h" // Contains Temboo account information 6 | 7 | // WiFi parameters 8 | char ssid[] = "wifi-name"; 9 | char password[] = "wifi-pass"; 10 | 11 | // WiFi client 12 | WiFiClient client; 13 | 14 | // Status 15 | int status = WL_IDLE_STATUS; 16 | 17 | void setup() { 18 | 19 | // Serial 20 | Serial.begin(115200); 21 | 22 | // For debugging, wait until the serial console is connected 23 | delay(4000); 24 | while(!Serial); 25 | 26 | // Connect to WiFi 27 | while (status != WL_CONNECTED) { 28 | Serial.print("Attempting to connect to SSID: "); 29 | Serial.println(ssid); 30 | status = WiFi.begin(ssid, password); 31 | 32 | // Wait 10 seconds for connection: 33 | delay(10000); 34 | } 35 | Serial.println("WiFi connected"); 36 | } 37 | 38 | void loop() { 39 | 40 | TembooChoreo PostChoreo(client); 41 | 42 | // Invoke the Temboo client 43 | PostChoreo.begin(); 44 | 45 | // Set Temboo account credentials 46 | PostChoreo.setAccountName(TEMBOO_ACCOUNT); 47 | PostChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); 48 | PostChoreo.setAppKey(TEMBOO_APP_KEY); 49 | 50 | // Set Choreo inputs 51 | String MessageValue = "Hello from Arduino"; 52 | PostChoreo.addInput("Message", MessageValue); 53 | String AccessTokenValue = "token"; 54 | PostChoreo.addInput("AccessToken", AccessTokenValue); 55 | 56 | // Identify the Choreo to run 57 | PostChoreo.setChoreo("/Library/Facebook/Publishing/Post"); 58 | 59 | // Run the Choreo; when results are available, print them to serial 60 | PostChoreo.run(); 61 | 62 | while(PostChoreo.available()) { 63 | char c = PostChoreo.read(); 64 | Serial.print(c); 65 | } 66 | PostChoreo.close(); 67 | 68 | 69 | Serial.println("\nWaiting...\n"); 70 | delay(30000); // wait 30 seconds between Post calls 71 | 72 | } 73 | -------------------------------------------------------------------------------- /chapter3/google_sheet/google_sheet.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include 3 | #include 4 | #include "DHT.h" 5 | 6 | // WiFi settings 7 | char ssid[] = "wifi-name"; 8 | char password[] = "wifi-pass"; 9 | 10 | // DHT11 sensor pins 11 | #define DHTPIN 5 12 | #define DHTTYPE DHT11 13 | 14 | // Initialize DHT sensor 15 | DHT dht(DHTPIN, DHTTYPE, 15); 16 | 17 | // Status 18 | int status = WL_IDLE_STATUS; 19 | 20 | // IFTTT settings 21 | const char* host = "maker.ifttt.com"; 22 | const char* eventName = "google_data"; 23 | const char* key = "key"; 24 | 25 | void setup() { 26 | 27 | Serial.begin(115200); 28 | delay(10); 29 | 30 | // Init DHT 31 | dht.begin(); 32 | 33 | // Connect to WiFi 34 | while (status != WL_CONNECTED) { 35 | Serial.print("Attempting to connect to SSID: "); 36 | Serial.println(ssid); 37 | status = WiFi.begin(ssid, password); 38 | 39 | // Wait 10 seconds for connection: 40 | delay(10000); 41 | } 42 | Serial.println("WiFi connected"); 43 | } 44 | 45 | 46 | void loop() { 47 | 48 | // Reading temperature and humidity 49 | float h = dht.readHumidity(); 50 | float t = dht.readTemperature(); 51 | 52 | // Measure light level 53 | int sensorData = analogRead(0); 54 | float lightLevel = (sensorData/1024.)*100; 55 | 56 | Serial.print("connecting to "); 57 | Serial.println(host); 58 | 59 | // Use WiFiClient class to create TCP connections 60 | WiFiClient client; 61 | const int httpPort = 80; 62 | if (!client.connect(host, httpPort)) { 63 | Serial.println("connection failed"); 64 | return; 65 | } 66 | 67 | // We now create a URI for the request 68 | String url = "/trigger/"; 69 | url += eventName; 70 | url += "/with/key/"; 71 | url += key; 72 | url += "?value1="; 73 | url += String(t); 74 | url += "&value2="; 75 | url += String(h); 76 | url += "&value3="; 77 | url += String(lightLevel); 78 | 79 | Serial.print("Requesting URL: "); 80 | Serial.println(url); 81 | 82 | // This will send the request to the server 83 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 84 | "Host: " + host + "\r\n" + 85 | "Connection: close\r\n\r\n"); 86 | int timeout = millis() + 5000; 87 | while (client.available() == 0) { 88 | if (timeout - millis() < 0) { 89 | Serial.println(">>> Client Timeout !"); 90 | client.stop(); 91 | return; 92 | } 93 | } 94 | 95 | // Read all the lines of the reply from server and print them to Serial 96 | while(client.available()){ 97 | String line = client.readStringUntil('\r'); 98 | Serial.print(line); 99 | } 100 | 101 | Serial.println(); 102 | Serial.println("closing connection"); 103 | 104 | // Wait 1 minute 105 | delay(1000); 106 | } 107 | -------------------------------------------------------------------------------- /chapter3/push_alerts/push_alerts.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include 3 | #include 4 | #include "DHT.h" 5 | 6 | // WiFi parameters 7 | char ssid[] = "wifi-name"; 8 | char password[] = "wifi-pass"; 9 | 10 | // DHT11 sensor pins 11 | #define DHTPIN 5 12 | #define DHTTYPE DHT11 13 | 14 | // Initialize DHT sensor 15 | DHT dht(DHTPIN, DHTTYPE, 15); 16 | 17 | // IFTTT settings 18 | const char* host = "maker.ifttt.com"; 19 | const char* eventName = "alert"; 20 | const char* key = "key"; 21 | 22 | // Status 23 | int status = WL_IDLE_STATUS; 24 | 25 | void setup() { 26 | 27 | Serial.begin(115200); 28 | delay(10); 29 | 30 | // Init DHT 31 | dht.begin(); 32 | 33 | // Connect to WiFi 34 | while (status != WL_CONNECTED) { 35 | Serial.print("Attempting to connect to SSID: "); 36 | Serial.println(ssid); 37 | status = WiFi.begin(ssid, password); 38 | 39 | // Wait 10 seconds for connection: 40 | delay(10000); 41 | } 42 | Serial.println("WiFi connected"); 43 | 44 | } 45 | 46 | 47 | void loop() { 48 | 49 | // Reading temperature and humidity 50 | float h = dht.readHumidity(); 51 | float t = dht.readTemperature(); 52 | 53 | // Check humidity data 54 | if (h > 30.00) { 55 | 56 | Serial.print("Connecting to "); 57 | Serial.println(host); 58 | 59 | // Use WiFiClient class to create TCP connections 60 | WiFiClient client; 61 | const int httpPort = 80; 62 | if (!client.connect(host, httpPort)) { 63 | Serial.println("connection failed"); 64 | return; 65 | } 66 | 67 | // We now create a URI for the request 68 | String url = "/trigger/"; 69 | url += eventName; 70 | url += "/with/key/"; 71 | url += key; 72 | 73 | Serial.print("Requesting URL: "); 74 | Serial.println(url); 75 | 76 | // This will send the request to the server 77 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 78 | "Host: " + host + "\r\n" + 79 | "Connection: close\r\n\r\n"); 80 | int timeout = millis() + 5000; 81 | while (client.available() == 0) { 82 | if (timeout - millis() < 0) { 83 | Serial.println(">>> Client Timeout !"); 84 | client.stop(); 85 | return; 86 | } 87 | } 88 | 89 | // Read all the lines of the reply from server and print them to Serial 90 | while(client.available()){ 91 | String line = client.readStringUntil('\r'); 92 | Serial.print(line); 93 | } 94 | 95 | Serial.println(); 96 | Serial.println("closing connection"); 97 | 98 | // Wait a long time before new alert 99 | delay(10 * 60 * 1000); 100 | 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /chapter3/sms_data/sms_data.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include 3 | #include 4 | #include "DHT.h" 5 | 6 | // WiFi settings 7 | char ssid[] = "wifi-name"; 8 | char password[] = "wifi-pass"; 9 | 10 | // DHT11 sensor pins 11 | #define DHTPIN 5 12 | #define DHTTYPE DHT11 13 | 14 | // Initialize DHT sensor 15 | DHT dht(DHTPIN, DHTTYPE, 15); 16 | 17 | // Status 18 | int status = WL_IDLE_STATUS; 19 | 20 | // IFTTT settings 21 | const char* host = "maker.ifttt.com"; 22 | const char* eventName = "text_data"; 23 | const char* key = "key"; 24 | 25 | void setup() { 26 | 27 | Serial.begin(115200); 28 | delay(10); 29 | 30 | // Init DHT 31 | dht.begin(); 32 | 33 | // Connect to WiFi 34 | while (status != WL_CONNECTED) { 35 | Serial.print("Attempting to connect to SSID: "); 36 | Serial.println(ssid); 37 | status = WiFi.begin(ssid, password); 38 | 39 | // Wait 10 seconds for connection: 40 | delay(10000); 41 | } 42 | Serial.println("WiFi connected"); 43 | } 44 | 45 | 46 | void loop() { 47 | 48 | // Reading temperature and humidity 49 | float h = dht.readHumidity(); 50 | float t = dht.readTemperature(); 51 | 52 | // Measure light level 53 | int sensorData = analogRead(0); 54 | float lightLevel = (sensorData/1024.)*100; 55 | 56 | Serial.print("connecting to "); 57 | Serial.println(host); 58 | 59 | // Use WiFiClient class to create TCP connections 60 | WiFiClient client; 61 | const int httpPort = 80; 62 | if (!client.connect(host, httpPort)) { 63 | Serial.println("connection failed"); 64 | return; 65 | } 66 | 67 | // We now create a URI for the request 68 | String url = "/trigger/"; 69 | url += eventName; 70 | url += "/with/key/"; 71 | url += key; 72 | url += "?value1="; 73 | url += String(t); 74 | url += "&value2="; 75 | url += String(h); 76 | url += "&value3="; 77 | url += String(lightLevel); 78 | 79 | Serial.print("Requesting URL: "); 80 | Serial.println(url); 81 | 82 | // This will send the request to the server 83 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 84 | "Host: " + host + "\r\n" + 85 | "Connection: close\r\n\r\n"); 86 | int timeout = millis() + 5000; 87 | while (client.available() == 0) { 88 | if (timeout - millis() < 0) { 89 | Serial.println(">>> Client Timeout !"); 90 | client.stop(); 91 | return; 92 | } 93 | } 94 | 95 | // Read all the lines of the reply from server and print them to Serial 96 | while(client.available()){ 97 | String line = client.readStringUntil('\r'); 98 | Serial.print(line); 99 | } 100 | 101 | Serial.println(); 102 | Serial.println("closing connection"); 103 | 104 | // Wait 10 minutes 105 | delay(10 * 60 * 1000); 106 | } 107 | -------------------------------------------------------------------------------- /chapter3/tweeting/TembooAccount.h: -------------------------------------------------------------------------------- 1 | /* 2 | IMPORTANT NOTE about TembooAccount.h 3 | 4 | TembooAccount.h contains your Temboo account information and must be included 5 | alongside your sketch. To do so, make a new tab in Arduino, call it TembooAccount.h, 6 | and copy this content into it. 7 | */ 8 | 9 | #define TEMBOO_ACCOUNT "account" // Your Temboo account name 10 | #define TEMBOO_APP_KEY_NAME "app" // Your Temboo app key name 11 | #define TEMBOO_APP_KEY "key" // Your Temboo app key 12 | 13 | /* 14 | The same TembooAccount.h file settings can be used for all Temboo SDK sketches. 15 | Keeping your account information in a separate file means you can share the 16 | main .ino file without worrying that you forgot to delete your credentials. 17 | */ 18 | -------------------------------------------------------------------------------- /chapter3/tweeting/tweeting.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include 3 | #include 4 | #include 5 | #include "TembooAccount.h" // Contains Temboo account information 6 | 7 | // WiFi parameters 8 | char ssid[] = "wifi-name"; 9 | char password[] = "wifi-pass"; 10 | 11 | // WiFi client 12 | WiFiClient client; 13 | 14 | // Status 15 | int status = WL_IDLE_STATUS; 16 | 17 | void setup() { 18 | 19 | // Serial 20 | Serial.begin(115200); 21 | 22 | // For debugging, wait until the serial console is connected 23 | delay(4000); 24 | while(!Serial); 25 | 26 | // Connect to WiFi 27 | while (status != WL_CONNECTED) { 28 | Serial.print("Attempting to connect to SSID: "); 29 | Serial.println(ssid); 30 | status = WiFi.begin(ssid, password); 31 | 32 | // Wait 10 seconds for connection: 33 | delay(10000); 34 | } 35 | Serial.println("WiFi connected"); 36 | } 37 | 38 | void loop() { 39 | 40 | // Runnin Choreo 41 | TembooChoreo StatusesUpdateChoreo(client); 42 | 43 | // Invoke the Temboo client 44 | StatusesUpdateChoreo.begin(); 45 | 46 | // Set Temboo account credentials 47 | StatusesUpdateChoreo.setAccountName(TEMBOO_ACCOUNT); 48 | StatusesUpdateChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); 49 | StatusesUpdateChoreo.setAppKey(TEMBOO_APP_KEY); 50 | 51 | // Set Choreo inputs 52 | String StatusUpdateValue = "Hello from Arduino!"; 53 | StatusesUpdateChoreo.addInput("StatusUpdate", StatusUpdateValue); 54 | String ConsumerKeyValue = "key"; 55 | StatusesUpdateChoreo.addInput("ConsumerKey", ConsumerKeyValue); 56 | String AccessTokenValue = "token"; 57 | StatusesUpdateChoreo.addInput("AccessToken", AccessTokenValue); 58 | String ConsumerSecretValue = "secret"; 59 | StatusesUpdateChoreo.addInput("ConsumerSecret", ConsumerSecretValue); 60 | String AccessTokenSecretValue = "secretToken"; 61 | StatusesUpdateChoreo.addInput("AccessTokenSecret", AccessTokenSecretValue); 62 | 63 | // Identify the Choreo to run 64 | StatusesUpdateChoreo.setChoreo("/Library/Twitter/Tweets/StatusesUpdate"); 65 | 66 | // Run the Choreo; when results are available, print them to serial 67 | StatusesUpdateChoreo.run(); 68 | 69 | while(StatusesUpdateChoreo.available()) { 70 | char c = StatusesUpdateChoreo.read(); 71 | Serial.print(c); 72 | } 73 | StatusesUpdateChoreo.close(); 74 | 75 | Serial.println("\nWaiting...\n"); 76 | delay(30000); // wait 30 seconds between StatusesUpdate calls 77 | } 78 | -------------------------------------------------------------------------------- /chapter4/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcoschwartz/iot-arduino-cookbook/e120c3a32a2765331638cf15a1ad0e78a5d7b73b/chapter4/.DS_Store -------------------------------------------------------------------------------- /chapter4/automated_light/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcoschwartz/iot-arduino-cookbook/e120c3a32a2765331638cf15a1ad0e78a5d7b73b/chapter4/automated_light/.DS_Store -------------------------------------------------------------------------------- /chapter4/automated_light/automated_light_lamp/automated_light_lamp.ino: -------------------------------------------------------------------------------- 1 | // Import required libraries 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | // Status 8 | int status = WL_IDLE_STATUS; 9 | 10 | // WiFi parameters 11 | const char* ssid = "wifi-name"; 12 | const char* password = "wifi-pass"; 13 | 14 | // Clients 15 | WiFiClient wifiClient; 16 | PubSubClient client(wifiClient); 17 | 18 | // Create aREST instance 19 | aREST rest = aREST(client); 20 | 21 | // Function 22 | void callback(char* topic, byte* payload, unsigned int length); 23 | 24 | void setup(void) 25 | { 26 | // Start Serial 27 | Serial.begin(115200); 28 | 29 | // Set callback 30 | client.setCallback(callback); 31 | 32 | // Give name and ID to device 33 | rest.set_id("305eyf"); 34 | rest.set_name("alarm_led"); 35 | 36 | // Connect to WiFi 37 | while (status != WL_CONNECTED) { 38 | Serial.print("Attempting to connect to SSID: "); 39 | Serial.println(ssid); 40 | status = WiFi.begin(ssid, password); 41 | 42 | // Wait 10 seconds for connection: 43 | delay(10000); 44 | } 45 | Serial.println("WiFi connected"); 46 | 47 | // Pin 6 as output 48 | pinMode(6, OUTPUT); 49 | 50 | } 51 | 52 | void loop() { 53 | 54 | // Connect to the cloud 55 | rest.handle(client); 56 | 57 | } 58 | 59 | // Handles message arrived on subscribed topic(s) 60 | void callback(char* topic, byte* payload, unsigned int length) { 61 | 62 | // Handle 63 | rest.handle_callback(client, topic, payload, length); 64 | 65 | } 66 | -------------------------------------------------------------------------------- /chapter4/automated_light/automated_light_sensor/automated_light_sensor.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include 3 | #include 4 | 5 | // Status 6 | int status = WL_IDLE_STATUS; 7 | 8 | // Credentials 9 | const char* ssid = "wifi-name"; 10 | const char* password = "wifi-pass"; 11 | 12 | // IFTTT settings 13 | const char* host = "maker.ifttt.com"; 14 | const char* key = "dPMHywdahaSxQZlCaoqnzHxcQ8vNYsTlk-42gSLAFQP"; 15 | 16 | // Threshold 17 | int light_threshold_low = 30; 18 | int light_threshold_high = 50; 19 | 20 | void setup() { 21 | 22 | // Seroa; 23 | Serial.begin(115200); 24 | delay(10); 25 | 26 | // Connect to WiFi 27 | while (status != WL_CONNECTED) { 28 | Serial.print("Attempting to connect to SSID: "); 29 | Serial.println(ssid); 30 | status = WiFi.begin(ssid, password); 31 | 32 | // Wait 10 seconds for connection: 33 | delay(10000); 34 | } 35 | Serial.println("WiFi connected"); 36 | 37 | Serial.println(""); 38 | Serial.println("WiFi connected"); 39 | Serial.println("IP address: "); 40 | Serial.println(WiFi.localIP()); 41 | } 42 | 43 | 44 | 45 | void loop() { 46 | 47 | // Reading from analog sensor 48 | int sensorValue = analogRead(sensorPin); 49 | float lightLevel = sensorValue/1024.*100; 50 | 51 | if (lightLevel > light_threshold_high) { 52 | 53 | // Use WiFiClient class to create TCP connections 54 | WiFiClient client; 55 | const int httpPort = 80; 56 | if (!client.connect(host, httpPort)) { 57 | Serial.println("connection failed"); 58 | return; 59 | } 60 | 61 | // We now create a URI for the request 62 | String url = "/trigger/light_high"; 63 | url += "/with/key/"; 64 | url += key; 65 | 66 | Serial.print("Requesting URL: "); 67 | Serial.println(url); 68 | 69 | // This will send the request to the server 70 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 71 | "Host: " + host + "\r\n" + 72 | "Connection: close\r\n\r\n"); 73 | unsigned long timeout = millis(); 74 | while (client.available() == 0) { 75 | if (millis() - timeout > 5000) { 76 | Serial.println(">>> Client Timeout !"); 77 | client.stop(); 78 | return; 79 | } 80 | } 81 | 82 | // Read all the lines of the reply from server and print them to Serial 83 | while(client.available()){ 84 | String line = client.readStringUntil('\r'); 85 | Serial.print(line); 86 | } 87 | 88 | Serial.println(); 89 | Serial.println("closing connection"); 90 | 91 | } 92 | 93 | if (lightLevel < light_threshold_low) { 94 | 95 | // Use WiFiClient class to create TCP connections 96 | WiFiClient client; 97 | const int httpPort = 80; 98 | if (!client.connect(host, httpPort)) { 99 | Serial.println("connection failed"); 100 | return; 101 | } 102 | 103 | // We now create a URI for the request 104 | String url = "/trigger/light_low"; 105 | url += "/with/key/"; 106 | url += key; 107 | 108 | Serial.print("Requesting URL: "); 109 | Serial.println(url); 110 | 111 | // This will send the request to the server 112 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 113 | "Host: " + host + "\r\n" + 114 | "Connection: close\r\n\r\n"); 115 | unsigned long timeout = millis(); 116 | while (client.available() == 0) { 117 | if (millis() - timeout > 5000) { 118 | Serial.println(">>> Client Timeout !"); 119 | client.stop(); 120 | return; 121 | } 122 | } 123 | 124 | // Read all the lines of the reply from server and print them to Serial 125 | while(client.available()){ 126 | String line = client.readStringUntil('\r'); 127 | Serial.print(line); 128 | } 129 | 130 | Serial.println(); 131 | Serial.println("closing connection"); 132 | 133 | } 134 | 135 | } 136 | 137 | -------------------------------------------------------------------------------- /chapter4/automated_sprinkler/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcoschwartz/iot-arduino-cookbook/e120c3a32a2765331638cf15a1ad0e78a5d7b73b/chapter4/automated_sprinkler/.DS_Store -------------------------------------------------------------------------------- /chapter4/automated_sprinkler/sensor_test/sensor_test.ino: -------------------------------------------------------------------------------- 1 | // Library 2 | #include 3 | 4 | // Specify data and clock connections and instantiate SHT1x object 5 | #define dataPin 6 6 | #define clockPin 7 7 | SHT1x sht1x(dataPin, clockPin); 8 | 9 | void setup() 10 | { 11 | Serial.begin(115200); // Open serial connection to report values to host 12 | Serial.println("Starting up"); 13 | } 14 | 15 | void loop() 16 | { 17 | float temp_c; 18 | float temp_f; 19 | float humidity; 20 | 21 | // Read values from the sensor 22 | temp_c = sht1x.readTemperatureC(); 23 | temp_f = sht1x.readTemperatureF(); 24 | humidity = sht1x.readHumidity(); 25 | 26 | // Print the values to the serial port 27 | Serial.print("Temperature: "); 28 | Serial.print(temp_c, DEC); 29 | Serial.print("C / "); 30 | Serial.print(temp_f, DEC); 31 | Serial.print("F. Humidity: "); 32 | Serial.print(humidity); 33 | Serial.println("%"); 34 | 35 | delay(2000); 36 | } 37 | -------------------------------------------------------------------------------- /chapter4/automated_sprinkler/sprinkler_relay/sprinkler_relay.ino: -------------------------------------------------------------------------------- 1 | // Import required libraries 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | // Status 8 | int status = WL_IDLE_STATUS; 9 | 10 | // WiFi parameters 11 | const char* ssid = "wifi-name"; 12 | const char* password = "wifi-pass"; 13 | 14 | // Clients 15 | WiFiClient wifiClient; 16 | PubSubClient client(wifiClient); 17 | 18 | // Create aREST instance 19 | aREST rest = aREST(client); 20 | 21 | // Function 22 | void callback(char* topic, byte* payload, unsigned int length); 23 | 24 | void setup(void) 25 | { 26 | // Start Serial 27 | Serial.begin(115200); 28 | 29 | // Set callback 30 | client.setCallback(callback); 31 | 32 | // Give name and ID to device 33 | rest.set_id("305eyf"); 34 | rest.set_name("alarm_led"); 35 | 36 | // Connect to WiFi 37 | while (status != WL_CONNECTED) { 38 | Serial.print("Attempting to connect to SSID: "); 39 | Serial.println(ssid); 40 | status = WiFi.begin(ssid, password); 41 | 42 | // Wait 10 seconds for connection: 43 | delay(10000); 44 | } 45 | Serial.println("WiFi connected"); 46 | 47 | // Pin 6 as output 48 | pinMode(6, OUTPUT); 49 | 50 | } 51 | 52 | void loop() { 53 | 54 | // Connect to the cloud 55 | rest.handle(client); 56 | 57 | } 58 | 59 | // Handles message arrived on subscribed topic(s) 60 | void callback(char* topic, byte* payload, unsigned int length) { 61 | 62 | // Handle 63 | rest.handle_callback(client, topic, payload, length); 64 | 65 | } 66 | -------------------------------------------------------------------------------- /chapter4/automated_sprinkler/sprinkler_sensor/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcoschwartz/iot-arduino-cookbook/e120c3a32a2765331638cf15a1ad0e78a5d7b73b/chapter4/automated_sprinkler/sprinkler_sensor/.DS_Store -------------------------------------------------------------------------------- /chapter4/automated_sprinkler/sprinkler_sensor/sprinkler_sensor.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include 3 | #include 4 | #include 5 | 6 | // Specify data and clock connections and instantiate SHT1x object 7 | #define dataPin 6 8 | #define clockPin 7 9 | SHT1x sht1x(dataPin, clockPin); 10 | 11 | // Status 12 | int status = WL_IDLE_STATUS; 13 | 14 | // Credentials 15 | const char* ssid = "wifi-name"; 16 | const char* password = "wifi-pass"; 17 | 18 | // IFTTT settings 19 | const char* host = "maker.ifttt.com"; 20 | const char* key = "dPMHywdahaSxQZlCaoqnzHxcQ8vNYsTlk-42gSLAFQP"; 21 | 22 | // Thresholds 23 | float humidity_threshold_low = 20; 24 | float humidity_threshold_high = 40; 25 | 26 | void setup() { 27 | 28 | // Seroa; 29 | Serial.begin(115200); 30 | delay(10); 31 | 32 | // Connect to WiFi 33 | while (status != WL_CONNECTED) { 34 | Serial.print("Attempting to connect to SSID: "); 35 | Serial.println(ssid); 36 | status = WiFi.begin(ssid, password); 37 | 38 | // Wait 10 seconds for connection: 39 | delay(10000); 40 | } 41 | Serial.println("WiFi connected"); 42 | 43 | Serial.println(""); 44 | Serial.println("WiFi connected"); 45 | Serial.println("IP address: "); 46 | Serial.println(WiFi.localIP()); 47 | } 48 | 49 | 50 | 51 | void loop() { 52 | 53 | // Read data from sensor 54 | float temp_c; 55 | float temp_f; 56 | float humidity; 57 | 58 | // Read values from the sensor 59 | temp_c = sht1x.readTemperatureC(); 60 | temp_f = sht1x.readTemperatureF(); 61 | humidity = sht1x.readHumidity(); 62 | 63 | if (humidity > humidity_threshold_high) { 64 | 65 | // Use WiFiClient class to create TCP connections 66 | WiFiClient client; 67 | const int httpPort = 80; 68 | if (!client.connect(host, httpPort)) { 69 | Serial.println("connection failed"); 70 | return; 71 | } 72 | 73 | // We now create a URI for the request 74 | String url = "/trigger/humidity_high"; 75 | url += "/with/key/"; 76 | url += key; 77 | 78 | Serial.print("Requesting URL: "); 79 | Serial.println(url); 80 | 81 | // This will send the request to the server 82 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 83 | "Host: " + host + "\r\n" + 84 | "Connection: close\r\n\r\n"); 85 | unsigned long timeout = millis(); 86 | while (client.available() == 0) { 87 | if (millis() - timeout > 5000) { 88 | Serial.println(">>> Client Timeout !"); 89 | client.stop(); 90 | return; 91 | } 92 | } 93 | 94 | // Read all the lines of the reply from server and print them to Serial 95 | while(client.available()){ 96 | String line = client.readStringUntil('\r'); 97 | Serial.print(line); 98 | } 99 | 100 | Serial.println(); 101 | Serial.println("closing connection"); 102 | 103 | } 104 | 105 | if (humidity < humidity_threshold_low) { 106 | 107 | // Use WiFiClient class to create TCP connections 108 | WiFiClient client; 109 | const int httpPort = 80; 110 | if (!client.connect(host, httpPort)) { 111 | Serial.println("connection failed"); 112 | return; 113 | } 114 | 115 | // We now create a URI for the request 116 | String url = "/trigger/humidity_low"; 117 | url += "/with/key/"; 118 | url += key; 119 | 120 | Serial.print("Requesting URL: "); 121 | Serial.println(url); 122 | 123 | // This will send the request to the server 124 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 125 | "Host: " + host + "\r\n" + 126 | "Connection: close\r\n\r\n"); 127 | unsigned long timeout = millis(); 128 | while (client.available() == 0) { 129 | if (millis() - timeout > 5000) { 130 | Serial.println(">>> Client Timeout !"); 131 | client.stop(); 132 | return; 133 | } 134 | } 135 | 136 | // Read all the lines of the reply from server and print them to Serial 137 | while(client.available()){ 138 | String line = client.readStringUntil('\r'); 139 | Serial.print(line); 140 | } 141 | 142 | Serial.println(); 143 | Serial.println("closing connection"); 144 | 145 | } 146 | 147 | } 148 | 149 | -------------------------------------------------------------------------------- /chapter4/cloud_m2m/cloud_m2m_button/cloud_m2m_button.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include 3 | #include 4 | 5 | // Status 6 | int status = WL_IDLE_STATUS; 7 | 8 | // Credentials 9 | const char* ssid = "wifi-name"; 10 | const char* password = "wifi-pass"; 11 | 12 | // IFTTT settings 13 | const char* host = "maker.ifttt.com"; 14 | const char* eventName = "toggle"; 15 | const char* key = "key"; 16 | 17 | void setup() { 18 | 19 | // Seroa; 20 | Serial.begin(115200); 21 | delay(10); 22 | 23 | // Connect to WiFi 24 | while (status != WL_CONNECTED) { 25 | Serial.print("Attempting to connect to SSID: "); 26 | Serial.println(ssid); 27 | status = WiFi.begin(ssid, password); 28 | 29 | // Wait 10 seconds for connection: 30 | delay(10000); 31 | } 32 | Serial.println("WiFi connected"); 33 | 34 | Serial.println(""); 35 | Serial.println("WiFi connected"); 36 | Serial.println("IP address: "); 37 | Serial.println(WiFi.localIP()); 38 | } 39 | 40 | 41 | 42 | void loop() { 43 | 44 | if (digitalRead(6)) { 45 | 46 | // Use WiFiClient class to create TCP connections 47 | WiFiClient client; 48 | const int httpPort = 80; 49 | if (!client.connect(host, httpPort)) { 50 | Serial.println("connection failed"); 51 | return; 52 | } 53 | 54 | // We now create a URI for the request 55 | String url = "/trigger/"; 56 | url += eventName; 57 | url += "/with/key/"; 58 | url += key; 59 | 60 | Serial.print("Requesting URL: "); 61 | Serial.println(url); 62 | 63 | // This will send the request to the server 64 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 65 | "Host: " + host + "\r\n" + 66 | "Connection: close\r\n\r\n"); 67 | unsigned long timeout = millis(); 68 | while (client.available() == 0) { 69 | if (millis() - timeout > 5000) { 70 | Serial.println(">>> Client Timeout !"); 71 | client.stop(); 72 | return; 73 | } 74 | } 75 | 76 | // Read all the lines of the reply from server and print them to Serial 77 | while(client.available()){ 78 | String line = client.readStringUntil('\r'); 79 | Serial.print(line); 80 | } 81 | 82 | Serial.println(); 83 | Serial.println("closing connection"); 84 | 85 | } 86 | 87 | } 88 | 89 | -------------------------------------------------------------------------------- /chapter4/cloud_m2m/cloud_m2m_led/cloud_m2m_led.ino: -------------------------------------------------------------------------------- 1 | // Import required libraries 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | // Status 8 | int status = WL_IDLE_STATUS; 9 | 10 | // WiFi parameters 11 | const char* ssid = "wifi-name"; 12 | const char* password = "wifi-pass"; 13 | 14 | // Clients 15 | WiFiClient wifiClient; 16 | PubSubClient client(wifiClient); 17 | 18 | // Create aREST instance 19 | aREST rest = aREST(client); 20 | 21 | // Variables to be exposed to the API 22 | bool ledState = false; 23 | 24 | // Function 25 | int ledControl(String command); 26 | void callback(char* topic, byte* payload, unsigned int length); 27 | 28 | void setup(void) 29 | { 30 | // Start Serial 31 | Serial.begin(115200); 32 | 33 | // Set callback 34 | client.setCallback(callback); 35 | 36 | // Function to be exposed 37 | rest.function("toggle", ledToggle); 38 | 39 | // Give name and ID to device 40 | rest.set_id("305eyf"); 41 | rest.set_name("mkr_led"); 42 | 43 | // Connect to WiFi 44 | while (status != WL_CONNECTED) { 45 | Serial.print("Attempting to connect to SSID: "); 46 | Serial.println(ssid); 47 | status = WiFi.begin(ssid, password); 48 | 49 | // Wait 10 seconds for connection: 50 | delay(10000); 51 | } 52 | Serial.println("WiFi connected"); 53 | 54 | // Pin 6 as output 55 | pinMode(6, OUTPUT); 56 | 57 | } 58 | 59 | void loop() { 60 | 61 | // Connect to the cloud 62 | rest.handle(client); 63 | 64 | } 65 | 66 | // Custom function accessible by the API 67 | int ledToggle(String command) { 68 | 69 | ledState = !ledState; 70 | 71 | digitalWrite(6, ledState); 72 | return 1; 73 | } 74 | 75 | // Handles message arrived on subscribed topic(s) 76 | void callback(char* topic, byte* payload, unsigned int length) { 77 | 78 | // Handle 79 | rest.handle_callback(client, topic, payload, length); 80 | 81 | } 82 | -------------------------------------------------------------------------------- /chapter4/local_m2m/local_m2m_button/local_m2m_button.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include 3 | #include 4 | 5 | // Status 6 | int status = WL_IDLE_STATUS; 7 | 8 | // Credentials 9 | const char* ssid = "wifi-name"; 10 | const char* password = "wifi-pass"; 11 | 12 | // Target 13 | const char* host = "192.168.0.108"; 14 | 15 | void setup() { 16 | 17 | // Seroa; 18 | Serial.begin(115200); 19 | delay(10); 20 | 21 | // Connect to WiFi 22 | while (status != WL_CONNECTED) { 23 | Serial.print("Attempting to connect to SSID: "); 24 | Serial.println(ssid); 25 | status = WiFi.begin(ssid, password); 26 | 27 | // Wait 10 seconds for connection: 28 | delay(10000); 29 | } 30 | Serial.println("WiFi connected"); 31 | 32 | // Print the IP address 33 | IPAddress ip = WiFi.localIP(); 34 | Serial.print("IP Address: "); 35 | Serial.println(ip); 36 | } 37 | 38 | 39 | 40 | void loop() { 41 | 42 | if (digitalRead(6)) { 43 | 44 | // Use WiFiClient class to create TCP connections 45 | WiFiClient client; 46 | const int httpPort = 80; 47 | if (!client.connect(host, httpPort)) { 48 | Serial.println("connection failed"); 49 | return; 50 | } 51 | 52 | // We now create a URI for the request 53 | String url = "/toggle"; 54 | 55 | Serial.print("Requesting URL: "); 56 | Serial.println(url); 57 | 58 | // This will send the request to the server 59 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 60 | "Host: " + host + "\r\n" + 61 | "Connection: close\r\n\r\n"); 62 | unsigned long timeout = millis(); 63 | while (client.available() == 0) { 64 | if (millis() - timeout > 5000) { 65 | Serial.println(">>> Client Timeout !"); 66 | client.stop(); 67 | return; 68 | } 69 | } 70 | 71 | // Read all the lines of the reply from server and print them to Serial 72 | while(client.available()){ 73 | String line = client.readStringUntil('\r'); 74 | Serial.print(line); 75 | } 76 | 77 | Serial.println(); 78 | Serial.println("closing connection"); 79 | 80 | } 81 | 82 | } 83 | 84 | -------------------------------------------------------------------------------- /chapter4/local_m2m/local_m2m_led/local_m2m_led.ino: -------------------------------------------------------------------------------- 1 | // Import required libraries 2 | #include 3 | #include 4 | #include 5 | 6 | // Status 7 | int status = WL_IDLE_STATUS; 8 | 9 | // Create aREST instance 10 | aREST rest = aREST(); 11 | 12 | // WiFi parameters 13 | const char* ssid = "wifi-name"; 14 | const char* password = "wifi-pass"; 15 | 16 | // The port to listen for incoming TCP connections 17 | #define LISTEN_PORT 80 18 | 19 | // Create an instance of the server 20 | WiFiServer server(LISTEN_PORT); 21 | 22 | // Variables to be exposed to the API 23 | bool ledState = false; 24 | 25 | // Function 26 | int ledControl(String command); 27 | 28 | void setup(void) 29 | { 30 | // Start Serial 31 | Serial.begin(115200); 32 | 33 | // Function to be exposed 34 | rest.function("toggle", ledToggle); 35 | 36 | // Give name and ID to device 37 | rest.set_id("1"); 38 | rest.set_name("mkr_led"); 39 | 40 | // Connect to WiFi 41 | while (status != WL_CONNECTED) { 42 | Serial.print("Attempting to connect to SSID: "); 43 | Serial.println(ssid); 44 | status = WiFi.begin(ssid, password); 45 | 46 | // Wait 10 seconds for connection: 47 | delay(10000); 48 | } 49 | Serial.println("WiFi connected"); 50 | 51 | // Start the server 52 | server.begin(); 53 | Serial.println("Server started"); 54 | 55 | // Print the IP address 56 | IPAddress ip = WiFi.localIP(); 57 | Serial.print("IP Address: "); 58 | Serial.println(ip); 59 | 60 | // Pin 6 as output 61 | pinMode(6, OUTPUT); 62 | 63 | } 64 | 65 | void loop() { 66 | 67 | // Handle REST calls 68 | WiFiClient client = server.available(); 69 | if (!client) { 70 | return; 71 | } 72 | while(!client.available()){ 73 | delay(1); 74 | } 75 | rest.handle(client); 76 | 77 | } 78 | 79 | // Custom function accessible by the API 80 | int ledToggle(String command) { 81 | 82 | ledState = !ledState; 83 | 84 | digitalWrite(6, ledState); 85 | return 1; 86 | } 87 | -------------------------------------------------------------------------------- /chapter4/m2m_alarm/alarm_led/alarm_led.ino: -------------------------------------------------------------------------------- 1 | // Import required libraries 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | // Status 8 | int status = WL_IDLE_STATUS; 9 | 10 | // WiFi parameters 11 | const char* ssid = "wifi-name"; 12 | const char* password = "wifi-pass"; 13 | 14 | // Clients 15 | WiFiClient wifiClient; 16 | PubSubClient client(wifiClient); 17 | 18 | // Create aREST instance 19 | aREST rest = aREST(client); 20 | 21 | // Variables 22 | bool alarmState = false; 23 | bool ledState = false; 24 | 25 | // Function 26 | int activateAlarm(String command); 27 | void callback(char* topic, byte* payload, unsigned int length); 28 | 29 | void setup(void) 30 | { 31 | // Start Serial 32 | Serial.begin(115200); 33 | 34 | // Set callback 35 | client.setCallback(callback); 36 | 37 | // Function to be exposed 38 | rest.function("alarm", activateAlarm); 39 | 40 | // Give name and ID to device 41 | rest.set_id("305eyf"); 42 | rest.set_name("alarm_led"); 43 | 44 | // Connect to WiFi 45 | while (status != WL_CONNECTED) { 46 | Serial.print("Attempting to connect to SSID: "); 47 | Serial.println(ssid); 48 | status = WiFi.begin(ssid, password); 49 | 50 | // Wait 10 seconds for connection: 51 | delay(10000); 52 | } 53 | Serial.println("WiFi connected"); 54 | 55 | // Pin 6 as output 56 | pinMode(6, OUTPUT); 57 | pinMode(7, OUTPUT); 58 | 59 | } 60 | 61 | void loop() { 62 | 63 | // Connect to the cloud 64 | rest.handle(client); 65 | 66 | } 67 | 68 | // Custom function accessible by the API 69 | int activateAlarm(String command) { 70 | 71 | tone(7, 500); 72 | digitalWrite(6, HIGH); 73 | 74 | return 1; 75 | } 76 | 77 | // Handles message arrived on subscribed topic(s) 78 | void callback(char* topic, byte* payload, unsigned int length) { 79 | 80 | // Handle 81 | rest.handle_callback(client, topic, payload, length); 82 | 83 | } 84 | -------------------------------------------------------------------------------- /chapter4/m2m_alarm/alarm_sensor/alarm_sensor.ino: -------------------------------------------------------------------------------- 1 | // Libraries 2 | #include 3 | #include 4 | 5 | // Status 6 | int status = WL_IDLE_STATUS; 7 | 8 | // Credentials 9 | const char* ssid = "wifi-name"; 10 | const char* password = "wifi-pass"; 11 | 12 | // IFTTT settings 13 | const char* host = "maker.ifttt.com"; 14 | const char* eventName = "alarm"; 15 | const char* key = "key"; 16 | 17 | void setup() { 18 | 19 | // Seroa; 20 | Serial.begin(115200); 21 | delay(10); 22 | 23 | // Connect to WiFi 24 | while (status != WL_CONNECTED) { 25 | Serial.print("Attempting to connect to SSID: "); 26 | Serial.println(ssid); 27 | status = WiFi.begin(ssid, password); 28 | 29 | // Wait 10 seconds for connection: 30 | delay(10000); 31 | } 32 | Serial.println("WiFi connected"); 33 | 34 | Serial.println(""); 35 | Serial.println("WiFi connected"); 36 | Serial.println("IP address: "); 37 | Serial.println(WiFi.localIP()); 38 | } 39 | 40 | 41 | 42 | void loop() { 43 | 44 | if (digitalRead(6)) { 45 | 46 | // Use WiFiClient class to create TCP connections 47 | WiFiClient client; 48 | const int httpPort = 80; 49 | if (!client.connect(host, httpPort)) { 50 | Serial.println("connection failed"); 51 | return; 52 | } 53 | 54 | // We now create a URI for the request 55 | String url = "/trigger/"; 56 | url += eventName; 57 | url += "/with/key/"; 58 | url += key; 59 | 60 | Serial.print("Requesting URL: "); 61 | Serial.println(url); 62 | 63 | // This will send the request to the server 64 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 65 | "Host: " + host + "\r\n" + 66 | "Connection: close\r\n\r\n"); 67 | unsigned long timeout = millis(); 68 | while (client.available() == 0) { 69 | if (millis() - timeout > 5000) { 70 | Serial.println(">>> Client Timeout !"); 71 | client.stop(); 72 | return; 73 | } 74 | } 75 | 76 | // Read all the lines of the reply from server and print them to Serial 77 | while(client.available()){ 78 | String line = client.readStringUntil('\r'); 79 | Serial.print(line); 80 | } 81 | 82 | Serial.println(); 83 | Serial.println("closing connection"); 84 | 85 | } 86 | 87 | } 88 | 89 | --------------------------------------------------------------------------------