Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the application of yeelink on PX2

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about the use of yeelink on PX2. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

First of all, introduce yeelink, this is an open cloud server platform, everyone can sign up for their own account, and then they can use its server, that is, as long as your device is connected to the Internet, no matter where you are, you can control your device from the network, and it provides a variety of API interfaces. As long as we are familiar with tcp programming, we can easily use it. Yeelink can also view and modify our equipment on the official Wechat account, which is very easy.

The official website is this, http://www.yeelink.net/. After registering, we can go to the user center and see this interface.

And find our apikey in the account, this value will be used when programming, in addition, in device management, we can create our own device, choose to create our own sensor in the device, you can choose numerical type and switch type, and so on. As follows, you can see the ID of the device and the ID of the sensor in this position of the sensor. These two values are also required.

Then we first test whether the communication is successful. We can use the network assistant as a server side and a device to send commands to the yeelink server.

One of the following two commands is to send a value to the server, and the other is to obtain a value from the server. The effect of successful communication is as follows. If the communication fails, you can try to get the apikey value again, which can be obtained from the account. (remember to change the ID and apikey values below to your own)

Then we can write a tcp communication program, and add the opening of the device to it. Here, the landlord simply uses a GPIO port as an example. If we want, we can also write a temperature detection program, and then use Wechat to detect the temperature at home or somewhere in real time.

The procedure is as follows

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# Server port of define portnumber 80//yeelink

# define DEVICE_NAME "/ dev/rkpx2_GPIO" / / gpio device. The kernel provided already includes four pins of gpio4 1234 defined in the definition. One pin is used here.

Int main (int argc,char * argv [])

{

Int sockfd//tcp process number

Device number of int fd//gpio

Length of data received by int recdata//

Char * addr// a character pointer that is used to find the location of the data value. The following value is the value of the key on the website.

The length of the int offset//value to find the key value

Char * yeelink//yeelink server ip address

Char recv_data [1024] / / memory for receiving data

Char flag// button valu

Yeelink= "42.96.164.52"

Struct sockaddr_in server_addr

Struct sockaddr_in cilent_addr

Char send_data1 [300] = "GET / v1.0/device/ × × × / sensor/ × × × / datapoints HTTP/1.1\ r\ nHost: api.yeelink.net\ r\ nContent-Length: 11\ r\ nU-ApiKey: * (here please replace the previous ID with your own ID)\ r\ n\ n {\" value\ ": 0}\ r\ n" >

Struct hostent * host

Fprintf (stderr, "start")

While (1) {/ / cycle, read the key value every 5s

If ((host=gethostbyname (yeelink) = = NULL) / / get the server address

{fprintf (stderr, "gethostname Error:%s\ a\ n", strerror (errno))

Exit (1)

}

If ((sockfd=socket (AF_INET,SOCK_STREAM,0) =-1) / / establish socket

{

Fprintf (stderr, "socketError:%s\ a\ n")

Exit (1)}

Bzero (& server_addr, sizeof (server_addr)) / / Stack initialization, all changed to 0

Server_addr.sin_family=AF_INET

Server_addr.sin_port=htons (portnumber)

Server_addr.sin_addr=* ((struct in_addr *) host- > h_addr)

If (connect (sockfd, (struct sockaddr*) (& server_addr), sizeof (struct sockaddr)) = =-1) / / apply for a connection

{

Fprintf (stderr, "connect Error:%s\ a\ n", strerror (errno))

Exit (1)

}

Send (sockfd,send_data1,sizeof (send_data1), 0) / / pass the http file over

Recdata = recv (sockfd, recv_data, sizeof (recv_data), 0) / / accept the value returned by the server

Recv_ data [recdata] =''/ / with Terminator

Addr = strstr (recv_data, "\" value\ ") / / find the location of the key value in the received data, that is, value. The style of the received data is as follows: {" value ": key}

Offset=strlen ("\" value\ ":") / / determine the length of the value

Flag = * (addr + offset) / / find the value of the key and assign it to flag

Fprintf (stderr, "the value is% c\ n")

Close (sockfd) / / close socket, remember, it has to be closed early, or a segment error will occur.

Fd=open (DEVICE_NAME,O_RDWR) / / Open the GPIO device

If (fd==-1) {

Fprintf (stderr, "open devices% s error\ n", DEVICE_NAME)

}

If (flag=='1') {/ / determines the value of flag, and then performs the corresponding operation

Ioctl (fd,1)

Fprintf (stderr, "the value is% c\ n", flag)

}

Else

{

Ioctl (fd,0)

Fprintf (stderr, "the value is% c\ n", flag)

}

Fprintf (stderr, "please wait 5s\ n")

Sleep (10) / / delay 10s to make the experimental effect more obvious.

Close (fd)

}

Exit (0)

}

Because it uses the GPIO port, it is difficult to add pictures to the GPIO4_1 landlord on the board, so the effect will not be demonstrated, but it has been verified that it is feasible. The kernel and program files used by the landlord are all put in the compressed package. If you are interested in downloading the files in the compressed package, one of the files in the compressed package is kernel, which supports VGA display, and contains a GPIO driver of the landlord. In the program, one is the source code. We can compile the executable file in the source package, the last one is the executable file, you can use the adb tool push to enter the board, and then execute it. Of course, here is the landlord's own account, ID and APIKEY are all from the landlord's account, so if you want to use it, you only need to change these three values in the program, and then you can use the equipment of your own account to control it.

Yeelink.zip

Some of the pictures during the test are as follows, pressing that switch will change the value of the GPIO port, and when changing the port, the debug port will also have a hint, there will also be a hint, you can use this port level change, control lights, buzzer and so on.

Now let's talk about several mistakes encountered by the landlord in the program.

1. For the header of the http file sent in the past, there should be two carriage returns in front of {"value": * *}. Don't miss this. This is a format requirement. If you miss it, you can't put the value on the server normally.

two。 When we get the value of the server, we have to deal with the value in the server, because the value he sends has no Terminator, so we have to add the Terminator manually.

After reading the above, do you have any further understanding of the use of yeelink on PX2? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report