Introduction: IOT - Motion Detector With Email Notification W/ NodeMCU, PIR, Carriots, WIFI, PCB,Casing and More.

Welcome to the IOT device 2 this becomes a part of the building security use case. This use case is valued at $210 billion by 2020. Enough for all players small and big.

Motion Detector :

This instructable will help you to use NodeMCU (ESP8266 Dev Kit), PIR Motion sensor, Carriots to design, build, code using the Arduino IDE, produce and install Smart Motion detector for commercial and Home use cases.

The theme it revolves around is Security of a building or Home or a restricted area. This IOT device picks up a couple of notches with casing and the complexity of the code.

Product Functionality:

The Device will detect any motion within a specific range using a PIR sensor and will do two things

1) Turn on a device using a relay in our example it is a light for about 30 sec.

2) Simultaneously send a email to the user, using the IOT - Carriots Platform over WIFI.

Improvisations:

The relay can be connected to any device light, alarm, camera, security system ...

The triggering even can be SMS, Calling authorities , calling another services ...

Complete Guide to Build IOT Things from Scratch to Market is here !

I have built an enabler course for you to build IOT products from scratch and take it to market, I will take you through Arduino, NodeMCU, Nano, ESP8266, Sensors, Displays, Shields, Keypads, Software, Coding, Designing, Talking to internet, Protocols, IOT platforms, Casing, Manufacturing, Raising Capital, Selling, Coaching and more. We will learn all this and make 3 Products in the course from different verticals, ya you will get to see the entire creating process of a product from design to casing.

***** For a limited time, you can take this course just for $12, that is 96% OFF, it is practically for free guys !! ****

https://www.udemy.com/complete-guide-to-build-iot-things-from-scratch-to-market/?couponCode=TAKEOFF96

Step 1: Hardware Required:

Parts Needed

NodeMcu Lua WIFI Internet of Things development board based on ESP8266-- 1 ( MCU and WIFI capability)

http://www.seeedstudio.com/depot/NodeMCU-v2-Lua-based-ESP8266-development-kit-p-2415.html

Motion detector shield -- 1

https://www.sparkfun.com/products/13285

1 relay module with opto isolation supports high and low trigger 5V--- 1

http://www.aliexpress.com/store/product/1-relay-module-with-opto-isolation-supports-high-and-low-trigger-one-relay-expansion-board-5V/237733_32294986881.html

12V1A \ 5V1A fully isolated switching power supply / AC-DC modules / 220V switch 12v 5v lose-lose (C4B1) -- 1

http://www.aliexpress.com/store/product/2PCS-LOT-12V1A-5V-1A-fully-isolated-switching-power-supply-AC-DC-modules-220V-switch-12v/639111_32227692682.html

Generic PCB with bus lines ---- 1

http://www.amazon.com/Solderable-BreadBoard-matches-tie-point-breadboards/dp/B0040Z3012/ref=pd_sim_328_2?ie=UTF8&dpID=61oLmBfSgYL&dpSrc=sims&preST=_AC_UL160_SR160%2C160_&refRID=1TRKGBX4YJ9BD19Z3KWE

Casing -- 1

http://www.budind.com/view/NEMA+Boxes/Blue+Transparent+NEMA+4X

Soldering KIT

http://www.homedepot.com/p/Weller-25-Watt-Standard-Duty-Soldering-Iron-Kit-SP25NKUS/204195328

Insulation TAPE -- 1

** For India please contact me for a course kit. It has all the parts and accessories exactly as I have used which will help you to follow the course, test your knowledge by creating products.

Step 2: Circuit Diagram:

We will use Fritzing to create the Circuit diagram

Connections:


NodeMCU(ESP8266 Dev Kit) D1 ---> INI of the Relay

NodeMCU(ESP8266 Dev Kit) D2 ---> Digital Out PIR Sensor

NodeMCU (ESP8266 Dev Kit) VCC ---> VCC (+) of the battery pack

NodeMCU (ESP8266 Dev Kit) GND ----> GND (-) of the battery pack

Relay VCC ---> VCC (+) of the battery pack

Relay GND ----> GND (-) of the battery pack

PIR Sensor VCC ---> VCC (+) of the battery pack

PIR Sensor GND ----> GND (-) of the battery pack

Run one of the cables of the (device) light through the NO(normally open) and the C ( common) terminal of the relay.

If you are using a permanent power supply, then you will need a power convertor, like I use in the actual project please check the course.

you will have to connect the power connection(220/110) to the power convertor, which will give out 5v or 10v, each will have + and - use these as your VCC and GND respectively.

Step 3: Designing Your PCB:

PCB Layout:

PCB is a technique and a product which you will have to eventually use in a mass production situation, or even if it for small numbers. It's a onetime activity and gives you the freedom to replicate your product number of times.

Step 4: Assembly and Casing

Step 5: The Code

CODE:

Well I see you have come far, you have an idea about hardware, the circuit diagram and the PCB, how to case it nicely. Let's now look at the intangible magic which makes all this work.

Here we write the code in code using the Arduino IDE to make NodeMCU work with a relay, PIR motion sensor and use IOT platform carriots over WIFI

Guys I spent a long time trouble shooting with this project. there were various issues. I am going to list some of them to help you skip burning the midnight oil.

1) Check the NodeMCU pins if they are giving the correct i/o as you are assuming, for e.g. lets say that you are taking the pin no 4 (GPIO) as an input pin. Now by default this pin should read ........ low that't right but for some reason it reads high even after initializing the pin to input. test it!! the consequences of not testing are long and not so fun.

