树莓派3G无线网卡拨号上网

栏目: 服务器 · 发布时间: 7年前

内容简介:树莓派3G无线网卡拨号上网

树莓派对常见型号的无线上网卡支持非常完善,基本可以做到即插即用,这里我使用的3G模块为SIM5218,支持WCDMA/GSM,即联通2G/3G,移动2G,测试卡为中国联通SIM卡。

内核打印信息中3G模块自动识别,能看到 ttyUSB0-4 即说明驱动加载成功

pi@raspberrypi:~ $ lsusb
    Bus 001 Device 005: ID 05c6:9000 Qualcomm, Inc. SIMCom SIM5218 modem

pi@raspberrypi:~ $ dmesg
    [   19.612188] usbcore: registered new interface driver usbserial
    [   19.612317] usbcore: registered new interface driver usbserial_generic
    [   19.612439] usbserial: USB Serial support registered for generic
    [   19.722022] usbcore: registered new interface driver option
    [   19.722206] usbserial: USB Serial support registered for GSM modem (1-port)
    [   19.723265] option 1-1.4:1.0: GSM modem (1-port) converter detected
    [   20.077635] EXT4-fs (mmcblk0p2): re-mounted. Opts: (null)
    [   20.136701] usbcore: registered new interface driver cdc_wdm
    [   20.145665] usb 1-1.4: GSM modem (1-port) converter now attached to ttyUSB0
    [   20.146191] option 1-1.4:1.1: GSM modem (1-port) converter detected
    [   20.188948] usb 1-1.4: GSM modem (1-port) converter now attached to ttyUSB1
    [   20.191974] option 1-1.4:1.2: GSM modem (1-port) converter detected
    [   20.248902] usb 1-1.4: GSM modem (1-port) converter now attached to ttyUSB2
    [   20.250694] option 1-1.4:1.3: GSM modem (1-port) converter detected
    [   20.288505] usb 1-1.4: GSM modem (1-port) converter now attached to ttyUSB3
    [   20.289022] option 1-1.4:1.4: GSM modem (1-port) converter detected
    [   20.300976] usb 1-1.4: GSM modem (1-port) converter now attached to ttyUSB4
    [   20.336944] usbcore: registered new interface driver qmi_wwan

pi@raspberrypi:~ $ ls /dev/ttyUSB*
    /dev/ttyUSB0  /dev/ttyUSB1  /dev/ttyUSB2  /dev/ttyUSB3  /dev/ttyUSB4

接下来确定操作的串口,这就需要一个一个的去尝试,寻找对每个 AT 命令都能正常返回的串口,如果后面遇到串口卡死不能用的情况,建议换个串口试试,例如在后面执行 pppd 拨号过程中, ttyUSB2 一旦拨号便无法继续使用,必须重新插拔才能回复正常,而 ttyUSB3 就很正常

至于为什么要生成这么多 USB 串口,我想是多个进程访问串口的需要,一旦 ppp 拨号成功,其他进程便无法 /dev/ttyUSB3 的资源,这时还想获取信息必须使用 /dev/ttyUSB2

拨号

ppp

编译安装ppp

wget https://download.samba.org/pub/ppp/ppp-2.4.5.tar.gz
tar zxvf ppp-2.4.5.tar.gz
cd ppp-2.4.5/
./configure --prefix=/usr/local/ppp
make && make install
sudo ln -s /usr/sbin/pppd -> /usr/local/ppp/sbin/pppd
sudo ln -s /usr/sbin/chat -> /usr/local/ppp/sbin/chat

新建配置文件

sudo mkdir -p /etc/ppp/peer
sudo touch /etc/ppp/options
$ cat /etc/ppp/peers/wcdma

#set debug ,send message to /var/log/messages
debug
#To keep pppd on the terminal
nodetach
lock
#set seriral
/dev/ttyUSB3
# set baudrate
115200
user "card"
password "card"
# set flowdate
crtscts
show-password
usepeerdns
noauth
noipdefault
novj
novjccomp
noccp
# Accept the peer's idea of our local IP address
ipcp-accept-local
# ipcp-accept-remote
ipcp-accept-remote
connect '/usr/sbin/chat -s -v -f /etc/ppp/peers/wcdma-chat-connect'
disconnect '/usr/sbin/chat -s -v -f /etc/ppp/peers/wcdma-chat-disconnect'

设置拨号脚本

$ cat /etc/ppp/peers/wcdma-chat-connect

ABORT 'NO CARRIER'
ABORT 'ERROR'
ABORT 'NO DIALTONE'
ABORT 'BUSY'
ABORT 'NO ANSWER'
'' \rAT
OK \rATZ
#这里3gnet是联通3g的接入点
OK \rAT+CGDCONT=1,"IP","3gnet",,0,0
#前坑高能 ATDT拨号命令在不同的模块可能有差异 一定注意看手册
OK-AT-OK ATDT*99#
CONNECT \d\c

拨号错误处理

$ cat /etc/ppp/peers/wcdma-chat-disconnect

ABORT "ERROR"
ABORT "NO DIALTONE"
SAY "\nSending break to the modem\n"
'' "\K"
'' "+++ATH"
SAY "\nGoodbay\n"

拨号上网

