[问题] Arduino Post方法请益

楼主: he24132972 (Ray)   2017-08-17 00:52:31
因排版问题重发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);
}
******************************************************
麻烦各位前辈指点
若那里还有其他问题也虚心受教><

Links booklink

Contact Us: admin [ a t ] ucptt.com