My Make Projects
Introduction
This experiment moves a Little Crane arm (used some Lego parts) up and down via browser connected to a B4J websocket server.
When the arm moves, the Red LED is turned on else the Green LED is on (waiting for the servo to move).
Experiment uses as hardware a Raspberry Pi, Arduino & mini Servo Motor and is with B4R and B4J from Anywhere Software.

The Servo
The servo moves the arm between 30 and 90 degrees angle (defined in the server settings file located in the dirapp folder littlecrane.set [a map]).
Depending on the servo used, pressing multiple times on a move button might be required to move the crane arm (ToDo: test with another servo type).
If the servo is not moving, its starts to jitter. To avoid jittering, the servo motor is detached and attached again when pressing a move button.
Note: there are various threads in the Arduino forums about this problem; tried several solutions where as detach and attach works best.

The Server The B4J server is running on a Raspberry Pi, which has an Arduino connected via an USB port (/dev/ttyACM0).
The server sends array of bytes to the Arduino: byte 0 = servoid (in case more then one servo = future thinking), 1 = angle position, 2 = 0 to set new angle or 1 to request angle position.
The Arduino receives the data via Asyncstream, acts accordingly and sends the update position (in JSON format) back to the server which updates the connected browser (Crane Position, this was a tricky one to solve = ConnectedBrowsers.Keys and CallSubDelayed).
Instead of JSON format, it is also possible to use array of bytes instead.

Download B4R & B4J Project Source here.
Published External: B4R Forum here.
Run Experiment
  • Upload the B4R program to the Arduino.
  • Connect the Arduino to the Raspberry Pi.
  • Get the Raspberry Pi connected USB port - most cases ttyACM0.
  • Create a Raspberry Pi folder /home/pi/b4x/littlecrane.
  • Copy from the development device B4J project folder, the files littlecrane.jar, littlecrane.set and the www folder to /home/pi/b4x/littlecrane.
  • Open littlecrane.set and set the comport to f.e. comport=/dev/ttyACM0.
  • Start littlecrane.jar from the folder /home/pi/b4x/littlecrane: sudo -jar littlecrane.jar.
  • Open the browser and enter as URL: RasPi-IP-Address:51042, like 192.168.0.19:51042.
  • Play with the Little Crane.

This experiment open for any creative enhancements (like additional servos, use slider to move arm) esp. using Lego Technic.

Hardware
  • 1x Arduino MEGA
  • 1x Mini-Servo Motor MC1811
  • 1x LED Red
  • 1x LED Green
  • 2x Resitor 220 Ohm
  • 1x Raspberry Pi 2B or 3
  • Few LEGO bricks [LEGO is a trademark of the LEGO Group]
Wiring
Arduino = MC1811
GND = GND (Black)
5v = VCC (Red)
Pin 10 = Signal (Brown)
Arduino = LED GREEN
GND = GND (Black)
Pin 6 = Signal (Green)
Arduino = LED RED
GND = GND (Black)
Pin 5 = Signal (Orange)
Note: 220Ohm resistor between the LED and Arduino
To-Do List
  • NEW:ESP8266 solution (instead of the Arduino + Raspberry Pi), multiple servos, MQTT to control.
Changelog
  • 20160620: Version 1.0 published.
LittleCrane, 2017 by Robert W.B. Linn
Circuit
Prototype
Prototype Smartphone
Browser