Screen Shot 2015-05-19 at 2.11.31 PM

Note: The company formerly known as Spark has rebranded as Particle.

You’ve amassed a small fortune in diamonds, wood, coal, iron, food, and the other resources you need. You’ve spent hours building the perfect Minecraft fortress to stockpile your goods. But who will watch your stash while you’re on another server? In this project guide, you’ll learn to use Arduino coding on a tiny Spark Core microcontroller to build an alarm system to alert you about Minecraft intruders — in the real world!

The Spark Core is a microcontroller programmed with the Arduino language. The important feature that the Spark adds over the basic Arduino boards is the internet! It can communicate over Wi-Fi to other internet-connected devices. In this project, you’ll program a Minecraft tripwire in the game to send signals to your Spark, using a Minecraft mod called ComputerCraft. The Spark will then use a voice module to alert you in real life of the intruder.

This project is quick and easy and it requires no soldering — just plug in the jumper wires. Once you’ve learned this trick you can trigger all kinds of gadgets and robotic behaviors from inside Minecraft.

image07

1. Connect the electronics.

IMPORTANT: Leave the Spark Core unplugged from the USB port on your computer while you’re connecting the wiring.

Use the male-to-female jumper wires to connect the pins from the ISD1820 voice module board to the Spark Core’s breadboard as follows:

Voice module

Spark Core

VCC

3V3

GND

GND

PLAYE

D5

One end is a socket that slides onto the pin on the voice module, and the other end is a pin that plugs into the breadboard. (These jumper wires are much easier to use than the 2 blue wires that I soldered in the picture!)

Next, connect the 2 speaker wires to the 2 screw terminals on the voice module marked SPEAKER.

Now plug the USB cable into your computer to power up the circuit. Hold down the REC button on the voice module, and say “Intruder Alert!” in your most alarmed voice. Press the PLAYE button to play back your recording for testing.

2. Set up your Spark Core.

Now that your Spark Core microcontroller is powered on, you need to connect it to your Wi-Fi network, create an account with Spark, and “claim” your core to add it to your account.

All of these steps are most easily accomplished through the iPhone or Android apps offered by Spark. Search for “Spark Core” in your app store, or follow these links for iPhone or Android.

If you need any help with the app, or if you don’t have an iPhone or Android phone, the Spark getting started web page has information and alternate setup methods.

3. Program your Spark Core.

The Spark Core microcontroller is programmed through a web interface at the Spark website: https://build.spark.io Sign in with the username and password you created when you set up your Spark Core. Then create a new “App” and name it something descriptive like “Minecraft-Alarm.”

image03

Here’s the code to enter for your app:

void setup() {
Spark.function("alarmOn", alarmOn);
pinMode(D5, OUTPUT);
}
void loop() {
}
int alarmOn (String command) {
digitalWrite(D5, HIGH); // signal alarm
delay(1000); // wait for a second
digitalWrite(D5, LOW); // stop signalling alarm
}

If you’ve done any Arduino programming before, this code will look very familiar to you. The one main difference is that there is no code in the loop() section. This is because our code waits to be triggered by a signal coming in from the internet.

The alarmOn function simply sends a HIGH output on pin D5 to activate the voice module’s PLAYE pin connected there. In the setup() section, we register this function with the Spark cloud to let it know it is ready for this signal.

Spark.function("alarmOn", alarmOn);

After you’ve entered the code, press the lightning bolt symbol in the upper right corner of the Spark interface to upload your program to your Spark.

4. Test your code.

Before you try and trigger the Spark program from Minecraft, you’ll test it over the web. Whenever you’re creating a project with multiple interconnected parts, it’s good practice to test each part by itself to simplify your troubleshooting.

Go to this Spark Control Panel:
https://andyforest.github.io/sparkControl/examples/Spark_Control_Panel.html

image04

With this interface, you can send commands to your Spark. There are 3 fields you need to fill in on this form. You can get the first 2 — your Core ID and Access Token — from the Spark interface.

image06

image08

Note that I have blurred out my Access Token. The reason for this is that if someone has this token, they can take complete control of your Spark device. We’re going to use the token in our code anyway, because if our Spark starts doing strange things because someone has hacked it, we can always press that Reset Token button and regain control of it. Spark is working on a way to allow anonymous access to remote functions without exposing your access token, but that feature is not ready yet.

The final parameter is our Function Name: alarmOn

Once you’ve entered these 3 things into the Spark Control Panel, press the Send button next to where you entered alarmOn, and listen to your alarm in all its glory!

