Documente Academic
Documente Profesional
Documente Cultură
1 de 6
http://forum.arduino.cc/index.php?topic=97264.0;wap2
//*****************************************ETHERNET*****************************************//
byte mac[] = {
0x00, 0x11, 0x22, 0x33, 0xFB, 0x11 }; // Mac del dispositivo ethernet
byte ip[] = {
192, 168, 1, 18 };
// ip del dispositivo ethernet
//byte miServidor[] = { 192, 168, 1, 15 };
byte miServidor[]= {
192,168,1,10};
// ip del servidor web donde esta alojada la pagina
EthernetClient client;
int pinLDR=0;
int valorLDR;
int pinLDRext=1;
int valorLDRext;
boolean luzEncendida=false;
void setup(){
Wire.begin();
Serial.begin(9600);
pinMode(pinLDR,INPUT);
sensors.begin();
// start up the library for the DS18B20
int numSensors = sensors.getDeviceCount();
// store the number of sensors to the variable numSensors,
float temparray[numSensors];
// array with "numSensors" storage places for the temperature of each sensor
// "sensors.getDeviceCount" is a function in the library
28/08/2014 14:38
2 de 6
http://forum.arduino.cc/index.php?topic=97264.0;wap2
if(numSensors > 0)
// if there is at least one sensor:
{
Serial.print("Numero de sensores: ");
//print the number of sensors to serial port
Serial.print(numSensors);
}
else
//if there is no sensor:
{
Serial.println("No sensors enumerated."); // tell the serial port
}
}
void loop(){
Serial.print("Luminosidad dentro: ");
valorLDR=analogRead(pinLDR);
//lectura del valor LDR
Serial.print(valorLDR);
if(valorLDR <10){
//diferentes rangos de luz
Serial.println(" - Muy oscuro");
}
else if(valorLDR <200){
Serial.println(" - Oscuro");
}
else if(valorLDR <500){
Serial.println(" - Tenue");
}
else if(valorLDR <800){
Serial.println(" - Brillante");
}
else{
Serial.println(" - Muy brillante");
}
Serial.print("Luminosidad fuera: ");
valorLDRext=analogRead(pinLDRext);
//lectura del valor LDR
Serial.print(valorLDRext);
if(valorLDRext <10){
//diferentes rangos de luz
Serial.println(" - Muy oscuro");
}
else if(valorLDRext <200){
Serial.println(" - Oscuro");
}
else if(valorLDRext <500){
Serial.println(" - Tenue");
}
else if(valorLDRext <800){
Serial.println(" - Brillante");
}
else{
Serial.println(" - Muy brillante");
}
28/08/2014 14:38
3 de 6
http://forum.arduino.cc/index.php?topic=97264.0;wap2
Ethernet.begin(mac, ip);
EthernetClient client;
Serial.println();
Serial.println("Inicializando Conexion");
Serial.println("Conectando");
delay(1000); //This one keeps it from hanging, anteriormente 1000
if (client.connect(miServidor,80)) {
Serial.println("Conectado");
client.print("GET http://192.168.1.10/PHPFile.php?");
Serial.print("GET http://192.168.1.10/PHPFile.php?");
for (int i=0; i<numSensors; i++){ //va metiendo las variables t0, t1... segun el numero de sensores
client.print("t");
Serial.print("t");
client.print(i);
Serial.print(i);
client.print("=");
Serial.print("=");
client.print(temparray[i]);
Serial.print(temparray[i]);
if (i < numSensors-1){
client.print("&");
Serial.print("&");
}
else{
}
}
client.print("&luzInterna=");
Serial.print("&luzInterna=");
client.print(valorLDR);
Serial.print(valorLDR);
client.print("&esBooleano=");
Serial.print("&esBooleano=");
if (luzEncendida){
client.print(1);
Serial.print(1);
}
else{
client.print(0);
Serial.print(0);
}
client.print("&luzExterna=");
Serial.print("&luzExterna=");
client.print(valorLDRext);
Serial.print(valorLDRext);
client.println(" HTTP/1.1");
Serial.println(" HTTP/1.1");
client.println("Host: http://192.168.1.10");
Serial.println("Host: http://192.168.1.10");
client.println();
}
else
{
Serial.println("Connection failed");
}
//}
//stop client
client.stop();
while(client.status() != 0)
{
delay(5);
}
}
Cdigo PHP:
(por si se puede adaptar para que se manden las variables al Arduino)
Code:
<html>
<?php
$t0 = $_GET['t0'];
$t1= $_GET['t1'];
$luzInterna=$_GET['luzInterna'];
$Encendido=$_GET['esBooleano'];
$luzExterna=$_GET[luzExterna];
28/08/2014 14:38
4 de 6
http://forum.arduino.cc/index.php?topic=97264.0;wap2
//Connect to database
$opendb = mysql_connect("192.168.1.10", "usuario", "pass") or mysql_error("Error");
mysql_select_db("domos");
if ($opendb){
mysql_query("INSERT INTO mytable (Fecha,t0, t1, luzInterna, Encendido, luzExterna) VALUES ( NOW(), $t0 , $t1, $luzInterna, $Encendido,$luzExterna)");
mysql_close($opendb);
}
?>
</html>
Gracias, un saludo.
Sergegsx:
q te parece llamar a un php con un parametro que sea indique el dato que deseas.
el php reconoce el parametro, hace la consulta mysql e imprime con un echo el resultado.
el arduino lee lo que devuelve y actua.
Hallenbeck:
Lo probar, a ver si saco algo maana.
Gracias por la respuesta!
Hallenbeck:
He llegado a una solucin y parece que funciona, por lo menos hace lo que yo quiero.
Me he basado en este ejemplo: http://bildr.org/2011/06/arduino-ethernet-client/
Code:
#include <Ethernet.h>
#include <SPI.h>
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte server[] = { 192,168,1,10 }; //ip Address of the server you will connect to
28/08/2014 14:38
5 de 6
http://forum.arduino.cc/index.php?topic=97264.0;wap2
client.println(location);
client.println();
//Connected - Read the page
return readPage(); //go and read the output
}else{
return "connection failed";
}
}
String readPage(){
//read the page, and capture & return everything between '<' and '>'
stringPos = 0;
memset( &inString, 0, 32 ); //clear inString memory
while(true){
if (client.available()) {
char c = client.read();
if (c == '<' ) { //'<' is our begining character
startRead = true; //Ready to start reading the part
}else if(startRead){
if(c != '>'){ //'>' is our ending character
inString[stringPos] = c;
stringPos ++;
}else{
//got what we need here! We can disconnect now
startRead = false;
client.stop();
client.flush();
Serial.println("disconnecting.");
return inString;
}
}
}
}
}
Este cdigo espera recibir una serie de datos encerrados por < y >.
El cdigo php es el siguiente:
Code:
<?php
$username="usuario";
$password="pass";
$database="domo";
mysql_connect("192.168.1.10",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM actuadores";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
$LuzPrincipal=mysql_result($result,$i,"LuzPrincipal");
$LuzMesa=mysql_result($result,$i,"LuzMesa");
$Persiana=mysql_result($result,$i,"Persiana");
$Store=mysql_result($result,$i,"Store");
$Clima=mysql_result($result,$i,"Clima");
$Ventilacion=mysql_result($result,$i,"Ventilacion");
echo '<'.",".$LuzPrincipal.",".$LuzMesa.",".$Persiana.",".$Store.",".$Clima.",".$Ventilacion.'>'
?>
28/08/2014 14:38
6 de 6
http://forum.arduino.cc/index.php?topic=97264.0;wap2
Primero hace la consulta con los datos que quiero que pasen al arduino, los guardo en variables y luego simplemente hace un echo uniendo todas las variables,
separndolas por comas para luego en el cdigo del Arduino poder guardarlas en variables diferentes (cosa que an est por hacer).
Ahora a ver si lo pongo todo junto en el mismo cdigo y empiezo a separar las variables.
Un saludo.
Navigation
[0] Message Index
28/08/2014 14:38