commit
						aa8b1067dc
					
				| @ -9,6 +9,7 @@ import './sensebox-display'; | ||||
| import './sensebox-lora'; | ||||
| import './sensebox-led'; | ||||
| import './sensebox-sd'; | ||||
| import './mqtt'; | ||||
| import './text'; | ||||
| import './io'; | ||||
| import './audio'; | ||||
|  | ||||
							
								
								
									
										87
									
								
								src/components/Blockly/blocks/mqtt.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								src/components/Blockly/blocks/mqtt.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,87 @@ | ||||
| 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) | ||||
|             .appendField(new Blockly.FieldDropdown([["Adafruit IO", 'adafruitio'], ["DIOTY", 'dioty'], ["Other Service", 'custom']]), "service"); | ||||
|         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, "passwordmsg") | ||||
|             .appendField(new Blockly.FieldTextInput("Password"), "password"); | ||||
|         this.setPreviousStatement(true, null); | ||||
|         this.setNextStatement(true, null); | ||||
|     }, | ||||
|     onchange: function (e) { | ||||
|         let service = this.getFieldValue('service'); | ||||
|         switch (this.getFieldValue('service')) { | ||||
|             case 'adafruitio': | ||||
|                 this.getField('server').setValue("io.adafruit.com"); | ||||
|                 this.getField('port').setValue("1883"); | ||||
|                 this.getField('passwordmsg').setValue("Adafruit IO Key"); | ||||
|                 break; | ||||
|             case 'dioty': | ||||
|                 this.getField('server').setValue("mqtt.dioty.co"); | ||||
|                 this.getField('port').setValue("1883"); | ||||
|                 this.getField('passwordmsg').setValue(Blockly.Msg.senseBox_mqtt_password); | ||||
|                 break; | ||||
| 
 | ||||
|             case "custom": | ||||
|                 this.getField('server').setValue("server"); | ||||
|                 this.getField('port').setValue("port"); | ||||
|                 this.getField('passwordmsg').setValue(Blockly.Msg.senseBox_mqtt_password); | ||||
|                 break; | ||||
|             default: | ||||
| 
 | ||||
|                 break; | ||||
|         } | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| 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); | ||||
|     } | ||||
| }; | ||||
| @ -8,6 +8,7 @@ import './sensebox-display'; | ||||
| import './sensebox-lora'; | ||||
| import './sensebox-led'; | ||||
| import './sensebox-sd'; | ||||
| import './mqtt'; | ||||
| import './logic'; | ||||
| import './text'; | ||||
| import './math'; | ||||
|  | ||||
							
								
								
									
										80
									
								
								src/components/Blockly/generator/mqtt.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								src/components/Blockly/generator/mqtt.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,80 @@ | ||||
| import * as Blockly from 'blockly/core'; | ||||
| /** | ||||
| * MQTT Blocks  | ||||
| */ | ||||
| 
 | ||||
| let service; | ||||
| 
 | ||||
| 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'); | ||||
|     service = this.getFieldValue('service'); | ||||
|     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"'; | ||||
| 
 | ||||
|     switch (service) { | ||||
|         case 'adafruitio': | ||||
|             Blockly.Arduino.definitions_['mqtt_' + feed_client + ''] = 'Adafruit_MQTT_Publish ' + feed_client + ' = Adafruit_MQTT_Publish(&mqtt, USERNAME "/feeds/' + feedname + '");' | ||||
|             break; | ||||
|         case 'dioty': | ||||
|             Blockly.Arduino.definitions_['mqtt_' + feed_client + ''] = 'Adafruit_MQTT_Publish ' + feed_client + ' = Adafruit_MQTT_Publish(&mqtt, "/"USERNAME"/' + feedname + '");' | ||||
|             break; | ||||
|         case 'custom': | ||||
|             Blockly.Arduino.definitions_['mqtt_' + feed_client + ''] = 'Adafruit_MQTT_Publish ' + feed_client + ' = Adafruit_MQTT_Publish(&mqtt, ' + feedname + ');' | ||||
|             break; | ||||
|         default: | ||||
|             break; | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     //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; | ||||
| }; | ||||
| @ -10,7 +10,8 @@ const colours = { | ||||
|     text: 160, | ||||
|     variables: 330, | ||||
|     audio: 250, | ||||
|     arrays: 33 | ||||
|     arrays: 33, | ||||
|     mqtt: 90 | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
| @ -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 = "Verbinde mit 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; | ||||
|  | ||||
| @ -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 = "Connect to 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; | ||||
|  | ||||
| @ -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" /> | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user