add first draft for mqtt blocks

This commit is contained in:
Mario 2020-11-11 11:09:19 +01:00
parent 3df66cd425
commit fe328f9da3
8 changed files with 157 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import './sensebox-display';
import './sensebox-lora';
import './sensebox-led';
import './sensebox-sd';
import './mqtt';
import './text';
import './io';
import './audio';

View File

@ -0,0 +1,62 @@
import * as Blockly from 'blockly/core';
import { getColour } from '../helpers/colour';
import * as Types from '../helpers/types'
/**
* MQTT Blocks
*/
Blockly.Blocks["sensebox_mqtt_setup"] = {
init: function () {
//this.setTooltip(Blockly.Msg.senseBox_wifi_);
this.setHelpUrl('');
this.setColour(getColour().mqtt);
this.appendDummyInput()
.appendField(Blockly.Msg.senseBox_mqtt_init);
this.appendDummyInput()
.setAlign(Blockly.ALIGN_LEFT)
.appendField(Blockly.Msg.senseBox_mqtt_server)
.appendField(new Blockly.FieldTextInput("Server"), "server");
this.appendDummyInput()
.setAlign(Blockly.ALIGN_LEFT)
.appendField(Blockly.Msg.senseBox_mqtt_port)
.appendField(new Blockly.FieldTextInput("Port"), "port");
this.appendDummyInput()
.setAlign(Blockly.ALIGN_LEFT)
.appendField(Blockly.Msg.senseBox_mqtt_username)
.appendField(new Blockly.FieldTextInput("Username"), "username");
this.appendDummyInput()
.setAlign(Blockly.ALIGN_LEFT)
.appendField(Blockly.Msg.senseBox_mqtt_password)
.appendField(new Blockly.FieldTextInput("Password"), "password");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
}
};
Blockly.Blocks["sensebox_mqtt_publish"] = {
init: function () {
this.setColour(getColour().mqtt);
this.appendDummyInput()
.appendField(Blockly.Msg.senseBox_mqtt_publish);
this.appendValueInput('value')
.setCheck(null)
.appendField('Feed')
.appendField(new Blockly.FieldTextInput('Feedname'), 'publishfeed');
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
}
};
Blockly.Blocks["sensebox_mqtt_subscribe"] = {
init: function () {
this.setColour(getColour().mqtt);
this.appendDummyInput()
.appendField(Blockly.Msg.sensebox_mqtt_subscribe);
this.appendDummyInput()
.appendField(new Blockly.FieldTextInput('Feedname'), 'subscribefeed');
this.setOutput(true, null);
}
};

View File

@ -8,6 +8,7 @@ import './sensebox-display';
import './sensebox-lora';
import './sensebox-led';
import './sensebox-sd';
import './mqtt';
import './logic';
import './text';
import './math';

View File

