2017-08-13 10 views
-2

私はarduinoとイーサネットシールドを使ってサーバーにデータをアップロードしています。 最近、ローカルデータベースを使用してWebホスティングサービス(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(); 
} 
+0

なぜある[お読みください "誰かが私を助けることはできますか?"実際の質問ではありませんか?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question)と[ask]それを自分でやろう。壁に当たったら、苦労していることを説明することができ、おそらく助けを得るでしょう。しかし、誰もあなたのコードを翻訳するのを手伝ってくれるでしょう – Piglet

+0

あなたは間違っています –

+0

macアドレスは私のイーサネットシールドのもので変更する必要がありますか?私はローカルデータベースを持っていた。 –

答えて

0

私は最終的にそれは私のウェブホストされたデータベースで動作するようになった[OK]を、私は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"); 
} 
} 
+0

関数が数値1、-1、-2、-3または-4を返すだけなので、 'if(client.connect(server、80))'は依然として意味がありません。したがって、関数が1を返す場合にのみ入力すべきifブロックを常に入力します。他のすべての戻り値は接続エラーです。さまざまなエラーメッセージを出力する小さな関数を作成することをお勧めします。 – Piglet

関連する問題