diff --git a/src/components/Blockly/blocks/sensebox-web.js b/src/components/Blockly/blocks/sensebox-web.js index 6045657..3d77fa9 100644 --- a/src/components/Blockly/blocks/sensebox-web.js +++ b/src/components/Blockly/blocks/sensebox-web.js @@ -1,60 +1,112 @@ -import Blockly from 'blockly'; -import { getColour } from '../helpers/colour' +import Blockly from "blockly"; +import { getColour } from "../helpers/colour"; -Blockly.Blocks['sensebox_wifi'] = { - init: function () { - this.setTooltip(Blockly.Msg.senseBox_wifi_tooltip); - this.setHelpUrl(''); - this.setColour(getColour().sensebox); - this.appendDummyInput() - .appendField(Blockly.Msg.senseBox_wifi_connect); - this.appendDummyInput() - .setAlign(Blockly.ALIGN_LEFT) - .appendField(Blockly.Msg.senseBox_wifi_ssid) - .appendField(new Blockly.FieldTextInput("SSID"), "SSID"); - this.appendDummyInput() - .setAlign(Blockly.ALIGN_LEFT) - .appendField(Blockly.Msg.senseBox_output_password) - .appendField(new Blockly.FieldTextInput("Password"), "Password"); - this.setHelpUrl(Blockly.Msg.senseBox_wifi_helpurl) - this.setPreviousStatement(true, null); - this.setNextStatement(true, null); - }, - onchange: function (e) { - var legal = false; - // Is the block nested in a loop? - var block = this; - do { - if (this.LOOP_TYPES.indexOf(block.type) !== -1) { - legal = true; - break; - } - block = block.getSurroundParent(); - } while (block); - if (legal) { - this.setWarningText(null); - - } else { - this.setWarningText(Blockly.Msg.CONTROLS_FLOW_STATEMENTS_WARNING); - - } - }, - LOOP_TYPES: ['arduino_functions'], +Blockly.Blocks["sensebox_wifi"] = { + init: function () { + this.setTooltip(Blockly.Msg.senseBox_wifi_tooltip); + this.setHelpUrl(""); + this.setColour(getColour().sensebox); + this.appendDummyInput().appendField(Blockly.Msg.senseBox_wifi_connect); + this.appendDummyInput() + .setAlign(Blockly.ALIGN_LEFT) + .appendField(Blockly.Msg.senseBox_wifi_ssid) + .appendField(new Blockly.FieldTextInput("SSID"), "SSID"); + this.appendDummyInput() + .setAlign(Blockly.ALIGN_LEFT) + .appendField(Blockly.Msg.senseBox_output_password) + .appendField(new Blockly.FieldTextInput("Password"), "Password"); + this.setHelpUrl(Blockly.Msg.senseBox_wifi_helpurl); + this.setPreviousStatement(true, null); + this.setNextStatement(true, null); + }, + onchange: function (e) { + var legal = false; + // Is the block nested in a loop? + var block = this; + do { + if (this.LOOP_TYPES.indexOf(block.type) !== -1) { + legal = true; + break; + } + block = block.getSurroundParent(); + } while (block); + if (legal) { + this.setWarningText(null); + } else { + this.setWarningText(Blockly.Msg.CONTROLS_FLOW_STATEMENTS_WARNING); + } + }, + LOOP_TYPES: ["arduino_functions"], }; -Blockly.Blocks['sensebox_startap'] = { - init: function () { - this.setTooltip(Blockly.Msg.senseBox_wifi_startap_tooltip); - this.setHelpUrl(''); - this.setColour(getColour().sensebox); - this.appendDummyInput() - .appendField(Blockly.Msg.senseBox_wifi_startap); - this.appendDummyInput() - .setAlign(Blockly.ALIGN_LEFT) - .appendField(Blockly.Msg.senseBox_wifi_ssid) - .appendField(new Blockly.FieldTextInput("SSID"), "SSID"); - this.setHelpUrl(Blockly.Msg.senseBox_wifi_helpurl) - this.setPreviousStatement(true, null); - this.setNextStatement(true, null); +Blockly.Blocks["sensebox_startap"] = { + init: function () { + this.setTooltip(Blockly.Msg.senseBox_wifi_startap_tooltip); + this.setHelpUrl(""); + this.setColour(getColour().sensebox); + this.appendDummyInput().appendField(Blockly.Msg.senseBox_wifi_startap); + this.appendDummyInput() + .setAlign(Blockly.ALIGN_LEFT) + .appendField(Blockly.Msg.senseBox_wifi_ssid) + .appendField(new Blockly.FieldTextInput("SSID"), "SSID"); + this.setHelpUrl(Blockly.Msg.senseBox_wifi_helpurl); + this.setPreviousStatement(true, null); + this.setNextStatement(true, null); + }, +}; + +Blockly.Blocks["sensebox_ethernet"] = { + init: function () { + this.setTooltip(Blockly.Msg.senseBox_ethernet_tooltip); + this.setHelpUrl(""); + this.setColour(getColour().sensebox); + this.appendDummyInput() + .appendField("Connect with Ethernet") + .appendField("using") + .appendField( + new Blockly.FieldDropdown([ + ["dhcp", "Dhcp"], + ["manual configuration", "Manual"], + ]), + "dhcp" + ); + this.appendDummyInput() + .appendField("MAC-Address") + .appendField( + new Blockly.FieldTextInput("0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED"), + "mac" + ); + + this.setHelpUrl(Blockly.Msg.senseBox_ethernet_helpurl); + this.setPreviousStatement(true, null); + this.setNextStatement(true, null); + this.getField("dhcp").setValidator( + function (val) { + this.updateShape_(val === "Manual"); + }.bind(this) + ); + }, + + updateShape_(isManual) { + // console.log(isDhcp); + if (isManual) { + this.appendDummyInput("ip-field") + .appendField("Ip Adress") + .appendField(new Blockly.FieldTextInput("192.168.1.100"), "ip"); + this.appendDummyInput("subnetmask-field") + .appendField("Subnetmask") + .appendField(new Blockly.FieldTextInput("255.255.255.0"), "subnetmask"); + this.appendDummyInput("gateway-field") + .appendField("Gateway") + .appendField(new Blockly.FieldTextInput("192.168.1.1"), "gateway"); + this.appendDummyInput("dns-field") + .appendField("DNS") + .appendField(new Blockly.FieldTextInput("8.8.8.8"), "dns"); + } else { + this.removeInput("ip-field", true); + this.removeInput("subnetmask-field", true); + this.removeInput("gateway-field", true); + this.removeInput("dns-field", true); } -}; \ No newline at end of file + }, +}; diff --git a/src/components/Blockly/generator/sensebox-osem.js b/src/components/Blockly/generator/sensebox-osem.js index cdefc2d..6681006 100644 --- a/src/components/Blockly/generator/sensebox-osem.js +++ b/src/components/Blockly/generator/sensebox-osem.js @@ -17,6 +17,16 @@ Blockly.Arduino.sensebox_send_to_osem = function (block) { }; Blockly.Arduino.sensebox_osem_connection = function (Block) { + var workspace = Blockly.getMainWorkspace(); + var wifi = false; + var ethernet = false; + if (workspace.getBlocksByType("sensebox_wifi").length > 0) { + wifi = true; + ethernet = false; + } else if (workspace.getBlocksByType("sensebox_ethernet").length > 0) { + ethernet = true; + wifi = false; + } var box_id = this.getFieldValue("BoxID"); var host = this.getFieldValue("host"); var branch = Blockly.Arduino.statementToCode(Block, "DO"); @@ -42,12 +52,22 @@ Blockly.Arduino.sensebox_osem_connection = function (Block) { 'const char SENSEBOX_ID [] PROGMEM = "' + box_id + '";'; Blockly.Arduino.definitions_["host"] = "const char server [] PROGMEM =" + host + ";"; - if (ssl === "TRUE") { - Blockly.Arduino.definitions_["WiFiSSLClient"] = "WiFiSSLClient client;"; - port = 443; - } else if (ssl === "FALSE") { - Blockly.Arduino.definitions_["WiFiClient"] = "WiFiClient client;"; - port = 80; + if (wifi === true) { + if (ssl === "TRUE") { + Blockly.Arduino.definitions_["WiFiSSLClient"] = "WiFiSSLClient client;"; + port = 443; + } else if (ssl === "FALSE") { + Blockly.Arduino.definitions_["WiFiClient"] = "WiFiClient client;"; + port = 80; + } + } else if (ethernet === true) { + if (ssl === "TRUE") { + Blockly.Arduino.definitions_["WiFiSSLClient"] = "WiFiSSLClient client;"; + port = 443; + } else if (ssl === "FALSE") { + Blockly.Arduino.definitions_["EthernetClient"] = "EthernetClient client;"; + port = 80; + } } Blockly.Arduino.definitions_["measurement"] = `typedef struct measurement { diff --git a/src/components/Blockly/generator/sensebox-web.js b/src/components/Blockly/generator/sensebox-web.js index 8727328..eedf246 100644 --- a/src/components/Blockly/generator/sensebox-web.js +++ b/src/components/Blockly/generator/sensebox-web.js @@ -1,25 +1,76 @@ -import Blockly from 'blockly'; - +import Blockly from "blockly"; /* Wifi connection and openSenseMap Blocks*/ Blockly.Arduino.sensebox_wifi = function (block) { - var pw = this.getFieldValue('Password'); - var ssid = this.getFieldValue('SSID'); - Blockly.Arduino.libraries_['library_senseBoxMCU'] = '#include "SenseBoxMCU.h"'; - Blockly.Arduino.definitions_['define_network'] = 'Bee* b = new Bee();'; - if (pw === "") { - Blockly.Arduino.setupCode_['sensebox_network'] = 'b->connectToWifi("' + ssid + '");\ndelay(1000);'; - } else - Blockly.Arduino.setupCode_['sensebox_network'] = 'b->connectToWifi("' + ssid + '","' + pw + '");\ndelay(1000);'; - var code = ''; - return code; + var pw = this.getFieldValue("Password"); + var ssid = this.getFieldValue("SSID"); + Blockly.Arduino.libraries_["library_senseBoxMCU"] = + '#include "SenseBoxMCU.h"'; + Blockly.Arduino.definitions_["define_network"] = "Bee* b = new Bee();"; + if (pw === "") { + Blockly.Arduino.setupCode_["sensebox_network"] = + 'b->connectToWifi("' + ssid + '");\ndelay(1000);'; + } else + Blockly.Arduino.setupCode_["sensebox_network"] = + 'b->connectToWifi("' + ssid + '","' + pw + '");\ndelay(1000);'; + var code = ""; + return code; }; Blockly.Arduino.sensebox_startap = function (block) { - var ssid = this.getFieldValue('SSID'); - Blockly.Arduino.libraries_['library_senseBoxMCU'] = '#include "SenseBoxMCU.h"'; - Blockly.Arduino.definitions_['define_network'] = 'Bee* b = new Bee();'; - Blockly.Arduino.setupCode_['sensebox_network'] = 'b->startAP("' + ssid + '");' - var code = ''; - return code; -}; \ No newline at end of file + var ssid = this.getFieldValue("SSID"); + Blockly.Arduino.libraries_["library_senseBoxMCU"] = + '#include "SenseBoxMCU.h"'; + Blockly.Arduino.definitions_["define_network"] = "Bee* b = new Bee();"; + Blockly.Arduino.setupCode_["sensebox_network"] = + 'b->startAP("' + ssid + '");'; + var code = ""; + return code; +}; + +Blockly.Arduino.sensebox_ethernet = function () { + var ip = this.getFieldValue("ip"); + var gateway = this.getFieldValue("gateway"); + var subnetmask = this.getFieldValue("subnetmask"); + var dns = this.getFieldValue("dns"); + var mac = this.getFieldValue("mac"); + var dhcp = this.getFieldValue("dhcp"); + + Blockly.Arduino.libraries_["library_senseBoxMCU"] = + '#include "SenseBoxMCU.h"'; + Blockly.Arduino.libraries_["library_ethernet"] = "#include "; + + Blockly.Arduino.definitions_["ethernet_config"] = ` + byte mac[] = { ${mac}};`; + if (dhcp === "Manual") { + Blockly.Arduino.definitions_["ethernet_manual_config"] = ` + //Configure static IP setup (only needed if DHCP is disabled) + IPAddress myIp(${ip.replaceAll(".", ", ")}); + IPAddress myDns(${dns.replaceAll(".", ",")}); + IPAddress myGateway(${gateway.replaceAll(".", ",")}192, 168, 0, 177); + IPAddress mySubnet(${subnetmask.replaceAll(".", ",")}255, 255, 255, 0); + `; + Blockly.Arduino.setupCode_["ethernet_setup"] = ` + Ethernet.init(23); + // start the Ethernet connection: + if (Ethernet.begin(mac) == 0) { + // no point in carrying on, so do nothing forevermore: + // try to congifure using IP address instead of DHCP: + Ethernet.begin(mac, myIp); + } + // give the Ethernet shield a second to initialize: + delay(1000); + `; + } else { + Blockly.Arduino.setupCode_["ethernet_setup"] = ` + Ethernet.init(23); + // start the Ethernet connection: + Ethernet.begin(mac); + // give the Ethernet shield a second to initialize: + delay(1000); + `; + } + + var code = ""; + return code; +}; diff --git a/src/components/Blockly/helpers/maxInstances.js b/src/components/Blockly/helpers/maxInstances.js index 1b0ebc1..f546233 100644 --- a/src/components/Blockly/helpers/maxInstances.js +++ b/src/components/Blockly/helpers/maxInstances.js @@ -14,6 +14,7 @@ const maxInstances = { sensebox_lora_initialize_otaa: 1, sensebox_lora_initialize_abp: 1, sensebox_phyphox_init: 1, + sensebox_ethernet: 1, }; export const getMaxInstances = () => { diff --git a/src/components/Blockly/toolbox/Toolbox.js b/src/components/Blockly/toolbox/Toolbox.js index 0ee760e..39e2095 100644 --- a/src/components/Blockly/toolbox/Toolbox.js +++ b/src/components/Blockly/toolbox/Toolbox.js @@ -94,6 +94,9 @@ class Toolbox extends React.Component { + + +