$ sudo pppd call wcdma

    abort on (NO CARRIER)
    abort on (ERROR)
    abort on (NO DIALTONE)
    abort on (BUSY)
    abort on (NO ANSWER)
    send (^MAT^M)
    expect (OK)
    AAT^M^M
    OK
    -- got it
    send (^MATZ^M)
    expect (OK)
    ^M
    ATZ^M^M
    OK
    -- got it
    send (^MAT+CGDCONT=1,"IP","3gnet",,0,0^M)
    expect (OK)
    ^M
    AT+CGDCONT=1,"IP","3gnet",,0,0^M^M
    OK
    -- got it
    send (ATDT*99#^M)
    expect (CONNECT)
    ^M
    ATDT*99#^M^M
    CONNECT
    -- got it
    send (\d)
    Serial connection established.
    Using interface ppp0
    Connect: ppp0 <--> /dev/ttyUSB3
    CHAP authentication succeeded
    CHAP authentication succeeded
    Could not determine remote IP address: defaulting to 10.64.64.64
    local  IP address 10.140.231.235
    remote IP address 10.64.64.64
    primary   DNS address 218.104.111.114
    secondary DNS address 218.104.111.122

分配完IP地址的各种配置即拨号成功,也自动添加了 ppp0 接口

pi@raspberrypi:~ $ ifconfig ppp
ppp0      Link encap:Point-to-Point Protocol  
          inet addr:10.140.231.235  P-t-P:10.64.64.64  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:5 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3 
          RX bytes:55 (55.0 B)  TX bytes:80 (80.0 B)

拨号拿到的 DNS 只是在 /etc/ppp/resolv.conf ,必须链接到全局 DNS 才能域名访问

pi@raspberrypi:~ $sudo mv /etc/resolv.conf /etc/resolv.conf.bak
pi@raspberrypi:~ $sudo ln -s /etc/ppp/resolv.conf /etc/resolv.conf
pi@raspberrypi:~ $ cat /etc/resolv.conf

    nameserver 218.106.127.114
    nameserver 218.104.111.122

另外路由配置也要更改,调整树莓派的默认网关为 ppp 连接的另一端地址

pi@raspberrypi:~ $ sudo route add default gw 10.64.64.64
pi@raspberrypi:~ $ route -n
Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         10.64.64.64     0.0.0.0         UG    0      0        0 ppp0
    0.0.0.0         192.168.0.1     0.0.0.0         UG    202    0        0 eth0
    0.0.0.0         192.168.0.1     0.0.0.0         UG    303    0        0 wlan0
    10.64.64.64     0.0.0.0         255.255.255.255 UH    0      0        0 ppp0
    192.168.0.0     0.0.0.0         255.255.255.0   U     202    0        0 eth0
    192.168.0.0     0.0.0.0         255.255.255.0   U     303    0        0 wlan0

测试

查看公网IP

pi@raspberrypi:~ $ curl cip.cc
IP    : 113.57.250.187
地址    : 中国  湖北省  武汉市
运营商    : 联通

数据二    : 湖北省武汉市 | 联通

URL    : http://www.cip.cc/113.57.250.187

ping测试

pi@raspberrypi:~ $ ping sfantree.com -c 4
PING sfantree.com (123.56.22.253) 56(84) bytes of data.
64 bytes from 123.56.22.253: icmp_seq=1 ttl=50 time=108 ms
64 bytes from 123.56.22.253: icmp_seq=2 ttl=50 time=97.1 ms
64 bytes from 123.56.22.253: icmp_seq=3 ttl=50 time=105 ms
64 bytes from 123.56.22.253: icmp_seq=4 ttl=50 time=144 ms

--- sfantree.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 97.181/114.013/144.633/18.166 ms

路由跟踪

pi@raspberrypi:~ $ traceroute sfantree.com
traceroute to sfantree.com (123.56.22.253), 30 hops max, 60 byte packets
 1  * * *
 2  * * *
 3  211.91.153.5 (211.91.153.5)  135.674 ms  135.721 ms  135.593 ms
 4  211.91.152.102 (211.91.152.102)  135.521 ms 113.57.249.186 (113.57.249.186)  134.105 ms 211.91.152.138 (211.91.152.138)  143.938 ms
 5  211.91.152.209 (211.91.152.209)  153.055 ms  163.428 ms  153.207 ms
 6  * * *
 7  124.65.194.18 (124.65.194.18)  67.194 ms 124.65.194.22 (124.65.194.22)  76.475 ms 124.65.194.30 (124.65.194.30)  86.204 ms
 8  * * *
 9  61.49.137.14 (61.49.137.14)  79.101 ms  79.579 ms  89.555 ms
10  123.56.34.22 (123.56.34.22)  148.611 ms 123.56.34.6 (123.56.34.6)  129.153 ms 123.56.34.22 (123.56.34.22)  89.278 ms
11  * * *
12  * * 123.56.34.66 (123.56.34.66)  715.710 ms
13  * * *
14  123.56.22.253 (123.56.22.253)  816.901 ms  815.764 ms  817.479 ms

ssh

学校网今天断网了,正好利用树莓派连上云主机更新博客,你能看到这篇文章就说明ssh测试成功…


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learning Processing

Learning Processing

Daniel Shiffman / Morgan Kaufmann / 2008-08-15 / USD 49.95

Book Description Teaches graphic artists the fundamentals of computer programming within a visual playground! Product Description This book introduces programming concepts in the context of c......一起来看看 《Learning Processing》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

html转js在线工具
html转js在线工具

html转js在线工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具