Está en la página 1de 4

Práctica no.

28 Insertar datos en firebase Comunicación de datos y Interface Web


por medio de php y librería CURL

Aplicaciones de IoT Quinto cuatrimestre

FORMULARIO EN HTML

FORMULARIO EN HTML “formulario.html”

<html>
<head>
<title>Formulario de entrada del datos</title>
</head>
<body>
<form method="post" action="firebaseinsertar.php">
Ingresa temperatura:
<input type="text" name="temperatura">
<br>
Ingresa mensaje:
<input type="text" name="mensaje">
<br>
<input type="submit" value="confirmar">
</form>
</body>
</html>

SALIDA DEL SCRIPT EN PHP


PROGRAMA EN PHP “firebaseInsertar.php”

<!DOCTYPE html>
<html>
<head>
<title>TEMPERATURA EN FIREBASE</title>
<meta charset="UTF-8">
</head>
<body background="" bgcolor="FFCECB" >
<!-- The core Firebase JS SDK is always required and must be listed first --> <script
src="https://www.gstatic.com/firebasejs/8.8.1/firebase-app.js"></script> <!-- TODO:
Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries --> <script
src="https://www.gstatic.com/firebasejs/8.8.1/firebase-analytics.js"></script> <script>
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
var firebaseConfig = {
apiKey: "AIzaSyBW8B9R8UtTM9HnK_PWvUQ7oEc-LkSs-LI",
authDomain: "nenat195.firebaseapp.com",
databaseURL: "https://nenat195-default-rtdb.firebaseio.com",
projectId: "nenat195",
storageBucket: "nenat195.appspot.com",
messagingSenderId: "579544161214",
appId: "1:579544161214:web:ea824748e0ee8c61a937ef",
measurementId: "G-LC5HF62W85"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
</script>
<?php
echo "La temperatura ingresada es:";
echo $_REQUEST['temperatura'];
$temp=$_REQUEST['temperatura'];
echo "<br> El mensaje ingresado es:";
echo $_REQUEST['mensaje'];
$mens=$_REQUEST['mensaje'];
echo "<br>";
$datos='{"LM35":'.'"'.$temp.'"'.','.'"Mensaje":'.'"'.$mens.'"'.'}';
$url="https://nenat195-default-rtdb.firebaseio.com/Sensores.json";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datos);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$respuesta_datos =curl_exec($ch);
if(curl_errno($ch)){
echo 'Error de:'.curl_errno($ch);
}else{
echo "Los datos se insertaron con éxito";}
?>
</body>
</html>
RESULTADO
Formulario para insertar datos

RETO:

Por medio de un formulario, consultar el dato de una de las colecciones de Sensores empleando
para ello CURL.

También podría gustarte