What's new

PHP How to display Load Cell Output from raspi to php webpage

SilverLining

Forum Veteran
Elite
Joined
Jul 13, 2016
Posts
1,233
Solutions
13
Reaction
583
Points
557
Hello mga idol baka po pwede makahingi ng tulong kase nagawa ako project, web based siya PHP, hindi ko mapalabas yung ouput nung load cell sa sa webpage.


Python Script:
Python:
from hx711 import HX711

hx = HX711(dout_pin=5, pd_sck_pin=6)
hx.set_reading_format("MSB", "MSB")
hx.set_reference_unit(1)
hx.reset()
hx.tare()

def read_weight():
    return hx.get_weight()

while True:
    weight = read_weight()
    print("Current weight:", weight)

PHP Webpage:
PHP:
<!DOCTYPE html>

<html>

<head>

    <title>Load Cell Weight</title>

</head>

<body>

    <h1>Current Weight:</h1>

    <div id="weight"></div>

    <script>

        function updateWeight() {

            var xhttp = new XMLHttpRequest();

            xhttp.onreadystatechange = function() {

                if (this.readyState == 4 && this.status == 200) {

                    document.getElementById("weight").innerHTML = this.responseText;

                }

            };

            xhttp.open("GET", "get_weight.php", true);

            xhttp.send();

        }

        setInterval(updateWeight, 1000);

        updateWeight();

    </script>

</body>

</html>


PHP:
PHP Script:
<?php

$weight = exec("python3 read_weight.py");

echo $weight;

?>
 
Hello! It seems like you're having trouble displaying the output of the load cell on your PHP webpage. Let's take a closer look at your code and see how we can troubleshoot it.

Here are a few things you can check and adjust:

1. Python Script:
- Your Python script looks good, and it is printing the current weight to the console. However, you need to modify the script so that it outputs the weight in a format that can be read by your PHP script.

2. PHP Script:
- The PHP script is using exec("python3 read_weight.py") to execute the Python script. Make sure that the path to the Python script is correct and that the Python script is executable.
- Modify the Python script to output the weight in a format that can be read by the PHP script. You can use print(weight) in the Python script to output the weight.

3. Webpage:
- In your PHP webpage, make sure that the path to the PHP script (get_weight.php) is correct.
- Update the PHP script to correctly read the output from the Python script and echo it back to the webpage.

Here is an example of how you can modify your Python script to output the weight in a format that can be read by the PHP script:

Python:
from hx711 import HX711

hx = HX711(dout_pin=5, pd_sck_pin=6)
hx.set_reading_format("MSB", "MSB")
hx.set_reference_unit(1)
hx.reset()
hx.tare()

def read_weight():
    return hx.get_weight()

while True:
    weight = read_weight()
    print(weight)  # Output the weight

And you can update your PHP script (get_weight.php) to correctly read the output from the Python script:

PHP:
<?php

$weight = exec("python3 read_weight.py");
echo $weight;

?>

Make sure to check the file paths and permissions, and ensure that the Python script is outputting the weight in a format that can be read by the PHP script. Let me know if you need further assistance!
 

Similar threads

Back
Top