haas506 2.0开发教程-高级组件库-modem
1.获取与设备相关的信息
案例说明:
- 使用modem、network、net模块获取与设备有关的信息(插入SIM卡)
main.py
import network
import modem
import net
import utime as time
g_connect_status = False
def on_4g_cb(args):
global g_connect_status
pdp = args[0]
netwk_sta = args[1]
if netwk_sta == 1:
g_connect_status = True
else:
g_connect_status = False
def connect_network():
global Net,on_4g_cb,g_connect_status
Net = network.NetWorkClient()
g_register_network = False
if Net._stagecode is not None and Net._stagecode == 3 and Net._subcode == 1:
g_register_network = True
else:
g_register_network = False
if g_register_network:
Net.on(1,on_4g_cb)
Net.connect(None)
else:
print('网络注册失败')
while True:
if g_connect_status:
print('网络连接成功')
break
time.sleep_ms(20)
if __name__=="__main__":
connect_network()
print('网络连接状态:',g_connect_status)
print("获取当前网络状态(3代表连接成功):",Net.getStatus())
print("_stagecode(3代表成功):",Net._stagecode)
print("_subcode(1代表成功):",Net._subcode)
print("网络类型(0:wifi,1:蜂窝网,2:以太网,3:未知):",Net.getType())
print('设备IMEI:',modem.getDevImei())
print("设备模组:",modem.getDevModel())
print("设备固件版本",modem.getDevFwVersion())
print("设备id:",modem.getDevProductId())
print("ICCID:",modem.sim.getIccid())
print("IMSI:",modem.sim.getImsi())
print("信号强度:",net.csqQueryPoll())
日志输出
网络连接状态: True
获取当前网络状态(3代表连接成功): 3
_stagecode(3代表成功): 3
_subcode(1代表成功): 1
网络类型(0:wifi,1:蜂窝网,2:以太网,3:未知): 1
设备IMEI: 867082058707142
设备模组: M601
设备固件版本 amp-v1.16 build on 2021-11-17, 15:18:23
设备id: HaaS506
ICCID: 89860440112170090087
IMSI: 460046042210087
信号强度: 31
2.发短信
- 注意: ‘yourPhoneNumber’ 需要改成通话号码(字符串格式)
main.py
from modem import sms
import utime as time
def sms_cb(args):
ind_flag = args[0]
mes_buf = args[1]
mes_num = args[2]
print('new message:ind_flag:{},mes_buf:{},mes_num:{}'.format(ind_flag,mes_buf,mes_num))
if __name__ == '__main__':
smss = sms()
time.sleep(5)
smss.setCallback(sms_cb)
smss.sendTextMsg('yourPhoneNumber','welcome to use haas506','GSM')
smss.sendTextMsg('yourPhoneNumber','欢迎使用haas506','UCS2')
效果图-发送 效果图-接收 注意:用手机给haas506发短信时,串口助手不一定能显示中文,此时有两种解决方法:(1)换一个可以设置中文显示的串口助手。(2)将串口日志保存一下,打开日志文件,就会显示中文字符。
3. 打电话
main.py
from modem import voiceCall
from audio import Audio
def voice_callback(args):
if args[0] == 10:
print('voicecall incoming call, PhoneNO.: ', args[6])
elif args[0] == 11:
print('voicecall connected, PhoneNO.: ', args[6])
elif args[0] == 12:
print('voicecall disconnect')
elif args[0] == 13:
print('voicecall is waiting, PhoneNO.: ', args[6])
elif args[0] == 14:
print('voicecall dialing, PhoneNO.: ', args[6])
elif args[0] == 15:
print('voicecall alerting, PhoneNO.: ', args[6])
elif args[0] == 16:
print('voicecall holding, PhoneNO.: ', args[6])
if __name__ == '__main__':
vc = voiceCall()
ad = Audio()
ad.set_pa()
ad.setVolume(10)
vc.setCallback(voice_callback)
vc.setAutoAnswer(5000)
vc.callStart('phonenumber')
4.Class-modem
modem
getDevImei | getDevModel | getDevProductID | getDevFwVersion | reboot | sim | voiceCall | sms |
---|
获取设备的IMEI | 获取设备model | 获取设备id | 获取设备固件版本号 | 设备重启 | | 打电话 | 实现短信发送功能 |
modem.sim()
getImsi | getIccid | setSimDet | getSimDet |
---|
获取IMSI | 获取ICCID | 配置SIM卡热插拔相关参数 | 获取SIM卡热插拔相关配置参数 |
modem.voiceCall()
setAutoAnswe | callStart | callAnswer | callEnd | setCallback |
---|
设置自动应答时间 | 拨打电话 | 接听电话 | 挂断电话 | 设置监听回调函数 |
modem.sms()
setCallbacks | sendTextMsg |
---|
设置接收短信回调函数 | 实现短信发送功能 |
modem
- modem.getDevImei()
- 作用:获取设备的IMEI(国际移动设备识别码)
- 返回:IMEI
- modem.getDevModel()
- modem.getDevProductId()
- modem.getDevFwVersion()
- modem.reboot()
- modem.sim()
- 实例化:s=modem.sim()
- s.getImsi()
- 作用:获取IMSI(国际移动用户识别码)
- 返回:IMSI
- s.getIccid()
- 作用:获取ICCID(集成电路卡识别码)
- 返回:ICCID
- s.setSimDet(detenable, insertlevel)
- 作用: 设置SIM卡热插拔相关配置
- 参数: detenable-开启或者关闭SIM卡热插拔功能,0:关闭 1:打开;insertlevel-高低电平配置(0/1)
- 返回:0成功
- s.getSimDet()
- 作用: 获取SIM卡热插拔相关配置
- 返回:一个元组数据,即(detenable,detenable)
- modem.sms()
- 实例化:smss=modem.sms()
- smss.setCallbacks()
- smss.sendTextMsg()
- modem.voiceCall()
- 实例化:vc=modem.voiceCall()
- vc.setCallback()
- vc.setAutoAnswer(ms)
- 作用: 设置自动应答时间
- 参数: 自动应答时间,单位ms
- vc.callStart()
- vc.callAnswer()
- vc.callEnd()
5.总结
本节介绍了如何使用haas506高级组件库中的sms、voiceCall、sim模块,实现了获取设备信息、发送短信、拨打电话功能。当前版本不支持sim卡的热插拔功能。
|