99
Oracle IoT Kids Workshop Stephen Chin Java Technology Ambassador JavaOne Content Chair @steveonjava

Oracle IoT Kids Workshop

Embed Size (px)

DESCRIPTION

Internet of Things workshop for kids. Shows how you can use Java on Raspberry Pi and Lego Mindstorms with some fun projects that are easy to program.

Citation preview

Page 1: Oracle IoT Kids Workshop

Oracle IoT Kids WorkshopStephen ChinJava Technology AmbassadorJavaOne Content Chair

@steveonjava

Page 2: Oracle IoT Kids Workshop

What Runs Java?

Page 3: Oracle IoT Kids Workshop

What Runs Java?

Page 4: Oracle IoT Kids Workshop

Java and 3G in a Tiny Package

> Cinterion EHS5

Page 5: Oracle IoT Kids Workshop

Really Tiny…

27.6mm

18.8

mm

Page 6: Oracle IoT Kids Workshop

http://upload.wikimedia.org/wikipedia/commons/3/3d/Cloud_forest_Ecuador.jpg

Page 7: Oracle IoT Kids Workshop

=

Have Java With Your DessertRaspberry Pi

Page 8: Oracle IoT Kids Workshop

Pis are Affordable

$35

Page 9: Oracle IoT Kids Workshop

Pis are Affordable

$35 1 Box of Diapers

Bicycle(but just 1 wheel)

A Cake

Page 10: Oracle IoT Kids Workshop

Chalkboard Electronics Touchscreen

10" or 7" Form Factor

Connects via HDMI/USB

Tested with JavaFX 8

10% Exclusive Discount:

G1F0U796Z083

Page 11: Oracle IoT Kids Workshop

How to Setup Your Pi

> Step 1: Install Linux

> Step 2: Download/Copy Java 8 for ARM

> Step 3: Deploy and Run JVM Language Apps

http://steveonjava.com/javafx-on-raspberry-pi-3-easy-steps/

Page 12: Oracle IoT Kids Workshop

Electronic Safety!

> Unplug from wall before wiring

> Get rid of static by touching a metal surface

> Don't touch exposed wires/metal

> Never remove/insert SD Card while power is on

12

Page 13: Oracle IoT Kids Workshop

What Comes in Your Lab Kit

1. Touch Screen2. SD Card3. Keyboard4. Yellow Box:

Power Adapter LVDS Cable/Board Raspberry Pi Model B Mini-USB Cable (power)

Please Save All the Packaging for Later

Page 14: Oracle IoT Kids Workshop

Hooking Up the Pi (Part A)

1. Insert the SD Card in to the Pi Will appear upside down when looking at the top

of your Pi

2. Insert the HDMI board into the Pi's HDMI jack

3. Connect the Pi power to the HDMI board Use the Micro USB Cable (short one)

14

Important: Connect everything before plugging into the wall

Page 15: Oracle IoT Kids Workshop

Hooking Up the Pi (Part B)

4. Slide the LCD cable into the back of the display Side with gold connectors goes up Be careful, the connector is fragile!

5. Hook up the USB keyboard6. Connect the USB end to one of the Pi's USB host ports

This provides touch input

15

Verify connections and plug into power now

Page 16: Oracle IoT Kids Workshop

Is it Working?

> Should get a bunch of flashing LEDs to indicate booting Boot takes approx 30 seconds

> The LCD screen should light up Might be dim if the light sensor is obstructed

> And you will should see a Linux boot screen with lots of text

Page 17: Oracle IoT Kids Workshop

Logging In

At the login prompt type your username:> piAnd enter the password:> raspberry

Page 18: Oracle IoT Kids Workshop

Running the JavaFX Sample Application

Change directory to the project folder> cd MaryHadALittleLambdaRun the build script> ant

Page 19: Oracle IoT Kids Workshop

19

Page 20: Oracle IoT Kids Workshop

Hacking the Code

Run the nano text editor:> nano src/sample/MapObject.javaSave your changes:> Control-O EnterExit Nano:> Control-XCompile/Run:> ant

Page 21: Oracle IoT Kids Workshop

Mary Had a Little Lambda

Mary had a little lambdaWhose fleece was white as snowAnd everywhere that Mary wentLambda was sure to go!

https://github.com/steveonjava/MaryHadALittleLambda

Page 22: Oracle IoT Kids Workshop

Generating Streams

From a collection:> anyCollection.stream();Known set of objects:> Stream.of("bananas", "oranges", "apples");Numeric range:> IntStream.range(0, 50)Iteratively:> Stream.iterate(Color.RED, > c -> Color.hsb(c.getHue() + .1, c.getSaturation(), > c.getBrightness())); 22

