Introduction to the Internet of Things Control APP (5) --- Use android studio to directly write the Internet of Things control APP

Abstract: As the final version of the series of articles on the production of IoT control APP, the benefit of this article is to let everyone learn to make an Android APP that is completely controllable by themselves. Compared with the previous articles introduced using third-party platform automatic generation, third-party platform help packaging, and android studio production framework to display the control page produced by IoT Studio, the biggest advantage is that the source code is controllable, followed by free (for development In terms of those). The difficulty is that there are too many technologies to master. This article introduces the basic principles and operating steps of the implementation in as much detail as possible, and finally provides the source code that can be compiled normally, so that you have both theoretical learning and practical objects.

The achieved effect is as follows:

The software environment used: win7 x64, android studio 3.5.1.

table of Contents

1. Why do third-party platforms charge fees?

2. What is the difficulty in making android studio programs by yourself?

3. The basic idea of ​​making android studio programs by yourself

4. Android studio makes IoT control APP actual combat

5 Conclusion


1. Why do third-party platforms charge fees?

Because of the high threshold of the Internet of Things control system, these online products designed by third-party platforms have condensed the hard work of a large number of programmers. Imagine after you learn it, will you use this skill to find a job or do a project, then what is the purpose of finding a job or doing a project? Think about it in another place and you will understand.

Third-party platforms provide high-quality services, and you pay a reasonable price. If you want it for free, you can, and spend dozens or even hundreds of times the time and energy to learn how to do it.

2. What is the difficulty in making android studio programs by yourself?

Difficult to debug.

Old programmers who do embedded must understand this truth.

You think all the codes are fine, but they just can't be adjusted. Sometimes a punctuation traps you for a month.

This article is also the same. The seemingly simple source code actually made it successfully generate the apk file. Before successfully communicating with the cloud platform, it has solved countless problems and spent many nights.

In other words, even if some code is given to you and the installation package is generated, it will not be able to communicate with the cloud platform and control the IoT devices.

The IoT control app is like this. It is not a simple native activity, you can operate it yourself, but communicate with the cloud, and communicate with real-world IoT devices to achieve the goal.

3. The basic idea of ​​making android studio programs by yourself

Whether it is a third-party or self-made, the core is to realize the transmission of communication data. Students who can insist on seeing here, by default, you have mastered the basic knowledge of MQTT, if you have not mastered it, please check the information and study on CSDN.

Let me briefly describe it. MQTT (Message Queuing Telemetry Transport) is a communication protocol developed by IBM many years ago, which can be reliably transmitted under low bandwidth (adapted to low-cost small traffic packets or IoT cards) , Was not originally designed for the Internet of Things, but it has grown under the boom of the Internet of Things and has almost become the de facto international standard for Internet of Things communication protocols.

Students in need can simply browse my other article to understand. I am not an MQTT expert, so I don't know much about it.

Use the arduino D1 wifi module (WeMos D1) to connect to the Alibaba Cloud IoT platform and successfully realize the APP to light the onboard LED (5) ---MQTT.fx analog device connects to Alibaba Cloud

It can be understood from the above that the MQTT.FX software can be used on the PC to connect to the Alibaba Cloud IoT platform and subscribe/publish messages.

However, if you want to successfully communicate with the Alibaba Cloud IoT platform, you need to set the MQTT client. There are several key parameters, such as Broker Address, Broker Port, and Client ID. The Client ID is calculated by encrypting several parameters (the type of Alibaba Cloud platform is hmacsha1).

Need encryption? Of course. Otherwise, how to ensure the reliability of the Internet of Things communication! So you can use the server to build the MQTT server, and you can play it. If you really want to use it for actual projects, it is recommended to use a large platform to operate it. The big platform will provide all necessary security and encryption measures.

With these basic conditions, you can successfully connect to the cloud platform and start the subscription/publishing business.

Whether it is an APP, a cloud platform, or an Internet of Things terminal, the subscription/publishing method is actually adopted for communication, which is quite advanced.

