Wednesday, June 28, 2017

IFTTT notification with Temperature and humidity value_DTH11

Switch works as a trigger to send data. it could be motion sensor, vibration sensor, moisture sensor, etc. we can send three values using IFTTT trigger, name as Value1, Value2, Value3.

Ingredient you need during this tutorial,
1) NodeMCU integration with Arduino IDE
2) Basic hardware
a.    NodeMCU
b.    Breadboard
c.    Switch
d.    10K resistor x2
e.    Jumper wires.
f.     DTH 11 sensor (Or module)
g.     Internet connection 😉
3) Thinger.io – open source IOT platform  
4) IFTTT 
IFTTT is free platform that helps you do more interaction with all your apps and devices. To accomplish multiple task, please sign up in a free service called IFTTT, which stands for “If This Then That”
You can cross connect your IOT device with notification, Email, tweet, Facebook, etc…

To start with IFTTT,
Create a free IFTTT account.
Go the official site: https://ifttt.com/ and click the “Sign Up” button in top of the page.



Once you "Sign-up", you see…



In the top raw, select the "My applets" or create one.

Click on "this" to make trigger activity.
In our case we want to use a NodeMCU with button to make a trigger.

Select "Maker webhooks" It will allow you to make the IOT hardware integration.



I created an event name "Temp_sensor" (you need in Arduino code) written in the box. Do remember this name you will need this in future for the testing.


So It will show you if button pressed (On NodeMCU)  then what…!!!



It has multiple action services, which will allow you to connect through possible services with simple selection. In our-case we are sending notification in this tutorial. (You need to download an app for notification service)

In this Notification dialog, please add the message you want to see. Also please add ingredient call "value1", "value2",
"Value 1" stands for the temperature, and "Value 2" stands for the Humidity in our case.



Compete with the action by a click "create action".



So once it is finished, Check by clicking a "check now" button, It should not give you any error during this.

Now, Go back to search on the top, write "Maker Webhooks", It will show you below options.

Now go in to "setting", and copy a "URL" and past in the internet browser. It will show you your active key with event box.



Write an "event name" in the box Temp_sensor in our case and click on the test button at bottom.
This will work as a trigger in absents of a trigger hardware and gives you an indication of system working. (you should receive a notification by doing this)

Now IFTTT will not get the value directly from the Hardware, So we required one more interface call thinger.io, which will support us to transfer DTH value to IFTTT. (It works as inter-mediator)

To configure thinger.io interface. Go to https://thinger.io/ and register yourself.



Once you sign in, you see.


You need to add device and endpoints to make this work with nodeMCU.
click on the Device and add Device ID (Required in Arduino Code) and Device Credentials could be any name (Required in Arduino Code).

Once you click “Add device” it will add this device in the thinger.io platform. Now go to endpoints and add required details as below.



Select IFTTT maker channel trigger and add IFTTT details like event name and IFTTT key.



So Internet configuration over, Now let finish hardware connection and Arduino Coding.

Hardware design:




Connect NodeMCU to the breadboard with switch and resistor as shown above.
Switch: Pin number -D2
DTH11 sensor : Pin number D1

Breadboard connection:



Software:

Before you start, make sure your Arduino IDE has installed ESP8266 board with thinger.io library.

You can download thinger.io library from:
https://github.com/thinger-io/Arduino-Library

Arduino Code 1:
It will send you a notification with Temperature and Humidity data when you press a button.
------------------------------------------------------------------------------------------------------------------------------------------------------
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>
#include "DHT.h"

#define USERNAME "User name" // Your login name from thiner.io
#define DEVICE_ID "NodeMCU_DTH11" // Your device ID from thinger.io
#define DEVICE_CREDENTIAL "lorem_ipsum" // your crefential from thinger.io

#define SSID "WIFI network"
#define SSID_PASSWORD "WIFI password"

int pin = 4;

#define DHTPIN D1
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void PINStatus() {
  digitalWrite(BUILTIN_LED, LOW);
  pson DTH_value;
  DTH_value["value1"] = dht.readTemperature();
  DTH_value["value2"] = dht.readHumidity();
  thing.call_endpoint("DTH_post", DTH_value);
  digitalWrite(BUILTIN_LED, HIGH); /// it will be on untill data gets on the thinger.io site
            
}


void setup() {
   
 dht.begin();
 pinMode(pin, OUTPUT);
 attachInterrupt(digitalPinToInterrupt(pin), PINStatus, FALLING);

 pinMode(BUILTIN_LED, OUTPUT);
 digitalWrite(BUILTIN_LED, HIGH);
 
 thing.add_wifi(SSID, SSID_PASSWORD);

  // resource for reading sensor temperature from API
 thing["dht11"] >> [](pson& out){
 out["humidity"] = dht.readHumidity();
 out["celsius"] = dht.readTemperature();
 out["fahrenheit"] = dht.readTemperature(true);
 };
}

void loop() { 
  thing.handle();
}

------------------------------------------------------------------------------------------------------------------------------------------------------ 

Arduino Code 2:

It will give you a notification at every hour with temperature and humidity data, also works when you press a button.

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>
#include "DHT.h"

#define USERNAME "User name" // Your login name from thiner.io
#define DEVICE_ID "NodeMCU_DTH11" // Your device ID from thinger.io
#define DEVICE_CREDENTIAL "lorem_ipsum" // your crefential from thinger.io

#define SSID "WIFI network"
#define SSID_PASSWORD "WIFI password"

int pin = 4;

#define DHTPIN D1
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

unsigned long previousMillis = 0;      // to know when to post the temperature
unsigned int minutes_interval = 60;              // variable in minutes for posting the temperature

