此代码应通过php文件将值上传到数据库

时间:2017-08-13 08:20:44

标签: arduino iot arduino-uno gsm gprs

我使用arduino和以太网屏蔽将数据上传到服务器。 最近我改变了使用本地数据库来使用网络托管服务(000webhost),但我无法使其工作,Arduino IDE中没有显示任何错误,但它只是在它所说的行中停止了#34;制作插入"。

当我在本地拥有数据库时,一切正常。

当我直接在浏览器中输入网址时 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();
}

1 个答案:

答案 0 :(得分:0)

好的,我终于让它与我的网络托管数据库一起工作了,我在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");
}
}