I´d like to have scriptcraft communicate with Arduinos in the house using say, MQTT

425 views
Skip to first unread message

Simen Sommerfeldt

unread,
Jan 13, 2014, 12:22:27 PM1/13/14
to scriptcraft---sc...@googlegroups.com
Hi,

I am one of the coordinators of the Norwegian movement "Lær Kidsa Koding" - a sister organization to code.org, code club (UK), and Computing at School (UK). Please check out our website www.kidsakoder.no

I have been programming in Java for many years, but am not very capable in minecraft or javascript. Luckily, I have two sons aged 11 and 13 who are very proficient in Minecraft. The older one has even created his own bukkit plugins in Java

I have two agendas: 
  • To look closer into scriptcraft for our local code club sessons (yes I have seen the excellent http://cdathenry.wordpress.com/category/modderdojo/) , 
  • To see if we can incorporate scriptcraft into a "maker" setup with Arduinos and create a local "internet of things" where several devices and programs talk to each other. 
There are several ways to accomplish the latter?
  1. Having HTTP client/server in scriptcraft?
  2. Using a message queue (I have seen a JS MQTT client. is it possible to use in ScriptCraft?)
  3. Using two files to pipe commands in/out of scriptcraft? (how do you read/write a file)?
As you can see, I am pretty open. The coolest solution would be to use MQTT, but then I would have to spawn a thread who listens to it, and allow for drones or others to register callbacks on specific MQTT patterns. 

I am sorry if the question is too broad. I have so much to learn…


Simen Sommerfeldt

unread,
Jan 13, 2014, 12:30:47 PM1/13/14
to scriptcraft---sc...@googlegroups.com
This looks like a candidate for a MQTT client lib: https://github.com/adamvr/MQTT.js/

Walter Higgins

unread,
Jan 13, 2014, 12:45:54 PM1/13/14
to scriptcraft---sc...@googlegroups.com
Hi Simen,

The mqtt lib above is for node only I'm afraid.
To read and write a file in ScriptCraft is straightforward...

// read...
var FileReader = java.io.FileReader;
var BufferedReader = java.io.BufferedReader;
var contents = [], line = null;
var in = new BufferedReader(new FileReader('filename.txt');
while ( ( line = in.readLine() ) != null) { 
   contents.push('' + line);
}

(hint - it uses java.io.* classes)

// write
var File = java.io.File;
var PrintWriter = java.io.PrintWriter;
var FileWriter = java.io.FileWriter;

var f = new File('output.txt');
var out =  new PrintWriter(new FileWriter(f));
out.println('Hello World');
out.close()

you might be able to do it using the unix screen command too?

Walter

Simen Sommerfeldt

unread,
Jan 15, 2014, 3:08:49 AM1/15/14
to scriptcraft---sc...@googlegroups.com
Ok. I´m almost there! As you can see from this blogpost 
(where the code is awful, but it proves a point), I'm sending commands to to the Arduino.
All I am missing now is to APPEND text to the file, not write a new one every time. I read the doc and added "true" to the FileWriter constructor, but to no avail. I know this is a typical n00b question…

Here´s the code so far:

events.on('player.PlayerInteractEvent', function (listener, event) { 
var player = event.player;
var block = event.getClickedBlock();
var type = block.getType();
//player.sendMessage(block.toString());
if(type==org.bukkit.Material.LEVER) {
if (block.data==3){
player.sendMessage('Powered');
writeStatus('commands.txt','DOWN');
}
else
{
player.sendMessage('Not powered');
writeStatus('commands.txt','UP');
}
}    
})


writeStatus=function(filename,status){
var File = java.io.File;
var PrintWriter = java.io.PrintWriter;
var FileWriter = java.io.FileWriter;

var f = new File(filename);
var out =  new PrintWriter(new FileWriter(f));
out.println(status);
out.close()
}


Walter Higgins

unread,
Jan 15, 2014, 6:03:19 AM1/15/14
to scriptcraft---sc...@googlegroups.com
Add true as the 2nd parameter to the FileWriter() constructor (boolean literal true not string "true")

var f = new File(filename);
var out =  new PrintWriter(new FileWriter(f, true ));
out.println(status);

Simen Sommerfeldt

unread,
Jan 15, 2014, 6:22:07 AM1/15/14
to scriptcraft---sc...@googlegroups.com
Thanks! Now it works. I'll make a blogpost about scriptcraft soon. I find it more attractive for code clubs, since the children/youth are much more likely to encounter javascript in the future than Lua.

What is your best suggestion for letting scriptcraft receive updates from an Arduino?

Simen Sommerfeldt

unread,
Jan 15, 2014, 6:36:43 AM1/15/14
to scriptcraft---sc...@googlegroups.com
BTW the Mosquitto client i'v found here doesn't seem to require Node.js but it requires websockets. is that supported in scriptcraft? 


Again, sorry if I take up too much of your time. 

Walter Higgins

unread,
Jan 15, 2014, 7:29:11 AM1/15/14
to scriptcraft---sc...@googlegroups.com
Hi Simen,

No. ScriptCraft uses CommonJS style modules (require() and exports) but does not include any of the standard node.js library.
Let me know when you've posted the stuff on your blog. This is a really interesting project (bridging virtual and real world).

Walter

Simen Sommerfeldt

unread,
Jan 15, 2014, 10:23:08 AM1/15/14
to scriptcraft---sc...@googlegroups.com
Hi Yes I will. 

The Computercraft entry I wrote was picked up by a teacher who wanted to repeat it with her students here in Norway :-) 

So a viable way to to any kinds of communications would be to write a bukkit plugin in Java and then the public methods would be exposed to ScriptCraft? Or would one need to do something in addition?

Could the receiver part of such a plugin run a background thread and reach callback/handler function in JS? Or perhaps feed a FIFO queue that somehow is available to JS?

Cheers, Simen

Walter Higgins

unread,
Jan 15, 2014, 11:28:53 AM1/15/14
to scriptcraft---sc...@googlegroups.com
> So a viable way to to any kinds of communications would be to write a bukkit plugin in Java and then the public methods would be exposed to ScriptCraft?

No. Just write the bukkit plugin in Javascript - that's what ScriptCraft is for! :-)

Anything you can do in Java with Bukkit, you can do in Javascript using ScriptCraft. What do you want to do?

Simen Sommerfeldt

unread,
Jan 15, 2014, 12:59:43 PM1/15/14
to scriptcraft---sc...@googlegroups.com
I´d like to have some sorts of asyncronous messaging between different Arduino sensors/servos and perhaps some manifestation of this in Minecraft. And I have registered that MQTT is often used for this kind of connections. So I was wondering if ScriptCraft could somehow talk to or embed an MQTT client. Hence the need for websockets. 

Walter Higgins

unread,
Jan 15, 2014, 2:01:30 PM1/15/14
to scriptcraft---sc...@googlegroups.com
mqtt looks interesting. I'll look into it.

Walter Higgins

unread,
Jan 15, 2014, 4:16:49 PM1/15/14
to
Hi Simen,

These are instructions for using MQTT from within ScriptCraft.

First you'll need to run a broker somewhere - I recommend mosquitto.org


Once downloaded and installed, start the mosquitto broker.

Next you'll need to download the IA92 client software. 


Unzip the ia92.zip file and then  and copy the wmqtt.jar file (also attached) to the jre/lb/ext directory for your installed jvm (I don't know which version of java you're using or which os so the path will differ depending).

Then start craftbukkit.

You should be able to connect to the broker using the following code...

var MqttClient = com.ibm.mqtt.MqttClient;
var connStr = 'tcp://localhost:1883';
var client = new MqttClient(connStr);
client.connect('minecraft', true, 5);
client.publish('light', [1], 0, false); // on
client.publish('light', [0], 0, false); // off

Take a look at the IA92 documentation enclosed in the zip file downloadable from the link above. I've tested this and it works.

You'd obviously have to have your arduino subscribing to the 'light' topic and responding appropriately - I'm afraid I don't know much about arduinos.

Walter

wmqtt.jar

Walter Higgins

unread,
Jan 15, 2014, 5:31:04 PM1/15/14
to scriptcraft---sc...@googlegroups.com
Hi Simen,

If you don't want to (or can't) copy the wmqtt.jar to your jvm's jre/lib/ext directory, you can also just include wmqtt.jar in the classpath when you start craftbukkit like this...

