Useful Java ME 8 Embedded code snippets

Collection of ME 8 Embedded code snippets:

  • Using java.util.TimerTask and Timer – for scheduling periodic tasks (reading a sensor, checking a status etc eg
MyTimerTask myTimerTask = new MyTimerTask();
Timer timer = new Timer();
timer.schedule(myTimerTask, millisBeforeStarting, millisBetweenExecution);
  • Opening a GPIO pin:
this.pin = PeripheralManager.open(this.pinId); 
this.pin.setValue(true); // true = on, false = off
  • Adding an EventListener to a switch:
GPIOPinConfig config = new GPIOPinConfig(this.portId, this.pinId,
    GPIOPinConfig.DIR_INPUT_ONLY, PeripheralConfig.DEFAULT,
    GPIOPinConfig.TRIGGER_BOTH_EDGES,
this.pin = PeripheralManager.open(config);
this.pin.setInputListener(this);

… where this implements PinListener and valueChanged() method:

public void valueChanged(PinEvent event) {
    GPIOPin eventPin = event.getPeripheral();
    ...
}
  • Opening an I2C device for input/output:
I2CDeviceConfig config = new I2CDeviceConfig(i2cBus, address, addressSizeBits, serialClock);
myDevice = PeripheralManager.open(config);
  • Read from a UART device:
this.uart = PeripheralManager.open(UART_DEVICE_ID);
this.uart.setBaudRate(9600);

InputStream is = Channels.newInputStream(uart);
this.serialBufferedReader = new BufferedReader(new InputStreamReader(is));
  • Read using Generic Connection Framework:
CommConnection conn = (CommConnection)Connector.open("comm:/dev/ttyAMA0;baudrate=9600");
InputStream inputStream = conn.openInputStream();
this.serialBufferedReader = new BufferedReader(new
    InputStreamReader(inputStream));
  • Open and write to a record store:
this.store = RecordStore.openRecordStore(storeName, true);
byte[] dataBytes = data.getBytes();
recordNum = store.addRecord(dataBytes, 0, dataBytes.length);

tmux useful commands

tmux is a great alternative to screen if you’re looking for multiple virtual sessions in one terminal window.

Useful commands:

Ctrl-b c : open new session

Ctrl-b n | p : cycle next / previous through sessions

Ctrl-b % | ” : split horizontally / vertically

Ctrl-b left | right | up |down : cycle back /forward through visible split windows (left/right if split horizontally, up/down if split vertically)

Using Putty SSH, if you’re not getting line characters for the splits, change session charset to UTF8.

Reading from the Adafruit GPS sensor on the Pi

Per instructions in the Oracle Embedded Java MOOC this week, here’s the steps to get gpsd setup to read from the sensor if it’s attached via a USB TTL cable:

sudo apt-get install gpsd gpsd-clients python-gps

ls /dev/ttyUSB* – to check what USB port we’re connected to

sudo gpsd /dev/ttyUSBx -F /var/run/gpsd.sock to start the daemon, replace x with the port number from step above

cgps -s – to run the client

 

If the GPS is attached via GPIO pins, then, similarly to above:

ls /dev/ttyAMA* – to check what AMA port we’re connected to

sudo gpsd /dev/ttyAMAx -F /var/run/gpsd.sock to start the daemon

Check check you’re reading data from the connected port, you can do:

cat /dev/ttyAMA0 – and you should see the raw messages being read from the card.

To interpret data coming from the card, read a line until you get a line prefixed with $, and then the next 5 chars represent the message type:

$GPGGA = gps position data

$GPVTG = velocity data

Enabling i2c on Raspbian

Add the following to /etc/modules:

i2c-bcm2708
i2c-dev

Install i2c-tools:

sudo apt-get install i2ctools

Edit /etc/modprobe.d/raspi-blacklist.conf and remove/comment out these two lines:

blacklist spi-bcm2708
blacklist i2c-bcm2708

Detect your ic2 interface with:

sudo i2cdetect -y 1 #1 = Pi model B

If you get this error:

Error: Could not open file '/dev/i2c-1' or `/dev/i2c/1': No such file or directory

Then run this first and you should be good to go:

sudo modprobe i2c-dev

More info here.