Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spacebrew, Unity3d, Processing & Arduino

Stacey
July 29, 2014

Spacebrew, Unity3d, Processing & Arduino

Add a physical element to your Unity project using Spacebrew, a web sockets solution built on Node. Full tutorial here: https://onedrive.live.com/redir?resid=7F39F5A167CA774F%2112049
Github repo:
https://github.com/bitchwhocodes/Spacebrew-Unity3d-Arduino-Processing

Stacey

July 29, 2014
Tweet

More Decks by Stacey

Other Decks in Technology

Transcript

  1. 2

  2. When connecting different devices – software & hardware, there are

    a few options that exist. OSC and Web sockets are two common ways to connect devices. OSC is a communication protocol that utilizes a URL format to transfer data - it was originally created with the intention of communicating information between musical instruments- hence the name OSC ( Open Sound Communication). Many creative coders & hobbyists use this protocol as you can find a library for pretty much any language you might be working in. Web sockets is another common way for multiple devices to communicate. Spacebrew is described as a “open source toolkit to create interactive spaces “ . Built in Node, it utilizes Web Sockets as its communication protocol for real time communication. http://docs.spacebrew.cc/ 3
  3. In Spacebrew there are a few key components. Clients –

    are unique and connect to the Spacebrew server. A client can be both a Publisher ( sending messages ) and a Subscriber ( receive messages). A client can subscribe to data properties – and can publish changes to data properties. Clients can listen or publish to one another by being connected together on the data property. For example, if a web page ( client A ) is a publisher of a data property named “visitorCount” , another client like a Python app could subscribe to that client by connecting one to the other on the same data property. Clients, and publishers/subscribers are connected to each other through an admin page which gives you full control over who listens or publishes what and to whom. The data property must share the same type and name before you can connect two clients together. 5
  4. Spacebrew can transfer three different types of data currently :

    Boolean ( true/false), String, or an Ranges. 6
  5. Spacebrew has a variety of sdk’s available for a bunch

    of language http://docs.spacebrew.cc/exampleses 7
  6. You can run spacebrew locally ( or put it up

    on a remote server if you want to ) For local installation 1. Make sure you have Node installed. Not sure? Open your terminal / command and type -> node –v . If you get a number back, you have it installed. If you don’t and it doesn’t recognize that command you need to go and installed Node. 2. Download Spacebrew from the GitHub Repo (https://github.com/spacebrew/spacebrew/zipball/master ) and unpack the zip. 3. Navigate to the unpacked Spacebrew directory. 4. Install the dependencies for Spacebrew. They are forever and ws – forever will restart the server in the event it just ups and quits, and ws is for websockets. Npm install ws. Npm install forever-monitor 5. You are already in the Spacebrew directory. Time to start the server – node node- forever-server.js GIST FOR COMMANDS - https://gist.github.com/bitchwhocodes/8e1b3e606ef82ac3c3c8 6. Be default,Spacebrew runs on port 9000. Unless you start it with a different port, you should be able to go to http://localhost:9000 . If its up, you will see a page rendered. Ok, we have Spacebrew running. Time to start connecting clients. 9
  7. 7. Open the index.html page in the admin folder of

    the Spacebrew directory you are in. You can just drop it in the browser. We will refer to this when we start to connect clients. 9
  8. Time too hook up Unity3d into the mix with spacebrew.

    First things first, you can download all code here: https://github.com/bitchwhocodes/Spacebrew-Unity3d-Arduino-Processing You can read my blog post here: http://thebitchwhocodes.com/?p=7191 10
  9. 1. Download Unity3d sdk for Spacebrew https://github.com/Spacebrew/spacebrewUnity 2. Unpack it

    3. Drag the SpacebrewClient package file into your Unity Project – or create a new Unity Project and add it in 11
  10. 4. Select Import. You will get a dialog – for

    now just import everything 5. Once imported you will see the spacebrew folder in your project folder / assets 12
  11. 1. We are going to want to use the Spacebrew

    Prefab. Drag the prefab into the scene 2. Configure the Prefab by selecting it, and bringing up the Property Inspector. 3. Set the server address to be the address you set your server to run. By default, Spacebrew runs on port 9000. You need the “ws” in the address, it would not work without it, although you will get a warning that mentions this will be deprecated in future versions of the SDK 4. For this example, we want Unity to publish an event, and to listen to that same event 5. Under the Publishers section, change the Pub Type to be STRING, and change its name to be “button_pressed”. 6. Under the Subscribers section, change the Sub type to be STRING and change its name to be “button_pressed”. 7. Now, the SpacebrewClient.cs will take care of connecting, and configuring it so that we can publish and subscribe to this property channel. You can open up that file and take a look to see what it is doing – if you add more publish properties or subscription properties, it will just dynamically take what was entered in the properties panel and do the work of configuring for you. 8. SpacebrewEvents.cs is the file we can see how it references the prefab,and 13
  12. handles the callbacks for subscriptions. Currently it just traces it

    out, but you could write your own code here to do whatever you wanted. 9. Press Play – and open the console in Unity. You should get some information being written out that tells you if the server connection worked or not. 13
  13. 1. Open the admin page from the Spacebrew folder –

    if you didn’t already open it, it is in the admin folder ( admin/index.html). Just drop it in a browser, it doesn’t need to be served up. 2. You might need to refresh it. You should see our Unity client connected – showing that it is both a Publisher and a Subscriber of the button_pressed property, which is a String. 3. Now Unity is ready to go – it can publish no problem. But it won’t receive those events until we connect the publisher and subscriber. 4. Click the circle on the publisher property – and then click the circle on the subscriber property. We can connect these two because a) the property name is the same and b) the property data type is the same. 5. Once you connect them, it should draw a link between them, showing they are linked. 6. To test, we need to send a message – and see if we receive that event. 14
  14. 1. Open the admin page from the Spacebrew folder –

    if you didn’t already open it, it is in the admin folder ( admin/index.html). Just drop it in a browser, it doesn’t need to be served up. 2. You might need to refresh it. You should see our Unity client connected – showing that it is both a Publisher and a Subscriber of the button_pressed property, which is a String. 3. Now Unity is ready to go – it can publish no problem. But it won’t receive those events until we connect the publisher and subscriber. 4. Click the circle on the publisher property – and then click the circle on the subscriber property. We can connect these two because a) the property name is the same and b) the property data type is the same. 5. Once you connect them, it should draw a link between them, showing they are linked. 6. To test, we need to send a message – and see if we receive that event. 7. Download the simple Unity Project here to test > https://github.com/bitchwhocodes/Spacebrew-Unity3d-Arduino-Processing. Take the unity folder and run that project. You can use this one now if you want, as it has a simple textfield and button set up. 15
  15. 1. To connect to an Arduino, I am going to

    use Processing, and the Spacebrew Processing SDK. At first we won’t need the Arduino, we’ll make sure we can connect via Processing, then we’ll add the Arduino in. 2. We will need to download the Processing IDE ( I used 32bit on Windows so that it will support the Serial communication for the Arduino ) and the Spacebrew for Processing SDK https://processing.org/download/ and get the Arduino Library https://processing.org/download/ 3. Install the Libraries http://wiki.processing.org/w/How_to_Install_a_Contributed_Library 4. Restart Processing 5. Copy the code from this GIST https://gist.github.com/bitchwhocodes/dfeec09ce4724078d28d 6. Set the server address to be the address of your local Spacebrew install in the code 7. Run Play 17
  16. 1. If the libraries are installed, you should be able

    to look at the Spacebrew examples. Click File -> Examples. You should see Arduino and Spacebrew in there now. 2. Copy the code from this GIST https://gist.github.com/bitchwhocodes/dfeec09ce4724078d28d 3. Set the server address to be the address of your local Spacebrew install in the code 4. Run Play 18
  17. 1. Open the Spacebrew admin ( admin/index.html in the Spacebrew

    folder ) 2. Refresh your page 3. See if your processing client now shows up and is listed. 4. Your processing sketch should also show you if it connected 19
  18. You now need to connect the clients > publishers/ subscribers

    in the Spacebrew admin page. I connected them to each other and to listen to themselves – that way I can test each piece independently. 20
  19. At this point, we can probably just trace out values

    and see that Unity can communicate to Processing. In our But perhaps we want to add something else into the mix? 21
  20. I have an Arduino Uno, a push button, an LED,

    two resistors ( 10k for the button, 220k for the LED ). Connect the button to 5v and then through the transistor into ground and pin 2. Connect the led to ground, and through the resistor to pin 12. You can get these images: http://thebitchwhocodes.com/?p=7191 23
  21. 1. Open the Arduino IDE ( download if you don’t

    have it ) 2. Select the board from Tools > Board ( Arduino Uno for example, as this is the one I have. Check what you have and select that) 3. Select the Serial Port . If it is grayed out, and you are on Windows 8 > read this tutorial. 4. Put the Standard Firmata on the Arduino. Go to File > Examples > Firmata > Standard Firmata 5. Hit upload 24
  22. 1. Now we just need to do a standard format

    on the Arduino so it can communicate with Procesing. 2. Put the Standard Firmata on the Arduino. Go to File > Examples > Firmata > Standard Firmata 3. Hit upload 25
  23. 1. We want to test that processing can talk to

    Arduino before we start adding in Spacebrew – build in steps, debug in steps. 2. Copy and paste this GIST > 3073e62e86ea1b4a58fd – Processing will communicate to Arduino over the serial port. 3. Press the button down – LED should blink. 4. We are now ready to hook it all up. 26
  24. 1. Copy and Paste this GIST https://gist.github.com/bitchwhocodes/76bebb4694d6552e2cc3 into the Processing

    IDE. It does the same thing as before, but it publishes and subscribes to events from Spacebrew. 2. Make sure Unity is on Play, play the Processing Sketch 3. Open the admin panel in Spacebrew – and make sure publishers and subscribers are connected. 4. Click the button on the circuit. You should see a trace in the Unity console. Debugging: First, take steps apart. For example if you are getting messages to Processing but the Arduino circuit is not working, try testing just that circuit and remove the complexities ( do a basic Arduino sketch ). Make sure you have the property names the same (“button_pressed”) and types the same. Make sure both clients show up in the admin panel for Spacebrew and that you have connected them. 27
  25. Granted, not that exciting. I’ll post how to make your

    own DDR mat and maybe have something a little more thrilling to add into the mix. Now we have one way communication here in terms of Arduino - > processing to Unity. You can easily create a button in Unity to broadcast or send a message that we could capture from Processing to light that LED. 27