EV3

Setting up EV3

  • Turn on EV3
  • Connect to computer with USB

For LabVIEW 2017:

  • Open the following link: https://calamari.bitballoon.com/
  • At the top of the screen you'll see a pop-up, download and install the EV3 driver on your computer
  • If necessary, update your EV3 to the latest firmware
  • After device updates, unplug USB from comptuer
  • Turn the EV3 off and back on again and plug in
  • Connect desired sensor to one of the EV3 numbered ports (a good one to start with is the UltrasonicSensor)

LabVIEW to Thingworx

For LabVIEW 2017:

1. Get the IoT Devices folder from Chris or someone who has it and save it within your user.lib

2. Test that the EV3 is connecting to your computer:

      • Open LabVIEW
      • Open the VI: LEGO EV3_Demo from within User Libraries, IoT Devices, LEGO EV3
      • Run the code, and your device should beep once and turn the motors if any are connected. This will show if your EV3 is connected properly

3. To get a sensor value from the EV3:

      • Open a new VI (see example code below)
      • Copy and paste the LEGO EV3 ExpressVI from LEGO EV3_Demo
      • Add the LEGO EV3_GetDeviceListing, LEGO EV3_Connect, LEGO EV3_Ports and LEGO EV3_Connect VI's
        • For LEGO EV3_GetDeviceListing, create a constant for the "desired name" input
          • Go to the settings tab on the EV3 and find the brick name - enter this as your constant
        • For the first EV3_Connect, set the boolean value to True, for the second set it to False (this connects and then disconnects from the EV3)
      • LEGO EV3_Ports has an output simply called "value", wire this to an Unbundle by Name
        • Within the cluster titled "hardwarePortValues" are the port ID's (1.1, 1.2, 1.3, 1.4, 1.A, 1.B, 1.C, 1.D), the type ID of the sensor or motor and the value that the sensor or motor is reading
      • Use Array Subset to pull only the data from the first 8 clusters (ports 1.1-1.D)
      • Next use Array to Cluster and wire that into Unbundle by Name again
        • Tip: you will need to drag down on the bottom of your Unbundle by Name function in order to expand it to three values. Click on the each one and use the drop down menu to select your desired port and data type
      • You should now have three separate outputs for each of: portID, typeID and value
      • Make sure you have selected the portID, typeID and value for the port to which your sensor is connected (if you have indexed from 0 to 7, the IDs will line up such that 0 is 1.1, 1 is 1.2, etc.)
      • It is usually the data type "value" that is usually pushed to Thingworx

For LabVIEW 2016 (w/LEGO Mindstorms)

1-3. To get a sensor value from the EV3:

      • Open a new VI (see example code below)
      • From within the MINDSTORMS Robotics folder, I/O folder, select the Read Sensor VI
      • Use the drop down under the VI to select your desired sensor
      • The output will vary with the sensor and mode (For example, Read EV3 Color - Detect Mode outputs one of 7 colors, Read Touch Sensor - Pressed outputs a Boolean of T = sensor pressed or F = not pressed)

For LabVIEW 2016/2017:

4. Push values to Thingworx:

      • Wire your desired value to Number to Decimal String (or other appropriate conversion)
      • Use Bundle to make a cluster of 4 strings: the Thing Name, the Property Name, the data type, and your sensor value (2017: easiest to do this by opening up Thingworx_Demo from IoT Devices and copying the "Property In" cluster from there)
      • Edit the constants so they have the correct names and data type
      • Wire the cluster to the input node: Property In on the Thingworx_Write VI
      • Wire in the output of the ThingworxAppKey VI to the input node: Device In on the Thingworx_Write VI
        • Note: open up the ThingworxAppKey VI and double check that the AppKey inside the header is correct
      • Tip: Put all of your code inside a while loop with a stop button control so you can run it continuously until you decide to stop

5. Get values from Thingworx:

      • To get values from the Thingworx cloud, use the Thingworx_Read VI (see example code below)
      • The Device In input to Thingworx_Read is the AppKey VI (can also wire in the Device Out node from Thingworx_Write if wiring them up within the same VI)
      • The Property In input to Thingworx_Read is a cluster containing your Thing Name, Property Name, data type and an empty constant
      • Unbundle the output cluster from Thingworx_Read and from here you can pull and manipulate the data in which you're interested (usually the value)

Important Note

  • This tutorial is intended to simply introduce the basics for how to go about getting sensor values from the EV3 and send them up to Thingworx - in order to pull values from multiple sensors at the same time and post to different properties, each sensor will need its own Thingworx_Write VI (see example code)

Scratch

Downloading and Installing the "Scratch Device Plugin":

  1. Choose the right version of the Scratch plugin needed depending on what device you are using (do not use Chrome) by going to the site "https://scratch.mit.edu/info/ext_download/" .
  2. Once downloaded, open it and follow the instructions to install the plug-in.
  3. Open the site “http://scratchx.org/#extensions” .
  4. Select the "EV3+Scratch" extension.
  5. Go to your browsers preferences and find Internet Plug-ins and allow the “Scratch Device Plugin” to run on “scratchx.org” .

Connecting an EV3 to ScratchX via Bluetooth:

  1. Open Settings on your EV3 device (all the way to the right) and go to "Brick Name".
  2. Change the brick's name to the form "serialXXXXXX" (must start with "serial" in lower caps so Scratch can automatically recognize the device).
  3. Return to the Settings and select "Bluetooth" and turn Bluetooth on. Then also check "Visibility" and "iPhone/iPad/iPod".
  4. Go to Bluetooth on your computer and your EV3 should show up. Press Connect. A pop-up may appear on the EV3 screen asking if you would like to connect to the computer. When you say yes, a code will appear (default: 1234). If you just press OK, all you have to do is make sure to type in the same code on your computer screen when prompted.
  5. It will say "Connected" for about 5 seconds then will disconnect. This should happen.
  6. Once it disconnects, return to the "Bluetooth" Setting on the EV3 and deselect the "iPhone/iPad/iPod" box and hit the check mark. You should hear a series of three tones play form the EV3 and see a green dot next to "EV3 Control" on the ScratchX site, showing that it is connected and can be controlled via ScratchX.

Opening the Scratch Thingworx Extension in your existing ScratchX project:

  1. Download a text editor that is compatible with JavaScript. Copy and paste the code found at "https://github.com/wcrooks/Scratch-Thingworx-Extension" called "IoTextension.js" in to your text editor and save it. Change the "url" under "getData" and "sendData" to http://localhost:XXXX" where "XXXX" is a port number of your choice (ex. 8001).
  2. Control click on the "Load Experimental Extension" option in your ScratchX project and select the JavaScript file you just saved. An extension named "Learninator" with two blocks labeled "dataOut" and "dataIn" should appear below the "EV3 Control" extension in your ScratchX project.
        • The "dataOut" block is the data that is sent to Thingworx from Scratch and is associated with the "sendData" function in the extension javascript.
        • "The "dataIn" block represents the data that is received from Thingworx and is associated with the "getData" function in the extension javascript.
  3. You can now connect things such as the dataOut block to the measure distance block to send the value from the Ultrasonic Sensor on an EV3 to Thingworx.

Connecting to Thingworx:

  1. Open up an HTTP Server VI in LabVIEW that sits on a port number and listens for any commands coming through that port. Make sure the port number the VI is listening to is the same as the port number in the url in the javascript extension.
  2. Create a LabVIEW code that analyzes the HTTP request that comes through the listening port and either sends or reads data from Thingworx depending on whether it is a GET or POST request using the Thingworx Read and Write VI's. An example code that receives a POST command from Scratch and posts the value to Thingworx is below: