想通过网站控制LED,我使用了Processing程序,每件事情都很好。但当我从笔记本电脑上拔掉我的arduino时,arduino无法响应来自网络的请求。它起作用了。我不想把我的arduino一直连接到我的笔记本电脑上。
我有一个名为LEDstate.txt的文件,它将数据包括0或1发送到Processing程序,我有简单的html页面,ON OFF超链接包括txt文件的状态。
这些是我用过的代码
处理程序的代码
import processing.serial.*;
Serial port;
void setup() {
/* change the number between the brackets after Serial.list() to meet the PORT number that arduino is connected to */
port = new Serial(this, Serial.list()[1], 9600);
}
void draw() {
String LED_Status[] = loadStrings("http://www.daffostore.com/ard/LEDstate.txt");
print(LED_Status[0]);
if (LED_Status[0].equals("1") == true) {
println("LEDstate.txt have ( 1 )- sending 'H' to Arduino ");
port.write('H');
}
else {
println("LEDstate.txt have ( 0 )- sending 'L' to Arduino ");
port.write('L');
}
delay(1000); // This makes a 5 sec delay between each check for the value that is stored in LED.txt
}
这是arduino IDE代码
int Led = 13; // led is connected to pin number 12
int incomingByte; // variable to read the incoming serial data coming from "Processing" sketch through USB cable
void setup() {
Serial.begin(9600);
pinMode(Led, OUTPUT);
}
void loop() {
if (Serial.available() > 0) { // checking if there is incoming serial data
incomingByte = Serial.read(); // reading the incoming data from "Processing"
if (incomingByte == 'H') { // if "Processing"" is sending H , turn on the LED
digitalWrite(Led, HIGH); }
if (incomingByte == 'L') { // if "Processing" is sending L , turn off the LED
digitalWrite(Led, LOW); }
}
}
这是请求的php代码
<?php
$onoroff = $_GET["state"]; // Declares the request from index.html as a variable
$textfile = "LEDstate.txt"; // Declares the name and location of the .txt file
$fileLocation = "$textfile";
$fh = fopen($fileLocation, 'w ') or die("Something went wrong!"); // Opens up the .txt file for writing and replaces any previous content
$stringToWrite = "$onoroff"; // Write either 1 or 0 depending on request from index.html
fwrite($fh, $stringToWrite); // Writes it to the .txt file
fclose($fh);
header("Location: index.html"); // Return to frontend (index.html)
?>
答案 0 :(得分:0)
你有一个非常复杂的解决方案:
[Browser (any PC)] -> {the web} -> [PHP web server (PC)] -> /LEDstate.txt (PC) -> [Processor (PC)] -> Serial Port (PC to Arduino) -> [Arduino IDE code (Arduino)] -> [LED]
正如您所看到的,您的PC正在完成大部分工作。因此,你不能把它关掉。
理想情况下你想要的是:
[Browser (any PC)] -> {the web} -> [Web server (Board; e.g. Arduino)] -> [LED]
要到达此处,您需要与互联网或交换机板进行Arduino通话,并选择一个可以与互联网通信的终端。
您可以在Arduino IDE中使用Arduino Wifi Shield或Arduino Eithernet Shield和re-write the "Web server"(您将不再需要“处理器”或当前的“Arduino IDE代码”)。
您可以更换电路板并选择内置网络的电路板。例如,您可以使用可以运行PHP和Apache的Raspberry Pi,甚至可以使用example on how to do just this。
或者您可以使用ESP8266。这个小家伙可以使用Arduino IDE,也可以使用Lua或一堆不同的环境。
您选择的选项取决于您想花多少钱以及您使用Arduino的程度。 Wifi和任一网屏蔽都比ESP8266贵,但ESP8266与现有的Arduino电路板无法比拟。