0

I want to create a Raspberry Pi water level monitor system using HCSR04 ultrasonic range sensor. The HCSR04 is placed on the roof of water tank. Then the distance is measured and the the distance is send to the Bottle server. My program is:

#Name of program is web_sensor.py
import os, time
import RPi.GPIO as GPIO
from bottle import route, run, template

def water_level():
    GPIO.setmode(GPIO.BCM)

    # Define GPIO to use on Pi
    GPIO_TRIGGER = 23
    GPIO_ECHO    = 24

    # Speed of sound in cm/s at temperature
    temperature = 20
    speedSound = 33100 + (0.6*temperature)


    # Set pins as output and input

    GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
    GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo

    # Set trigger to False (Low)
    GPIO.output(GPIO_TRIGGER, False)

    # Allow module to settle
    time.sleep(0.5)

    # Send 10us pulse to trigger
    GPIO.output(GPIO_TRIGGER, True)
    # Wait 10us
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)
    start = time.time()

    while GPIO.input(GPIO_ECHO)==0:
        start = time.time()

    while GPIO.input(GPIO_ECHO)==1:
        stop = time.time()

    # Calculate pulse length
    elapsed = stop-start

    # Distance pulse travelled in that time is time
    # multiplied by the speed of sound (cm/s)
    distance = elapsed * speedSound

    # That was the distance there and back so halve the value
    distance = distance / 2
    water_level=distance -100 #since depth of tank is 1meter
    return water_level


@route('/temp')
def temp():
    return water_level()

@route('/')
def index():
    return template('main.html')

@route('/raphael')
def index():
    return template('raphael.2.1.0.min.js')

@route('/justgage')
def index():
    return template('justgage.1.0.1.min.js')

run(host='0.0.0.0', port=80)

And the main.html file is

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="raphael"></script>
<script src="justgage"></script>

<script>
function callback(tempStr, status){
    if (status == "success") {
        temp = parseFloat(tempStr).toFixed(2);
        g.refresh(temp);
        setTimeout(getReading, 1000);
    }
    else {
        alert("There was a problem");
    }
}

function getReading(){
    $.get('/temp', callback);
}
</script>
</head> 

<body>
<div id="gauge" class="200x160px"></div>

<script>
var g = new JustGage({
    id: "gauge",
    value: 0,
    min: 10,
    max: 100,
    title: "water level in cm"
});
getReading();
</script>

</body>
</html>

I have also a justguage.1. 0.1. min.js and rapheal.2. 1.0.min.js available from http://justgage.com. The reference for this program was Simon Monk's Raspberry Pi cookbook and code shared on github. The program is actually written for measure CPU temperature of Raspberry Pi.The output of that program is pleasing. However it is not working for my ultrasonic range measurement. The server starts without any error. But the interactive output using the JavaScript files not showing anything. Please help me to find out the error...

  • If you're not getting any values then you're premature with your web code. Ditch that, get the sensor functioning properly, then figure out how to publish the data. – goobering May 31 '17 at 8:24
  • @goobering The sensor is working properly. Is any change I want to made on codes? Please help me.. – Arun May 31 '17 at 8:27
  • @goobering yes created... drive.google.com/file/d/0B5HkM_BeqkJxWlZRMlUwX1VsSU0/… – Arun May 31 '17 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.