Arduino未正确地(通过PHP)向SQL发送数字
所以,基本上,我正在尝试从Arduino板获取一些值,以保存到存储在PC上的SQL数据库中 我在网上查阅了多个资源,花了大约5-6个小时试图破解这个问题,但现在我在这里寻求您的帮助 所以,这似乎是可行的 我可以通过键入以下内容将值直接发送到数据库: [ 这给了我PHP脚本的预期输出并更新到数据库中。然而,当我在Arduino上运行它时,它会像运行一样遍历代码,但它不会向SQL Server发送任何数据 下面是Arduino代码Arduino未正确地(通过PHP)向SQL发送数字,php,mysql,arduino,Php,Mysql,Arduino,所以,基本上,我正在尝试从Arduino板获取一些值,以保存到存储在PC上的SQL数据库中 我在网上查阅了多个资源,花了大约5-6个小时试图破解这个问题,但现在我在这里寻求您的帮助 所以,这似乎是可行的 我可以通过键入以下内容将值直接发送到数据库: [ 这给了我PHP脚本的预期输出并更新到数据库中。然而,当我在Arduino上运行它时,它会像运行一样遍历代码,但它不会向SQL Server发送任何数据 下面是Arduino代码 #include <SPI.h> #include &l
#include <SPI.h>
#include <Ethernet.h>
int rand1 = 0;
int rand2 = 0;
//EthServer(80);
//IPAddress server(10,0,0,1);
char server[] =("10.0.0.1");
EthernetClient client;
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x0
};
byte ip[] = { 10, 0, 0, 2 };
void setup(void)
{
// start serial port
Serial.begin(9600);
Ethernet.begin(mac, ip);
}
void printIPAddress()
{
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
void loop() {
delay(1000);
rand1 = random(1, 3);
rand2 = random(25);
Serial.println(rand1);
Serial.println(rand2);
// Connect to the server (your computer or web page)
if (client.connect(("http://10.0.0.1"), 80)) {
client.print("GET /myapp/write_data.php?"); // This
client.print("value1="); // This
client.print(rand1); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
client.print(";");
client.print("value2=");
client.print(rand2);
client.println(" HTTP/1.1"); // Part of the GET request
client.println("Host: 10.0.0.1"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
client.println(); // Empty line
client.println(); // Empty line
client.stop(); // Closing connection to server
}
else {
// If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed\n");
printIPAddress();
}
delay(10000);
}
任何帮助都会很棒
干杯
John如果您没有收到“连接失败”的响应,则应将其连接。请重试
client.print("&");
反而
client.print(";");
如果仍然不起作用,在PHP文件中,使用
error_log("text");
检查server error.log以确定PHP脚本是否已经启动,并检查是否设置了$\u GET变量
编辑:添加:
在if语句中不应使用client.connect
返回指示连接状态的int(1,-1,-2,-3,-4)
SUCCESS 1
TIMED_OUT -1
INVALID_SERVER -2
TRUNCATED -3
INVALID_RESPONSE -4
所以打印出返回,然后继续调试。一定是您提供的IP地址有问题。我认为PHP代码与此无关,您电脑的IP地址是10.0.0.1?url有效?尝试从另一台电脑连接,并检查它是否有效。
client.connect
只接受IP或域,而不是url。嘿,伙计们,IP地址是192.168.179.1。我在代码中更改了它,也是因为我认为我的子网可能是问题所在。当我只使用它时,客户端连接失败,我在串行监视器中得到以下信息。我的IP地址:192.168.179.58.1 12-->连接失败我的IP地址:192.168.179.58。从这台电脑可以工作,我可以向数据库中添加值。谢谢约翰·汉克斯的回复。正如一些人指出的那样,当我使用Client.connect时,它不会工作。但是如果没有它,连接就会失败。在这些代码中,连接可能会失败的任何其他原因?谢谢约翰的更新答案。
SUCCESS 1
TIMED_OUT -1
INVALID_SERVER -2
TRUNCATED -3
INVALID_RESPONSE -4