Ultrasonic Sensor and Bolt Wifi Module Project

Hi,
I am trying to build a very simple project. I want to use Ultrasonic sensor to measure distance. To do so, I connected the Ultrasonic sensor with Bolt Wifi Module. I am facing issues so your help will be appreciated.
Requisites:
A python code that should trigger the sensor to measure the distance and return the value. I will use this value to calculate the distance accurately(I am assuming sensor gives values and not distance). Once calculated, I will print the distance on screen (or python console).
Next, I want to plot the distance on the graph but the value returned by this sensor is not the distance so need a way to convert this value to distance before plotting.
Presently, I am using Bolt Cloud, but I am open to other Cloud platforms for solution too.

My Attempted Solution:
I tried coding this, here is my python code:

import requests, time, math, json
from boltiot import Bolt
import conf2 as conf #my Configuration File

data = [] #Empty list for storing sensor values…
mybolt = Bolt(conf.bolt_api_key, conf.device_id) #My Bolt
mybolt.digitalWrite(1,“LOW”)

#Function:Get Sensor Value
def get_sv():
try:
mybolt.digitalWrite(1,“LOW”)
time.sleep(2/1000000.0)
mybolt.digitalWrite(1,“HIGH”)
time.sleep(10/1000000.0)
mybolt.digitalWrite(1,“LOW”)

	response = mybolt.digitalRead(2)
	data = json.loads(response)
	if data["success"]!=1:
		print("Request Failed")
		print("Response:",data)
		return -999
	
	duration = int(data["value"])
	distance= duration*0.034/2;
	return distance
except Exception as e:
	print("Something went wrong")
	print(e)
	return -999

#MAIN:->
while True:
sensor_value = get_sv()
print("Sensor value is = ",sensor_value)

if sensor_value == -999:
	print("Request unsuccessful.Skipping...")
	time.sleep(5)
	continue

if(sensor_value <= conf.threshold):
	print("Alert: Accident Occured")
	message = "Crashed! " + "The current sensor value is: " + str(sensor_value)
	
data.append(sensor_value)
time.sleep(5)

#END

my JS Code on Bolt Cloud is:
setChartLibrary(‘google-chart’);
setChartTitle(‘Obstacle Distance’);
setChartType(‘areaGraph’);
setAxisName(‘Time Stamp’, ‘Distance’);
plotChart(‘time_stamp’, ‘distance’);

Please feel free to clear any ambiguity. You help is greatly appreciated, thanks in advance :slight_smile

@ayushsingh425
For seeing the value in distance you have already written the code Distance = (Time x SpeedOfSound) / 2
and for showing that distance in graph again you have to use that formula in JS or HTML file for reflecting the distance in km/m/cm etc.

Hi @ayushsingh425
In the above code digitalread doesn’t give the duration of pulse.It just gives the voltage of pulse.
correct me if I am wrong.If I am right can you help me how to get duration of pulse in python?

hi ayush i am facing the same issue can please suggest me the code fo it

@ayushsingh425
Were you able to complete your project successfully