25

Android-Arduino interaction via Bluetooth

Embed Size (px)

DESCRIPTION

Rapida dimostrazione di quanto sia semplice automatizzare elettrodomestici casalinghi. In questo caso, comanderemo una macchina da caffè espresso via Android e Bluetooth.

Citation preview

Page 1: Android-Arduino interaction via Bluetooth
Page 2: Android-Arduino interaction via Bluetooth

Android-Arduino interaction via Bluetooth

ovvero, come fare anche il caffè da Android

Salvatore Carotenuto, Associazione “Open Makers Italy”

Page 3: Android-Arduino interaction via Bluetooth

Parte 1

L'Associazione “Open Makers Italy”

Page 4: Android-Arduino interaction via Bluetooth

L'Associazione “Open Makers Italy”

Siamo appassionati di making, elettronica, informatica...e di tutto ciò che è possibile costruire e modificare con le nostre mani.

http://www.openmakersitaly.org

plus.google.com/u/0/101202526052803869784 www.facebook.com/OpenMakersItaly twitter.com/openmakersitaly

Page 5: Android-Arduino interaction via Bluetooth

L'Associazione “Open Makers Italy”

learn, make, share!because it's better when it's made with your hands!

http://www.openmakersitaly.org

Attività

● eventi di making

● promozione di eventi

● diffusione delle ultime tecnologie

● corsi di formazione

● workshop e seminari

● partecipazione ad eventi

● progetti e consulenza

● collaborazione con i FabLAB e gli HackLAB italiani

● promozione e divulgazione della tecnologia nel Sud Italia

Page 6: Android-Arduino interaction via Bluetooth

2013

Giffoni HackLab 2013@Giffoni FilmFestival 2013,@StartUp Solutions Lab

Page 7: Android-Arduino interaction via Bluetooth

2013

Giffoni HackLab 2013@Giffoni FilmFestival 2013,@StartUp Solutions Lab

Page 8: Android-Arduino interaction via Bluetooth

2013

Electronics Lab [with Arduino]@Flussi Media Arts Festival 2013,@Teatro Gesualdo, Avellino

Page 9: Android-Arduino interaction via Bluetooth

2013

Giffoni Open Makers Day@Giffoni Valle Piana, Antica Ramiera

Page 10: Android-Arduino interaction via Bluetooth

L'Associazione “Open Makers Italy”

learn, make, share!because it's better when it's made with your hands!

http://www.openmakersitaly.org

Come partecipare

● essere parte attiva dei progetti esistenti

● proporre nuovi progetti

● proporre percorsi didattici

● rappresentanza

● workshop e seminari

● partecipazione ad eventi

● gestire tutorial e blog

● donazioni

Page 11: Android-Arduino interaction via Bluetooth

Parte 2

Android-Arduino Interaction via Bluetooth

Page 12: Android-Arduino interaction via Bluetooth

Arduino e Bluetooth ?!?

Il modulo HC-05 è un adattatore Bluetooth-UART.

Ci permette di convertire il flusso dati bluetooth in un flusso dati UART, ovvero una normalissima

porta seriale, e viceversa.

Page 13: Android-Arduino interaction via Bluetooth

Arduino e Bluetooth ?!?

Connessione del modulo HC-05 ad Arduino

Page 14: Android-Arduino interaction via Bluetooth

Il progetto

Page 15: Android-Arduino interaction via Bluetooth

Arduino

Codice Arduino

Pagina 1 di 2

#define POWER_PIN 8#define PUMP_PIN 9

int incomingByte = 0; // for incoming serial data

