Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to use IoT-based D2D automation

By EG Projects March 30, 2025

In this tutorial, we’ll learn how to use device-to-device (D2D) communication to make daily life a little simpler. For example, you’ll be able to control household appliances, such as the coffee machine, a light switch, or the AC — and do so from inside your vehicle. So, if you’re arriving home one evening, the presence of your vehicle can signal the porch light to switch on before you get to the front door.

D2D communication typically refers to the technology that lets devices or appliances “communicate” without the use of network infrastructures.

In this case, the D2D detects the presence of your car by using an ultrasonic sensor and the MQ Telemetry Transport (MQTT) protocol for signaling. MQTT is a network protocol that transports messages between devices.

D2D communication is completely automated and does not rely on human interaction. This means that the use of a CCTV camera is unnecessary to detect your vehicle’s presence. (You can even get a notification if someone tries to tamper with your car.)

Circuit diagram

The switch board socket. You can also use the standard Arduino UNO3 and the ESP8266 WiFi microchip, separately. The key is to ensure everything fits inside the switchboard.

Customized Atmgea 328p board with an ESP8266 and a relay circuit.

Note: The rest of the circuit setup is the same if connecting with the ESP8266.

Technical insights
For this project we use Arduino UNO (Atmega 328p), ESP8266, and an ultrasonic distance-measuring sensor (to detect the presence of the vehicle), and the MQTT protocol for communication between devices.

To ensure a successful D2D communication, it’s first necessary to generate a control signal. This signal is sent between the smart sensor that detects the vehicle’s presence to the device that controls the home appliance (the lights, AC, coffee maker, etc.). The control device will require a pre-defined definition of what each control signal means.

For instance, if the vehicle is newly arriving or leaving the driveway, the sensor will send a different control signal message.

Since these signals are sent via the MQTT protocol, they can be accessed by multiple devices using their “topics.” This means that it’s possible to control multiple devices.

Block diagram | Algorithm

We’ll need to make two devices. One for detecting the vehicle’s presence in the driveway (we’ll call the detection device) and another for controlling the home appliance (the controlling device).

The controlling device uses a simple PCB board that connects the ESP8266 and Atmega328’s (or Arduino UNO’s) controller with a relay circuit. It “listens” (or subscribes) for a control signal, which is sent over the MQTT protocol on a specific topic. 

The detection device is an ultrasonic distance-measuring sensor, used with the Atmega 328p controller and the ESP8266 to communicate. This device sends a control signal on the topic, “ts/light.” Essentially, this device is continually sensing the presence or absence of the vehicle.

Now, let’s take a look at how these devices will communicate with one another.

How it works
There are three scenarios as described below of this device on how it can work. 

1. Absence of vehicle: If there’s no vehicle within the detection device’s sensor range, it will continually send an “OFF” signal to the appliance-controlling device. So, the appliance connected to this device will remain off.

2. Parked vehicle: When the car is parked in the driveway, the detection device sends an “ON” signal that’s on the “ts/light” topic. The home appliance is then turned on.

3. Vehicle departure: If the vehicle leaves the driveway, the detection device sends an “OFF” signal to the controlling device.

It’s possible to add more complicated control signals but, for this project, we’re keeping it simple.

Understanding the source code
There are two main parts of the code.

1. The detection device. The vehicle is monitored by the detection device’s ultrasonic sensor. If the distance in front of the sensor matches a set condition, it flags that car as detected.

if (int(sensor) < 100.00) {
    times1 = times1 + 1;
  }

To ensure it’s not a false detection, it repeats this five times to ensure the condition is true. If so, it sends an “ON” signal.

if(times1 == 5){
Serial.print(“ON”);
    times1 = 0;
delay(1200);}

2. Network communication
The common subscription is published to the ESP8266.

const char* topicSubscribe = “ts/light”;
const char* topicPublish = “ts/report”;

To access the network, both ESP8266 and Atmega328P are used. Anything from Atmega328p is directly published (“send”) as a control signal to the “ts/light” topic.

if (Serial.available()) {
String recivedData = Serial.readString();
temp_str = recivedData;
char temp[temp_str.length() + 2];
temp_str.toCharArray(temp, temp_str.length() + 1);
client.publish(topicPublish, temp);
}

Note: The ode snippet of the ESP8266.

Also, anything received over the MQTT is sent on the serial port from the ESP8266 to Atmega328p.

void Received_data(char* topic, byte* payload, unsigned int length) {
data_r_in_string = “”;
for (int i = 0; i < length; i++) {   

data_r_in_string = String(data_r_in_string + (char)payload[i]);

//Serial.print((char)payload[i]);
}

 Serial.print(data_r_in_string);
}

To provide a proper communication delay, the ESP function takes one second as a timeout. It will also consider anything received within that second as a single string.

Also, as one device is publishing on the “ts/light” topic, the other must be subscribed to the same topic if it’s to receive the message sent.

https://www.engineersgarage.com/wp-content/uploads/2021/08/VideoDemo.mp4

You may also like:


  • Raspberry Pi Server based Hotel/Restaurant Order Management System on IoT…

  • What are LoRa gateways and what types are available?

  • Arduino Based IoT Garden Monitoring System

  • TCP/IP Based IoT Communication with ThingSpeak Platform : IoT Part…

  • Introduction to Internet of Things: IOT Part 1

  • Transmission Control Protocol/Internet Protocol (TCP/IP) : IOT Part 28

Filed Under: Electronic Projects, PIC Microcontroller
Tagged With: Arduino, automation, circuit, communication, d2dcommunication, IoT, MQTT
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • ADS optimization cockpit window does not open
  • Voltage mode pushpull is a nonsense SMPS?
  • Will this TL084C based current clamp circuit work?
  • How to determine the maximum PAD frequency ?
  • Xiaomi Mijia 1C Robot problem of going backwards while working

RSS Electro-Tech-Online.com Discussions

  • Siemens large industrial PLC parts
  • Parts required for a personal project
  • Cant log in to Easy PC forum
  • Failure of polypropylene motor-run capacitors
  • Wideband matching an electrically short bowtie antenna; 50 ohm, 434 MHz

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection
  • Convection-cooled power controller integrates EtherCat connectivity

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe