2017-08-13 3 views
-2

J'utilise un Arduino et un blindage Ethernet pour télécharger des données sur un serveur. Dernièrement, j'ai changé d'utiliser une base de données locale pour utiliser un service d'hébergement Web (000webhost) mais je ne peux pas le faire fonctionner, aucune erreur n'apparaît dans l'IDE Arduino mais elle s'arrête dans la ligne "INSERTION".Ce code doit télécharger une valeur dans une base de données via un fichier php.

Tout fonctionnait correctement lorsque j'avais la base de données localement.

Quand j'entrez l'URL directement dans le navigateur mythesisinacap.000webhostapp.com/writemydata.php?value=0 il fonctionne très bien insérer la valeur winrar dans la base de données ... ce qui signifie qu'il n'y a rien de mal avec le php fichier dans le serveur.

Voici mon code.

#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

S'il vous plaît lire [Pourquoi "Quelqu'un peut-il me aider?" pas une question réelle?] (https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) et [demander]. Essayez de le faire vous-même. Une fois que vous avez frappé un mur, vous pouvez expliquer ce que vous luttez et vous obtiendrez probablement de l'aide. – Piglet

+0

Vous avez raison –

+0

est-ce que l'adresse MAC doit être changée, peut-être avec celle de mon blindage Ethernet ?, même si cela fonctionne sans problème sans changer l'adresse mac quand J'avais une base de données locale. –

Répondre

0

Ok je finalement arrivé à travailler avec ma base de données hébergée web, j'ai utilisé cet exemple de github et adapté à mon cas, maintenant je vais devoir ajouter ma logique de capteur et de calcul.

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

'if (client.connect (serveur, 80))' est toujours non-sens car la fonction ne renvoie que les nombres 1, -1, -2, -3 ou -4. Vous entrez donc toujours ce if-block qui ne doit être entré que si la fonction renvoie 1. Toutes les autres valeurs de retour sont des erreurs de connexion. Je vous suggère de faire une petite fonction qui imprimera les différents messages d'erreur. – Piglet