void setup() { pinMode(POWER_PIN, OUTPUT); pinMode(PUMP_PIN, OUTPUT); // Serial.begin(9600); // opens serial port, sets data rate to 9600 bps Serial.println("READY"); }

void loop() { String line = ""; char character;

while(Serial.available()) { delay(10); character = Serial.read(); line.concat(character); }

if(line != "") { Serial.println("received: " + line); parseCommand(line); } }

Page 16: Android-Arduino interaction via Bluetooth

Arduino

Codice Arduino

Pagina 2 di 2

void parseCommand(String line) { // splits received line in command/argument by ':' int separatorIndex = line.indexOf(':', 0); String command = line.substring(0, separatorIndex); separatorIndex = line.indexOf(':', separatorIndex); String argument = line.substring(separatorIndex+1, line.length());

Serial.print("command: " + command); Serial.println(" - argument: " + argument);

if(command == "POWER") { if(argument == "ON") { digitalWrite(POWER_PIN, HIGH); Serial.println("Power: ON"); } else if(argument == "OFF") { digitalWrite(POWER_PIN, LOW); Serial.println("Power: OFF"); } } else if(command == "PUMP") { if(argument == "ON") { digitalWrite(PUMP_PIN, HIGH); Serial.println("Pump: ON"); } else if(argument == "OFF") { digitalWrite(PUMP_PIN, LOW); Serial.println("Pump: OFF"); } } }

Page 17: Android-Arduino interaction via Bluetooth

L'applicazione Android

Page 18: Android-Arduino interaction via Bluetooth

Java

L'applicazione Android, il codice

Pagina 1 di 5: creazione dell'activity

import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.bluetooth.BluetoothSocket;import java.io.InputStream;import java.io.OutputStream;import java.util.UUID;

public class EspressoDroidActivity extends Activity implements OnClickListener, OnSeekBarChangeListener { // bluetooth stuff private static String btDeviceAddress = "00:15:FF:F3:C7:B2"; private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); private BluetoothAdapter bluetoothAdapter = null; private BluetoothSocket btSocket = null; Private Handler handler = new Handler();

// serial stuff private InputStream inStream = null; private OutputStream outStream = null; private byte[] readBuffer = new byte[1024]; private int readBufferPosition = 0; private byte lineDelimiter = 10; private boolean pauseSerialWorker = false;

@Override protected void onCreate(Bundle savedInstanceState) { … …

// Bluetooth initialization BluetoothDevice device = bluetoothAdapter.getRemoteDevice(btDeviceAddress); }

Page 19: Android-Arduino interaction via Bluetooth

Java

L'applicazione Android, il codice

Pagina 2 di 5: connessione e apertura socket bluetooth

public void bluetoothConnect() { Log.d(TAG, "Bluetooth device address: " + btDeviceAddress); BluetoothDevice device = bluetoothAdapter.getRemoteDevice(btDeviceAddress); Log.d(TAG, "Connecting to ... " + device); bluetoothAdapter.cancelDiscovery(); try { btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); btSocket.connect(); Log.d(TAG, "Connection made."); Toast.makeText(getApplicationContext(), "Connected!", Toast.LENGTH_SHORT).show(); } catch (IOException e) { Toast.makeText(getApplicationContext(), "Unable to connect!", Toast.LENGTH_SHORT).show(); try { btSocket.close(); } catch (IOException e2) { Log.d(TAG, "Unable to end the connection"); } Log.d(TAG, "Socket creation failed"); }

// starts bluetooth-serial listening thread beginListenForData(); }

Page 20: Android-Arduino interaction via Bluetooth

Java

L'applicazione Android, il codicePagina 3 di 5: listener thread su socket bluetooth

public void beginListenForData() { try { inStream = btSocket.getInputStream(); } catch (IOException e) { }

Thread workerThread = new Thread(new Runnable() { public void run() { while (!Thread.currentThread().isInterrupted() && !pauseSerialWorker) { try { int bytesAvailable = inStream.available(); if (bytesAvailable > 0) { byte[] packetBytes = new byte[bytesAvailable]; inStream.read(packetBytes); for (int i = 0; i < bytesAvailable; i++) { byte b = packetBytes[i]; if (b == lineDelimiter) { byte[] encodedBytes = new byte[readBufferPosition]; System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length); final String data = new String(encodedBytes, "US-ASCII"); readBufferPosition = 0; handler.post(new Runnable() { public void run() { txtSerialMonitor.append("\n" + data); } }); } else readBuffer[readBufferPosition++] = b; } } } catch (IOException ex) { pauseSerialWorker = true; } } } }); workerThread.start(); }

Page 21: Android-Arduino interaction via Bluetooth

Java

L'applicazione Android, il codice

Pagina 4 di 5: invio dei comandi e chiusura socket bluetooth

private void writeData(String data) { if (this.btSocket == null) { return; } try { outStream = btSocket.getOutputStream(); } catch (IOException e) { Log.d(TAG, "Error BEFORE writing on bluetooth socket: ", e); }

Log.d(TAG, "Sending string: " + data); try { outStream.write(data.getBytes()); } catch (IOException e) { Log.d(TAG, "Error writing on bluetooth socket!", e); } }

@Override protected void onDestroy() { super.onDestroy(); try { btSocket.close(); } catch (IOException e) { } }

Page 22: Android-Arduino interaction via Bluetooth

Java

L'applicazione Android, il codice

Pagina 5 di 5: gestione degli eventi utente

@Override public void onClick(View control) { switch (control.getId()) { case R.id.btnConnect: this.bluetoothConnect(); break; case R.id.btnPower: if (btnPower.isChecked()) { writeData("POWER:ON"); } else { writeData("POWER:OFF"); } break; case R.id.btnManualPump: if (btnManualPump.isChecked()) { writeData("PUMP:ON"); } else { writeData("PUMP:OFF"); } break; } }

public void onProgressChanged(SeekBar seekBar, int value, boolean fromUser) { this.writeData("ANGLE:" + value); // value between 0 and 180 (defined in layout.xml) }

Page 23: Android-Arduino interaction via Bluetooth

Part 3

Live Demo!

Page 24: Android-Arduino interaction via Bluetooth

< Thank You! />

[email protected]

plus.google.com/u/0/101202526052803869784 www.facebook.com/OpenMakersItaly twitter.com/openmakersitaly

Page 25: Android-Arduino interaction via Bluetooth