Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key 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
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

How to use IoT-based D2D automation

By EG Projects

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…

  • 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, How to, More Editor's Picks
Tagged With: Arduino, automation, circuit, communication, d2dcommunication, IoT, MQTT
 

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.

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!


Featured Tutorials

  • Adjustable 0 to 30V 2A DC Power Supply Circuit (Part 1/13)
  • Adjustable +/- 1.25V to +/-22V 1A Power Supply Circuit (Part 2/13)
  • Adjustable 0 to 15V 1A Mini Power Supply (Part 3/13)
  • Constant 12V Power Supply for LED Circuits (Part 4/13)
  • Constant +/-9V DC Symmetrical Power Supply Circuit (Part 5/13)
  • Making a Circuit Breaker (Part 6/13)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

“ee

“ee

“ee

“ee

“ee

“ee

Recent Articles

  • Infineon offers Press Pack IGBT for transmission and distribution applications
  • STMicroelectronics outlines path to $20B+ revenue
  • STMicroelectronics and MACOM announce successful RF GaN-on-Si prototypes
  • Infineon expands its CoolSiC portfolio
  • STMicroelectronics and AWS collaborate for secure IoT connections

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics Research robot samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Space Vector PWM Help Needed
  • RFIC LNA cascode base and ground layout problem
  • Unable to launch virtuoso: libvisadev.so error
  • DIY test leads - source for lead ends?
  • P-Channel MOSFET always on

RSS Electro-Tech-Online.com Discussions

  • Voltage Regulator 12 - 5volt
  • How do I test amplifier speaker output polarity?
  • Adding Current Limit Feature to a Buck Converter
  • How to get lots of PCBs made cheap?
  • 24v dc relays not de-energising
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 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 | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key 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
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering