[Personal use] Cloud server uses docker to build HomeAssistant + MQTT IoT platform

Overview

1. Overview of the construction process
2. Preparation work
3. Start building!
4. Summary
If you want to see how ESP32 or other microcontrollers programmed using MicroPython are connected to the cloud server to realize HomeAssistant control of the microcontroller, please read the next article of my blog.

1. Overview of the construction process

0.Overall process

We need to first have a cloud server, then build docker on it (just use Pagoda Fool to build it, I will write how to build it later), and then use docker to create 2 containers, which are equipped with HomeAssistant and MQTT respectively. This is equivalent to what we are doing. There are 2 "services (servers)" running on 1 cloud server. The specific content of the MQTT network messaging protocol is as follows. You can think of it as a transfer station for connecting the ESP32 IoT terminal and the HomeAssistant platform:

Insert image description here

Insert image description here


2. Preparation work

0. Purchase a cloud server

0.1 Buy a cloud server (A cloud, Teng cloud, Hua cloud doesn’t matter, a lightweight server should be enough, it’s up to you. If you don’t know how to buy it, search for it yourself)

1. Software or services that need to be downloaded temporarily on personal PC

1.1.

2. Software and services that temporarily need to be downloaded on the cloud server

2.1 Pagoda (for easier use of docker and other operations)


3. Construction work begins

1. Use Pagoda to access your cloud server and install docker in a fool-proof manner

Click on docker in the left column and you should have an "Install" on the page. After clicking, the installation will proceed. It may take a long time, please wait patiently until the installation is completed.

Insert image description here

2. After the docker installation is completed, directly pull the HomeAssistant image

2.1 Pull HomeAssistant
docker -> Mirror -> Pull from the warehouse -> Fill in the content: homeassistant/home-assistant:latest -> Confirm
Insert image description here

2.2 Install HomeAssistant steps:

Insert image description hereInsert image description here

3. Create docker container

3.1 Create a container folder File -> Go to the home/your username/
path in the root directory and create a new folder "homeassistant-config", as shown in the figure. 3.2 Open the terminal and start creating the container
Insert image description here

Insert image description here

3.3 Enter the create container command and press Enter:

sudo docker run -d --name="homeassistant-v1" -v xxx:/config -p 8123:8123 homeassistant/home-assistant:latest

Please note! In the above command, "xxx" is the path to create the container and should be replaced with your own path! ! ! For example, my command is changed to:

sudo docker run -d --name="homeassistant-v1" -v /home/admin/homeassistant-config:/config -p 8123:8123 homeassistant/home-assistant:latest

3.4 Container created successfully

No error is reported when running the command. Go and take a look at the empty folder you just created. If there is already something there, the creation is successful. As follows:

Insert image description here

4. Open cloud server firewall port 8123

4.1 Open port
I am too lazy to write. You can directly enter the terminal of the pagoda or other software such as xshell, as long as you can enter the cloud server terminal, and run the following commands in sequence:

firewall-cmd --add-port=8123/tcp --permanent
firewall-cmd --reload

4.2 Check whether the port is open. You can see 8123/tcp, which means the port is open successfully.

firewall-cmd --list-ports

Insert image description here4.3 Open ports on the cloud platform

This depends on what cloud you are using. You can search the firewall options of your server and add the port (I am demonstrating Alibaba Cloud):

Insert image description here

5. Configure HomeAssistant

5.1 Access HomeAssistant

Use your ip:8123 to access the HomeAssistant web page. For example, if your cloud server's external IP is 182.96.213.203, then you can visit the following URL in your browser: (I'm just giving an example, don't fill in 182.96.213.203, fill in your own server's external IP!)

182.96.213.203:8123

Insert image description here

6. Install MQTT server

6.1 Visit the EMQX official website download area
https://www.emqx.io/zh/downloads
6.2 Run the command to put EMQX into docker.
Execute the "Get Docker Image" and "Start Docker Container" in the figure below on the server terminal. Two pieces of code.

Code piece 1:

docker pull emqx/emqx:5.1.4

Code piece 2:

docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx/emqx:5.1.4

Insert image description here

6.3 After the EMQX download is completed, check whether EMQX is already in docker:

Use command:

sudo docker ps

Insert image description here

6.4 Open ports 18083 and 1883 on the firewall

6.4.1 Run the commands in sequence, open the port and restart the firewall to take effect:

firewall-cmd --add-port=18083/tcp --permanent

firewall-cmd --add-port=1883/tcp --permanent

firewall-cmd --reload

6.4.2 Run the following command to see if we have opened ports 18083 and 1883:

firewall-cmd --list-ports

Insert image description here

6.4.3 Open ports 18083 and 1883 on the server console:

Insert image description here

6.5 Visit the EMQX website
6.5.1 URL:
http://xxx.xxx.xxx.xxx:18083
xxx.xxx.xxx.xxx represents the public IP address of your cloud server

Insert image description here

6.5.2 Login

The default username is: admin
The default password is: public

6.6 Return to HomeAssistant website

6.6.1 Operation process:
i: Configuration (left column) ->
ii: Devices and Services ->
iii: Add integration (lower right corner) ->
iv: Search for "MQTT" ->
v: Click "MQTT" ->
vi: Click "MQTT" ->
vii: Enter relevant information (please note that the username and password are the username and password you just used to log in to EMQX)
viii: Submit, successful. Our HomeAssistant has successfully connected to the MQTT service

Insert image description hereInsert image description hereInsert image description hereInsert image description hereInsert image description hereInsert image description here

6.7 Return to EMQX website

6.7.1 Operation process:

Monitoring->Client-> Found a new connection, ok

Insert image description here

6.7.2 Confirm that MQTT and HomeAssistant have been successfully connected:
1. Go to the HomeAssistant website
2. Enter the MQTT just now
3. Click "Options"
4. After turning on listening, send a data packet to test, and it is successful.

Insert image description hereInsert image description here

Insert image description here

4. Summary

At this point, the task of using docker to build the HomeAssistant platform on the cloud server and allowing MQTT to establish communication with the HomeAssistant platform has been successfully completed.If you want to see how to connect ESP32 or other microcontrollers programmed with MicroPython, please read the next article of my blog.

Guess you like

Origin blog.csdn.net/qq_43768851/article/details/132233085
Recommended