arduino用php,電腦透過php控制arduino | Davidou的 Blog

用VB控制arduino應該有不少範例才對,不過我想透過網頁來控制我的arduino,所以先來做一個簡單的範例測試。

首先先把arduino接上usb跟電腦相連

然後燒錄

const int ledPin = 13; // the pin that the LED

int incomingByte; // a variable to read incoming serial data into

void setup() {

// initialize serial communication:

Serial.begin(9600);

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

}

void loop() {

// see if there's incoming serial data:

if (Serial.available() > 0) {

// read the oldest byte in the serial buffer:

incomingByte = Serial.read();

// if it's a capital H (ASCII 72), turn on the LED:

if (incomingByte == 'H') {

digitalWrite(ledPin, HIGH);

}

// if it's an L (ASCII 76) turn off the LED:

if (incomingByte == 'L') {

digitalWrite(ledPin, LOW);

}

}

}

燒完後 你可以到 COM的界面去輸入H或是L 你應該可以看到ARDUINO板上的LED燈被打開或關閉,要是會亮就代表目前為止都沒問題

接下來就是PHP部分拉

首先我們程式是連9600的 所以PHP裡面也是寫9600 然後我的電腦ARDUINO目前是連接到COM7 這個每個人電腦都不一樣 你的可能是COM3之類的 就要修改一下PHP程式

然後因為我們ARDUINO程式設定說打上H 就是開LED 打上L就是關閉LED 所以PHP這邊就會有 openSerial(“H”); 這個東西出來

function openSerial($command) {

$openSerialOK = false;

try {

exec("mode com4: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off");

$fp =fopen("com7", "w");

//$fp = fopen('/dev/ttyUSB0','r+'); //use this for Linux

$openSerialOK = true;

} catch(Exception $e) {

echo 'Message: ' .$e->getMessage();

}

if($openSerialOK) {

fwrite($fp, $command); //write string to serial

fclose($fp);

}

}

openSerial("Without this line, the first control will not work. I don't know way.");

if(isset($_POST['submit1'])) {

openSerial("H");

}

if(isset($_POST['submit2'])) {

openSerial("L");

}

?>

就這樣 就可以透過php去執行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值