Connect.Tech- Android Development For Arduino 101
-
Upload
stablekernel -
Category
Software
-
view
288 -
download
0
Transcript of Connect.Tech- Android Development For Arduino 101
Android Development For Arduino 101By: Bryan Jones Richardson Software Engineer stable|[email protected] stablekernel.com
We’re stable|kernel.
stable|kernel is an Atlanta-based mobile development company to craft smartly-designed mobile applications that connect brands directly with their users.
The Internet of Things?
@stablekernel
@stablekernel
The internet of things (IoT) is the network of physical devices, embedded with electronics, software, sensors, actuators, and network connectivity that enable them to collect and exchange data.
• Transportation (Street Lights)• Healthcare• Agriculture• Durables & Wearable• Object Oriented World
What Is Arduino?
@stablekernel
@stablekernel
Arduino is an open-source electronics platform based on easy-to-use hardware and software. It's intended for anyone making interactive projects.
(Basically, a small computer that you can connect to anything).• Uno• Mega• Yun• Minimal Cost Barriers• Raspberry Pi, Edison, etc.
Let’s Make Music
@stablekernel
Arduino Communication Strategies
@stablekernel
•USB•Bluetooth•Network
Android & Arduino via USB
@stablekernel
@stablekernel
Android USB Classes• UsbManager• UsbDevice• UsbDeviceConnection• UsbInterface• UsbEndpoint• UsbConstants
@stablekernel
Detect Android USB Connectionprivate final BroadcastReceiver usbConnectionAttachedReceiver = new BroadcastReceiver() { @Override public void onReceive(@NonNull Context context, @NonNull Intent intent) { activeUsbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); connectUsb(); }};
private final BroadcastReceiver usbConnectionDetachedReceiver = new BroadcastReceiver() { @Override public void onReceive(@NonNull Context context, @NonNull Intent intent) { UsbDevice detachedDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (detachedDevice == activeUsbDevice) { disconnectUsb(); } }};
@stablekernel
Determine if Arduino is Connected for (int i = 0; i < activeUsbDevice.getInterfaceCount(); i++) { UsbInterface usbInterface = activeUsbDevice.getInterface(i);
if (usbInterface.getEndpointCount() == 2) { for (int j = 0; j < endpointCount; j++) { UsbEndpoint endpoint = usbInterface.getEndpoint(j); if (endpoint.getType() != UsbConstants.USB_ENDPOINT_XFER_BULK) { continue;
} if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) { endpointOut = usbInterface.getEndpoint(j); } else if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) { endpointIn = usbInterface.getEndpoint(j); } } } } }
@stablekernel
Establish Communication w/ Arduinoprivate void setupUSBComm() { UsbManager manager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE); Boolean permitToRead = manager.hasPermission(arduinoDevice);
if (permitToRead) { connection = manager.openDevice(arduinoDevice); if (connection != null) { connection.claimInterface(activeUSBInterface, true);
//requestType, SET_CONTROL_LINE_STATE, value, index, buffer, length, timeout connection.controlTransfer(RQSID_SET_REQUEST_TYPE, RQSID_SET_CONTROL_LINE_STATE, 0, 0, null, 0, 0); connection.controlTransfer(RQSID_SET_REQUEST_TYPE, RQSID_SET_LINE_CODING, 0, 0, encodingSetting, 7, 0); } } else { manager.requestPermission(arduinoDevice, permissionIntent); }}
@stablekernel
Send Android Data to Arduino
@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) { playNote(position);}
public void playNote(int note_value) { byte[] bytesOut = new byte[]{ (byte) note_value};
if(arduinoDevice != null) { usbArduinoConnection.bulkTransfer(endpointOut, bytesOut, 1, 0); }}
@stablekernel
Process Android Data on Arduinoint speakerPin = 9;int tones[] = { 440, 494, 523, 587, 659, 698, 784};
void setup() { pinMode(speakerPin, OUTPUT); Serial.begin(9600);}
void loop() { if (Serial.available()) { int incomingByte = Serial.read();
if (incomingByte > -1) { playNote(incomingByte); } }}
void playNote(int note) { tone(speakerPin, tones[note], 200);}
@stablekernel
Hardware Setup
Ground Pin 9
Piezo BreadBoardUno USB
Android & Arduino via Bluetooth
@stablekernel
@stablekernel
Android Bluetooth Classes
<uses-permission android:name = "android.permission.BLUETOOTH_ADMIN"/><uses-permission android:name = “android.permission.BLUETOOTH”/>
private BluetoothAdapter bluetoothAdapter;private BluetoothSocket bluetoothSocket;private Set<BluetoothDevice> pairedBluetoothDevices;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@stablekernel
Connect To BlueTooth Device
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();BluetoothDevice btDevice = bluetoothAdapter.getRemoteDevice(bluetoothAddress);btSocket = btDevice.createInsecureRfcommSocketToServiceRecord(myUUID);BluetoothAdapter.getDefaultAdapter().cancelDiscovery();btSocket.connect();
@stablekernel
Send Android Data to Arduino
byte[] bytesOut = new byte[]{ note_value };
try { btSocket.getOutputStream().write(bytesOut);} catch (IOException e) { // handle exception}
@stablekernel
Debugging on Arduino
@stablekernel
Debugging on Arduino
digitalWrite(ledPin, LOW);
digitalWrite(ledPin, HIGH);
serialPrint(…);
@stablekernel
Hardware Setup
BluetoothTransmitter
LED
Android & Arduino via Network
@stablekernel
@stablekernel
Configure Yun For Connectivity
@stablekernel
Configure Yun For Connectivity
@stablekernel
Configure Yun For Connectivity
@stablekernel
Implement Arduino Code
#include <Bridge.h>#include <YunServer.h>#include <YunClient.h>
YunServer server;...
@stablekernel
Implement Arduino Code
void setup() { ...
Bridge.begin(); server.listenOnLocalhost(); server.begin();}
@stablekernel
Implement Arduino Code
void loop() { YunClient client = server.accept();
if (client) { process(client); client.stop(); }
delay(50);}
void process(YunClient client) { String incomingNote = client.readStringUntil('/'); playNote(incomingNote.toInt());}
@stablekernel
Send Android Data to Arduino Yun @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState);
String[] musicalNotes = new String[] { "A","B","C","D","E","F","G"};
ArrayAdapter<String> musicalNotesAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, musicalNotes); notesListView.setOnItemClickListener(this); notesListView.setAdapter(musicalNotesAdapter); }
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String note = String.valueOf(position);
HttpClient client = new DefaultHttpClient(); String URL = “http://10.0.0.19/arduino/“ + note; HttpGet request = new HttpGet(URL); ResponseHandler<String> responseHandler = new BasicResponseHandler();
client.execute(httpget, responseHandler); }
@stablekernel
Hardware Setup
Live Demo
@stablekernel
Questions?
Business Inquiries:Sarah WoodwardDirector of Business [email protected]
Bryan Jones RichardsonSoftware [email protected]