Skip to main content

Code Export

If you feel like you’ve reached the limits of what you can build with blokdots and you want to continue to refine your prototype, you can use the “Code Export” feature to export a JavaScript version of your project that you can then edit and adapt.

cmd ⌘E

Exporting the Project

To export your project, go to File › Export to Code and pick a location. It can be helpful to give your project components and cards custom names before exporting as these names will be used for variables and functions in the code, making it more easily readable.

blokdots will create a folder containing three files:

  • your project as a Javascript file
  • a package.json
  • a README.md

The javascript file contains your project in an executable format. The README.md file contains a brief explanation of how to use the generated folder and where to connect your components to. It is written in markdown. The package.json contains all the necessary information to continue working on your project.

Javascript File

Here is an example of what this code could look like.

At the top two packages are imported: We are using Johnny Five to communicate with the Arduino. If you want to dive deeper into the generated JavaScript code, we suggest you have a look at its API to adjust the code to your needs. Additionally, a package from blokdots is imported. This one is not used in all projects, but it is necessary for some components and especially for all control components and integrations to work. Before running the project you must make sure these two packages are installed.

Underneath that some variables for the components are set up, then come all the functions for the reactions defined in your cards. Finally, the board is initiated, and all the components are set up at the event listeners that trigger the reactions are registered. In the end, there is a cleanup function that you can call to stop the project from running.

var blokdots = require("@blokdots/components");
var five = require("johnny-five");

// ------------- Setup the board -------------
var board = new five.Board();

// --- Setup vars ---
var button;
var led;

// ------------- IFTTT -------------

// Project Start
function projectStartReaction() {
led.on();
}

// Button Press
function buttonPressReaction() {
led.toggle();
}

// ------------- Run -------------
board.on("ready", function () {
// --- Setup components ---
button = new five.Button({
pin: 2,
holdtime: 1000,
});
led = new five.Led(4);

// --- Run "when project starts" reactions ---

// Project Start
projectStartReaction();

// --- Setup event listeners ---

// Button Press
var buttonPressCounter = 0;
button.on("down", function () {
buttonPressCounter++;
if (buttonPressCounter >= 1) {
buttonPressCounter = 0;
buttonPressReaction();
}
});
});

// ------------- Stop -------------
function stop() {
// --- Remove event listeners ---
if (button && button._events) {
button.removeAllListeners();
}
if (led && led._events) {
led.removeAllListeners();
}
}

Installing and Running the Exported Project

First, make sure you have node.js installed.

Then navigate to the exported folder in your Terminal (macOS) or cmd.exe (Windows). Then run:

npm install

This will install the required dependencies for your project to run, that we mentioned above.

Now you can start the project by running:

npm start

The project should now work as in the blokdots software.