Unable to get analog value in html using javascript

I use javascript Analog.read() function to get analog value in html but it get [object Promise] as output.

Code :

<!DOCTYPE html>
<html>
<head>
	<title>LED</title>
	<script src="https://unpkg.com/bolt-iot-wrapper/umd/boltIotWrapper.min.js"></script>
</head>
<body>
	<p id="demo"></p>
	<script>
		var mybolt = boltApi.Devices.add("device_id", "api_key");
		var response = mybolt.Analog.read("A0");
		document.getElementById('demo').innerHTML = response;
	</script>
</body>
</html>

Output :

Please Help : @PPV @vinayak.joshi @rahul.singh1

I guess analogRead(‘A0’) is the function. If it doesn’t work, try creating separate files for Js and HTML and include in <script src =“url” tag.

@rahul.singh1 Can you please share the correct syntax?

Hey there in line" var response=mybolt.Analog.read(“A0”); "
Analog ‘A’ should be small, it should be analog.read(“A0”); .

But, according to the documentation it is correct.

Hi,

How can i read value from analogRead() function and display it to webpage. I tried but got undefined output and error in console

Uncaught TypeError: Cannot set property 'innerHTML' of null
 at XMLHttpRequest.xmlhttp.onreadystatechange (boltCommands.js:95)

Code -

<!DOCTYPE html>
<html>
<head>
<title>Bolt IoT Platform</title>
<script type="text/javascript" src="https://cloud.boltiot.com/static/js/boltComma
nds.js"></script>
<script>
setKey('api-key','device-id');
</script>
</head>
<body>
<center>
<button onclick="digitalWrite(0, 'HIGH');">ON</button>
<button onclick="digitalWrite(0, 'LOW');">OFF</button>
</center>
<p id="demo"></p>
<script>
	let temp = analogRead('A0');
	document.getElementById("demo").innerHTML = temp;
</script>
</body>
</html>

Console Output -

analogRead function takes two parameters pin no and element id.

Code:

ON


OFF
</body>

This will continuously update the Pin Value after 10s.
To read about analogRead function refer to site https://cloud.boltiot.com/static/js/boltCommands.js


1 Like