此代码应通过php文件将值上载到数据库
我正在使用arduino和以太网盾将数据上传到服务器。 最近我更改了使用本地数据库来使用虚拟主机服务(000webhost),但我无法使它工作,没有错误显示在Arduino IDE中,但它只是停在它说“MAKING INSERTION”的行中。此代码应通过php文件将值上载到数据库
一切工作正常,当我有本地数据库。
当我直接输入url到浏览器 mythesisinacap.000webhostapp.com/writemydata.php?value=0它工作正常插入适当的值到数据库...所以这意味着没有错的PHP文件在服务器中。
这里是我的代码。
#include <Ethernet.h>
int isparked;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// Enter the IP address for Arduino
// Be careful to use , insetead of . when you enter the address here
IPAddress ip(192, 168, 0, 170);
int vcc = 5; //attach pin 2 to vcc
int trig = 6; // attach pin 3 to Trig
int echo = 7; //attach pin 4 to Echo
int gnd = 8; //attach pin 5 to GND
char server[] = "mythesisinacap.000webhostapp.com";
// Initialize the Ethernet server library
EthernetClient client(80);
void setup()
{
isparked=0;
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
}
void loop() {
if (client.connect(server, 80))
{
Serial.print("CONNECTED");
Serial.println();
Serial.print("MAKING INSERTION");
Serial.println();
client.print("GET /writemydata.php?value=");
client.print(isparked5);
client.println(" HTTP/1.1");
client.println("Host: mythesisinacap.000webhostapp.com");
client.println("Connection: close");
client.println();
client.println();
client.stop();
}
else
{
Serial.print("NO CONNECTION");
}
}
}
}
}
Serial.println();
Serial.print("FINNISH LOOPING");
Serial.println();
}
好吧,我终于得到了它与我的网站托管数据库的工作,我用从GitHub这个例子中,它适用于我的情况,现在我必须添加我的传感器逻辑和计算。
https://github.com/adafruit/Ethernet2/blob/master/examples/WebClient/WebClient.ino
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xDD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "mythesis2017.000webhostapp.com"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup()
{
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
delay(10000);
insert();
}
}
void insert()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80))
{
Serial.println("connected");
// Make a HTTP request:
client.println("GET /writemydata.php?val=1 HTTP/1.1");
client.println("Host: mythesis2017.000webhostapp.com");
client.println("Connection: close");
client.println();
}
else
{
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
'if(client.connect(server,80))'仍然是无意义的,因为函数只返回数字1,-1,-2,-3或-4。因此,您将始终输入只应在函数返回1时才输入的if块。所有其他返回值都是连接错误。我建议你制作一个小函数来打印不同的错误信息。 – Piglet
请仔细阅读[为什么 “有人能帮助我吗?”不是真正的问题?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question)和[问]。尝试自己做。一旦你撞上了墙,你可以解释你正在挣扎的东西,你很可能会得到帮助。但没有人会帮你翻译一些代码 – Piglet
对不对 –
mac地址是否需要更改,也许与我的以太网盾上的一个?,即使它工作没有问题,而不改变mac地址时我有一个本地数据库。 –