设备(模块)驱动开发实例 - Shaofa/AliOS-Things-Certification-Manual GitHub Wiki

#端口与模组支持情况

端口/模组 ESP32 DevelopKit
GPIO Y Y
PWM Y N
ADC Y N
DAC Y N
UART Y Y
I2C N Y
RTC Y Y
WDG Y N

#模组和传感器用例

#DeveloperKit之LED

#硬件

image | left

#接线

#配置

/*apps/js/board_config.json*/
{
	"GPIO":[
		{
		"id":"gled",
		"port":60,
		"dir":0,
		"pull":1
		}
	]
}

#代码

print('start gled test.....................');
var gled_level = 0;
var gled_handle = GPIO.open('gled'); 
setInterval(function() {
    GPIO.write(gled_level,1-gled_level);
    gled_level = 1-gled_level; 
}, 5000);
print('end gled test........................');

#现象

gled 间隔5s进行闪烁。

#DeveloperKit之KEY

#硬件

1.png | left | 330x400

#接线

#配置

/*apps/js/board_config.json*/
{
	"GPIO":[
		{
		"id":"key",
		"port":74,
		"dir":2,
		"pull":1
		}
	]
}

#代码

print('start key test.....................');
var key_handle = GPIO.open('key'); 
GPIO.on(key_handle,'both',function(value){
    console.log('key_value:'+value);
});
print('end key test........................');

#现象

按压按键,PC端串口软件能捕获按下状态。

#DeveloperKit之SHTC1

#硬件

image | left

#接线

#配置

/*apps/js/board_config.json*/
{
"I2C":[{
        "id":"shtc1",
        "port":2,
        "address_width":7,
        "freq":100000,
        "mode":1,
        "dev_addr":224
}],

}

#代码

print('start i2c test.....................');
setInterval(function() {

    var shtc1Handle = I2C.open('shtc1'); 
    var Reg=[0xEF,0xC8]; 
    I2C.write(shtc1Handle,Reg); 
    var retArray=I2C.read(shtc1Handle,2); 
    console.log('ID:'+retArray[1]); 
    Reg[0]=0x80; 
    Reg[1]=0x5D; 
    I2C.write(shtc1Handle,Reg); 
    Reg[0]=0x7C; 
    Reg[1]=0xA2; 
    I2C.write(shtc1Handle,Reg); 
    var TempHumm = I2C.read(shtc1Handle,6); 
    console.log('read len:'+TempHumm.length); 
    var i =0; 
    for(i=0;i<TempHumm.length;i++){ 
    	console.log('rawData['+i+']:'+TempHumm[i]); 
    }
    I2C.close(shtc1Handle); 
}, 2000);
print('end i2c test........................');

#现象

每隔2s,程序会去读取SHTC1的温度和湿度的裸数据,用手触摸SHTC1传感器,其裸值将会改变。

image | left

#DeveloperKit之LTR553

#硬件

1)developerkit开发板;

1.png | left | 313x380

#接线

#配置

{
    "I2C":[
        {
            "id":"ltr553",
            "port":2,
            "address_width":7,
            "freq":200000,
            "mode":1,
            "dev_addr":70
        }
    ]
}

#代码

/*samples/js/ltr553.js*/
var ltr553_Handle = I2C.open('ltr553');
setInterval(function(){
	HW.getltr553(ltr553_Handle,function(als,ps){
		console.log('ltr553:als='+als+' ps='+ps);
	});
},3000);

#现象

间隔3s打印当光强&距离值,当有物体靠近该传感器时,ps值会发生相应的改变。

1.png | left | 317x209

#DeveloperKit之MMC3680KJ

#硬件

1)developerkit开发板;

1.png | left | 275x343

#接线

#配置

{
    "I2C":[
        {
            "id":"mmc3680kj",
            "port":1,
            "address_width":7,
            "freq":400000,
            "mode":1,
            "dev_addr":96
        }
    ]
}

#代码

/*samples/js/mmc3680kj.js*/
var mmc3680kj_Handle = I2C.open('mmc3680kj');
setInterval(function(){
	HW.getMmc3680kj(mmc3680kj_Handle,function(x,y,z){
		console.log('mmc3680kj:x='+x+' y='+y + ' z='+z);
	});
},3000);

#现象

间隔3s打印当前磁力计xyz三轴的值:

1.png | left | 408x216

#DeveloperKit之LSM6DSL

#硬件

1)DeveloperKit开发板

1.jpg | left | 304x368

#接线

#配置

