(Image deletion)

At the end of the year, I believe everyone is as busy as I am. I have a little spare time recently, so hurry to update our FlappyBird. Last time said to add an online battle function, think about this bird is not suitable for war class, but it doesn’t matter, this does not prevent us to study the development of board networking methods. So this article is about how I got the development pad to connect to wifi and enable Socket Server, and then developed a HarmonyOS-based mobile remote control, or virtual controller prototype. Speaking of which, this is the story between hongmeng OS mobile phone and Hongmeng Hi3861 development board oh.

Let’s Harmony Hi3861 Wifi IoT development board is originally tailored for Wifi smart home, so Wifi support must be indispensable, in reading a variety of official documents, codes, and read a few articles of The teacher Lianzhi ‘an (link), finally is to have an idea.

In general, wifi-related interfaces are in the “vendor\hisi\hi3861\hi3861\third_party\lwip_sack\include\lwip\netifapi.h” header file. It is helpful to read the comments in the code carefully. I did not include the network distribution function of mobile phone this time, because I thought it would be troublesome to operate the mobile phone every time, so I used the way of directly connecting the designated AP in STA mode, which can be improved into the networking mode of touch, which will be left for follow-up study.

Without further ado, above:

This is the overall architecture. The router does the AP, and the mobile phone and development board are connected to the same Wifi.

Here is the flowchart of the development board starting the server:

After the socket Server is started, it receives the connection request from the client, and then circularly receives the data and executes the action according to the command. Another important point to note is that the network related operation functions are placed in a separate thread, that is, the background network connection, to avoid blocking the main thread. The socket Server listening port is set to 8888.

Soon I realized I didn’t know the IP address of the development board. Although I could see all the device IP addresses on the router administration page, it was always inconvenient to have to go to the router administrator everywhere. So I added the ability to display IP addresses with one click. Netifapi_netif_get_addr (netifapi.h)

/*
 * Func Name:  netifapi_netif_get_addr
 */
/**
 * @ingroup Threadsafe_Network_Interfaces
 *
 * @brief
 *
 *  This is a thread safe API, used to get IP_add configuration for a network interface
 *  (including netmask and default gateway).
 *  It is recommended to use this API instead of netif_get_addr()
 *
 * @param[in]    netif          Indicates the network interface to get.
 * @param[in]    ipaddr         Indicates the IP address.
 * @param[in]    netmask        Indicates the network mask.
 * @param[in]    gw             Indicates the default gateway IP address.
 *
 * @returns
 *  0 : On success \n
 *  Negative value : On failure \n
 *
 * @par Related Topics
 * netif_get_addr()
 *
 * @note
 *   - netmask and/or gw can be passed NULL, if these details about the netif are not needed
 */
err_t netifapi_netif_get_addr(struct netif *netif,
                              ip4_addr_t *ipaddr,
                              ip4_addr_t *netmask,
                              ip4_addr_t *gw);
Copy the code

The first parameter, netif, is the pointer to the NEtif structure used to connect to wifi. The other three parameters are the IP address, subnet mask and gateway used to return.

So how do you convert the returned IP4_ADdr_t structure to a generic numeric and period string format (such as 192.168.1.1)? The answer is the IP4addr_ntoa function.

char *ip = ip4addr_ntoa(ipAddr); printf("ip: %s\n", ip); Free (IP)// Remember to release after useCopy the code

In fact, there are a series of conversion functions defined in the \vendor\hisi\hi3861\hi3861\third_party\lwip_sack\include\lwip\ip4_addr.h header file. They are defined as follows and can be used as needed:

u32_t ipaddr_addr(const char *cp);
int ip4addr_aton(const char *cp, ip4_addr_t *addr);
/** returns ptr to static buffer; not reentrant! */
char *ip4addr_ntoa(const ip4_addr_t *addr);
char *ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen);

int inet_pton4(const char *src, unsigned char *dst);
const char *lwip_inet_ntop4(const unsigned char *src, char *dst, u32_t size);
Copy the code

In terms of mobile phone controller, I made a version of App based on Hongmeng OS in DevEcoStudio, which mainly realized Tcp connection to the specified IP and sent instructions. However, because there is no real machine in hand, so I can not debug, so I can only make an Android version App instead of debugging, some regret, I hope to take this opportunity to get the P40, complete this dream, but also see the beauty of Hongmeng OS.

Here’s a screenshot of the project in DevEcoStudio:

Emulators run like this:

The simulator can’t connect to the Internet. However, this does not prevent an Android version for testing, the core code is as follows:

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Add button event callback this.findViewById(R.i.b.tn_go).setonClickListener (new view.onClickListener () {@override public void onClick(View view) { new Thread(new Runnable() { @Override public void run() { EditText editIp = findViewById(R.id.edit_ip); // add an IP address input box String IP = editip.gettext ().toString(); sendCmd(ip); // send instructions to the specified IP address}}).start(); }}); } private Socket socket; private void sendCmd(String ipAddr){ int port = 8888; 8888 try {if(socket == null){socket = new socket (ipAddr, port); // Create socket client log. I ("JoyStick", "Create socket!!" ); } String cmdData = "A"; // Send A byte data[] = cmddata.getbytes (); socket.getOutputStream().write(data); Log.i("JoyStick", "Send success!!" ); showMessage("Send success!" , Toast.LENGTH_SHORT); } catch (Exception e) { e.printStackTrace(); Log.e("JoyStick", e.toString()); showMessage("Send Error! \n" + e.toString(), Toast.LENGTH_SHORT); }}}Copy the code

The running interface of APP on my Mate20 is as follows:

See more >>>

Author: Old ferryman

Want to know more, please visit: 51 cto and huawei officials strategic cooperation HongMeng technology community at https://harmonyos.51cto.com/