0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M5ユニットをESP32用Javascript実行環境で動かすサンプルコード

Last updated at Posted at 2022-05-07

手元にあるM5ユニットをESP32用のJavascript実行環境で動かしましたので、備忘録として残しておきます。

以下のESP32用のJavascript実行環境のファームウェアを使います。

「電子書籍:M5StackとJavascriptではじめるIoTデバイス制御」

サンプルコード

(i) 超音波測距ユニットI/O

main.js
import * as sonicio from "UnitSonicIo";

sonicio.begin(32, 33);

function loop(){
	var distance = sonicio.getDistance();
	console.log(distance.toFixed(2) + 'mm');
	delay(1000);
}

(ii) 振動モータユニット

main.js
import * as ledc from "Ledc";

ledc.setup(0, 10000, 10);
ledc.attachPin(32, 0);

function loop(){
	ledc.write(0, 512);
	delay(1000);
	ledc.write(0, 0);
	delay(1000);
}

(iii) カラーセンサユニット

main.js
import * as color from "UnitColor";

color.begin();

function loop(){
	var data = color.getRawData();
	console.log(JSON.stringify(data));
	delay(1000);
}

(iv) 環境センサユニット

main.js
import * as env from "Env";

function loop(){
	var temp = env.dht12_readTemperature();
	var humidity = env.dht12_readHumidity();
	console.log(temp.toFixed(2) + "" + humidity.toFixed(2) + "%");
	delay(1000);
}

(v) 赤外線送受信ユニット

受信側

main.js
import * as ir from "Ir";

ir.recvBegin(33);
ir.recvStart();

function loop(){
	var data = ir.checkRecv();
	if( data )
		console.log('received: ' + data);
}

送信側。M5Stick-C搭載の赤外線送信ポートを利用。

main.js
import * as ir from "Ir";

ir.sendBegin(9);

var counter = 0;

function loop(){
	ir.send(counter++);
	delay(1000);
}

(vi) 光センサユニット

main.js
import * as gpio from "Gpio";

gpio.pinMode(32, gpio.INPUT);

function loop(){
	var dout = gpio.digitalRead(32);
	var aout = gpio.analogRead(33);
	console.log(dout  + ', ' + aout);
	delay(1000);
}

(vii) ジェスチャーユニット

main.js
import * as gesture from "UnitGesture";

gesture.begin();

function loop(){
	var ges = gesture.getGesture();
	if( ges != gesture.None )
		console.log(gesture.getDescription(ges));
}

(viii) RGB LEDユニット

main.js
import * as pixels from "Pixels";

pixels.begin(32, 3);
var color = 0xff0000;
var counter = 0;

function loop(){
	counter++;
	pixels.setPixelColor(0, ((counter % 3) == 0) ? color : 0);
	pixels.setPixelColor(1, ((counter % 3) == 1) ? color : 0);
	pixels.setPixelColor(2, ((counter % 3) == 2) ? color : 0);
	delay(1000);
}

(ix) ファンユニット

main.js
import * as ledc from "Ledc";

ledc.setup(0, 10000, 10);
ledc.attachPin(32, 0);

function loop(){
	ledc.write(0, 512);
	delay(5000);
	ledc.write(0, 0);
	delay(2000);
}

(x) PIRセンサユニット

main.js
import * as gpio from "Gpio";

gpio.pinMode(33, gpio.INPUT);

var last_value = -1;

function loop(){
	var value = gpio.digitalRead(33);
	if( value !=last_value ){
		if( value )
			console.log('detected');
		else
			console.log('lost');
		last_value = value;
	}
}

(xi) ガスセンサユニット

main.js
import * as gas from "UnitGas";

gas.begin();

function loop(){
	var value = gas.IAQmeasure();
	console.log("TVOC=" + value.TVOC + " eCO2=" + value.eCO2);
	delay(1000);
}

(xii) 回転角ユニット

main.js
import * as gpio from "Gpio";

var last_value = -1;

function loop(){
	var value = gpio.analogRead(33);
	if( last_value < 0 || Math.abs( last_value - value ) > 50 ){
		console.log(value);
		last_value = value;
		delay(500);
	}
}

(xiii) フェーダーユニット

main.js
import * as pixels from "Pixels";
import * as gpio from "Gpio";

pixels.begin(32, 14);
var unit = Math.floor(4096 / (14 + 1));
var color = 0x00ff00;
var last_value = -1;

function loop(){
	var value = gpio.analogRead(33);
	if( last_value < 0 || Math.abs(last_value - value) > 20 ){
		for( var i = 0 ; i < 14 ; i++ )
			pixels.setPixelColor(i, (((i + 1) * unit) <= value) ? color : 0x000000);
		last_value = value;
	}
}

本ファームウェアのサポートサイト

〇電子書籍:M5StackとJavascriptではじめるIoTデバイス制御

〇ファームウェアのサポートサイト

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?