I am using Ultrasonic sensor to get reading and am using python program for getting reading. And am using Raspberry pi for this. when am running python program independently its giving me reading continuously because am calling get_ultra function of program in while loop. But when am calling get_ultra function in python tornado websocket server using threading which call get_ultra function in while loop ,its stop giving reading after giving some readings. Here is my ultrasonic.py file code:

import time

import RPi.GPIO as GPIO


def get_ultra():

        global componentconfig

        GPIO_TRIGGER = 26

        GPIO_ECHO    = 26



        GPIO.output(GPIO_TRIGGER, False)


        GPIO.output(GPIO_TRIGGER, True)
        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()

        elapsed = stop - start

        distance = elapsed * 34300
        distance = distance / 2
        distance1 = "%.2f" % distance
        print "ultra sonic distance" , distance1

if __name__ == '__main__':
        while 1:

here is my python tornado websocket server:

import time

import thread

import traceback

import tornado.httpserver

import tornado.ioloop

import tornado.options

import tornado.web

import tornado.websocket

import ultrasonic

def Ultrasonic(T_name,a):

        while 1:



class ServerHandler(tornado.websocket.WebSocketHandler):

        def check_origin(self, origin):

                return True

        def open(self):

                print"Connection open"
        def on_close(self):
                print"Connection close"

        def on_message(self,message):
                print message
                if message=="start":
                     t12=threading.Thread(target=Ultrasonic,args('ultrasonic', 0 ))

if __name__=="__main__":






Note:-With "t12" thread am calling ten another thread which i have not mention here.I think may be memory leak problem is there .


This seems to be based on the same piece of depressingly bad code published on site after site. It was written by someone who has no desire to be a software engineer.

These ultrasonic rangers work by waiting for a greater than 10 µs trigger. They then send a series of high pitched chirps. They then raise the echo line (about 400 µs after the original trigger from memory) and wait for an echo to the chirp. When the echo is returned the echo line is lowered (some devices also lower the echo line after a timeout period).

I have indicated in the comments why and how the code may fail. I suggest you search online for better source code.

    GPIO.output(GPIO_TRIGGER, False)
    start = time.time()

    # If a reschedule occurs or the object is very close
    # the echo may already have been received in which case
    # the following will loop continuously.

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

    # if an object is not detected some devices do not
    # lower the echo line in which case the following will
    # loop continuously.

    while GPIO.input(GPIO_ECHO)==1:
            stop = time.time()
  • you are right joan.Can you suggest me better code. – Rakesh Wadbudhe Jan 15 '16 at 18:24
  • You need to set some sort of timeout condition so that the second loop so that it will eventually stop looping. For example, stop_time = start + 100, and in your while condition check to make sure that stop < stop_time. – NomadMaker Jun 18 '18 at 7:08
  • A better way might be to use interrupt routines to deal with sensors like this so that the system will respond as quickly as possible, though you would still need to put timeout conditions in. – NomadMaker Jun 18 '18 at 7:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.