We use android studio to make our own APP, the core is to implement our own client inside the APP, and encrypt it in accordance with the regulations inside the APP. Then you can subscribe/publish topics. Be able to subscribe/publish topics, then the rest are all details, there is nothing hard to get from you.

Alibaba Cloud's IoT Studio mobile visual development is actually equivalent to the default component/support package for communication with the Alibaba Cloud IoT platform on the web side, so it can realize the control page development with a small amount of code or even no code.

4. Android studio makes IoT control APP actual combat

First, you need to build new products and equipment on the Alibaba Cloud IoT platform

How to add a new device to the Alibaba Cloud IoT platform

Then prepare the android studio development environment. This article uses android studio3.5.1, and gradle uses gradle-5.4.1-all. Please note that the domestic mirror image is used, otherwise it may be stuck in a day without moving.

Reference article: Different versions of gradle are too slow to download---Tencent has made a domestic mirror and can be downloaded directly

Then in the project build.gradle, add the Paho warehouse address. Please note that while adding the paho warehouse, modify the addresses of other warehouses, because the warehouse provided by Alibaba Cloud is faster.

Refer to the article How to set build.gradle as Alibaba Cloud effective address by android studio

Then in the application build.gradle, add Paho Android Service.

Then add the following information in AndroidManifest.xml

Also add permissions. At this point, the basic settings of paho are completed.

What is paho? The following is an English explanation. It is a client written in standard C language, which ensures very good portability.

MQTT C Client for Posix and Windows

The Paho MQTT C Client is a fully featured MQTT client written in ANSI standard C. C was chosen rather than C++ to maximize portability. A C++ API over this library is also available in Paho.

In fact there are two C APIs. "Synchronous" and "asynchronous" for which the API calls start with MQTTClient and MQTTAsync respectively. The synchronous API is intended to be simpler and more helpful. To this end, some of the calls will block until the operation has completed, which makes programming easier. In contrast, only one call blocks in the asynchronous API - waitForCompletion. Notifications of results are made by callbacks which makes the API suitable for use in environments where the application is not the main thread of control.

Next, start to connect to the Internet of Things and add triples, topics and client information in MainActivity.

Next, prepare to connect. Calculate the MQTT connection parameters clientId, username and password, and set the username and password to the MqttConnectOptions object.

Next, create a MqttAndroidClient object, set the callback interface, and then use mqttConnectOptions to call the connect method to establish a connection.

Wrapper function for publishing message

Publish a message by pressing the button in OnCreate, and publish a message "hello IoT" to the Alibaba Cloud platform.

The last step is to compile. After all the details of the settings are resolved, the compilation is successful.

Copy the apk file to the phone, install it, and run it again.

It can be found that the corresponding equipment of the Alibaba Cloud IoT platform is already online.

 

Click the PUBULISH button, go back to the log under the Alibaba Cloud Internet of Things platform to check, and the message was successfully released.

So far, a complete MQTT client that can connect to the Alibaba Cloud IoT platform has been debugged. We learn to publish, then we can subscribe, and the core of IoT MQTT communication is mastered. Next, no matter which IoT device or attribute is controlled, similar operations can be taken.

5 Conclusion

The key to Internet of Things control APP programming is the MQTT client first, which is like having a built-in MQTT.FX software in your own APP, except that the information connected to the Internet of Things platform is automatically calculated and connected by the APP, and you don’t need your own The calculation is based on the triples; the second is the realization of the subscription and publishing function. The Internet of Things communication uses a subscription/publishing model. Different topics correspond to different attributes. Mastering this skill can realize the ever-changing "things." The last and most important thing is the debugging skills. Without this skill, if you give you the source code, you will not be able to compile it. If the compilation is successful, just make a simple change and you won’t.

This article is based on several Alibaba Cloud help documents and my own experience in debugging the android studio program. Not all the words in the source code are written by myself. Thanks to Alibaba Cloud partners, and to teachers and students who helped me write a series of articles.

Hope to help you all.

The source code has condensed a long time of labor, for friends in need.

https://download.csdn.net/download/youngwah292/14015473

All kinds of flips are welcome.

 

Guess you like

Origin blog.csdn.net/youngwah292/article/details/112131225