Controlling the main entrance gate using the MQTT protocol
In this section, we will learn how we can control the appliances in IoT projects using the MQTT protocol. We will use the HiveMQ public MQTT broker, whose credentials are available at https://www.hivemq.com/mqtt/public-mqtt-broker/.
For the living room ESP32 microcontroller, we will upload the following code so that we can send the data to InfluxDB as well as receive the MQTT message to control the door lock. The connection diagram for the living room is provided in Figure 8.4. The code is available on GitHub at https://github.com/PacktPublishing/Programming-ESP32-with-Arduino-IDE/blob/main/chapter%208/livingroom/livingroom.ino:
#include <Adafruit_Sensor.h> #include <DHT_U.h> #include <WiFiMulti.h> #include <InfluxDbClient.h> //https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino.git #include <InfluxDbCloud.h> #include <PubSubClient.h> #include <ESP32Servo.h> // WiFi configuration...