2) Using a direct HTTP get/post method instead of using a visualizing function provided by BLYNK or Thinger.io. My deep respect to these guys for building amazing services and platforms.

My 2 cents :The reason being that the PIR output stays high for a couple of sends and you need to bring in necessary delay to avoid sending multiple emails. with some services like BLYNK this delay causes an issue. as it is required to call that function once is 6 or 7 sec. This is my experience I could be wrong.

3) Once it satisfies the condition from the if loop, call a function, instead of writing everything with the loop. This brings clarity to the code and help in trouble shooting. Again this is my feeling.

You could adjust the sensitivity of the PIR to reduce the time it stays high. something you could consider..

Have fun making this IOT device..

// written by Junaid Ahmed

// This is a free software protected by the creative commons license

// This is for as is use and there may be bugs and other issues.

#include

#include "ESP8266WiFi.h"

const char* ssid = "NETGXXXXX";

const char* password = "XXXXXXXXX";

const char* server = "api.carriots.com";

const String APIKEY = "1e2f6920a92e3ca079bf60329d7871d69b3881df88df9b2c44eXXXXXXXXXX"; // Replace with your Carriots apikey

const String DEVICE = "light@junaid.junaid"; // Replace with the id_developer of your device

int ledpin = 4;

int pirpin = 12;

int pirstate = LOW;

int val = 0;

void setup(){

Serial.begin(115200);

delay(10);

pinMode(ledpin,OUTPUT);

pinMode(pirpin,INPUT);

Serial.println("calibrating");

for(int i = 0; i < 20; i++){

Serial.print(".");

delay(1000);

}

// start 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());

}

// function to talk to the Carriot Platform

void sendStream()

{

String txt = ""; // Text to send

if ( pirstate == HIGH ) { // Alarm OFF

txt = "Motion Detected";

} else { // Alarm ON

txt = "Somethig wrong";

}

Serial.println(txt);

//Serial.println(val);// For debugging purpose only

WiFiClient client;

//const int httpPort = 80;

if (client.connect(server, 80)) { // If there's a successful connection

Serial.println(F("connected"));

// Build the data field

String json = "{\"protocol\":\"v2\",\"device\":\""+DEVICE+"\",\"at\":\"now\",\"data\":{\"light\":\""+txt+"\"}}";

// Make a HTTP request

client.println("POST /streams HTTP/1.1");

client.println("Host: api.carriots.com");

client.println("Accept: application/json");

client.println("User-Agent: Arduino-Carriots");

client.println("Content-Type: application/json");

client.print("carriots.apikey: ");

client.println(APIKEY);

client.print("Content-Length: ");

int thisLength = json.length();

client.println(thisLength);

client.println("Connection: close");

client.println();

client.println(json);

}

else {

// If you didn't get a connection to the server:

Serial.println(F("connection failed"));

}

}

void loop() {

val = digitalRead(pirpin);

Serial.println(val);

if(val == HIGH){

digitalWrite(ledpin,HIGH);

if (pirstate == LOW){

Serial.println("Motion Detected");

pirstate =HIGH;

Serial.println(F("Send Stream"));

sendStream();

delay(30000);

}

/* while(client.available()){

String line = client.readStringUntil('\r');

Serial.print(line);

delay(30000);

} */

}

else{

digitalWrite(ledpin,LOW);

if(pirstate == HIGH){

Serial.println("motion Detected email sent");

pirstate = LOW;

}

}

}

Step 6: Programming Triggers From Carriots to Send Email:

The ability to trigger an email is programmed or setup at the IOT platform for this product we are using the Carriots IOT platform. Its quite a mature platform with many features. spend some time in the platform to acquaint yourself with the functionalities and how to use them

Complete Guide to Build IOT Things from Scratch to Market is here !
I have built an enabler course for you to build IOT products from scratch and take it to market, I will take you through Arduino, NodeMCU, Nano, ESP8266, Sensors, Displays, Shields, Keypads, Software, Coding, Designing, Talking to internet, Protocols, IOT platforms, Casing, Manufacturing, Raising Capital, Selling, Coaching and more. We will learn all this and make 3 Products in the course from different verticals, ya you will get to see the entire creating process of a product from design to casing.

***** For a limited time, you can take this course just for $12, that is 96% OFF, it is practically for free guys !! ****

https://www.udemy.com/complete-guide-to-build-iot...

Step 7: Showtime - Demo and Testing:

All the hard work has paid off. You have a working IOT device to

sense motion and alert the user with an email over wifi. Pretty darn good!!!

Obviously this could be programmed to call, or a text or a alarm to the cops or whoever, the apllications a re numerous.

We will go through the demo and see this IOT device in action.

Time to call all your folks and arrange some popcorn.

If you are able to pull this off, you are well beyond the beginner level and now the world is your oyster, you have understood the fundamental aspects of working with Micro controllers,Arduino IDE, Wifi, PIR sensors, Power convertors, PCB's, Casing, relays, IOT platforms and also hooking up actual devices.

Nice work !!!!

Complete Guide to Build IOT Things from Scratch to Market is here !
I have built an enabler course for you to build IOT products from scratch and take it to market, I will take you through Arduino, NodeMCU, Nano, ESP8266, Sensors, Displays, Shields, Keypads, Software, Coding, Designing, Talking to internet, Protocols, IOT platforms, Casing, Manufacturing, Raising Capital, Selling, Coaching and more. We will learn all this and make 3 Products in the course from different verticals, ya you will get to see the entire creating process of a product from design to casing.

***** For a limited time, you can take this course just for $12, that is 96% OFF, it is practically for free guys !! ****

https://www.udemy.com/complete-guide-to-build-iot...