{
    "I2C":[
        {
            "id":"lsm6dsl",
            "port":1,
            "address_width":7,
            "freq":400000,
            "mode":1,
            "dev_addr":214
        }
    ]
}

#代码

/*samples/js/lsm6dsl.js*/
var lsm6dsl_Handle = I2C.open('lsm6dsl');
setInterval(function(){
	var acc = HW.getLsm6dslAcc(lsm6dsl_Handle);
	console.log('gx='+acc[0] + ' gy='+acc[1] + ' gz='+acc[2]);
	var gyro = HW.getLsm6dslGyro(lsm6dsl_Handle);
	console.log('x='+gyro[0] + ' y='+gyro[1] + ' z='+gyro[2]);
},3000);

#现象

间隔3s打印当前加速度xyz三轴值、陀螺仪xyz三轴值:

1.png | left | 367x193

#DeveloperKit之BMP280

#硬件

1)developerkit开发板;

image | left

#接线

#配置

{
    "I2C":[
        {
            "id":"bmp280",
            "port":2,
            "address_width":7,
            "freq":400000,
            "mode":1,
            "dev_addr":238
        }
    ]
}

#代码

/*samples/js/bmp280.js*/
var bmp280_Handle = I2C.open('bmp280');;
setInterval(function(){
	var pressValue = HW.getBmp280(bmp280_Handle);
	console.log('pressureValue:'+pressValue+'hPa');
},3000);

#现象

间隔2s打印当前环境的气压值:

image | left

#DeveloperKit之UART

#硬件

1)developerkit开发板; 2)FT232串口模块;

image | left

image | left

#接线

TX引脚 连接 FT232 的RX引脚; RX引脚 连接 FT232 的TX引脚; VCC引脚 连接 FT232 的VCC引脚; GND引脚 连接 FT232 的GND引脚;

#配置

/*apps/js/board_config.json*/
{
	"UART":[
		{
		"id":"uart2",
		"port":2,
		"data_width":3,
		"baud_rate":115200,
		"stop_bits":0,
		"flow_control":0,
		"parity_config":0
		}
	]
}

#代码

var handle=UART.open('uart2');
if(handle>0){
var value = UART.write(handle,'hello\r\n');
UART.on(handle,function(data){ 
    console.log(data);    
});
} else {
	console.log('open uart2 fail!');
}

#现象

FT232连接PC端的串口软件,可以进行数据的接收和发送。

#DeveloperKit之DS1307

#硬件

1)developerkit开发板; 2)ds1307模块;

image | left

#接线

developerKit 的VCC引脚连接DS1307模块的VCC; developerKit 的GND引脚连接DS1307模块的GND; developerKit 的SCL引脚连接DS1307模块的SCL; developerKit 的SDA引脚连接DS1307模块的SDA;

#配置

#代码

print('start rtc test.....................');
RTC.open();
RTC.setTime({'year':18,'month':4,'day':29,'hour':12,'minute':57,'second':0});
setInterval(function() {
	var rtcdata = RTC.getTime(); 
	console.log(rtcdata.year+'/'+rtcdata.month+'/'+rtcdata.day+':'+rtcdata.second);	
}, 2000);
print('start end test.....................');

#现象

image | left

#ESP32之DHT11

#硬件

1)ESP32开发板; 2)DHT11模块;

image | left

#接线

EPS32 IO19引脚 连接 DHT11模块的OUT引脚; EPS32 GND引脚 连接 DHT11模块的GND引脚; EPS32 3.3V引脚 连接 按键模块的VCC引脚;

#配置

/*apps/js/board_config.json*/
{
        "GPIO":[
            {
                "id":"dht11",
                "port":19,
                "dir":3,
                "pull":1
            }
        ]
}

#代码

/*samples/js/dht11.js*/
var dht11_handle = GPIO.open('dht11');
setInterval(function() {
		var dht11_buff = HW.getDht11(dht11_handle);
		console.log('dht11->hum:'+dht11_buff[0]+ '  temp:'+dht11_buff[1]);
}, 5000);

#现象

系统每间隔5s打印温湿度值。

image | left

#ESP32之KEY

#硬件

1)ESP32开发板; 2)按键模块;

image | left

#接线

EPS32 IO23引脚 连接 按键模块的OUT引脚; EPS32 GND引脚 连接 按键模块的GND引脚; EPS32 3.3V引脚 连接 按键模块的VCC引脚;

#配置

/*apps/js/board_config.json*/
{
	"GPIO":[
		{
		"id":"key",
		"port":23,
		"dir":2,
		"pull":1
		}
	]
}

#代码

print('start key test.....................');