@ -0,0 +1,61 @@
import * as Blockly from 'blockly/core';
/**
* MQTT Blocks
*/
Blockly.Arduino.sensebox_mqtt_setup = function () {
var server = this.getFieldValue('server');
var port = this.getFieldValue('port');
var username = this.getFieldValue('username');
var pass = this.getFieldValue('password');
Blockly.Arduino.libraries_['library_senseBoxMCU'] = '#include "SenseBoxMCU.h"';
Blockly.Arduino.libraries_['library_adafruitmqtt'] = '#include "Adafruit_MQTT.h"';
Blockly.Arduino.libraries_['library_adafruitmqttclient'] = '#include "Adafruit_MQTT_Client.h"';
Blockly.Arduino.definitions_['mqtt_server'] = '#define SERVER "' + server + '"';
Blockly.Arduino.definitions_['mqtt_port'] = '#define SERVERPORT ' + port + '';
Blockly.Arduino.definitions_['mqtt_username'] = '#define USERNAME "' + username + '"';
Blockly.Arduino.definitions_['mqtt_pass'] = '#define PASS "' + pass + '"';
Blockly.Arduino.definitions_['wifi_client'] = 'WiFiClient client;';
Blockly.Arduino.definitions_['mqtt_client'] = 'Adafruit_MQTT_Client mqtt(&client, SERVER, SERVERPORT, USERNAME, PASS);'
var code = '';
return code;
};
Blockly.Arduino.sensebox_mqtt_publish = function (block) {
var feedname = this.getFieldValue('publishfeed');
var res = feedname.split("/");
var feed_client = res[res.length - 1];
var value = Blockly.Arduino.valueToCode(this, 'value', Blockly.Arduino.ORDER_ATOMIC) || '"No Block connected"';
Blockly.Arduino.definitions_['mqtt_' + feed_client + ''] = 'Adafruit_MQTT_Publish ' + feed_client + ' = Adafruit_MQTT_Publish(&mqtt, USERNAME "/feeds/' + feedname + '");'
Blockly.Arduino.codeFunctions_['mqtt_connect_function'] = `// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
}`;
Blockly.Arduino.loopCodeOnce_['mqtt_connect'] = 'MQTT_connect();';
var code = '' + feed_client + '.publish(' + value + ');';
return code
};
Blockly.Arduino.sensebox_mqtt_subscribe = function (block) {
var feedname = this.getFieldValue('subscribefeed');
var x = 5, feed_client;
feed_client = feedname.substr(feedname.length - x, x);
Blockly.Arduino.definitions_['mqtt_' + feed_client + ''] = 'Adafruit_MQTT_Subscribe ' + feed_client + '= Adafruit_MQTT_Subscribe(&mqtt,' + feedname + ');';
Blockly.Arduino.codeFunctions_['mqtt_' + feed_client + 'callbackFunction'] = `void ` + feed_client + `Callback (double x){
Serial.println(x);
}`;
Blockly.Arduino.setupCode_['mqtt_' + feed_client + '_callback'] = '' + feed_client + '.setCallback(' + feed_client + 'Callback);';
Blockly.Arduino.setupCode_['mqtt_' + feed_client + '_subscribe'] = 'mqtt.subscribe(&' + feed_client + ');';
Blockly.Arduino.loopCodeOnce_['mqtt_processPackages'] = 'mqtt.processPackets(10);';
var code = '';
return code;
};

View File

@ -10,7 +10,8 @@ const colours = {
text: 160,
variables: 330,
audio: 250,
arrays: 33
arrays: 33,
mqtt: 90
}

View File

@ -779,4 +779,18 @@ Blockly.Msg.senseBox_ws2818_rgb_led = "senseBox WS2812 - RGB LED";
Blockly.Msg.senseBox_ws2818_rgb_led_position = "Position";
Blockly.Msg.senseBox_ws2818_rgb_led_brightness = "Helligkeit";
/***
* MQTT
*/
Blockly.Msg.senseBox_mqtt_init = "Initialisiere MQTT Broker";
Blockly.Msg.senseBox_mqtt_server = "Server";
Blockly.Msg.senseBox_mqtt_port = "Port";
Blockly.Msg.senseBox_mqtt_username = "Benutzername";
Blockly.Msg.senseBox_mqtt_password = "Passwort";
Blockly.Msg.sensebox_mqtt_subscribe = "Subscribe to Feed"
Blockly.Msg.senseBox_mqtt_publish = "Sende an Feed";
export const De = Blockly.Msg;

View File

@ -763,5 +763,16 @@ Blockly.Msg.senseBox_ws2818_rgb_led = "senseBox WS2812 - RGB LED";
Blockly.Msg.senseBox_ws2818_rgb_led_brightness = "Helligkeit";
Blockly.Msg.senseBox_ws2818_rgb_led_position = "Position";
/***
* MQTT
*/
Blockly.Msg.senseBox_mqtt_init = "Initialize MQTT Broker";
Blockly.Msg.senseBox_mqtt_server = "Server";
Blockly.Msg.senseBox_mqtt_port = "Port";
Blockly.Msg.senseBox_mqtt_username = "Username";
Blockly.Msg.senseBox_mqtt_password = "Password";
Blockly.Msg.sensebox_mqtt_subscribe = "Subscribe to Feed"
Blockly.Msg.senseBox_mqtt_publish = "Publish to Feed";
export const En = Blockly.Msg;

View File

@ -282,6 +282,11 @@ class Toolbox extends React.Component {
<Block type="sensebox_lora_cayenne_gps" />
</Category>
</Category>
<Category id="mqtt" name="MQTT" colour={getColour().mqtt}>
<Block type="sensebox_mqtt_setup" />
<Block type="sensebox_mqtt_publish" />
{/* <Block type="sensebox_mqtt_subscribe" /> */}
</Category>
<Category name="Logik" colour={getColour().logic}>
<Block type="controls_if" />
<Block type="controls_ifelse" />