Como usar a automação D2D baseada em IoT

How to use IoT-based D2D automation

In this tutorial, we'll learn how to use device-to-device (D2D) communication to make daily life a little simpler. For example, you will 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 arrive home one night, the presence of your vehicle may signal the porch light to come on before you reach the front door.

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

In this case, D2D detects the presence of your car 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 fully automated and does not depend on human interaction. This means that using a CCTV camera is unnecessary to detect the presence of your vehicle. (You might even get a notification if someone tries to tamper with your car.)

Circuit Diagram

The distribution board socket. You can also use the standard Arduino UNO3 and ESP8266 WiFi microchip separately. The secret is to ensure that everything fits within the distribution board.

Custom Atmgea 328p board with an ESP8266 and relay circuit.

Note: The rest of the circuit configuration is the same if it is connected to ESP8266.

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

To ensure successful D2D communication, a control signal must first be generated. This signal is sent between the smart sensor that detects the presence of the vehicle and the device that controls the appliance (lights, AC, coffee maker, etc.). The control device will require a predefined definition of the meaning of each control signal.

For example, if the vehicle is recently arriving or leaving the garage, the sensor will send a different control signal message.

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

Block Diagram Algorithm

We will need to make two devices. One to detect the presence of the vehicle in the garage (we will call it the detection device) and another to control the appliance (the control device).

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

The sensing device is an ultrasonic distance measuring sensor, used with the Atmega 328p controller and ESP8266 for communication. This device sends a control signal on the “ts/light” theme. Essentially, this device continuously detects the presence or absence of the vehicle.

Now let's take a look at how these devices will communicate with each other.

How it works
There are three scenarios described below this device on how it might work.

1. Absence of vehicle: If there is no vehicle within the sensor range of the detection device, it will continuously send an “OFF” signal to the unit’s control device. Therefore, the device connected to this device will remain switched off.

2. Parked vehicle: When the car is parked in the garage, the detection device sends an “ON” signal which is in the “ts/light” thread. The appliance is then turned on.

3. Vehicle leaving: If the vehicle leaves the garage, the detection device sends an “OFF” signal to the control 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 will flag the car as detected.

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

To make sure it's not a false detection, it repeats this five times to make sure the condition is true. In this case, it sends an “ON” signal.

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

2. Network communication
The common signature is published on the ESP8266.

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

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

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

Note: The excerpt from the ESP8266 ode.

Furthermore, everything received by MQTT is sent on the ESP8266 serial port to the Atmega328p.

void Received_data(char* topic, byte* payload, unsigned internal 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 adequate communication delay, the ESP function takes one second as a timeout. It will also consider anything received at that second as a single string.

Furthermore, since one device is publishing to the “ts/light” topic, the other must be subscribed to the same topic to receive the sent message.

Related Content

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.