Arduino - Este código debería cargar un valor a una base de datos a través de un archivo php

 
Vista:

Este código debería cargar un valor a una base de datos a través de un archivo php

Publicado por notarola (1 intervención) el 15/08/2017 04:17:53
Estoy usando un arduino y un shield Ethernet para cargar datos a un servidor. Últimamente he cambiado de usar una base de datos local para usar un servicio de alojamiento web (000webhost) pero no puedo hacer que funcione, no se muestran errores en el IDE de Arduino, solo se detiene en la línea donde dice "MAKING INSERTION".

Todo funcionaba bien cuando tenía la base de datos localmente.

Cuando ingreso la url directamente en el navegador

mythesisinacap.000webhostapp.com/writemydata.php?value=0

funciona bien insertando el valor apropiado en la base de datos ... lo que significa que no hay nada malo con el archivo php en el servidor.

Aquí está mi código.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <Ethernet.h>
 
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
 
float cm1;
float cm2;
float cm3;
float cm4;
float cm5;
 
bool isparked1;
bool isparked2;
bool isparked3;
bool isparked4;
bool isparked5;
 
long duration;
long duration1;
long duration2;
long duration3;
long duration4;
long duration5;
 
char server[] = "mythesisinacap.000webhostapp.com";
 
// Initialize the Ethernet server library
EthernetClient client(80);
 
void setup() {
 
pinMode (vcc, OUTPUT);
pinMode (gnd, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
 
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
}
//metodo para calcular los centimetros
float microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
 
//metodo para ejecutar la medicion
long ejecutarMedicion()
{
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(5);
digitalWrite(trig, LOW);
pinMode(echo, INPUT);
duration = pulseIn(echo, HIGH);//photocell
 
return duration;
}
 
 
void loop() {
 
digitalWrite(vcc, HIGH);
 
Serial.println();
Serial.print("comenzando loop");
Serial.println();
Serial.print("haciendo nueva medicion 1");
Serial.println();
 
//medicion
duration1 = ejecutarMedicion();
//fin medicion
 
//calculo en centimetros
cm1 = microsecondsToCentimeters(duration1);
//fin calculo de cm
Serial.print("Centimetros 1: ");
Serial.print(cm1);
Serial.println();
//si los centimetros son menos de 100 quiere decir que hay un auto estacionado, si es mayor a 100 el estacionamiento esta desocupado
if (cm1 <= 100)
{
isparked1 = 1;
Serial.print("Estacionado: SI 1");
Serial.println();
}
else if (cm1 > 100)
{
isparked1 = 0;
Serial.print("Estacionado: NO 1");
Serial.println();
}
Serial.print("esperando 2 segundos");
Serial.println();
delay(2000);
 
Serial.print("haciendo nueva medicion 2");
Serial.println();
duration2 = ejecutarMedicion();
 
cm2 = microsecondsToCentimeters(duration2);
Serial.print("Centimetros 2: ");
Serial.print(cm2);
Serial.println();
 
if (cm2 <= 100)
{
isparked2 = 1;
Serial.print("Estacionado: SI 2");
Serial.println();
}
else if (cm2 > 100)
{
isparked2 = 0;
Serial.print("Estacionado: NO 2");
Serial.println();
}
 
if (isparked1 != isparked2)
{
Serial.print("isparked1 es distinto de isparked2");
Serial.println();
Serial.print("esperando 2 segundos");
Serial.println();
 
delay(2000);
 
Serial.println();
Serial.print("haciendo nueva medicion 3");
Serial.println();
duration3 = ejecutarMedicion();
 
cm3 = microsecondsToCentimeters(duration3);
Serial.print("Centimetros 3: ");
Serial.print(cm3);
Serial.println();
if (cm3 <= 100)
{
  isparked3 = 1;
  Serial.print("Estacionado: SI 3");
  Serial.println();
}
else if (cm3 > 100)
{
  isparked3 = 0;
  Serial.print("Estacionado: NO 3");
  Serial.println();
}
 
if (isparked2 == isparked3)
{
  Serial.println();
  Serial.print("isparked2 == isparked3");
  Serial.println();
  Serial.print("esperando 2 segundos");
  Serial.println();
 
  delay(2000);
 
  Serial.print("haciendo nueva medicion 4");
  Serial.println();
  duration4 = ejecutarMedicion();
 
  cm4 = microsecondsToCentimeters(duration4);
  Serial.print("Centimetros 4: ");
  Serial.print(cm4);
  Serial.println();
  if (cm4 <= 100)
  {
    isparked4 = 1;
    Serial.print("Estacionado: SI");
    Serial.println();
  }
  else if (cm4 > 100)
  {
    isparked4 = 0;
 
    Serial.print("Estacionado: NO");
    Serial.println();
  }
 
  if (isparked3 == isparked4)
  {
    Serial.print("isparked3 == isparked4");
    Serial.println();
    Serial.print("esperando 2 segundos");
    Serial.println();
 
    delay(2000);
 
    Serial.print("haciendo nueva medicion 5");
    Serial.println();
    duration5 = ejecutarMedicion();
    cm5 = microsecondsToCentimeters(duration5);
    Serial.print("Centimetros 5: ");
    Serial.print(cm5);
    Serial.println();
 
    if (cm5 <= 100)
    {
      isparked5 = 1;
      Serial.print("Estacionado: SI");
      Serial.println();
    }
    else if (cm5 > 100)
    {
      isparked5 = 0;
      Serial.print("Estacionado: NO");
      Serial.println();
    }
 
    if (isparked2 == isparked3 && isparked3 == isparked4 && isparked4 == isparked5)
    {
      Serial.print("Todos los valores de 4 mediciones son iguales");
      Serial.println();
      Serial.print("Conectando...");
      Serial.println();
      if (client.connect(server, 80))
      {
        Serial.print("CONNECTED");
        Serial.println();
        Serial.print("nuevo valor: ");
        Serial.print(isparked5);
        Serial.println();
        Serial.print("MAKING INSERTION");
        Serial.println();
        client.print("GET /writemydata.php?value="); // This
        client.print(isparked5);
        if (isparked5==0)
        {
          Serial.println();
          Serial.print("NO ESTACIONADO");
        }
        else if(isparked5==1)
        {
          Serial.println();
          Serial.print("SI ESTACIONADO");
        }
 
        client.println(" HTTP/1.1"); // Part of the GET request
        client.println("Host: mythesisinacap.000webhostapp.com"); // 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
      {
        Serial.print("no hay conexion");
      }
    }
  }
}
}
Serial.println();
Serial.print("terminando loop");
Serial.println();
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder