2017年3月4日 星期六
2017年1月12日 星期四
2015年12月27日 星期日
Arduino 循跡智慧車、遙控車與Android智慧手機藍芽控制
用Arduino DIY做玩具是個很有趣的題目,既能學到組裝結構又能學習程式設計一舉兩得,看到車子沿著黑線前進遇到障礙物就自動停住跟買現成的玩具遙控車有著很不一樣的感受,跟小孩一起玩小孩也很有興趣學習。
需要的組件如下:
1.Arduino Uno 相容版。
2.藍芽 HC-06 接收與傳送訊息至Android手機
3.LED全彩燈球可以顯示循跡車狀態
4.兩顆1:48 DC馬達與輪組作為驅動力
5.萬向輪
6.L298N馬達驅動板,控制馬達正反轉與速度PWS變化
7.超音波感應器 HC-SR04讓循跡車能感應前方是否有障礙物
8.尋跡模組 TCRT5000 紅外線反射感應器,用來分辨黑白線
9.樂高Technic標準孔支架作為循跡車主結構
2015年12月24日 星期四
Arduino 機械手臂準備資料
2015年12月18日 星期五
實做Arduino + ESP8266 WIFI無線模組與Android手機互動
![]() |
| 接上電源準備起動 ESP8266 無線 WIFI 模組 |
![]() |
| 當Arduino ESP8266 接上家中WIFI 路由器時取得DHCP 192.168.1.54,另外ESP 8266 AP模式的固定IP 192.168.4.1 |
![]() |
| 螢幕顯示ESP8266 監聽Port 8015,這就是Android手機與ESP8266溝通Port |
![]() |
| 實際測試:Android手機直接連接ESP8266 AP模式 192.168.4.1 Port8015 並送出red訊息, 當Arduino收到red後把LED切到紅色 |
![]() |
| 實際測試:Android手機直接連接ESP8266 AP模式 192.168.4.1 Port8015 並送出green訊息, 當Arduino收到green後把LED切到綠色 |
實際測試影片
2015年12月4日 星期五
Android 與Arduino 資料交換方式
當你想要從手機發送訊息給Arduino或想從Arduino收到回傳訊息時都必須知道如何使用DataInputStream / DataOutStream
Example01 Example02
Example01 Example02
android的網路資料交換分為2種:基於socket的,和基於http協定的。
自己玩Android 與Arduino不論是透過藍芽 HC-05、HC-06或是無線網路ESP8266 都是用Socket方式來做資料交換。網路上這篇文章說明很清楚值得大家參考
基於socket的用法
伺服器端:-----------------------------------------------------------------------------------------
先啟動一個伺服器端的socket ServerSocket svr = new ServerSocket(8989);
開始偵聽請求
Socket s = svr.accept();
取得輸入和輸出
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
Socket 的交互通過流來完成,即是說傳送的位元組流,因此任何檔都可以在上面傳送。
誰打開的記得要關上。
誰打開的記得要關上。
用DataInputStream/DataOutputStream來進行包裝是因為我們想要他們對基底資料型別的讀寫功能readInt(),writeInt(),readUTF(),writeUTF()等等。
用戶端:-----------------------------------------------------------------------------------------
發起一個socket連接
Socket s = new Socket("192.168.1.16",8015);
取得輸入和輸出
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
之後就可以相互通信了。誰打開的記得要關上。
InputStream is = new FileInputStream("test");
//暫存BYTE陣列
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//緩衝
byte buffer[] = new byte[512];
//紀錄讀進來長度
int length = 0;
//假如等於-1代表沒有資料了
while( (length = is.read(buffer)) != -1){
//從緩衝區讀取buffer裡面0~length-1的位置
baos.write(buffer, 0, length);
}
//ByteArrayOutputStream轉成位元陣列
byte data [] = baos.toByteArray();
is.close();
baos.close();
InputStream is = new FileInputStream("test");
//暫存BYTE陣列
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//緩衝
byte buffer[] = new byte[512];
//紀錄讀進來長度
int length = 0;
//假如等於-1代表沒有資料了
while( (length = is.read(buffer)) != -1){
//從緩衝區讀取buffer裡面0~length-1的位置
baos.write(buffer, 0, length);
}
//ByteArrayOutputStream轉成位元陣列
byte data [] = baos.toByteArray();
is.close();
baos.close();
基於Http協定
一般是發送請求到某個應用伺服器。此時需要用到HttpURLConnection
先取得HttpURLConnection urlConn = new URL("http://www.google.com").openConnection();
設置標誌
urlConn.setDoOutput(true); urlConn.setDoInput(true);//post的情況下需要設置DoOutput為true
urlConn.setRequestMethod("POST");
urlConn.setUseCache(false);//設置是否用緩存
urlConn.setRequestProperty("Content-type","application/x-www-form-urlencoded");//設置content-type
獲得輸出流,便於想伺服器發送資訊。
DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
望流裡面寫請求參數
dos.writeBytes("name="+URLEncoder.encode("chenmouren","gb2312");
dos.flush();dos.close();//發送完後馬上關閉。
獲得輸入流,取資料
BufferReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
reader.readLine();//用 !=null來判斷是否結束
reader.close();
讀完了記得關閉connection urlConn.disconnect();
//-----------------------------------------------
//----------------------------------------------
Client:
//---------------------------------------
//-----------------------------------------------
import java.io.*;
import java.net.*;
public class client
{
public static void main(String [] argv)throws IOException
{
StartClient(1234);
}
public static void StartClient(int port)
{
try
{
//設定連線的主機位置
InetAddress add = InetAddress.getByName("ecc.johnsonlu.org");
SocketAddress sc_add= new InetSocketAddress(add,port);
Socket sc = new Socket();
int timeout = 2000 ; //timeout 2秒
System.out.println("連線中....");
//與主機連線
sc.connect(sc_add,timeout);
System.out.println(sc.getLocalAddress() + "連線到" + sc.getInetAddress());
//傳送資料到Server端
DataOutputStream out = new DataOutputStream(sc.getOutputStream());
out.writeUTF("我是Client");
out.flush();
//接收Server端資料
DataInputStream in = new DataInputStream(sc.getInputStream());
System.out.println("Server:" + in.readUTF());
}
catch(SocketTimeoutException e)
{
System.out.println("Timeout");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
//----------------------------------------------
Client:
try{
Socket socket = new Socket(mDestinationIP,mDestinationPort);
DataInputStream dataInputStream = new
DataInputStream(socket.getInputStream());
int data;
//read data from server
while ((data=dataInputStream.read()) != -1) {
//do sth
}
} catch (IOException e){
//handle socket IOExceptions
}
Client Buffered data streaming
try{
Socket socket = new Socket(mDestinationIP,mDestinationPort);
BufferedInputStream bufferedInputStream = new
BufferedInputStream((socket.getInputStream());
int data;
//read date from server
while ((data= bufferedInputStream.read()) != -1) {
//do sth
}
} catch (IOException e){
//handle socket IOExceptions
}
//---------------------------------------
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidClient extends Activity {
EditText textOut;
TextView textIn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textOut = (EditText)findViewById(R.id.textout);
Button buttonSend = (Button)findViewById(R.id.send);
textIn = (TextView)findViewById(R.id.textin);
buttonSend.setOnClickListener(buttonSendOnClickListener);
}
Button.OnClickListener buttonSendOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
socket = new Socket("192.168.1.101", 8888);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF(textOut.getText().toString());
textIn.setText(dataInputStream.readUTF());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}};
}
2015年12月1日 星期二
Arduino 字串處理
Serial.begin(9600);
String str="Hello World!";
Serial.println(str.length()); //輸出 12
Serial.println(str.indexOf(" ")); //輸出 5 (有找到傳回索引)
Serial.println(str.indexOf(" ", 6)); //輸出 -1 (沒找到傳回 -1)
Serial.println(str.lastIndexOf("!")); //輸出 11
Serial.println(str.substring(6)); //輸出 World!
Serial.println(str.substring(0,7)); //輸出 Hello W
str.replace("World","Tony");
Serial.println(str); //輸出 Hello Tony!
str.concat(" Good Day!");
Serial.println(str); //輸出 Hello Tony! Good Day!
str.remove(16); //從 Day 前面空格開始刪
Serial.println(str); //輸出 Hello Tony! Good
str.remove(11,5); //從 G 前面空格開始刪
Serial.println(str); //輸出 Hello Tony!
str.toLowerCase();
Serial.println(str); //輸出 hello tony!
str.toUpperCase();
Serial.println(str); //輸出 HELLO TONY!
Serial.println(str.charAt(1)); //輸出 E
str.setCharAt(5,'+'); //將空格改為 +
Serial.println(str); //輸出 HELLO+TONY!
String str2="Hello+Tony!";
Serial.println(str.equals(str2)); //輸出 0
Serial.println(str.equalsIgnoreCase(str2)); //輸出 1
Serial.println(str.compareTo(str2)); //輸出 -32 (不同, str 在前)
Serial.println(str.compareTo("HELLO+TONY!")); //輸出 0 (雷同)
Serial.println(str.startsWith("HELLO")); //輸出 1
Serial.println(str.endsWith("TONY!")); //輸出 1
str=" HELLO ";
str.trim();
Serial.println(str.length()); //輸出 5 (已刪除前後空格)
str="180 Days";
Serial.println(str.toInt()); //輸出 180
str="65.245KG";
Serial.println(str.toFloat()); //輸出 65.25 (四捨五入到小數第二位)
String str="Hello World!";
Serial.println(str.length()); //輸出 12
Serial.println(str.indexOf(" ")); //輸出 5 (有找到傳回索引)
Serial.println(str.indexOf(" ", 6)); //輸出 -1 (沒找到傳回 -1)
Serial.println(str.lastIndexOf("!")); //輸出 11
Serial.println(str.substring(6)); //輸出 World!
Serial.println(str.substring(0,7)); //輸出 Hello W
str.replace("World","Tony");
Serial.println(str); //輸出 Hello Tony!
str.concat(" Good Day!");
Serial.println(str); //輸出 Hello Tony! Good Day!
str.remove(16); //從 Day 前面空格開始刪
Serial.println(str); //輸出 Hello Tony! Good
str.remove(11,5); //從 G 前面空格開始刪
Serial.println(str); //輸出 Hello Tony!
str.toLowerCase();
Serial.println(str); //輸出 hello tony!
str.toUpperCase();
Serial.println(str); //輸出 HELLO TONY!
Serial.println(str.charAt(1)); //輸出 E
str.setCharAt(5,'+'); //將空格改為 +
Serial.println(str); //輸出 HELLO+TONY!
String str2="Hello+Tony!";
Serial.println(str.equals(str2)); //輸出 0
Serial.println(str.equalsIgnoreCase(str2)); //輸出 1
Serial.println(str.compareTo(str2)); //輸出 -32 (不同, str 在前)
Serial.println(str.compareTo("HELLO+TONY!")); //輸出 0 (雷同)
Serial.println(str.startsWith("HELLO")); //輸出 1
Serial.println(str.endsWith("TONY!")); //輸出 1
str=" HELLO ";
str.trim();
Serial.println(str.length()); //輸出 5 (已刪除前後空格)
str="180 Days";
Serial.println(str.toInt()); //輸出 180
str="65.245KG";
Serial.println(str.toFloat()); //輸出 65.25 (四捨五入到小數第二位)
訂閱:
文章 (Atom)














