2022-03-02 11:25:14 +00:00
|
|
|
|
Blockly.Blocks['ugj_dht11'] = {
|
|
|
|
|
init: function () {
|
|
|
|
|
this.appendValueInput("pin")
|
|
|
|
|
.setCheck("Number")
|
|
|
|
|
.appendField("GPIO");
|
|
|
|
|
this.appendDummyInput()
|
|
|
|
|
.appendField("の DHT11 センサーの値を取得");
|
|
|
|
|
this.setInputsInline(true);
|
|
|
|
|
this.setPreviousStatement(true, null);
|
|
|
|
|
this.setNextStatement(true, null);
|
|
|
|
|
this.setTooltip("");
|
|
|
|
|
this.setHelpUrl("気温・湿度センサーDHT11で、気温(摂氏)、湿度(%)を計測します。計測値は計測値ブロックで参照します。");
|
|
|
|
|
this.setStyle('sensor_blocks');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
Blockly.JavaScript['ugj_dht11'] = function (block) {
|
|
|
|
|
var value_pin = Blockly.JavaScript.valueToCode(block, 'pin', Blockly.JavaScript.ORDER_ATOMIC);
|
2023-01-10 12:46:36 +00:00
|
|
|
|
let pypath = apptool.path.join(ugj_const.blocks_dir, 'sensors', 'dht11', 'dht11.py');
|
2022-03-03 12:32:00 +00:00
|
|
|
|
var code = `let _th = require('child_process').spawnSync('python3', ['${pypath}', '${value_pin}'], {timeout: 5000}).stdout.toString();
|
2022-03-02 11:25:14 +00:00
|
|
|
|
let _dhtdata = JSON.parse(_th);`;
|
|
|
|
|
// let _dht11data[0] =
|
|
|
|
|
// console.log('t=' + obj.temperature);
|
|
|
|
|
// console.log('h=' + obj.humidity);
|
|
|
|
|
return code;
|
|
|
|
|
};
|
|
|
|
|
Blockly.Blocks['ugj_dht11_data'] = {
|
|
|
|
|
init: function () {
|
|
|
|
|
this.appendDummyInput()
|
|
|
|
|
.appendField(new Blockly.FieldDropdown([["気温", "temperature"], ["湿度", "humidity"]]), "th");
|
|
|
|
|
this.setInputsInline(true);
|
|
|
|
|
this.setOutput(true, null);
|
|
|
|
|
this.setStyle('sensor_blocks');
|
|
|
|
|
this.setTooltip("DHT11 の計測値を返します。");
|
|
|
|
|
this.setHelpUrl("");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
Blockly.JavaScript['ugj_dht11_data'] = function (block) {
|
|
|
|
|
var dropdown_th = block.getFieldValue('th');
|
|
|
|
|
var code = `_dhtdata.${dropdown_th}`;
|
|
|
|
|
return [code, Blockly.JavaScript.ORDER_ATOMIC];
|
2023-01-10 12:46:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
flyout_contents = flyout_contents.concat([
|
|
|
|
|
{
|
|
|
|
|
"kind": "label",
|
|
|
|
|
"text": "温湿度センサー DHT11",
|
|
|
|
|
"web-line": "4.0",
|
|
|
|
|
"web-line-width": "200"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"kind": "block",
|
|
|
|
|
"type": "ugj_dht11",
|
|
|
|
|
"inputs": {
|
|
|
|
|
"pin": {
|
|
|
|
|
"shadow": {
|
|
|
|
|
"type": "math_number",
|
|
|
|
|
"fields": {
|
|
|
|
|
"NUM": "8"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"kind": "block",
|
|
|
|
|
"type": "ugj_dht11_data",
|
|
|
|
|
"fields": {
|
|
|
|
|
"th": "temperature"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]);
|