Development of Smart home backend based on Laravel+swoole

Source: Internet
Author: User
Tags binary to decimal

Development of Smart home backend based on Laravel+swoole

In the previous article on how to gracefully use the swoole> I have probably talked about the usage of Laravel in conjunction with Swoole. <laravel

Today, I am involved in the smart home project has basically ended the Web server and the development of Android (boil an overnight assault to do the rest, so tired), hot to talk about the development of smart home based on Laravel+swoole key technology points.

Parsing of 16 binary ASCII code protocol

Hardware I don't talk about, I just need to care about data parsing. How to base on Swoole if you parse 16 ASCII code in PHP, this article is relatively small. But the core of the part can be sorted out, the key to use is chr (),Hexdec (),Bin2Hex ( ) These 3 functions, the Internet also mentions the use of pack (), Unpack (), because the previous 3 functions and a good solution to the problem, so there is no further depth. I recommend that you familiarize yourself with these functions in the PHP manual website.

CHR The () function returns characters from different ASCII values, and thehexdec() function converts the 16 binary to decimal. Typically combine these two functions to convert a 16 binary string into an ASCII string,echochr(hexdec(' Fe ')); // The output is the next function of the understanding of the 16-based FE, case insensitive Bin2Hex The () function converts a string of ASCII characters to a hexadecimal value, which is used to read the conversion of data from Swoole. For example:$bufferstr_split(bin2hex($data), 2); // $data is the value passed OnReceive event in Swoole

In the Resolution protocol, the $buffer array in the previous example stores each bit of the protocol. So how to parse it? The quickest and most lazy way is to read the data according to the Protocol of the hardware development, for example, the head is the first, then it is $buffer[0]; If several combinations are data, write a small function spell.


was not too weak for the chickens to point (. NET and Java have a rich set of methods for byte[] and strings)? If you need to, you can write your own conversion tool class (I do not have to tidy up for the time being).

In the next computer communication also has a very important technical point is the XOR check, I was found from StackOverflow source, directly affixed to the address.

16 Binary ASCII Code protocol creation

No nonsense, directly on the code:


For laravel users, in this section I strongly recommend that you review the quick Array function helper method provided by Laravel.

Curve communication with hardware

Based on the Swoole receive data in the previous article has been described in detail no longer, here is mainly about sending data to the hardware problem. In the previous article, I mentioned 2 methods, one is to take advantage of the fsockopen () function, and the other is to monitor the internal port. Here also with the hardware of the great God made a joke, blush ah. For TCP/IP protocol-based communication, because the port is always receiving listening, it is not feasible to use the Fsockopen () or Socket_write () function, and hardware communication must be implemented based on the second way curve.

First, add the internal port to the command to listen, note that the OnReceive event is received by the custom Innerhandler (the unintelligible person please review the previous and swoole documents). always remember to open your internal listening port in Iptables!!

In Innerhandler, if you receive a command to send to the hardware (that is, the data in the previous section), the connection entity of the hardware is extracted from the cache, and then the data is sent (please refer to the Swoole documentation).

Here the comparison curve is in this cache, the cache is to send data when the setting, then how to know the hardware connection entity is? Of course it is to maintain a KV structure data:)

The entire curve of the communication process is: when the need to send data to the hardware, the first to send data to the internal listening port, and then sent from here to the hardware.

How to send data to the internal listening port? With Fsockopen () or socket_write () are completely no problem, the code here please do your own search, right when the exercise, not too lazy.

Event is a boon

This should be used before you know how convenient, come here first good review. The implementation process please feel yourself, I just say where I used the event:

1.基于JPUSH推送消息2.向内部监听发送数据

In addition, it is not feasible to use event to send data via Swoole.

Elegant, fast test

Because PHP is a weak type, it is confusing to understand. Data types that receive data and send data to the hardware are not the same. With Bin2Hex () is the shape of the " one-xx-FE" string , with Chr (Hexdec ()) to get the ASCII code 16, if the echo output, it will be garbled. So how do you test (see) your own generated 16 ASCII code data is correct? 1 kinds is to find a TCP/IP tool sent past, this trouble point, I recommend using fiddler monitoring.


You can quickly and conveniently use a Web page to output your ASCII code, in the Fiddler Hexview, you can see the original 16 of the data you sent. In addition, because the Swoole listener class is the CLI run, so I also highly recommend to write a little more echo print state, in the Phpstorm SSH client can quickly understand the current situation, like Android-studio Loger.

Echo $devNum Php_eol; // The old driver advised you to write more, better packagist find a wheel or write a logger library.

Robust discussion, can be very complex

Let's spit out a slot. NET to do the next computer data processing robustness is really on pins and needles ah. Make the next computer communication is still the most suitable for C + + Ah, the following problems in here also need to gradually improve the latter:

1.如果硬件发送的数据不是一个包发完而是分批发怎么办?2.看门狗是一定要写的,对吧?3.与硬件通信的response处理要做吧?4.TimeOut怎么办?....

Ah ah ah ah ....

It's blessed to love Android.

Under the notice, based on the purpose of learning, recently is imitating fresh city, enjoy, half sugar, these 3 very representative e-commerce app home UI (is reversed to see Smali and extract res in imitation yo).

The android side of the city has already done a mold has a kind of, this time is forcing their own Android, iOS are to imitate out, no old driver take their own ways to improve.

First do Android, interested friends can wait patiently for code and articles.

Development of Smart home backend based on Laravel+swoole

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.