haas506开发教程-example-wifi定位
- 1.wifi定位
- 2.总结
1.wifi定位
- 案例说明
扫描周围所得到的wifi热点,使用mac进行定位 - mian.py
# coding=utf-8
#免注册api接口,仅供测试使用
#扫描周围的wifi,获取mac地址,然后再使用http.get()获取位置信息
import wifiScan
import utime as time
import http
import network
import ujson
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('network register failed')
while True:
if g_connect_status:
print('network register successed')
break
time.sleep_ms(20)
def get():
global htp
global mac
global mac_addr
for m in mac_addr:
# url = 'http://api.cellocation.com:82/wifi/?mac='+m+'&output=json'
url = 'http://api.cellocation.com:82/wifi/?mac='+m
ret = htp.get(url)
if ret < 0:
print('http get return failed %d'%ret)
return None
responsebody = htp.get_response()
print("responsebody:",responsebody)
result=ujson.loads(responsebody)
if result["errcode"]==0:
break
def wifi_scan():
global mac
global mac_addr
mac=""
mac_addr=[]
#查看平台是否支持wifi扫描
ret1=wifiScan.support()
print("ret1=",ret1)
#开启wifi扫描
ret2=wifiScan.control(1)
print("ret2=",ret2)
#获取wifi扫描参数
ret3=wifiScan.getConfig()
print("ret3=",ret3)
#设置wifi扫描参数
ret4=wifiScan.setConfig(5, 2, 13, 60, 0)
print("ret4=",ret4)
#开始扫描wifi
ret5=wifiScan.start()
print("ret5=",ret5)
#取mac地址
mac=ret5
print("mac:",mac)
for i in mac[1][::2]:
mac_addr.append(i)
print('mac_addr:',mac_addr)
if __name__=='__main__':
#实例化http
htp=http.client()
#连接网络
connect_network()
#扫描周围wifi,获取mac地址,作为定位参数
wifi_scan()
#get lbs
get()
- 日志输出
network register successed
ret1= True
ret2= 0
ret3= (0, 0, 0, 0, 0)
ret4= 0
ret5= (10, ['CC:C2:E0:AF:65:A5', -54, 'CE:C2:E0:9F:65:A5', 7, '92:DE:F9:EC:D3:40', -49, '8C:DE:F9:EC:D3:40', -49,
'B4:0F:3B:43:4C:71', -73, '48:0E:EC:89:52:8B', -72, '90:F0:52:14:93:33', -15, 'A8:A7:95:99:D8:47', -54,
'98:00:6A:B3:4B:AC', -50, '9A:00:6A:B3:4B:AC', -50])
mac: (10, ['CC:C2:E0:AF:65:A5', -54, 'CE:C2:E0:9F:65:A5', 7, '92:DE:F9:EC:D3:40', -49, '8C:DE:F9:EC:D3:40', -49,
'B4:0F:3B:43:4C:71', -73, '48:0E:EC:89:52:8B', -72, '90:F0:52:14:93:33', -15, 'A8:A7:95:99:D8:47', -54,
'98:00:6A:B3:4B:AC', -50, '9A:00:6A:B3:4B:AC', -50])
mac_addr: ['CC:C2:E0:AF:65:A5', 'CE:C2:E0:9F:65:A5', '92:DE:F9:EC:D3:40', '8C:DE:F9:EC:D3:40', 'B4:0F:3B:43:4C:71',
'48:0E:EC:89:52:8B', '90:F0:52:14:93:33', 'A8:A7:95:99:D8:47', '98:00:6A:B3:4B:AC', '9A:00:6A:B3:4B:AC']
responsebody: {"errcode": 0, "lat": "31.27669", "lon": "120.735703", "radius": "598",
"address": "江苏省苏州市吴中区西安交通大学B1号;文景路与松涛街路口东北436米"}
2.总结
本节介绍了如何使用wifi进行定位,需要注意的是,定位所用到的api接口仅供测试使用。