Friday, July 26, 2019

How NB-IoT module (SIM7000-C) work on Rasberry Pi and Ubuntu Server 18.04

最近study了一下NB-IoT,並且稍微筆記了一下如何實作。

不過可惜的是我手邊目前並沒有NB-IoT的Sim卡,所以使用normal SIM卡搭配SIM7000C這張module做了些練習。

以下是我的測試設備:



  • Rasberry Pi 2B
  • Waveshare SIM7000C NB-IoT HAT
  • CHT(中華電信) normal SIM
  • OS: Ubuntu Server 18.04

How to connect with CHT cell site via AT commands:

Connect to SIM7000C via USB-console:

$ sudo screen /dev/ttyUSB3 115200

Examine SIM7000C is ready to use

at

Check PIN status

at+cpin?
Ready: SIM is not pending for any password.

Configure APN(Access Point Name) automatically

at+cgdcont=1,”ip”,””
1: cid
ip: packet data protocol type 
“”: apn

Check signal

at+csq
+csq: rssi, ber
rssi:
0 -115 dBm or less
1 -111 dBm
2...30 -110... -54 dBm
31 -52 dBm or greater
99 not known or not detectable
ber:0...7 As RXQUAL values in the table in GSM 05.08 [20] subclause 7.2.4

Scan cell site:

It will take about 120 secs or more, and 46692 is CHT.
at+cops=?
+cops=?
(index,format,oper)
Index: Integer type: order number of operator in SIM preferred
Format: 3GPP MCC/MNC
MCC
MNC
ISP
466
11
中華電信 (CHT)
466
92
中華電信 (CHT)
466
93
台灣大哥大
466
97
台灣大哥大
466
99
台灣電信
466
03
遠傳電信
466
01
遠傳電信
466
88
遠傳電信
466
89
台灣之星
466
12
亞太電信
466
12
亞太電信
oper: String type(string should be included in quotation marks)

Number
Type
0
GSM
7
CAT-M1
9
NB-IoT

Connect to CHT cell site:

at+cops=1,2,”46692”

at+cgatt?

at+cpsi?
+cops=index,format,oper

Get APN

We need APN for connecting to the Internet.
It seems normal CHT SIM card is used internet as APN, and NB-IoT of CHT is internet.iot.
at+cgnapn
+cgnapn valid,APN
0: The network did not sent APN parameter to UE.In the case, is NULL.
1: The network sent APN parameter to UE.

Connect to the Internet

at+cstt=”internet”
+cstt APN,username,password

Initial mobile data network

at+ciicr

Check public IP address

at+cifsr

Test1: Ping testing 

at+cipping=”168.95.1.1”

Test2: TCP testing

at+cipstart=”tcp”,”ftp.isu.edu.tw”,21
+cipstart=mode,ip,port
Mode: tcp,udp

看到這裡相信很多人已經放棄了,不過請別氣餒其實有更簡單好用的方法唷!


Connect to the Internet via nmcli and mmcli

Issue: nmcli cannot manage interfaces


# nmcli c up id GSM

Error: Connection activation failed: No suitable device found for this connection.

Workaround:


# mv /usr/lib/NetworkManager/conf.d/10-globally-managed-devices.conf  /usr/lib/NetworkManager/conf.d/10-globally-managed-devices.conf_orig

# touch /usr/lib/NetworkManager/conf.d/10-globally-managed-devices.conf

# systemctl restart network-manager

Ensure that mmcli can recognize and scan cell site