Page 23: Oracle IoT Kids Workshop

Let's Create Some Barn Animals!

SpriteView tail = s.getAnimals().isEmpty() ? s : s.getAnimals().get(s.getAnimals().size() - 1);

Stream.iterate(tail, SpriteView.Lamb::new) .skip(1).limit(7) .forEach(s.getAnimals()::add);

23

Page 24: Oracle IoT Kids Workshop

24

Page 25: Oracle IoT Kids Workshop

Filtering Streams

Predicate Expression> public interface Predicate<T> {> public boolean test(T t);> }

Filter out minors> adults = attendees.filter(a -> a.getAge() >= 1.8)

25

Page 26: Oracle IoT Kids Workshop

Rainbow-colored Lambs!

s.getAnimals().stream() .filter(a -> a.getNumber() % 4 == 2) .forEach(a -> a.setColor(Color.YELLOW));s.getAnimals().stream() .filter(a -> a.getNumber() % 4 == 3) .forEach(a -> a.setColor(Color.CYAN));s.getAnimals().stream() .filter(a -> a.getNumber() % 4 == 0) .forEach(a -> a.setColor(Color.GREEN));

26

Page 27: Oracle IoT Kids Workshop

27

Page 28: Oracle IoT Kids Workshop

Filtering Collections

Collection.removeIf> Removes all elements that match the predicateList.replaceAll> In-place filtering and replacement using an unary operator

ObservableCollection.filtered> Returns a list filtered by a predicate this is also Observable

28

Page 29: Oracle IoT Kids Workshop

Picky Eaters…

Predicate<SpriteView> pure = a -> a.getColor() == null;

mealsServed.set(mealsServed.get() + s.getAnimals().filtered(pure).size());

s.getAnimals().removeIf(pure);

29

Page 30: Oracle IoT Kids Workshop

30

Page 31: Oracle IoT Kids Workshop

Mapping Streams

Applies a Map Function to each element:> Function<? super T, ? extends R>

Result: List is the same size, but may be a different type.

31

Page 32: Oracle IoT Kids Workshop

Single Map

