Skip to content

Commit

Permalink
Update index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelPC1000 authored Feb 16, 2025
1 parent 4e3eb5c commit 440b81e
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ <h2>Send Data to Firebase</h2>
<button onclick="convertAndSend()">Send</button>

<script>
let hasReset = false; // Flag to track if reset has already been done

function convertAndSend() {
// Get values from input fields
let opcode = document.getElementById("opcode").value;
let byteData = document.getElementById("byteData").value;

// Ensure that empty fields are sent as 0, not an empty string
opcode = opcode === "" ? 0 : Number(opcode);
byteData = byteData === "" ? 0 : Number(byteData);

// Firebase database URLs for different databases
const opcodeDatabaseURL = "https://local-roomba-control-default-rtdb.firebaseio.com/command.json"; // Replace with your actual database URL for opcode
const byteDatabaseURL = "https://local-roomba-control-bytes-default-rtdb.firebaseio.com/command.json"; // Replace with your actual database URL for byte data

// Send opcode and byte data directly as numbers (no conversion)
fetch(opcodeDatabaseURL, {
method: "PUT",
body: JSON.stringify(Number(opcode)), // Send as number
body: JSON.stringify(opcode), // Send as number, or 0 if empty
headers: { "Content-Type": "application/json" }
})
.then(response => response.json())
Expand All @@ -37,15 +43,37 @@ <h2>Send Data to Firebase</h2>

fetch(byteDatabaseURL, {
method: "PUT",
body: JSON.stringify(Number(byteData)), // Send as number
body: JSON.stringify(byteData), // Send as number, or 0 if empty
headers: { "Content-Type": "application/json" }
})
.then(response => response.json())
.then(data => console.log("Byte Data Sent:", data))
.catch(error => console.error("Error with byte data:", error));

alert(`Sent: Opcode: ${opcode}, Byte: ${byteData}`);

// Only reset inputs to 0 once
if (!hasReset) {
hasReset = true;

// Reset inputs to 0 after 2 seconds and call convertAndSend again
setTimeout(() => {
document.getElementById("opcode").value = '0'; // Reset to 0
document.getElementById("byteData").value = '0'; // Reset to 0

// Call convertAndSend() again to send the reset values (0)
convertAndSend();
}, 1000); // 1000 milliseconds = 1 second
}
}

// Reset the flag when the user starts editing the inputs
document.getElementById("opcode").addEventListener("input", () => {
hasReset = false; // Allow reset again once new input is entered
});

document.getElementById("byteData").addEventListener("input", () => {
hasReset = false; // Allow reset again once new input is entered
});
</script>
</body>
</html>

0 comments on commit 440b81e

Please sign in to comment.