Raspberry Pi: How to set up WiFi

本文详细介绍了如何为Raspberry Pi配置无线网络,包括使用USB适配器的方法、检查设备识别、设置桌面环境下的无线连接以及命令行方式的网络配置步骤。

Raspberry Pi

Raspberry Pi

Although the Raspberry Pi Model B comes with built-in 100Mbps wired Ethernet, it can also use WiFi via a USB dongle. The Model A doesn’t come with Ethernet at all, so using a WiFi adapter is a good way to get networking on that model. In both cases, simply plugging in a supported USB dongle and doing a simple bit of configuration will give your Pi access to wireless.

You can get yourself a USB adapter for less than $10 but it is important to buy one which is supported by the Raspberry Pi and Linux. For this tutorial, I will be using a dongle based on the Realtek RTL8188CUS chipset. The Realtek chipset is supported by Raspbian out-of-the-box and no additional configuration is needed for it to be recognized during boot.

The RPi USB Wi-Fi Adapters page has a comprehensive list of supported wireless adapters. If you are considering buying a dongle, you should check that page for compatibility.

Plug in the USB adapter and boot your Raspberry Pi. There are several ways to check if the adapter has been recognized. The easiest is to type:

ifconfig

Raspberry_Pi_WiFi_Config_ifconfig

You should see a listing for eth0 – the built-in wired Ethernet port; for lo – the loop back device; and wlan0 – the wireless adapter.

Alternatively you can list the current USB devices attached to the Pi using:

sudo lsusb

Raspberry_Pi_WiFi_Config_lsusb

The list should contain your wireless dongle. On my setup, the list shows a “Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter”.


Desktop set up

If you are using the desktop on Raspbian than connecting to a wireless network is quite easy. Double click the “WiFi Config” icon on the desktop to start the wireless configuration program. The “Adapter:” field will show your USB dongle (i.e. wlan0) and the “Network:” field will be empty (unless you have previously configured a wireless network). Click the “Scan” button. Now click “Scan” on the “Scan results” window. A list of the available wireless networks will be shown along with their signal strengths etc. To connect to one of the listed networks, double click on the SSID.

Raspberry_Pi_WiFi_Config_scan

On the connection window, verify the “Authentication” and “Encryption” fields and enter the password in the “PSK” field. Now Click “Add”. Close the “Scan results” window. The configuration program will automatically connect to the newly configured network.

Raspberry_Pi_WiFi_Config_Add_network

You can add multiple networks by repeating the same steps. You can select the desired network using the “Network:” drop down list. You can also connect and disconnect using the relevant buttons. You can use the “Managed Networks” tab to edit and remove networks.


Command line set up

If you aren’t using the desktop then the WiFi can be configured using the command line. Raspbian should come with all the correct packages pre-installed but if any of the commands or files mentioned below aren’t available, then run this command to install them:

sudo apt-get install wpasupplicant wireless-tools

The general network settings are configured in “/etc/network/interfaces” while the Wi-Fi details are set in the “/etc/wpa_supplicant/wpa_supplicant.conf” file. First edit the “interfaces” file:

sudo nano /etc/network/interfaces

Ensure that the section about wlan0 (typically found at the end of the file) reads as follows:

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

If there are difference then change them to accordingly. Don’t alter any of the lines about the lo adapter or the eth0 adapter. Press “CTRL + X” to exit nano (press Y and then press ENTER when prompted).

To get a list of the currently available wireless networks, use the iwlist command:

sudo iwlist wlan0 scan

If there is too much information, use grep to find the fields you need. For example to see just the ESSIDs, use:

sudo iwlist wlan0 scan | grep ESSID

Raspberry_Pi_WiFi_Config_iwlist

Pick a network and add the network authentication information in the “wpa_supplicant.conf” file:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

The first two lines should already read:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

Now add the following:

network={
        ssid="YourSSID"
        psk="password"
        key_mgmt=WPA-PSK
}

If your router is configured using WEP for encryption then the network information will look like this:

network={
        ssid="YourSSID"
        wep_key0="password12345"
        key_mgmt=NONE
}

For those of you familiar with advanced WiFi configurations, the network information can also include the following fields:

  • proto – Protocol type can be: RSN (for WP2) and WPA (for WPA1).
  • pairwise – CCMP or TKIP (for WPA2 or WPA1).
  • auth_alg – authentication algorithm, can be OPEN for both WPA1/WPA2 and less commonly SHARED or LEAP.

Press “CTRL + X” to exit nano and save the file, press Y and then press ENTER when prompted. Finally reboot your Pi:

sudo reboot

You can check the status of the wireless connection using ifconfig (to see if wlan0 has acquired an IP address) and iwconfig to check which network the wireless adapter is using.

Raspberry_Pi_WiFi_Config_iwconfig


Reference

From: http://www.maketecheasier.com/setup-wifi-on-raspberry-pi/

为了将树莓派的状态数据上传到阿里云物联网平台,需要完成以下几个主要步骤: ### 1. 准备树莓派环境 首先确保树莓派的操作系统已经安装好,并且可以通过网络连接互联网。通常使用的是Raspberry Pi OS,可以通过以下命令更新系统并安装必要的工具: ```bash sudo apt update && sudo apt upgrade -y sudo apt install python3-pip ``` ### 2. 安装阿里云IoT SDK 阿里云提供了适用于Python的IoT设备SDK,可以使用pip安装: ```bash pip3 install aliyun-python-sdk-core pip3 install aliyun-python-sdk-iot ``` ### 3. 获取设备证书 在阿里云IoT平台上创建产品和设备后,可以获得设备的三元组信息(ProductKey, DeviceName, DeviceSecret)。这些信息是设备与云端通信的身份验证凭证。 ### 4. 编写上传状态数据的代码 使用阿里云IoT SDK编写Python脚本,通过MQTT协议将树莓派的状态数据发送到云端。以下是一个简单的示例代码: ```python from aliyunsdkcore.client import AcsClient from aliyunsdkiot.request.v20180120 import PubRequest # 初始化AcsClient实例 client = AcsClient('<your-access-key-id>', '<your-access-secret>', 'cn-shanghai') def publish_device_status(product_key, device_name, payload): request = PubRequest.PubRequest() request.set_ProductKey(product_key) request.set_DeviceName(device_name) request.set_Payload(payload.encode('utf-8')) # 将数据编码为字节 response = client.do_action_with_exception(request) return response # 示例调用 product_key = 'your_product_key' device_name = 'your_device_name' payload = '{"temperature": 25, "humidity": 60}' # 示例状态数据 response = publish_device_status(product_key, device_name, payload) print(response) ``` ### 5. 定期采集并上传数据 可以使用定时任务(如cron)或者在代码中加入循环来定期采集树莓派的状态数据并上传到阿里云IoT平台。 ### 6. 配置阿里云IoT平台 在阿里云IoT平台上配置产品属性和服务,确保能够正确接收和处理来自树莓派的数据。可以在平台上设置规则引擎,将数据转发到其他服务进行进一步处理。 通过以上步骤,树莓派就可以成功地将状态数据上传到阿里云物联网平台[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值