s.getAnimals().setAll(s.getAnimals() .stream() .map(sv -> new Eggs(sv.getFollowing()) .collect(Collectors.toList()));

32

Page 33: Oracle IoT Kids Workshop

Or a Double Map!

s.getAnimals().setAll(s.getAnimals() .stream() .map(SpriteView::getFollowing) .map(Eggs::new) .collect(Collectors.toList()));

33

Page 34: Oracle IoT Kids Workshop

34

Page 35: Oracle IoT Kids Workshop

Flat Map

Applies a One-to-Many Map Function to each element:> Function<? super T, ? extends Stream<? extends R>>And then flattens the result into a single stream.

Result: The list may get longer and the type may be different.

35

Page 36: Oracle IoT Kids Workshop

Hatching Eggs

s.getAnimals().setAll(s.getAnimals() .stream() .flatMap(SpriteView.Eggs::hatch) .collect(Collectors.toList()));

36

Page 37: Oracle IoT Kids Workshop

37

Page 38: Oracle IoT Kids Workshop

Reduce

Reduces a list to a single element given:> Identity: T> Accumulator: BinaryOperator<T>

Result: List of the same type, but only 1 element left.

38

Page 39: Oracle IoT Kids Workshop

And the (formerly little) Fox ate them all!

Double mealSize = shepherd.getAnimals() .stream() .map(SpriteView::getScaleX) .reduce(0.0, Double::sum);

setScaleX(getScaleX() + mealSize * .2);setScaleY(getScaleY() + mealSize * .2);shepherd.getAnimals().clear();

39

Page 40: Oracle IoT Kids Workshop

40

Page 41: Oracle IoT Kids Workshop

Mary Had a Little Lambda Project

> Open-source project to demonstrate lambda features> Visual representation of streams, filters, and maps

41

https://github.com/steveonjava/MaryHadALittleLambda

Page 42: Oracle IoT Kids Workshop

Stuff to do…

> Changes to MaryHadALittleLambda: Change the number of sheep Make the rainbow have different colors Change the fox size to be fatter/skinnier Add new graphics (additional image files under images/extra)

42

camel.png lion.png greendragon.png cow.png Brownbear.png

Page 43: Oracle IoT Kids Workshop

Stephen Chintweet: @steveonjavablog: http://steveonjava.com

nighthacking.com

Real GeeksLive Hacking

NightHacking Tour

Hacking Time!

Page 44: Oracle IoT Kids Workshop

GPIO access

Page 45: Oracle IoT Kids Workshop

Wiring LEDs

> Wire the elements in series: Connect the long end

of the LED to GPIO Connect the short end

of the LED to the resistor

Connect the resistor to Ground

45

To GPIO To Ground

Page 46: Oracle IoT Kids Workshop

Using a Breadboard

> Pins are connected horizontally in the center

> The edges are connect vertically

> No current passes the center line

46

Page 47: Oracle IoT Kids Workshop

Pi Cobbler

> Connects your Pi to the Breadboard

> The white line is Pin 1

> Connect it in the center top of your Breadboard

47

Page 48: Oracle IoT Kids Workshop

Pi4J

> The samples are located in/opt/pi4j/examples

> Compile the samples by typing "./build"> Instructions for running the samples are

printed out at the end of the build> Pi4j needs root access to use GPIO (use

"sudo")

48

Page 49: Oracle IoT Kids Workshop

ControlGpioExample

49

> Demonstrates Controlling Pins: pin.low() pin.toggle() pin.pulse(duration, blocking)

> Try creating your own pattern!

sudo java -classpath .:classes:/opt/pi4j/lib/'*' BlinkGpioExample

Page 50: Oracle IoT Kids Workshop

Wiring Buttons

> Connect a circuit across two legs

> While the button is pressed… The legs numbered 1

and 2 are connected The legs numbered 3

and 4 are connected

50

Page 51: Oracle IoT Kids Workshop

ListenGpioExample

51

> Demonstrates Listening to a pin: addListener(pinListener)

> Change the message/action whena button is pressed!

sudo java -classpath .:classes:/opt/pi4j/lib/'*' ListenGpioExample

Page 52: Oracle IoT Kids Workshop

BlinkGpioExample

52

> Use LEDs and Buttons together!

sudo java -classpath .:classes:/opt/pi4j/lib/'*' BlinkGpioExample

Page 53: Oracle IoT Kids Workshop

Stephen Chintweet: @steveonjavablog: http://steveonjava.com

nighthacking.com

Real GeeksLive Hacking

NightHacking Tour

Hacking Time!

Page 54: Oracle IoT Kids Workshop

LeJOSHow it works on the EV3

Page 55: Oracle IoT Kids Workshop

The Heart of the EV3

> TI Sitara AM1808 ARM9, 300Mhz

> 64MB RAM / 16MB Flash> Analog to Digital Converter> 4 Motor Ports> 4 Sensor Ports> Bluetooth / USB> MicroSD

Page 56: Oracle IoT Kids Workshop

EV3 Motors

Page 57: Oracle IoT Kids Workshop

EV3 Sensors

Page 58: Oracle IoT Kids Workshop

Color and Light Sensor

Page 59: Oracle IoT Kids Workshop

High frequency sound waves

Measuring mode Vs Presence Mode

Ultrasonic Sensor

Page 60: Oracle IoT Kids Workshop

Infrared Sensor

Page 61: Oracle IoT Kids Workshop

Remote Control

Page 62: Oracle IoT Kids Workshop

Getting Started with LeJOS

> Micro SD Card (> 2GB)> Compatible WIFI adapter

NetGear WNA1100 EDIMAX EW-7811Un

> Linux (or a Linux VM)

> Details here:

Creating Your SD Card

http://sourceforge.net/p/lejos/wiki/Home/

Page 63: Oracle IoT Kids Workshop

Lego Duke Segway

Page 64: Oracle IoT Kids Workshop

Bluetooth Pairing

> Make sure your Lego is turned on> Open "Devices and Printers" from the

Start menu> Click "Add a device"> Select the Lego brick> After pairing, right click on the new

device and choose "Connect using" > "Access point"

Page 65: Oracle IoT Kids Workshop

Eclipse Setup

> Open Eclipse> Go to "Preferences"> Click on leJOS EV3> Change the brick name to

"10.0.1.1"

65

Page 66: Oracle IoT Kids Workshop

Creating a New LeJOS Project

> Go to "File" > "New" > "Project…"> Choose a LeJOS EV3 Project

66

Page 67: Oracle IoT Kids Workshop

Create a Class File

> Create a new class ("File" > "New" > "Class")

> Give it a package (e.g. sample)> Give it a name (e.g. LCDTest)

67

Page 68: Oracle IoT Kids Workshop

Simple LeJOS Application

import lejos.nxt.Button;import lejos.nxt.LCD;public class EV3FirstProgram { public static void main(String[] args) { LCD.clear(); LCD.drawString("First EV3 Program", 0, 5); Button.waitForAnyPress(); LCD.clear(); LCD.refresh(); }}

Page 69: Oracle IoT Kids Workshop

Stephen Chintweet: @steveonjavablog: http://steveonjava.com

nighthacking.com

Real GeeksLive Hacking

NightHacking Tour

Hacking Time!

Page 70: Oracle IoT Kids Workshop

Parts you will need Step 1

Assemble Brace

Page 71: Oracle IoT Kids Workshop

Step 2 Completed Brace

Assemble Brace

Page 72: Oracle IoT Kids Workshop

Parts you will need Step 1

Build Base

Page 73: Oracle IoT Kids Workshop

Step 2 Completed Base

Build Base

Page 74: Oracle IoT Kids Workshop

Motor parts Snap them on partially

Assemble Motor

Page 75: Oracle IoT Kids Workshop

Take motor and base And connect them like this

Assemble Motor

Page 76: Oracle IoT Kids Workshop

Foot parts Step 1

Add a Foot

Page 77: Oracle IoT Kids Workshop

Step 2 Add the Foot to the Base

Add a Foot

Page 78: Oracle IoT Kids Workshop

Assembled Foot and Base

Add a Foot

Page 79: Oracle IoT Kids Workshop

Lock parts Partially insert the red attachers

Add a Lock

Page 80: Oracle IoT Kids Workshop

Attach the lock Push down the red attachers to secure

Add a Lock

Page 81: Oracle IoT Kids Workshop

Tower parts Step 1 – push the rod all the way through

Construct the Tower

Page 82: Oracle IoT Kids Workshop

Step 2 Attach the Tower to the Base

Construct the Tower

Page 83: Oracle IoT Kids Workshop

Completed Tower

Construct the Tower

Page 84: Oracle IoT Kids Workshop

Fan Motor parts Step 1

Build the Fan Motor

Page 85: Oracle IoT Kids Workshop

Completed Fan Motor

Build the Fan Motor

Page 86: Oracle IoT Kids Workshop

Light Sensor parts Step 1

Construct the Light Sensor

Page 87: Oracle IoT Kids Workshop

Completed Light Sensor

Construct the Light Sensor

Page 88: Oracle IoT Kids Workshop

Fan Blade parts Step 1

Assemble the Fan Blades

Page 89: Oracle IoT Kids Workshop

Fan Blade, Light Sensor, and Fan Motor Completed Fan

Assemble the Fan

Page 90: Oracle IoT Kids Workshop

Assembled Wind Turbine

90

Page 91: Oracle IoT Kids Workshop

Wind Turbine Wiring

> Port A – Medium Moto Power Fan

> Port B – Large Motor Rotate Wind Turbine

> Port 1 – Light Sensor Track Ambient Light

91

Page 92: Oracle IoT Kids Workshop

Gear Box parts Step 1

EC: Gear Box Construction

Page 93: Oracle IoT Kids Workshop

Completed Fan with Gear Box

EC: Gear Box Construction

Page 94: Oracle IoT Kids Workshop

Making the Wind turbine turn

public class WindTurbine { public static void main(String[] args) { EV3MediumRegulatedMotor fan = new EV3MediumRegulatedMotor(MotorPort.A); fan.setSpeed(1500); fan.setAcceleration(150); fan.backward(); Delay.msDelay(10000); }}

Page 95: Oracle IoT Kids Workshop

Making the Tower spin

public class WindTurbine { public static void main(String[] args) { EV3LargeRegulatedMotor base = new EV3LargeRegulatedMotor(MotorPort.B); base.setSpeed(80); base.rotateTo(-90); base.rotateTo(90); }}

Page 96: Oracle IoT Kids Workshop

Checking the Ambient Light

public class WindTurbine { public static void main(String[] args) { EV3ColorSensor light = new EV3ColorSensor(SensorPort.S1); float[] sample = new float[1]; SensorMode mode = light.getAmbientMode(); mode.fetchSample(sample, 0); LCD.drawString("Light = " + sample[0], 0, 4); Delay.msDelay(5000); }}

Page 97: Oracle IoT Kids Workshop

Putting it all together…

> Can you write a program that will:1. Rotate the fan in a circle2. Check the ambient light while rotating3. Move back to the brightest angle4. Spin the fan

http://commons.wikimedia.org/wiki/Wind_generator#mediaviewer/File:Eolienne_et_centrale_thermique_Nuon_Sloterdijk.jpg

Page 98: Oracle IoT Kids Workshop

Stephen Chintweet: @steveonjavablog: http://steveonjava.com

nighthacking.com

Real GeeksLive Hacking

NightHacking Tour

Page 99: Oracle IoT Kids Workshop

Safe Harbor Statement

The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.