Rapida dimostrazione di quanto sia semplice automatizzare elettrodomestici casalinghi. In questo caso, comanderemo una macchina da caffè espresso via Android e Bluetooth.
4. 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
5. 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
10. 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
12. 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.
18. 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);
}
19. 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();
}
20. Java
L'applicazione Android, il codice
Pagina 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();
}
21. 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) { }
}
22. 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)
}