java -cp ./wmqtt.jar:craftbukkit.jar org.bukkit.craftbukkit.Main 
(linux)

or 

java -classpath .\wmqtt.jar;\.craftbukkit.jar org.bukkit.craftbukkit.Main 
(windows)

Walter Higgins

unread,
Jan 15, 2014, 7:26:41 PM1/15/14
to scriptcraft---sc...@googlegroups.com
Hi Simen,

See also this ...

https://gist.github.com/walterhiggins/ab049f18cd23323bc2a3

WebSockets isn't strictly needed. I got this working here fairly quickly - every block break event being published to the mqtt broker. Just follow the instructions in that gist.
Walter

Simen Sommerfeldt

unread,
Jan 16, 2014, 2:32:09 AM1/16/14
to scriptcraft---sc...@googlegroups.com
Wow. thanks! I have to try that. 

Simen Sommerfeldt

unread,
Jan 16, 2014, 3:36:41 AM1/16/14
to scriptcraft---sc...@googlegroups.com
I installed mosquitto and downloaded the client. When trying your sample, I received the following error upon execution of the third line
var MqttClient = com.ibm.mqtt.MqttClient;
var connStr = 'tcp://localhost:1883';
var client = new MqttClient(connStr);

09:34:45 [SEVERE] [ScriptCraftPlugin] Error while trying to evaluate javascript: var client = new MqttClient(connStr);, Error: JavaException: javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: [JavaPackage com.ibm.mqtt.MqttClient] is not a function, it is object. (<Unknown source>#1) in <Unknown source> at line number 1


As you can see, com.ibm.mqtt.MqttClient is recognized as a javapackage. 

Simen Sommerfeldt

unread,
Jan 16, 2014, 3:39:23 AM1/16/14
to scriptcraft---sc...@googlegroups.com
Also, to receive messages, we need a handler that implements the java interface 

MqttSimpleCallback

or the advanced callback. How does one go about that? 

Walter Higgins

unread,
Jan 16, 2014, 5:12:03 AM1/16/14
to scriptcraft---sc...@googlegroups.com
Did you run craftbukkit with the modified classpath ?

Run craftbukkit like this...

java -cp wmqtt.jar;craftbukkit.jar org.bukkit.craftbukkit.Main 

... replacing wmqtt.jar and craftbukkit.jar with the paths to those files in your own system. (if you're on linux,unix,mac replace ; with : as the path separator in the above command)

Walter Higgins

unread,
Jan 16, 2014, 5:29:32 AM1/16/14
to scriptcraft---sc...@googlegroups.com
Hi Simen,

Good question.
If it was an interface with a single method it would be easy - just use a js function. However this interface has 2 methods so for it to work with Java6/7 I'm going to have to provide an abstract class as part of ScriptCraft. I may end up just bundling wmqtt.jar as part of scriptcraft since I can see it becoming super useful.
If you're feeling adventurous you can upgrade to Java 8 where you can already create js objects that implement interfaces with more than one method...


MQTT looks too interesting not to be part of ScriptCraft so I'm definitely going to do more to support it in ScriptCraft.

Walter

Simen Sommerfeldt

unread,
Jan 16, 2014, 12:19:44 PM1/16/14
to scriptcraft---sc...@googlegroups.com
I think using Java8 is to stretch my ability a bit, So I wait until you eventually get around to incorporating MQTT.

And I really agree with you that incorporating mqtt in ScriptCraft would be a nice idea. I can think of many many' usages: not only communication with Arduinos, but actually connecting minecraft worlds with each other and many other things. Imagine all the things we could use it for in classrooms and code clubs. 

The blogpost is coming which ever way. Thanks to you, I have already performed the same as I did with ComputerCraft, and I think ScriptCraft is much more relevant and useful: Because you can do more things in mincecraft, and learning JavaScript is very useful for later web development. I will of course credit you for the help you gave me.

Thanks for having already done a wonderful job, and for being so helpful 

/Simen

Strat

unread,
Jan 16, 2014, 10:46:40 PM1/16/14
to scriptcraft---sc...@googlegroups.com
Why not use something like Socket.io? I think Arduinos support them; isn't it just C++ with some libraries?
(I used Arduinos before, just been a long time)

Simen Sommerfeldt

unread,
Jan 17, 2014, 1:40:45 AM1/17/14
to scriptcraft---sc...@googlegroups.com
Yes that is certainly a possibility.  

Two reasons why I´d prefer MQTT: 1) It opens up a lot of possibilities with regard to "internet of things", and I see a scenario where several Arduinos, a Mac, and a RasPi may be collaborating to implement a Halloween prank. 2) And to be honest I know next to nothing about raw socket programming for Arduino. 

