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.
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.
You may also like:
Filed Under: Electronic Projects, How to, PIC Microcontroller
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.