Jump to content


IOT - Motion detector with email notification w/ NodeMCU, PIR, Carriots, WIFI, PCB,Casing and more.

IOT PIR sensor NodeMCU WIFI Relays power converter IOT platform carriots platform casing PCB design

  • Please log in to reply
2 replies to this topic

#1 junaid



  • Members
  • Pip
  • 2 posts

Posted 19 February 2016 - 04:07 AM

Motion Detector - P2


Full DEMO:  https://www.youtube....h?v=jvdgsUxcIq0



You should be able to use NodeMCU, PIR Motion sensor, Carriots to  design, build, code, produce and install Smart Motion detector for commercial and Home use cases.

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.

The theme it revolves around is Security of a building or Home or a restricted area.




Product Functionality:

The Device will detect any motion within a specific range 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.


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

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



Hardware Required:

Product 2 - Safety

Parts Needed

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


Motion detector shield -- 1


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


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


Generic PCB with bus lines ---- 1


Casing -- 1

http://www.budind.co...sparent NEMA 4X

Soldering KIT


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.


Circuit Diagram and PCB:

We will use Fritzing to do create the circuit diagram





NodeMCU  D1 ---> INI of the Relay

NodeMCU D2  ---> Digital Out PIR Sensor

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

NodeMCU 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.

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.



Assembly and Casing:


see the demo




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 to make NodeMCU work with a relay, PIR motion sensor and use IOT platform carriots over WIFI

Attaching the code is a txt file as Udemy is yet to accept .ino files.

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 <SPI.h>

#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(){






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





  // start wifi




    Serial.print("Connecting to ");


   WiFi.begin(ssid, password);


  while (WiFi.status() != WL_CONNECTED) {






  Serial.println("WiFi connected"); 

  Serial.println("IP address: ");




// 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(val);// For debugging purpose only

  WiFiClient client;

  //const int httpPort = 80;

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


    // 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.print("Content-Length: ");

    int thisLength = json.length();


    client.println("Connection: close");





  else {

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

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




void loop() {

  val = digitalRead(pirpin);


  if(val == HIGH){



    if (pirstate == LOW){

      Serial.println("Motion Detected");

      pirstate =HIGH;

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




   /*   while(client.available()){

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



  } */




  if(pirstate == HIGH){

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

    pirstate = LOW;





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



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, Wifi, Ethernet chips, 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 !! ****








#2 CoraDias



  • Members
  • Pip
  • 1 posts

Posted 07 February 2018 - 06:20 PM

Hi....My project consists of one RPi 3 with motion sensor and camera attached. All this is linked to my gmail account. Once motion is detected, it will take a recording and email me saying that motion has been detected and it should also attach the image. All this happens automatically. I have ssmtp all set up for my gmail and everything works perfect, apart from the image is not attached to the email.


smt pcba

#3 jpastor


    Development leader

  • Administrators
  • 178 posts

Posted 09 February 2018 - 08:46 AM

Hi CoraDias


Thanks for sharing.
Perhaps providing more details about your implementation may be useful for other forum users.


Good job!

Also tagged with one or more of these keywords: IOT, PIR sensor, NodeMCU, WIFI, Relays, power converter, IOT platform, carriots platform, casing, PCB design

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users