Unity+vuforia virtual button connects to MQTT (EMQX) to realize AR+IOT (Unity C# code implementation) (2) (key monitoring of virtual buttons + publishing MQTT information)

Recently, after Unity tries to recognize the corresponding image, it presents the corresponding virtual model; and the corresponding virtual buttons appear for interaction; Unity connects to the EMQX server built by itself; it can send the corresponding information to EMQX, and then ESP8266/ESP32 subscribes to the corresponding topic to accept instructions and implement corresponding operations.

I want to write an article to record the process that can be posted. I originally wanted to write all the articles in one article, but found that it would be too long if squeezed into one article, so I planned to separate several articles to record.

This article is the second one, mainly introducing the use of virtual buttons and Unity connecting to EMQX and sending corresponding commands. Below is a final rendering of what this article can achieve.

Table of contents

development environment

Mobile phone Android version

hardware

Step 1: Import the MQTT library

Step 2: Create virtual buttons

Step 3: Configure virtual buttons

Step 4: Write the script corresponding to the virtual button

Step 5: Import the script into the virtual key

Step 6: Upload the Unity project to the Android phone

Step 7: Build EMQ private server

Step Eight: Inspection


Please refer to:

Unity+vuforia virtual button connected to MQTT (EMQX) to realize AR+IOT (Unity C# code implementation) (1) (preliminary AR implementation)_JASON、LI's Blog-CSDN Bloghttps: //blog.csdn.net/weixin_61908666/article/ details/124869084

development environment

Window 10

Unity2021.3.3f1c1

Vuforia Engine 10.7 ( add-vuforia-package-10-7-2.unitypackage )

Mobile phone Android version

Android12

hardware

ESP32/ESP8266

previous chapter

suggestion:

You can install VScode, which is more convenient for writing subsequent Unity scripts. Note that the language for script development is C#. Here is the download address of VScode Visual Studio Code - Code Editing. Redefined https://code.visualstudio.com/

Step 1: Import the MQTT library

Link: https://pan.baidu.com/s/1WsXRYm25qWIDsmvv2Met2w?pwd=4399 
Extraction code: 4399 

Go to the network disk to extract the file, and then unzip it

Open its corresponding Unity project in advance (the Unity project that needs to import MQTT)

Then find the script file of unity3d_mqtt.unitypackage in the folder \Unity3d_MQTT-master\Packages

Double-click to open .unitypackage, then it will appear in the Unity project, select all and click Import

Step 2: Create virtual buttons

Click the previously added ImageTarget in the project scene bar, then select the script named Image Target Behaviour (Script) in it, open Advanced and click Add Virtual Button to add a virtual button, after clicking, it will appear in the scene bar ImageTarget named The key of the VirtualButton.

Step 3: Configure virtual buttons

After clicking the VirtualButton, select the script named Virtual Button Behaviour (Scriptd) to set its corresponding Name (preferably simple and marked, and you will need to use it when you write the script); then the second option is Sensitivity Setting (this corresponds to Key recognition sensitivity, divided into three levels: high, medium and low) It is best to choose LOW because the recognition accuracy is too high and BUG is easy to occur.

Step 4: Write the script corresponding to the virtual button

First, create a folder under the Assets file under the Unity operation box (the name I created is qt. Then, as shown in the figure below, right-click the blank in the folder to create a new C#Script script. (Note that the name of the created script file must be Be consistent with the name of the public class MqttEvents: MonoBehaviour, otherwise it will cause an error) and then enter the corresponding code (if you need the code, please contact the author or add the author's QQ code FREE at the end of the article ! )

Step 5: Import the script into the virtual key

Drag the script into the script list corresponding to the virtual button as shown in the figure below, and then click Save in File to save (note that you must pay attention to saving otherwise the script will not hang) 

Step 6: Upload the Unity project to the Android phone

For details, please refer to the author's article

(2 messages) Unity+vuforia virtual button connects MQTT (EMQX) to realize AR+IOT (Unity C# code implementation) (1) (preliminary AR implementation)_JASON、LI's blog-CSDN blog_unity vuforia virtual button https: // blog.csdn.net/weixin_61908666/article/details/124869084

At this point, the Unity development of this article is completed, and the next step is to build an EMQ private server 

Step 7: Build EMQ private server

Method 1: Download EMQ Personal Edition directly on the EMQ official website

Try EMQ Products for Free (emqx.com) https://www.emqx.com/en/try?product=broker Method 2: Download the EMQ version corresponding to the author (It is recommended to use Method 2, because EMQ seems to have been revised recently, download The official website may not match the operation of the article)

Link: https://pan.baidu.com/s/1rw-J14pZUT_fp40_yhXjFA?pwd=4399 
Extraction code: 4399 

 After the download is complete, remember the file path corresponding to EMQ

Next win+R and enter cmd into the command box

Then enter the corresponding folder address, cd to open the bin in the folder, and finally enter emqx start

(If it is closed, cd into the bin folder and enter emqx stop)

 Open the browser and enter http://127.0.0.1:18083/#/

Account: admin Password: public

The following picture can be adjusted to Chinese from the settings

Step Eight: Inspection

Remember that the Android phone using the Unity project must be in the same local area network as the device that builds EMQ

Then click the client on the EMQ server to check whether the device is successfully connected to the EMQ server

This article ends here, and the next article will introduce the Android mobile phone to issue MQTT instructions through Unity, and then ESP8266/ESP32 subscribes to the corresponding topic to accept instructions and realize the corresponding operations.

 Thank you all for watching! ! ! I am a new author of Xiaomeng. I am currently groping in the basic ocean, and I am determined to become a master. I hope to post an article to record the learning process. If anyone finds a problem with the article, please actively suggest it. If like-minded friends want to delve into this aspect together, welcome to discuss it together, and actively share your opinions in the comment area. If you have friends who want to discuss further with the author, you can add me on QQ: 2640514807 

This article was written independently by the author

All original articles on this blog may not be used for commercial purposes and traditional media without my permission. Please indicate the source for reprinting by online media, otherwise it is an infringement. 

おすすめ

転載: blog.csdn.net/weixin_61908666/article/details/124906834