因排版問題重發QQ
各位版上前輩們好
最近在嘗試用Arduino UNO + Ethernet W5100的擴充板來傳送資料
目前架構是:
Step1.用一個UNO(1號)產出:
"No.__'s Transmit(換行)"
"random=(隨機數)(換行)"
Step2.用另一個UNO(2號)去接收
然後再插上W5100擴充板接上乙太網路
傳送接收到的資料進伺服器的資料庫
現在碰到的是2號可以收到來自1號的資料
但是怎樣嘗試好像都無法透過網路傳進伺服器的資料庫
查了一些文章但還是不太能理解他的用法
想請各位前輩看看哪裡的觀念出了問題
程式碼如下:
******************************************************
##include <SoftwareSerial.h>
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {0x**, 0x**, 0x**, 0x**, 0x**, 0x**}; //mac位置
IPAddress server(***, ***, *** ,**); //Server ip
EthernetClient client; //啟用EthernetClient
SoftwareSerial Lora(4, 5); //定義(Rx, Tx)
int LED=13; //定義LED訊號為第13腳位
String data;
void setup(){
Serial.begin(9600); //設定速率為9600
Lora.begin(9600); //設定速率為9600
pinMode(LED, OUTPUT);
if(!Lora.available()){ //若lora無資料傳進
Lora.println("LoRa Failed!"); //則顯示LoRa Failed
}
else{
delay(1000); //等待1秒
Lora.println("Connecting......");
Lora.println("Connect Success!");
Lora.print("My Address: ");
Lora.println(Ethernet.localIP());
//確定Lora有資料後印出My Address: [ip位置]
}
}
void loop(){
/* if(Lora.available()>0){ //這裡有個問題, 我如果以這種方式去讀取資料
String data; //arduino會將我的字串拆成字元並轉成ASCII對
data = Lora.read(); //應的數字, 然後每送一個數字就換行
Lora.println(data); //而以下面那種則可以一次送完完整的字串@@
}*/
if(Lora.available()>0){
String data="";
char c=Lora.read();
if(c !="\n"){
data+=c;
Lora.print(data);
}
}
if(!client.connect(server, 80)){
//連接到伺服器, 否則顯示Connect Failed
client.println("Server Connect Failed!");
return;
}
else{
client.println("Connect Success!");
//成功則顯示Connect Success
client.println("POST /var/www/html/data.php HTTP/1.1");
// 是指POST到server端/var/www/html/data.php檔案中嗎?
client.print("HOST: ");
client.println(server);
//顯示server的ip位置
client.println("Content-Type: text; charset=utf-8");
//以文字形式傳遞
client.println("Content-Length: ");
client.println(data.length());
//顯示data長度
client.println();
client.println(data);
//這裡的意思是 $收到的變數=$_POST["data"]?
client.println("Connection Closed.");
client.println();
client.stop(); //關閉連接
led_blink();
}
}
void led_blink(){
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(100);
}
******************************************************
麻煩各位前輩指點
若那裏還有其他問題也虛心受教><