5. Modify Minecraft by installing ComputerCraft.

Minecraft is an open ecosystem with people making all kinds of mods to extend its functionality. If you’ve been playing Minecraft for a while, you’ve probably installed some mods. If not, with this step, you will learn a new skill! It’s a bit convoluted the first time you install a mod, but once you know how to do it, you’ll open up a world of thousands of interesting Minecraft modifications!

For this project, you’ll use the ComputerCraft mod. ComputerCraft adds computer objects, which can be programmed with an old-school DOS-like prompt. These computers can have programs on them that detect “redstone” events within the game, and trigger all kinds of actions.

Like many other mods, ComputerCraft requires the Forge mod, which provides a framework for other mods.

It sometimes takes mod authors a while to update their mod to be compatible with the latest version of Minecraft, so many mods require a slightly older version of Minecraft to work properly. As of this writing, ComputerCraft is compatible with Minecraft 1.7.10. Check the ComputerCraft website for what version of Minecraft it is currently compatible with.

So in the next steps you’ll install Minecraft 1.7.10, Forge for 1.7.10, and the latest version of ComputerCraft.

5a. Install Minecraft 1.7.10

Open the Minecraft Launcher as usual, but instead of clicking Play right away, make a new profile first by clicking on the New Profile button in the lower right corner of the launcher. In the Version Selection section, select Use Version: Release 1.7.10.

image05

Click Save Profile, and click Play. You need to click Play in order for Minecraft to download the 1.7.10 files that Forge needs for modding. After it loads, quit Minecraft.

5b. Install Forge for 1.7.10

Download the Forge install file for version 1.7.10 from the minecraftforge.net site: http://files.minecraftforge.net/maven/net/minecraftforge/forge/1.7.10-10.13.3.1395-1710ls/forge-1.7.10-10.13.3.1395-1710ls-installer.jar

Run the installer, and leave the default option set to Install Client.

5c. Install ComputerCraft latest version

Download the latest ComputerCraft file, currently ComputerCraft1.73.jar as of this writing.

Next, put the downloaded file into your Minecraft Mods folder. Here’s how:

Windows
Click Start → Run.
Enter in %appdata%\.minecraft into the run dialog box, and this will open your Minecraft directory.
Open the Mods directory.

Mac
Click on the Finder.
Select Go → Go To Folder…
Enter in ~/Library/Application Support/minecraft and press the Go button.
Open the Mods folder.

Finally, put the ComputerCraft1.73.jar file into your Mods folder.

6. Program ComputerCraft to watch your tripwire

When you launch Minecraft, select the new Forge profile. This will load your newly modded version of Minecraft.

Now we’re ready to send the same signals from our tripwire in Minecraft! First, set up 2 tripwire hooks, with string connecting them. Place a ComputerCraft computer near one end of the tripwire, connecting the back of the computer to the tripwire hook with redstone.

image00

Right-click on the Computer to access its screen. You’ll see a black screen with “CraftOS 1.7” at the top, and a blinking cursor. We’re going to create a new program called Alarm by typing in “edit Alarm” and pressing enter.

image01

Now, line by line, type in this program. Change YOUR_DEVICE_ID_HERE and YOUR_ACCESS_TOKEN_HERE to the values you discovered back in Step 4.

sparkURL = "https://api.spark.io/v1/devices/YOUR_DEVICE_ID_HERE/alarmOn"
sparkToken = "access_token=YOUR_ACCESS_TOKEN_HERE"
while true do
os.pullEvent("redstone")
if redstone.getInput("back") == true then
print("Back activated")
returnData = http.post(sparkURL, sparkToken)
returnData.close()
end
end

image02

When you’re finished, press Ctrl to access the menu, and select Save, then Exit.

You will be back at the flashing cursor prompt now. Simply type in “Alarm” and press Enter to start your program running. It will continuously run, waiting for a redstone signal at its back. Done!

7. Now test your Minecraft alarm in the real world.

Run through the tripwire to test it, and your Spark Core alarm will be activated! You’ll hear your voice module and speaker: “Intruder alert!”

Taking It Further

Once you have this simple project up and running, you can use these techniques for all kinds of projects! Anything you can program with an Arduino can now be activated from Minecraft — robots, cellphone shields, there’s no limit.

And anything that can send a redstone signal in Minecraft can be used to activate the ComputerCraft program. Pressure plates, switches, buttons, minecart pressure plates, and more.

Get creative and make some Minecraft-activated robots of your own design!