简介
5.8G雷达感应传感器模块可以用来检测人体的存在/移动。使用传感器检测人体的移动,来控制灯光的亮灭。(使用人体热释电红外传感器也可以实现)
准备
本案例需要的硬件
器材 | 数量 |
---|
HaaS506开发板 | 1 | 5.8G雷达感应传感器模块 | 1 | SIM卡 | 1 | 杜邦线 | 若干 |
硬件连接图
代码流程
1、连接阿里云平台。 2、传感器检测人体活动,控制灯光开关,并将灯光状态上传阿里云。 3、设置阿里云远端控制灯光亮灭。
功能实现
1、物联网平台开发
第一次使用物联网平台的读者,需要开通实例后使用物联网平台功能。也可以使用免费的公共实例进行开发,在阿里云物联网平台中,左上角选择‘华东2-上海’,点击‘公共实例’,即可开通。
1、平台产品创建可参考haas506 2.0开发教程-aliyunIoT 2、创建产品属性(添加物模型) 选择产品功能定义–编辑草稿 添加自定义功能 设置标识符、数据类型、读写类型参数,标识符(light)要与代码保持一致。点击确定。
- 发布上线,点击确定。
2、设备端开发
main.py
from driver import GPIO
import network
import ujson
import utime as time
import modem
from aliyunIoT import Device
import kv
def on_connect(data):
global module_name,default_ver,productKey,deviceName,deviceSecret,on_trigger,on_download,on_verify,on_upgrade
print('***** connect lp succeed****')
data_handle = {}
data_handle['device_handle'] = device.getDeviceHandle()
def on_disconnect():
print('linkkit is disconnected')
def on_props(request):
params=request['params']
params=eval(params)
light = params["light"]
led1.write(light)
def on_service(id,request):
print('clound req id is {} , req is {}'.format(id,request))
def on_error(err):
print('err msg is {} '.format(err))
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)
def on_dynreg_cb(data):
global deviceSecret,device_dyn_resigter_succed
deviceSecret = data
device_dyn_resigter_succed = True
def dyn_register_device(productKey,productSecret,deviceName):
global on_dynreg_cb,device,deviceSecret,device_dyn_resigter_succed
key = '_amp_customer_devicesecret'
deviceSecretdict = kv.get(key)
print("deviceSecretdict:",deviceSecretdict)
if isinstance(deviceSecretdict,str):
deviceSecret = deviceSecretdict
if deviceSecretdict is None or deviceSecret is None:
key_info = {
'productKey': productKey ,
'productSecret': productSecret ,
'deviceName': deviceName
}
if not device_dyn_resigter_succed:
device.register(key_info,on_dynreg_cb)
def upload_led(led):
global led_data
led_data["light"]= led
led_data_str=ujson.dumps(led_data)
data={
'params':led_data_str
}
device.postProps(data)
if __name__ == '__main__':
ICCID=None
g_connect_status = False
net = None
device = None
deviceSecret = None
deviceName = None
productKey = "a1JuD7ay6Pj"
productSecret = "yV68ScGy5DgEczVx"
device_dyn_resigter_succed = False
connect_network()
deviceName = modem.getDevImei()
ICCID=modem.sim.getIccid()
device = Device()
if deviceName is not None and len(deviceName) > 0 :
dyn_register_device(productKey,productSecret,deviceName)
else:
print("获取设备IMEI失败,无法进行动态注册")
while deviceSecret is None:
time.sleep(0.2)
print('动态注册成功:' + deviceSecret)
key_info = {
'region' : 'cn-shanghai' ,
'productKey': productKey ,
'deviceName': deviceName ,
'deviceSecret': deviceSecret ,
'keepaliveSec': 60,
}
print(key_info)
device.on(device.ON_CONNECT,on_connect)
device.on(device.ON_DISCONNECT,on_disconnect)
device.on(device.ON_PROPS,on_props)
device.on(device.ON_SERVICE,on_service)
device.on(device.ON_ERROR,on_error)
device.connect(key_info)
led1=GPIO()
led1.open("led1")
radar=GPIO()
radar.open('radar')
led_data = {}
while True:
if radar.read()==1:
print("---------------------------detect body-------------------------------")
led1.write(1)
upload_led(1)
else:
print("no body")
led1.write(0)
upload_led(0)
time.sleep(1)
board.json
{
"name": "haas506",
"version": "2.0.0",
"io": {
"ADC0": {
"type": "ADC",
"port": 0,
"sampling": 12000000
},
"ADC1": {
"type": "ADC",
"port": 1,
"sampling": 12000000
},
"ADC2": {
"type": "ADC",
"port": 2,
"sampling": 12000000
},
"qma8981": {
"type": "I2C",
"port": 1,
"addrWidth": 7,
"freq": 400000,
"mode": "master",
"devAddr": 18
},
"KEY1": {
"type": "GPIO",
"port": 44,
"dir": "irq",
"pull": "pullup",
"intMode": "rising"
},
"led1": {
"type": "GPIO",
"port": 7,
"dir": "output",
"pull": "pulldown"
},
"led_g": {
"type": "GPIO",
"port": 32,
"dir": "output",
"pull": "pulldown"
},
"cs": {
"type": "GPIO",
"port": 15,
"dir": "output",
"pull": "pullup"
},
"radar":{
"type":"GPIO",
"port": 17,
"dir": "input",
"pull":"pullup"
},
"SPI0": {
"type": "SPI",
"port": 0,
"mode": "master",
"freq": 2000000
},
"serial1": {
"type": "UART",
"port": 0,
"dataWidth": 8,
"baudRate": 115200,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
},
"serial2": {
"type": "UART",
"port": 1,
"dataWidth": 8,
"baudRate": 9600,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
},
"serial3": {
"type": "UART",
"port": 2,
"dataWidth": 8,
"baudRate": 115200,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
}
},
"debugLevel": "ERROR",
"repl": "enable",
"replPort": 0
}
3、调试
调试串口使用TTL端口,打开设备管理器可以查看调试端口号。 串口调试工具使用putty
调试结果
1、串口调试工具log,网络连接成功–动态注册成功–传感器每秒钟检测一次是否有人,当检测到有人时LED灯亮起,打印‘detect body’ ;没有检测到人时LED灯灭,打印‘no body’。 2、阿里云平台,打开实时刷新,物模型会即时显示lLED当前状态。
|