var key_handle = GPIO.open('key');
GPIO.on(key_handle,'both',function(value){
	print('key_value:'+value);
});	
print('end key test........................');

#现象

按下按键,终端会打印对应的状态

#ESP32之无源蜂鸣器

#硬件

image | left

#接线

EPS32 IO4引脚 连接 无源蜂鸣器模块的'+'引脚; EPS32 GND引脚 连接 无源蜂鸣器模块的GND引脚;

#配置

/*apps/js/board_config.json*/
{
	"PWM":[
		{
		"id":"buzzer",
		"port":4,
		"freq":5000,
		"duty":50
		}
	]

}

#代码

/*samples/js/buzzer.js*/
print('buzzer uart test.....................');
var buzzer_handle=PWM.start('buzzer'); 
var cur_duty = PWM.getDuty(buzzer_handle);
console.log('cur_duty:'+cur_duty);
setInterval(function() {
	cur_duty += 1;
	if(cur_duty >= 100)cur_duty=0;
	PWM.setDuty(buzzer_handle,cur_duty);
}, 500);
print('end buzzer test........................');

#现象

蜂鸣器声音由弱到强转换。

#ESP32之光敏模块

#硬件

1)esp32Kit开发板 2)光敏模块

image | left

#接线

EPS32 IO34引脚 连接 按键模块的OUT引脚; EPS32 GND引脚 连接 按键模块的GND引脚; EPS32 3.3V引脚 连接 按键模块的VCC引脚;

#配置

/*apps/js/board_config.json*/
{

	"ADC":[
		{
		"id":"light",
		"port":34,
		"sampling":12000000
		}
	]

}

#代码

var light_handle = ADC.open('light');
setInterval(function(){
	var i = 0;
	var LigthValue = 0;
	for(i=0;i<10;i++){
		LigthValue += ADC.read(light_handle);
	}
	LigthValue = LigthValue/10;
	if(LigthValue > 4500){
		LigthValue = 4500;
	}
	LigthValue = 100 - LigthValue/45;
	console.log('LigthValue:'+LigthValue);
},5000);

#现象

光敏范围:0-100,越大代表亮度越强。

image | left

#ESP32之LED模块

#硬件

1)esp32Kit开发板 2)LED模块

image | left

#接线

EPS32 IO25引脚 连接 LED模块的正极; EPS32 GND引脚 连接 LED模块的负极;

#配置

/*apps/js/board_config.json*/
{

	"DAC":[
		{
		"id":"led",
		"port":1,
		"voltage":255
		}
	]

}

#代码

var led_handle = DAC.open('led');
var cur_voltage = DAC.getVol(led_handle);
console.log('cur_voltage:'+cur_voltage);
setInterval(function(){

	cur_voltage  = cur_voltage + 1;
	if(cur_voltage >= 255){
		cur_voltage = 0;
	}
	DAC.setVol(led_handle,cur_voltage);
},50);

#现象

LED亮度由弱变强,当低于某个值是,LED完全熄灭;

#ESP32之UART

#硬件

1)ESP32开发板; 2)FT232串口模块;

#接线

EPS32 IO32引脚 连接 FT232 的RX引脚; EPS32 IO33引脚 连接 FT232 的TX引脚; ESP32 3.3v引脚 连接 FT232 的VCC引脚; ESP32 GND引脚 连接 FT232 的GND引脚;

#配置

/*apps/js/board_config.json*/
{
	"UART":[
		{
		"id":"uart2",
		"port":2,
		"data_width":3,
		"baud_rate":115200,
		"stop_bits":0,
		"flow_control":0,
		"parity_config":0
		}
	]
}

#代码

/*js*/
print('start uart test.....................');
setInterval(function() {

    var handle=UART.open('uart2'); 
    if(handle>0){ 
        var value = UART.write(handle,'hello\r\n'); 
        UART.close(handle);
    } else {
        console.log('open uart2 fail!');
    }

}, 2000);
print('end uart test........................');

#现象

FT232连接PC端的串口软件,可以进行数据的发送。

#ESP32之RTC

#硬件

1)esp32Kit开发板

image | left

#接线

#配置

#代码

print('start rtc test.....................');
RTC.open();
RTC.setTime({'year':18,'month':4,'day':29,'hour':12,'minute':57,'second':0});
setInterval(function() {
	var rtcdata = RTC.getTime(); 
	console.log(rtcdata.year+'/'+rtcdata.month+'/'+rtcdata.day+':'+rtcdata.second);	
}, 2000);
print('start end test.....................');

#现象

每间隔2秒打印一次时间。

image | left

⚠️ **GitHub.com Fallback** ⚠️