Está en la página 1de 7

APLICACIONES IOT

● Carrera: Sistemas Computacionales

● Actividad: 07 - Arduino Sensor WiFi ESP8266 Base de Datos.

● Alumno:
1. Alan Alejandro Luna Gonzalez (18011658)

● Fecha de entrega: 22/11/2021

Arduino-Sensor-WiFi-ESP8266-Base de Datos.
Aplicaciones IOT
Alumno: Alan Alejandro Luna Gonzalez

Código entorno de Arduino


char mensajeJSON[400];
char mensajeHTTP[600];

int conexionTiempo = 30;


byte conexionComando1 = 2;
int contadorTiempo = 0;

char *LOCALHOST = "192.168.1.65"; // IP del dispositivo local


char *keyVar = "x9xyLx56v9G7b3WC"; // llave para autenticar
char *urlVar = "/capturaIoT.php"; // pagina para llamar
char *servidorVar = "192.168.1.65"; // direccion del servidor
char *usuarioHTTP = "claseIoT"; // usuario

long int t0 = 0;
long int t1 = 0;

boolean statusLED = false;


boolean debug = false;

//-------------------------------------------------

void setup()
{
Serial.begin(115200);
pinMode(13,OUTPUT);
}

//-------------------------------------------------

char* crearMensajeJSON(char *bufferTX, int variable)


{
sprintf(bufferTX,"{\"key\":\"%s\",\"var\":\"%d\"}", keyVar, variable);
return bufferTX;
}

//-------------------------------------------------

void crearMensajeHTTP(char *bufferTX, char *mensajeJSON, char *URL, char


*Servidor, char *Usuario)
{
int cuantosBytes = strlen(mensajeJSON);
char temporal[50];
sprintf(temporal, "POST %s HTTP/1.0\r\n", URL);
strcat(bufferTX, temporal);
sprintf(temporal, "Host: %s \r\n", Servidor);
strcat(bufferTX, temporal);
sprintf(temporal, "User-Agent: %s \r\n", Usuario);
strcat(bufferTX, temporal);
sprintf(temporal,"Content-Length: %i \r\n", cuantosBytes);
strcat(bufferTX, temporal);
strcat(bufferTX, "Content-Type: application/x-www-form-urlencoded\r\n");
strcat(bufferTX, "\r\n");
strcat(bufferTX, mensajeJSON);
return bufferTX;
}

//-------------------------------------------------

void enviarSerial(char *bufferTX)


{
int cuantosBytes = strlen(bufferTX);
int contador = 0;
if ( debug )
{
Serial.print("Bytes del mensaje: ");
Serial.println( cuantosBytes );
}
for(int j = 0; j <= ( cuantosBytes-1 ); j++)
{
Serial.write( (char) bufferTX[ j ]);
contador++;
}

if ( debug )
{
Serial.print("Bytes del enviados: ");
Serial.println( contador );
}
}

//-------------------------------------------------

void loop()
{
t1 = millis();
if ( t1 > ( t0 + 1000 ) )
{
t0 = millis();
contadorTiempo++;
statusLED = !statusLED;

if ( statusLED )
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
};

if ( statusLED )
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}

if ( debug )
{
Serial.print("Faltan :");
Serial.print( conexionTiempo - contadorTiempo );
Serial.println(" s");
}

if ( contadorTiempo >= ( conexionTiempo - conexionComando1 * 3 ) )


{
contadorTiempo = 0;

//Se mandara el mensaje


if ( debug )
{
Serial.println("Se creara el mensajeJSON");
crearMensajeJSON ( mensajeJSON, 1 );
enviarSerial ( mensajeJSON );
crearMensajeHTTP ( mensajeHTTP, mensajeJSON, urlVar, servidorVar,
usuarioHTTP );
enviarSerial ( mensajeHTTP );
}
else
{
crearMensajeJSON ( mensajeJSON, 1 );
crearMensajeHTTP ( mensajeHTTP, mensajeJSON, urlVar, servidorVar,
usuarioHTTP );

char temporal[ 50 ];

sprintf( temporal, "AT+CIPSTART=\"TCP\",\"%s\",80\r\n",servidorVar );


enviarSerial ( temporal );
delay( conexionComando1 * 1000 );
int cuantosBytesMJ = strlen( mensajeHTTP );
sprintf( temporal, "AT+CIPSEND=%d\r\n", cuantosBytesMJ );
enviarSerial( temporal );
delay( conexionComando1 * 2000 );
digitalWrite( 13, LOW );
enviarSerial( mensajeHTTP );
digitalWrite( 13, HIGH );
delay ( conexionComando1 * 2000 );
digitalWrite( 13, LOW );
} // if debug

} // if contador

} // if t1
} // loop
Código del servidor .php
<?php
/*
Practica Arduino-Sensor-Wifi-Base de datos
*/
?>

<?php
date_default_timezone_set('America/Mexico_City');
setlocal(LC_ALL,”es_ES”);

$quienB = $current_user->user_firstname;
$postdata = file_get_contents("php://input");
$obj = json_decode($postdata);
$pass = $obj->{'key'};

if($pass == 'x9xyLx56v9G7b3WC')
{
$fecha = new DateTime();
$fechaString = $fecha->format('Y-m-d H:i:s');
$fechaNumero = $fecha->getTimestamp();

$nombre = $obj->{'var'};
$dato = $obj->{'datoP'};
$dato2 = $obj->{'datoR'};
$dato3 = $obj->{'datoE'};

$connect = new PDO('mysql:host=localhost;dbname=MateriaIOT;charset=utf8', 'utj', 'hola1234');


// acomodar los campos segun la tabla a guardar
$stmt = $connect->prepare("INSERT INTO sensor1 (id, fecha, usuario, presion, ritmo, emergencia) VALUES
(?, ?, ?, ?, ?)");

if ( $stmt->execute( [$fechaNumero, $nombre, $dato, $dato2, $dato3] ) )


{
$NumCot = $connect->lastInsertId();
$stmt = null;
echo "{OK}-{$pass}-{$dato}-{$fechaString2}";
}
else
{
//echo "{NOK2}";
}

}
else
{
echo "{Nooooo OK}-{$pass}-{$dato}-{$fechaString2}-";
}

?>
Base de datos

Fritzing

Conclusiones: en la presente práctica, logre realizar conexión al localhost por medio


del simulador proteus, al declarar el enlace .hex que genera la ide del entorno de
Arduino, me percato que no envia nada, no existe conexión con el simulador y el
servidor, posteriormente implementó el diseño eléctrico (Arduino y ESP8266) en
Fritzing pero tampoco logra ejecutarlo.

También podría gustarte