# mmcli -m 0

 --------------------------------
  General  |     dbus path: /org/freedesktop/ModemManager1/Modem/0
           |            device id: fa2d54b8e3ad72c95e99f28ba51b4ce48565684b
  --------------------------------
  Hardware |         manufacturer: QUALCOMM INCORPORATED
           |                model: 0
           |             revision: MPSS.JO.3.0.2.c3-00140-9607_LTEONLY_PACK-1  1 [Feb 01 2018 07:00:00]
           |         h/w revision: 10000
           |            supported: gsm-umts, lte
           |              current: gsm-umts, lte
           |         equipment id: 865233030789763
  --------------------------------
  System   |       device: /sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3
           |              drivers: option1, qmi_wwan
           |               plugin: SimTech
           |         primary port: cdc-wdm0
           |                ports: ttyUSB0 (qcdm), ttyUSB2 (at), cdc-wdm0 (qmi), wwan0 (net), 
           |                       ttyUSB3 (at)
  --------------------------------
  Status   |         lock: sim-pin2
           |       unlock retries: sim-pin (3), sim-pin2 (3), sim-puk (10), sim-puk2 (10)
           |                state: registered
           |          power state: on
           |          access tech: lte
           |       signal quality: 94% (recent)
  --------------------------------
  Modes    |     supported: allowed: 2g; preferred: none
           |                       allowed: 4g; preferred: none
           |                       allowed: 2g, 4g; preferred: 4g
           |                       allowed: 2g, 4g; preferred: 2g
           |              current: allowed: 2g, 4g; preferred: 4g
  --------------------------------
  Bands    |     supported: egsm, dcs, pcs, g850, eutran-1, eutran-3, eutran-5, eutran-8
           |              current: egsm, dcs, pcs, g850, eutran-1, eutran-3, eutran-5, eutran-8
  --------------------------------
  IP       |     supported: ipv4, ipv6, ipv4v6
  --------------------------------
  3GPP     |         imei: 865233030789763
           |          operator id: 46692
           |        operator name: 466 92
           |         registration: home
  --------------------------------
  3GPP EPS | ue mode of operation: csps-2
  --------------------------------
  SIM      |     dbus path: /org/freedesktop/ModemManager1/SIM/0



# mmcli -m 0 --3gpp-scan --timeout=120

  ---------------------
  3GPP scan | networks: 46612 - 466 12 (lte, available)
            |           46692 - 466 92 (lte, current)
            |           46605 - 466 05 (gsm, forbidden)

Connect to the Internet with nmcli


# nmcli c add type gsm ifname "*" con-name GSM apn "internet"

Connection 'GSM' (e79462c8-d02d-4ae0-b3f4-a08db28087af) successfully added.

# nmcli c up id GSM

相信這個方法簡單了許多吧!不過了解一下底層的AT command如何運作也是相當不錯的機會啦!以下是AT command完整的說明文件: http://www.waveshare.net/w/upload/4/49/SIM7000_Series_AT_Command_Manual_V1.02.pdf

Appendix:

AT+CMNB Preferred selection between CAT-M and NB-IoT.

+cmnb: mode
1 LTE CAT-M
2 NB-IoT
3 CAT-M and NB-IoT

SIM7000C + pppd撥接上網

  1. To remove modemmanager firstly, otherwise ppp0 connection will be terminated by it.
$ sudo apt remove modemmanager

  1. To install ppp
$ sudo apt install ppp

  1. Configured ppp script for adding some AT commands.
$ cd /etc/ppp
$ vi gprs-connect-chat

TIMEOUT 15
ABORT "DELAYED"
ABORT "BUSY"
ABORT "ERROR"
ABORT "NO DIALTONE"
ABORT "NO CARRIER"
TIMEOUT 40
'' \rat
OK at+cstt=\"internet\"
OK at+cgatt?
OK at+cpsi?
OK at+ciicr
OK ATD*99***1#
CONNECT

  1.  Configured ppp dial-in configuration file:
$ cd /etc/ppp/peers
$ vi gprsdial

/dev/ttyUSB3
115200
crtscts
modem
debug
nodetach
usepeerdns
noipdefault
defaultroute
0.0.0.0:0.0.0.0
connect '/usr/sbin/chat -s -v -f /etc/ppp/gprs-connect-chat'

  1. Added “noauth” parameter in /etc/ppp/options
  2. Plug-in SIM7000C.
  3. To execute pppd call 
$ sudo pppd call gprsdial

root@Lawrence-ThinkPad-T420S:/etc/ppp/peers# pppd call gprsdial 
timeout set to 15 seconds
abort on (DELAYED)
abort on (BUSY)
abort on (ERROR)
abort on (NO DIALTONE)
abort on (NO CARRIER)
timeout set to 40 seconds
send (^Mat^M)
expect (OK)
^M
DST: 0^M
^M
*PSUTTZ: 19/06/26,08:14:58","+32",0^M
at^M^M
OK
 -- got it