With regards to 2): From what I have seen, programming in Arduino is very sequental by nature. What about buffering of incoming socket data if the single thread is busy regulating a servo? Again: this is really not my core competence. 

Walter Higgins

unread,
Jan 17, 2014, 12:02:38 PM1/17/14
to scriptcraft---sc...@googlegroups.com
Hi Simen,

Is comms over network a must-have? Have you considered serial ? (with arduino attached directly to computer)

Walter Higgins

unread,
Jan 17, 2014, 6:49:46 PM1/17/14
to scriptcraft---sc...@googlegroups.com
Hi Simen,


I've created a supplemental jar file for use when using mqtt. There's also a new 'mqtt' module in the ScriptCraft plugn (get it at http://scriptcraftjs.org/download/2014-01/2014-01-17/ ).

Follow the instructions at the link above and you should be good. I assume you're already using a mqtt broker.

Walter

Strat

unread,
Jan 17, 2014, 10:36:40 PM1/17/14
to scriptcraft---sc...@googlegroups.com

Simen Sommerfeldt

unread,
Jan 18, 2014, 1:33:20 AM1/18/14
to scriptcraft---sc...@googlegroups.com
Hi Yes, that was the first thing I did, just using a Unix Pipe. And for those Arduinos without a network shield, that is how to do it - using av Python process or similar as a proxy 

Simen Sommerfeldt

unread,
Jan 18, 2014, 1:34:52 AM1/18/14
to scriptcraft---sc...@googlegroups.com
Wow! thanks. that must be tested. Two of the kids and the wife all have their birthdays this weekend, so I'm not sure how much time I can get. 

Simen Sommerfeldt

unread,
Jan 18, 2014, 2:28:10 AM1/18/14
to scriptcraft---sc...@googlegroups.com
Hi. which example? The miliseconds one or something on wiring.org (and what URL there?)

Simen Sommerfeldt

unread,
Jan 19, 2014, 3:28:38 AM1/19/14
to scriptcraft---sc...@googlegroups.com
Hi. I just sent you a g+ message explaining how I plan to base two different articles as follow-ups to this

Please let me know if the message didn´t pull through

/Simen
Reply all
Reply to author
Forward
0 new messages