laitimes

Use the empty board single board computer to build a smart home system

author:Fun industry sharing

Imagine being able to control your lights with just a few taps while monitoring room temperature in real time, and a project with Nodemcu can help you achieve this.

As a smart hub, the empty board will become the core of the project, connecting three buttons to control the three LED lights through the efficient MQTT communication protocol, and using Nodemcu to achieve precise control. In addition, the project will integrate DHT11 sensors to monitor indoor temperature changes in all aspects. The project is designed as a beginner-friendly home automation portal, and with simple steps and minimal components, you will be incredibly surprised to discover how easily the space magic you have created will be integrated into your life. Let's dive deeper and build a smarter home.

Use the empty board single board computer to build a smart home system

Hardware materials required for the project

1. Empty board single board computer

2. NodeMcu development board

3. DHT11 temperature and humidity sensor

Fourth, three LED lights

5. A breadboard

6. 3V battery

7. Jumper

Step 1: Set up the empty board and VScode

Prerequisites:

Install Visual Studio Code on your computer. Install OpenSSH on both the computer and the empty board.

Steps:

Install the remote SSH extension:

打开Visual Studio Code,点击左侧边栏的方形图标进入扩展视图(或按Ctrl+Shift+X快捷键)。

搜索“Remote - SSH”扩展并安装。

Use the empty board single board computer to build a smart home system

To turn on the remote SSH extension:

Use the empty board single board computer to build a smart home system

Press F1 or Ctrl+Shift+P in VScode to open the command panel.

输入并选择“Remote-SSH: Connect to Host…”命令。

Enter the SSH connection string of the empty board: [email protected].

Follow the prompts configuration file and specify that the operating system type is Linux, and the default password is dfrobot.

After the connection is successful, VScode will open a new window to directly access Unihiker's file system and terminal via SSH.

Start Programming:

Now you can edit files, run commands, and work on empty board projects in Visual Studio Code as if you were working directly on your device.

Step 2: Libraries used at both ends

NodeMcu端:

Use the PubSubClient.h library to implement the MQTT protocol function.

DHT11 sensor data was processed using the DHT.h library.

Search for the DHT sensor library and PubSubClient library provided by DFRobot through the library manager and install them.

行空板端(Python):

Use the PyQT5 library to create a graphical user interface (GUI).

Use the paho.mqtt.client library to implement MQTT communication.

Other required libraries, such as the pinpong library, are used to access GPIO pins and on-board sensors. For example, in this example, an on-board light sensor is used to capture ambient light intensity and update the text display based on preset commands (this part is for testing purposes only and has nothing to do with the main code, but you can assign special tasks to it).

Install the Python library:

Enter the following command in the terminal to install the required libraries:

pip install PyQt5

pip install paho-mqtt

Step 3: MQTT configuration instructions

Python端MQTT配置:

Set the MQTT server address (MQTT_ADDRESS) to the IP address or hostname of the MQTT proxy server.

Provide the MQTT username and password, fill in the MQTT_USER and MQTT_PASSWORD variables separately, and ensure that these credentials can be securely accessed to your MQTT proxy server.

Configure the subscription topic and publish topic to ensure that they match the topic corresponding to the ESP device.

NodeMcu端MQTT配置:

Set the MQTT server address (mqtt_server) to the IP address or hostname of the MQTT broker server to be connected to.

Configure the topics (one_topic and two_topic for publishing and subscribing to to ensure that they are consistent with the corresponding topics on the Python side.

Fill in the MQTT username and password (mqtt_username and mqtt_password), as well as the unique client ID (clientID), making sure they match the settings in the MQTT proxy server.

Step 4: Overview of Python code

Use the empty board single board computer to build a smart home system

The Python code combines MQTT functionality and PyQt5 to build a smart home control interface, including:

1. Establish an MQTT connection, use your credentials to connect to the MQTT proxy server and subscribe to a specific topic to receive messages.

2. Use PyQt5 to create windows with labeled buttons to control lights and fans, and load pictures with different states (ON/OFF).

3. Use the LCD widget to display temperature readings.

4. Button interaction: Switch the status when you click the button, and send the corresponding MQTT message to control the devices (such as light bulbs and fans) connected to the ESP8266.

5. Light sensing: read the ambient light intensity, and update the label to show "day" or "night".

6. Integration: Integrate MQTT client functionality with PyQt5's event processing to enable seamless interaction between MQTT messages and the graphical interface.

NodeMcu 代码概񖇑

Use the empty board single board computer to build a smart home system

NodeMcu's ESP8266-based code configuration enables it to control smart home devices through MQTT proxy servers, and the key parts include:

1. Wi-Fi Settings: Use the specified SSID and password to connect the NodeMcu to the Wi-Fi network.

2. MQTT configuration: Set MQTT server details, topics for publishing and subscribing, and MQTT credentials.

3. Sensor and output processing: The integrated DHT11 temperature and humidity sensor takes readings and defines GPIO pins to control outputs (such as lights and fans).

4. Establish a connection to the MQTT proxy server and subscribe to a specific topic to receive messages, periodically publish temperature readings to a specified topic, respond to the received MQTT messages, and control the output devices (such as lights and fans) according to the specific commands received.

Step 6: Hardware connection

Use the empty board single board computer to build a smart home system

DHT11 sensor wiring:

Connect the VCC pin of the DHT11 sensor to the 5V pin of the NodeMcu, the GND pin to GND, and the DATA pin to the digital pin D2.

Output device (LED in this case):

Define the output pin, e.g. int Out1 = D4; int Out2 = D5; int Out3 = D1;。 Connect the positive (anode) of the LED light to the above output pins (D4, D5, D1) and the negative (cathode) or fan to the GND pin of the NodeMcu. If a 3V battery is used, then connect its positive terminal to the Vin or 5V wire and the negative terminal to the GND terminal.

Step 7: Run the project

Download all the required files from the GitHub link.

Program NodeMcu: Open the Arduino code file, change the credential information, and upload the code.

For the Unihiker side, copy the file to the root directory, which can be done by visiting the Unihiker local web page (http://10.1.2.3/pc/file-upload).

Open the main Python file and modify the credential information.

After the modification is completed, you can run the Python code directly in the VScode terminal or through the Unihiker GUI to start the project.

For more information about the "Building a Smart Home System with Empty Board Computers" campaign, you can visit the "DF Maker Community" to learn more.

Read on