He conseguido guardar los datos en texto plano en PHP.
Me queda guardarlo en una base de datos y me falla no se porque.
Codigo de arduino
/*
Arduino Data Web Server
encender led http://192.168.1.7/digitalWrite/7/1
apagar led http://192.168.1.7/digitalWrite/7/0
ver temperatura http://192.168.1.7/analogRead/0
*/
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
#include <SPI.h>
#include <avr/pgmspace.h>
#include <string.h>
// MAC address can be anything that is unique within your network.
// IP is the address the Arduino Ethernet Card would respond to. It needs to be an unused address within your network.
byte mac[] = {0x00, 0x1E, 0x2A, 0x77, 0x24, 0x02 };
byte ip[] = {192,168,1,26 }; // This is typically 10.0.0.x
EthernetServer server(80); // Port 80 is http.
//-- Commands and parameters (sent by browser) --
char cmd[15]; // Nothing magical about 15, increase these if you need longer commands/parameters
char param1[15];
char param2[15];
//-- Sample Ports ---
void SetupSamplePorts()
{
// To illustrate how to use this, I have an LED and a Potentiometer.
// The 10K potentiometer left lead is connected to GND, right lead to +5V, and middle lead to Analog 0.
// The LED cathode is on digital pin 7 and anode is on pin 8.
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT); // I use this pin as GND for the LED.
pinMode(8,OUTPUT); // Sample output, unable to use built-in LED at pin 13 because Ethernet Shield uses pins 10,11,12,13.
}
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
Serial.begin(57600);
SetupSamplePorts();
}
#define bufferMax 128
int bufferSize;
char buffer[bufferMax];
void loop()
{
EthernetClient client = server.available();
if (client)
{
WaitForRequest(client);
ParseReceivedRequest();
PerformRequestedCommands();
client.stop();
}
}
void WaitForRequest(EthernetClient client) // Sets buffer[] and bufferSize
{
bufferSize = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n')
break;
else
if (bufferSize < bufferMax)
buffer[bufferSize++] = c;
else
break;
}
}
PrintNumber("bufferSize", bufferSize);
}
void ParseReceivedRequest()
{
Serial.println("in ParseReceivedRequest");
Serial.println(buffer);
//Received buffer contains "GET /cmd/param1/param2 HTTP/1.1". Break it up.
char* slash1;
char* slash2;
char* slash3;
char* space2;
slash1 = strstr(buffer, "/") + 1; // Look for first slash
slash2 = strstr(slash1, "/") + 1; // second slash
slash3 = strstr(slash2, "/") + 1; // third slash
space2 = strstr(slash2, " ") + 1; // space after second slash (in case there is no third slash)
if (slash3 > space2) slash3=slash2;
PrintString("slash1",slash1);
PrintString("slash2",slash2);
PrintString("slash3",slash3);
PrintString("space2",space2);
// strncpy does not automatically add terminating zero, but strncat does! So start with blank string and concatenate.
cmd[0] = 0;
param1[0] = 0;
param2[0] = 0;
strncat(cmd, slash1, slash2-slash1-1);
strncat(param1, slash2, slash3-slash2-1);
strncat(param2, slash3, space2-slash3-1);
PrintString("cmd",cmd);
PrintString("param1",param1);
PrintString("param2",param2);
}
void PerformRequestedCommands()
{
if ( strcmp(cmd,"digitalWrite") == 0 ) RemoteDigitalWrite();
if ( strcmp(cmd,"analogRead") == 0 ) RemoteAnalogRead();
}
void RemoteDigitalWrite()
{
int ledPin = param1[0] - '0'; // Param1 should be one digit port
int ledState = param2[0] - '0'; // Param2 should be either 1 or 0
digitalWrite(ledPin, ledState);
//-- Send response back to browser --
server.print("El didodo conectado en el puerto ");
server.print(ledPin, DEC);
server.print(" esta ahora: ");
server.print( (ledState==1) ? "ENCENDIDO" : "APAGADO" );
//-- Send debug message to serial port --
Serial.println("RemoteDigitalWrite");
PrintNumber("ledPin", ledPin);
PrintNumber("ledState", ledState);
}
void RemoteAnalogRead()
{
// If desired, use more server.print() to send http header instead of just sending the analog value.
int analogPin = param1[0] - '0'; // Param1 should be one digit analog port
float analogValue =(float) analogRead(analogPin);
analogValue=analogValue*0.42;
//-- Send response back to browser --
// server.print("Por el pin A");
//server.print(analogPin, DEC);
//server.print(" esta leyendo una temperatura de: ");
//Cabecera HTTP estándar
server.println("HTTP/1.1 200 OK");
server.println("Content-Type: text/html");
server.println();
//Página Web en HTML
server.println("<html>");
server.println("<body>");
server.println(analogValue,DEC);
server.println("</body>");
server.println("</html>");
//-- Send debug message to serial port --
Serial.println("RemoteAnalogRead");
PrintNumber("analogPin", analogPin);
PrintNumber("analogValue", analogValue);
}
void PrintString(char* label, char* str)
{
Serial.print(label);
Serial.print("=");
Serial.println(str);
}
void PrintNumber(char* label, int number)
{
Serial.print(label);
Serial.print("=");
Serial.println(number, DEC);
}
Código PHP
<?php
//tomo las medidas de los puertos de arduno mediante peticion HTTP
$medida1="http://192.168.1.26/analogRead/0";
$medida2="http://192.168.1.26/analogRead/1";
$medida3="http://192.168.1.26/analogRead/2";
$medida4="http://192.168.1.26/analogRead/3";
$medida5="http://192.168.1.26/analogRead/4";
$valor1=file_get_contents($medida1);
$valor2=file_get_contents($medida2);
$valor3=file_get_contents($medida3);
$valor4=file_get_contents($medida4);
$valor5=file_get_contents($medida5);
//muestro por pantalla el contenido de los valores (Devuelven un HTML)
echo $valor1;
echo $valor2;
echo $valor3;
echo $valor4;
echo $valor5;
//conexion con base de datos del servidor
mysql_pconnect('localhost','root','');
//seleccion base de datos servidor
mysql_select_db('centroarduino');
//consulta de accion
if(isset($_POST['nombre'])) //devuelve true o false si existe o si no existe la variable
{
$valor1=$_POST['valor1'];
$valor2=$_POST['valor2'];
$valor3=$_POST['valor3'];
$valor4=$_POST['valor4'];
$valor5=$_POST['valor5'];
$cad="insert into arduino1(valor1,valor2,valor3,valor4,valor5) Values('".$valor1."','".$valor2."','".$valor3."','".$valor4."','".$valor5."')";
echo $cad; //muestro pantalla
mysql_query($cad);//ingreso base de datos
echo 'Ingreso realizado';
}
//Autorecarga de código para que cada 10 segundos guarde dato en la base de datos mySQL
$self = $_SERVER['PHP_SELF']; //Obtenemos la página en la que nos encontramos es decir a.php
header("refresh:10; url=$self"); //Refrescamos cada 10 segundos
?>
Un saludo y gracias