-
Notifications
You must be signed in to change notification settings - Fork 14
/
Luminosidad_POST.ino
86 lines (73 loc) · 2.24 KB
/
Luminosidad_POST.ino
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
#include <Ethernet.h>
#include <SPI.h>
#define NUM_ARDUINO "X" //Poner en X el número de Arduino
const int LDRPin = A0;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xYY}; //Sustituir YY por el numero de MAC correcto
byte ip_raspberrypi[] = {192, 168, 6, Z}; //Sustituir Z por la IP de la Raspberry Pi
EthernetClient client;
void setup()
{
Serial.begin(9600);
// start the Ethernet connection:
Serial.println("inicializando red...");
//Ethernet.begin(mac, ip, DNS, gateway, subnet);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
for (;;)
;
}
else {
Serial.print("IP asignada por DHCP: ");
Serial.println(Ethernet.localIP());
}
delay(1000);
}
void loop()
{
//Leo datos de luminosidad
int luminosidad = analogRead(LDRPin);
String post_string = "arduino=" + String(NUM_ARDUINO) + "&IntensidadLuminosa=" + String(luminosidad);
//Conecto a Raspberry Pi
if (client.connect(ip_raspberrypi, 80)) {
Serial.println("connected");
client.println("POST /Graba_POST.php HTTP/1.1");
client.println("Host: miraspberry");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Connection: close");
client.print("Content-Length: ");
client.println(post_string.length());
client.println("");
client.println(post_string);
client.println("");
Serial.println("Peticion enviada---->");
Serial.println("POST /Graba_POST.php HTTP/1.1");
Serial.println("Host: miraspberry");
Serial.println("Content-Type: application/x-www-form-urlencoded");
Serial.println("Connection: close");
Serial.print("Content-Length: ");
Serial.println(post_string.length());
Serial.println("");
Serial.println(post_string);
Serial.println("");
}
else {
Serial.println("connection failed");
}
//Espero respuesta raspberry Pi
while (client.available() == 0) {
//espero
Serial.print(".");
delay(100);
}
String webString = "";
//Leo respuesta Raspberry Pi
do {
char caracter_leido = client.read();
webString += caracter_leido;
delay(5);
} while (client.available() > 0);
Serial.print("He recibido de Raspberry Pi: ");
Serial.println(webString);
client.stop();
delay(5000);
}