void PINStatus() { 
  digitalWrite(BUILTIN_LED, LOW);
  pson DTH_value;
  DTH_value["value1"] = dht.readTemperature();
  DTH_value["value2"] = dht.readHumidity();
  thing.call_endpoint("DTH_post", DTH_value);
  digitalWrite(BUILTIN_LED, HIGH);
             
}


void setup() {
    
 dht.begin();
 pinMode(pin, OUTPUT);
 attachInterrupt(digitalPinToInterrupt(pin), PINStatus, FALLING);

 pinMode(BUILTIN_LED, OUTPUT);
 digitalWrite(BUILTIN_LED, HIGH);
  
 thing.add_wifi(SSID, SSID_PASSWORD);

  // resource for reading sensor temperature from API
 thing["dht11"] >> [](pson& out){
 out["humidity"] = dht.readHumidity();
 out["celsius"] = dht.readTemperature();
 out["fahrenheit"] = dht.readTemperature(true);
 }; 
}

void loop() {  
  thing.handle();
  unsigned long currentMillis = millis();
  if(minutes_interval>0 && currentMillis - previousMillis >= minutes_interval * 60000) 
  {
    previousMillis = currentMillis;   
    PINStatus();
  }
  
}

-----------------------------------------------------------------------------------------------------------------------------------------------------


Please make sure you customize the highlighted area in CODE

So when ever you press a button, it will send an notification with temperature and humidity value or every hour

Execution video:






Tuesday, June 27, 2017

IFTTT button with NodeMCU

In this tutorial, we are going to monitor the status of a switch using an ESP8266-12E. The goal of this project is to show the endless possibilities that this small module and integration with IFTTT.

Ingredient you need during this tutorial,
1) NodeMCU integration with Arduino IDE
2) Basic hardware
a.    NodeMCU from Amazon
b.    Breadboard from Amazon
c.    Switch
d.    10K resistor
e.    Jumper wires.
f.     Internet connection 😉

3) Basic of IFTTT

Lets start with IFTTT, 

IFTTT is a free platform that helps you do more interaction with all your apps and devices. In order to accomplish multiple tasks you have to sign up for one free service, which stands for “If This Then That”
You can interconnect your IOT device with notification, Email, tweet, Facebook, etc…

To start with IFTTT,
Create a free IFTTT account.
Go to official site: https://ifttt.com/ and click the “Sign Up” button in a top of the page.




Once you signup…



 In the top raw, select My applets or create one.


Click on this to make trigger activity.
In our case, we want to use an ESP-8266 with button to make a trigger.


Select maker webhooks, It will allow you to make the IOT hardware integration.


I created an event name SOS button_ written in the box. Do remember this name, you will need it in a future for testing. (in Arduino software)


So It will show you if a button pressed then what…!!!


It has multiple action services, which will allow you to connect through possible services with simple selection. So In our-case we would like to send an Email (It could be a notification or anything else) 

Compete this action with click and create an action.

So once it is finished, Check by clicking a check now, It should not give you any error during this.

Now, Go back to search on the top, Write Maker Webhooks, It will show you below options.


Now go to setting, and copy a URL and paste to internet browser. 

It will show you your active key with event box.



Write an event name in the box, "SOS_button" in our case and click on the test button in the bottom.
This will work as a trigger in absents of a trigger hardware and gives you an indication of system working.

Congratulation..!!! 
You finished your first step to IOT world, with basic IFTTT service. Now let’s finish on the hardware and coding part.

Hardware design/Connection:


Connect NodeMCU to the breadboard with switch and resistor as shown above. I considered Pin number D4 as an input, but this is not a same number in the Arduino IDE.

Node MCU pin number mapping with Arduino IDE,



So according to above image, My hardware pin number D4 is pin number 2 in the Arduino IDE.

Software:

Before you start, make sure your Arduino IDE has installed ESP8266 board with a basic library.
Adafruit tutorial will help you to setup NodeMCU board with Arduino IDE. 
Link: https://learn.adafruit.com/adafruit-io-basics-esp8266-arduino/overview

Arduino Code:
------------------------------------------------------------------------------------------------------------------------------------------------------

#include <ESP8266WiFi.h>

const char* ssid = "WIFI network";
const char* password = "WIFI password";
const char* host = "maker.ifttt.com";
const char* apiKey = "Your API key from the IFTTT";

int pin = 2;

volatile int flag = false;
const char* PIN_state = "not pressed";

void PINStatus() {
            PIN_state = "SOS pressed";  
             flag = true;
}


void setup() {
    Serial.begin(115200);

    Serial.println("SOS button integration with IFTTT");
    delay(100);
    pinMode(pin, OUTPUT);
    attachInterrupt(digitalPinToInterrupt(pin), PINStatus, FALLING);

    Serial.print("Connecting to ");
    Serial.println(ssid);
    
    WiFi.begin(ssid, password);
    
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
  
    Serial.println("");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());  
}

void loop() {  

          if(flag){
          Serial.print("connecting to ");
          Serial.println(host);
          
          WiFiClient client;
          const int httpPort = 80;
          if (!client.connect(host, httpPort)) {
            Serial.println("connection failed");
            return;
          }
    
          String url = "/trigger/SOS_Button/with/key/";
          url += apiKey;
          
          Serial.print("Requesting URL: ");
          Serial.println(url);
          client.print(String("POST ") + url + " HTTP/1.1\r\n" +
                       "Host: " + host + "\r\n" + 
                       "Content-Type: application/x-www-form-urlencoded\r\n" + 
                       "Content-Length: 13\r\n\r\n" +
                       "value1=" + PIN_state + "\r\n");
          flag = false;
      }  
      delay(100);
      
}
------------------------------------------------------------------------------------------------------------------------------------------------------
Please make sure you customize the highlighted area in CODE

So when ever you press a button, it will send an Email to registered mail ID.

Execution video: