Source: https://www.cnblogs.com/heat-man/p/4657157.html
Previously, on the embedded Linux development board, we implemented a remote control system for smart homes from the lowest level, but it was connected to a switch using an Ethernet cable, which felt outdated.
Coincidentally, while looking for a job in Beijing, I found a company that makes WiFi modules. My boss tasked me with running our WiFi module on the min210 development board to familiarize myself with the environment. After a lot of effort, I finally got it working yesterday and tested the server I wrote. The feeling of wireless is just different!
Let’s summarize the steps:
First, you need some hardware: an ARM development board, a computer (Ubuntu system, a virtual machine is also acceptable), and a WiFi module (any company’s module is fine, as long as it comes with drivers and some open-source software that supports the module).
Setting up the embedded environment: first, you need to flash the Linux system onto the ARM development board.
With the above preparations, we can move on to the substantial setup, summarized in steps:
1. To enable WiFi functionality in your embedded system, we need the WiFi module and the corresponding driver. The first step is to compile the driver for the module (generally, the module supplier will provide the corresponding driver package, follow the instructions to compile it).
2. Load the driver. (At this point, our embedded Linux system can perform some wireless operations, but we haven’t done anything yet; it’s like buying a WiFi module and just following the manual to load the driver).
3. Use the open-source software hostapd to turn our WiFi into a hotspot. (This way, networked terminals can scan our embedded Linux system to access our smart home control system).
4. Enable DHCP service to automatically assign IP addresses. (Forgive me, this is the first time I heard about this, but after using it once, I fell in love; no more manually setting IPs!).
5. Start the smart home service system, allowing other networked terminals to access the smart home service system via WiFi. (Just a quick test).
In the above five steps, the first step is closely related to the WiFi supplier; as long as you follow the instructions, there shouldn’t be any major issues. Here, we mainly record the configuration and use of the two major open-source software: hostapd and DHCP.
So, we directly move to the third step. At this point, your embedded system should have WiFi functionality, and we need to use hostapd to turn it into a master (WiFi hotspot).
hostapd – Creating a Wireless Router
About hostapd, the official website elegantly and succinctly states that it is a user-space daemon for access point control and authentication services. To translate, this is a service for access point control and authentication services, so managing access points and authentication services sounds just like the functionality of a router, right?
Forgive my ignorance! I just think of hostapd as creating a router! (In professional terms, it should be said: “hostapd can switch WiFi to master mode, mimicking the functionality of a router, creating a soft AP”).
hostapd is open-source software, and when using it, you must check whether it supports the wireless network card on your machine. The hostapd we use in the company has added support for the company’s module, so the hostapd downloaded last night was useless. However, the configuration is still similar; it’s just that the hostapd provided by the WiFi module supplier has added support for their module.
In summary, whether you obtain hostapd from the module supplier or online, make sure it supports your wireless network card.
After obtaining the source code, cross-compile to get the commands hostapd and hostapd_cli, and copy these two commands to the /bin directory of the target board. This way, we have the command to start hostapd.
To start the hostapd service, we need a configuration file because creating a router requires a name, password, and encryption method… All these parameters are concentrated in a configuration file, and of course, you can name the file whatever you like, but it’s best to use the .conf suffix.
Wow, it sounds so easy, but I have no idea how to start with the configuration file! Is there a demo? The answer is yes; it seems that open-source on Linux is very considerate. This file is found in the hostapd source directory as hostapd.conf. The type of router you want to create depends on the parameters inside.
Each configuration item inside has an explanation, but of course, it’s in technical jargon! Once you get better at it, you can add your own jargon in there.
For example, ssid: set the name of your hotspot, interface: network card interface, wpa: set your authentication related settings, and based on your needs, you can draft a router setup plan.
Copy your router setup plan, which is hostapd.conf, to the /etc directory of the target board, and then you can start the service:
./hostapd /etc/hostapd.conf -B
Executing the above command creates a WiFi hotspot according to your settings, where -B indicates to run in the background. (Assuming you created a hotspot named “heat”, you will find “heat” in the wireless list when you pull out your phone or open your computer, but connecting will timeout, why?)
It turns out that the IP being on different subnets is the culprit; that is, the IP of the host creating the WiFi and your phone and computer are not in the same local area network. So, next, manually set the IP of your computer and the host of the WiFi hotspot to be in the same subnet, and it works! However, do you want everyone who connects to your WiFi to manually set their IP? The key is that not everyone can set IPs proficiently, and customers are kings; it must allow the client’s networked terminals to automatically assign IP addresses.
dhcp – Dynamically Assigning IPs to Access Points
How can this be achieved? The answer is to start the DHCP service on our embedded Linux system. For information on the DHCP service, you can refer to the book by Bird Brother, which explains it quite clearly. The main purpose of this service is to automatically assign IPs that can communicate with your network to the terminals connecting to your network.
This command is located in the sbin/ directory, named udhcp. You can obtain the related commands by configuring busybox, and you also need to select support for this function in the kernel configuration.
So, what is the range for automatic IP assignment, and what is the maximum connection duration? Where are these settings? The answer is still in the configuration file, similar to hostapd.conf, so I won’t elaborate. There are many tutorials online about how to configure it.
Next, you can start the DHCP service. During the startup process, make sure to execute the ifconfig wlan0 up command, and also assign an IP to wlan0.
Next, execute:
udhcpd -fS /etc/udhcpd.conf
Then an error will report that a certain file is missing in a certain directory; simply create it using touch and continue executing the above command!
Next: The terminal will print that the udhcpd service has started: then it seems to have no response…
In fact, you are already successful!~ Connect your phone or computer to your WiFi hotspot, and you will see the IP assignment information printed.
Finally, start my smart home control system. By connecting your phone and computer to the WiFi hotspot, you can successfully see the homepage of the system. Oh yeah! The wireless server is running on the embedded Linux system just like that.
Now, you can control the switches of electrical appliances at home using your phone! If an app could be made, that would be even more impressive~~
Copyright Statement:This article is sourced from the internet, free to convey knowledge, and the copyright belongs to the original author. If there are any copyright issues, please contact me for deletion.
You May Also Like
Share an OTA upgrade-related application practice!
A key processing module for microcontrollers!
Practical | Sharing several very useful open-source projects
How to send and receive float data with STM32?
How to view the source code of Linux command tools?
Share a must-have drawing tool for embedded developers!
Reply 1024 in the WeChat public account chat interface to obtain embedded resources; reply m to view the article summary.