send (at+cstt=\"internet\"^M)
expect (OK)
^M
at+cstt="internet"^M^M
OK
 -- got it

send (at+cgatt?^M)
expect (OK)
^M
at+cgatt?^M^M
+CGATT: 1^M
^M
OK
 -- got it

send (at+cpsi?^M)
expect (OK)
^M
at+cpsi?^M^M
+CPSI: LTE NB-IOT,Online,466-92,0xC8C9,54027119,280,EUTRAN-BAND8,3640,0,0,-9,-59
,-50,20^M
^M
OK
 -- got it

send (at+ciicr^M)
expect (OK)
^M
at+ciicr^M^M
OK
 -- got it

send (ATD*99***1#^M)
expect (CONNECT)
^M
ATD*99***1#^M^M
CONNECT
 -- got it

Script /usr/sbin/chat -s -v -f /etc/ppp/gprs-connect-chat finished (pid 2813), status = 0x0
Serial connection established.
using channel 12
Using interface ppp0
Connect: ppp0 <--> /dev/ttyUSB3
sent [LCP ConfReq id=0x1 ]
rcvd [LCP ConfReq id=0x0 ]
sent [LCP ConfNak id=0x0 ]
rcvd [LCP ConfAck id=0x1 ]
rcvd [LCP ConfReq id=0x1 ]
sent [LCP ConfAck id=0x1 ]
sent [LCP EchoReq id=0x0 magic=0x640f0536]
sent [PAP AuthReq id=0x1 user="Lawrence-ThinkPad-T420S" password=]
rcvd [LCP DiscReq id=0x2 magic=0xd41b8a15]
rcvd [LCP EchoRep id=0x0 magic=0xd41b8a15 64 0f 05 36]
rcvd [PAP AuthAck id=0x1 ""]
PAP authentication succeeded
sent [CCP ConfReq id=0x1 ]
sent [IPCP ConfReq id=0x1 ]
rcvd [LCP ProtRej id=0x3 80 fd 01 01 00 0f 1a 04 78 00 18 04 78 00 15 03 2f]
Protocol-Reject for 'Compression Control Protocol' (0x80fd) received
rcvd [IPCP ConfReq id=0x0]
sent [IPCP ConfNak id=0x0 ]
rcvd [IPCP ConfRej id=0x1 ]
sent [IPCP ConfReq id=0x2 ]
rcvd [IPCP ConfReq id=0x1]
sent [IPCP ConfAck id=0x1]
rcvd [IPCP ConfNak id=0x2 ]
sent [IPCP ConfReq id=0x3 ]
rcvd [IPCP ConfAck id=0x3 ]
Could not determine remote IP address: defaulting to 10.64.64.64
not replacing default route to enp0s25 [10.101.47.1]
local  IP address 10.169.25.71
remote IP address 10.64.64.64
primary   DNS address 168.95.1.1
secondary DNS address 168.95.192.1
Script /etc/ppp/ip-up started (pid 2824)
Script /etc/ppp/ip-up finished (pid 2824), status = 0x0

$ ifconfig ppp0
ppp0: flags=4305  mtu 1500
        inet 10.169.25.71  netmask 255.255.255.255  destination 10.64.64.64
        ppp  txqueuelen 3  (Point-to-Point Protocol)
        RX packets 9  bytes 382 (382.0 B)
        RX errors 0  dropped 0 overruns 0  frame 0
        TX packets 18  bytes 1385 (1.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0 collisions 0

$ ping 168.95.1.1 -I ppp0 -c 3
PING 168.95.1.1 (168.95.1.1) from 10.169.121.115 ppp0: 56(84) bytes of data.
64 bytes from 168.95.1.1: icmp_seq=1 ttl=248 time=292 ms
64 bytes from 168.95.1.1: icmp_seq=2 ttl=248 time=460 ms
64 bytes from 168.95.1.1: icmp_seq=3 ttl=248 time=267 ms

--- 168.95.1.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 267.265/340.248/460.987/85.994 ms

Friday, June 21, 2019

初探WSL


What is WSL?

白話來說就是在Windows上執行Linux啦~ 但是比起一般的虛擬機器來說使用上會更方便,簡單,快速。畢竟這是Windows自己就打算支持的功能嘛。

WSL是微軟與Canonical合作開發並於2016年正式發表,它運作的原理就是在Windows系統裡擔任上層Linux application與Windows NT kernel之間翻譯的橋樑,看下圖不難理解吧?




What can WSL do?

WSL除了可以像虛擬機器讓使用者在Windows上使用Linux外,它主要的賣點應該是可以讓Windows與Linux無縫整合在一起,也就是說在Windows cmd下可以使用Linux的命令,比如:find, awk, sed, grep ....., etc,如果有需要還可以透過package management(apt or yum) 去安裝需要的軟體,更好玩的是你可以在Windows上面運行X window,讓WSL執行運算,然後Windows負責顯示。

Requirements

1. 安裝64 bit的Windows10 (Windows 2019似乎也支援)

2. 版本等於或大於1607


Installing WSL

4個步驟即可安裝跟配置好!
1. Open a new PowerShell as Administrator.

2. At the PowerShell prompt, run the following command:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

3. Reboot Windows.

4. To install Linux distros from Windows Store.



Demonstrate - Getting started Ubuntu 

安裝完後,不用懷疑直接點選Ubuntu的icon就可以運行Linux。


Demonstrate - execute Linux tools 

之前說到在Windows下無縫接軌使用Linux command的方法有三種:
1. wsl [Linux command]

2. bash -c “Linux command”

3. ubuntu1804 run [Linux command]


上面這個例子就是在Windows cmd下使用Linux的ifconfig, grep, awk去過濾出系統的IPv4 addresses.


Demonstrate - X window

簡單三步驟讓你的Windows搖身一變成X window.
1. 下載Xming for Windows10.

2. 開啟Ubuntu並透過apt安裝常用的desktop applications.
$ sudo apt install ubuntu-desktop

3. 在Ubuntu裡把X window display設定成本機127.0.0.1:0
$ export DISPLAY=127.0.0.1:0


以上可以看到我可以在Windows底下透過Ubuntu使用xeyes, Firefox, Thunderbird, GIMP。



WSL2 is coming
剛剛所介紹的都是WLS1,微軟近期將打算推出WLS2並且號稱比WSL1會有更好的效能改進,主要的原因是它真的在Windows上運行Linux kernel! 而且第一個版本的kernel會是4.19!

WSL2可以說是整個架構的翻新( 參考下圖),它不在只是擔任翻譯的角色而是真正的Linux kernel,可以預期有更好的支援性,比如你將有機會在Windows下透過WLS2執行snap, docker, Kubernetes等等。

將來還可以透過Windows update來更新Linux kernel,微軟真的從此愛上了Linux嗎?讓我們繼續看下去~

參考:

Tuesday, June 11, 2019

如何在Ubuntu 18.04定期做資料備份到Google drive

上星期放連假,看到了一篇文章介紹如何在Ubuntu 18.04下透過內建的backup軟體將本機資料定期備份到Google drive,有興趣的朋友可以參考以下的連結來實做看看:
https://vitux.com/how-to-schedule-file-backups-to-google-drive-on-ubuntu/
主要的步驟就是先在Ubuntu Online Accounts註冊好你的Google account。(Google預設有免費15GB的使用空間),並且記得Files一定要選到。



















接著打開Nautilus window,你應該會看到你的Google account會出現在側邊欄,請記得點選它。(感覺如同去mount一個USB或外接硬碟之類的)














這時候系統應該可以成功存取Google drive,然後參考上邊的連結去設定Ubuntu backup tool。

如果將來需要restore資料的話,一樣可透過Ubuntu backup tool執行restore的動作。

Step1. 點選Restore button.









Step2. 從Google drive來restore.












Step3. 從哪一天的資料restore.












Step4. 選擇要restore到本機哪個資料夾。


Wednesday, May 29, 2019

測試網路下載速度的小工具

在Ubuntu下若想要簡單測試目前Internet下載速度的話,可以用Netflix出的一個小軟體fast來測試。

安裝:
$ snap install fast

執行
$ fast















就是這麼簡單。

Monday, May 13, 2019

Canonical QA offsite meeting之Python 練習

5月初部門舉辦了一次offsite meeting,主題是學習Python。

內部同事兼講師,教材準備得很好,上課內容又豐富,所以當然要把回家作業完成。

Exercise 1

Write a function that takes a string and returns the inverted string (e.g. Taiwan → nawiaT). This function must use a for loop.

Exercise 2

Same as exercise 1, but using the easiest/shortest way (hint: the answer is somewhere in the previous sections.

Exercise 3

Dictionary containing all Ubuntu releases. The key is the release number (e.g. “18.04”) and the value is the release codename (e.g. “Bionic Beaver”):

  • Using Python code:return the release codenames for versions released in October
  • find how many releases were released neither in April nor October
  • return the version number for versions whose codename contains a “k”

Exercise 4

Write a function that reads the content of a text file and returns a dictionary with the number of occurrences for each letter of the alphabet

以下是我寫的code:

Exercise 1

def revert(word):
    for i in range(len(word)-1, -1, -1):
        print(word[i],end="")

revert("I love Taiwan")

Exercise 2

string="Taiwan"
print(string[::-1])

Exercise 3

Ubuntu={ "4.10": "Warty Warthog", "5.04": "Hoary Hedgehog", "5.10": "Breezy Badger", "6.06": "Dapper Drake", "6.10": "Edgy Eft", "7.04": "Feisty Fawn", "7.10": "Gutsy Gibbon", "8.04": "Hardy Heron", "8.10": "Intrepid Ibex", "9.04": "Jaunty Jackalope", "9.10": "Karmic Koala", "10.04": "Lucid Lynx", "10.10": "Maverick Meerkat", "11.04": "Natty Narwhal", "11.10": "Oneiric Ocelot", "12.04": "Precise Pangolin", "12.10": "Quantal Quetzal", "13.04": "Raring Ringtail", "13.10": "Saucy Salamander", "14.04": "Trusty Tahr", "14.10": "Utopic Unicorn", "15.04": "Vivid Vervet", "15.10": "Wily Werewolf", "16.04": "Xenial Xerus", "16.10": "Yakkety Yak", "17.04": "Zesty Zapus", "17.10": "Artful Aardvark", "18.04": "Bionic Beaver", "18.10": "Cosmic Cuttlefish", "19.04": "Disco Dingo" } for number in Ubuntu.keys(): if number.split(".")[1]=="10": print("OCT=",Ubuntu[number]) for number in Ubuntu.keys(): if number.split(".")[1]!="10" and number.split(".")[1]!="04": print("Neither in APR or OCT:",Ubuntu[number]) for codename in Ubuntu.values(): if "k" in codename.lower(): print("Codename contains a K or k:",codename)

Exercise 4

LIST=[]
for w in range(ord('a'), ord('z') + 1):
    LIST.append(chr(w))

ACCOUNT=[]
for i in range(0,26):
    ACCOUNT.append(0)

with open('/usr/share/doc/python3/copyright','r') as f:
    for alphabet in f.read():
        for i in range(0,26):
            if alphabet.lower()==LIST[i]:
                ACCOUNT[i]=ACCOUNT[i]+1
                break


dictionary=dict(zip(LIST,ACCOUNT))
print(dictionary)

老實說我有一段時間沒寫程式了,但稍微學習了一下Python後,它的語法很簡潔好懂,果然是受歡迎的程式語言之一呀!

Wednesday, May 08, 2019

在Ubuntu 18.04 Server 快速架設FreeRADIUS Server

比起以前在Linux架設FreeRADIUS server的難度與花費的時間,目前在Ubuntu安裝並配置FreeRADIUS server真的是輕鬆了許多。廢話不多說,以下筆記如何在Ubuntu Server 18.04安裝並且配置一台可以使用的FreeRADIUS server。

環境:
Ubuntu Server 18.04.02 LTS















安裝:
# apt install freeradius















配置:
1. 建立使用者帳號:
先切換到FreeRADIUS server的目錄,然後開啟檔案users。
# cd /etc/freeradius/3.0
# vi users
















這裡我將套用一個內建的帳號bob:















修改clients.conf: (這個檔案主要是限制哪些client可以使用FreeRADIUS server)
# vi clients.conf
















以上我新增了於subnet 10.101.47.0/24的client都可以使用FreeRADIUS server,並且secret是testing123。

重新啟動FreeRADIUS server:
# systemctl restart freeradius
# systemctl status freeradius



















從遠端機器驗證:
$ radtest bob hello 10.101.47.37 1812 testing123

bob -> 帳號
hello -> 密碼
10.101.47.37 -> FreeRADIUS server's IP
1812 -> FreeRADIUS server's port (認証用)
testing123 -> FreeRADIUS server's secret



















一個FreeRADIUS server到這邊已經可以正常運行在Ubuntu Server 18.04,有空在跟大家介紹如何讓FreeRADIUS支援802.1X或是結合mysql儲存使用者的帳號密碼。

Friday, April 12, 2019

移除一些舊版的snap packages來增加disk space

找到一篇不錯的文章,可以幫助user去移除一些也許已經用不到的舊版snap packages,這點對我來說蠻實用的,因為我的系統透過snap裝了蠻多軟體,而且預設上snap會針對每個軟體同時存著三個版本(如果有軟體本身有升級過好幾次),好處是萬一將來發現升級上來的軟體有bug,之後可以馬上revert package,但缺點就是比較浪費硬碟空間。(你可以想像系統同時間裝了三份相同的軟體)













比較一下執行script前與後的使用空間:
Before:












After:












真的差蠻多的~ 萬一系統空間不足時,建議可以先清理一下snap舊版的軟體。