Code to automate led

Dear sir/madam,
I am writing this forum to find out why the code attached below is not working. I used ldr and led in my hardware connections.
In light of above situation i would request you check what is wrong in the code at your earliest convinience.

Thank you.
yours sincerely,
Akhilesh.

Hi @me23btech11058

Could you please share us the screenshots of hardware connections and code ? You can also refer to this text tutorial to check that your connections and code are correct.
https://docs.boltiot.com/docs/controlling-devices.

@me23btech11058
Can you tell me from where you found the Javascript code reference in your screenshot? So that accordingly we can debug and help you. Did you find it in our documentation or projects link? Do share the link.

You can alternative use this 3rd party JS library of Bolt. The information is given here https://docs.boltiot.com/docs/bolt-iot-javascript-library. Note that it is a 3rd party library so you might face issues. We will try and help you with them if there are any issues.

You can use the library same code to write your html code and the JS function to perform the necessary action.

Let me know if you are still facing any issue.

I didn’t find find the code anywhere i written it on my own just to try will that work sir.
I thought analogRead() function helps me to get input from ldr if that value is in particular my led should be on or off.
I thought it will work but its not working sir.

i actually checked the devices seperately after my hardware connections are done they worked but the code attached to the document when tried it didn’t work

Hi @me23btech11058,

You can achieve the intended functionality of reading an analog value from a sensor and control a digital output using JavaScript Library. However, it’s important to note that the provided JavaScript code functions only when the web page loads with the code that you write for this functionality.
For your reference, you can find the JavaScript library at this link: Bolt IoT Javascript library.

Additionally, please ensure that you review the API access rules outlined in the documentation (API access rules) as there are limitations on the number of API calls allowed.

For your functionality you can try this code

<html>

<head>
    <!-- CDN script tag-->
    <script src="https://unpkg.com/bolt-iot-wrapper/umd/boltIotWrapper.min.js"></script>
</head>

<body>
    <h1>LDR Tester</h1>

    Bolt ID: <input type="text" id="bolt_id" value="BOLT">
    <br>
    API Key: <input type="text" id="api_key" value="">
    <br>
    Analog Pin: <input type="number" id="analog_pin" value=0>
    <br>
    Digital Output Pin: <input type="number" id="digital_pin" value=1>
    <br>
    <button onclick="read_ldr_and_control_led()">Read LDR and Control LED</button>

    <script>
        var bolt = null;

        function setUpBolt() {
            console.log("Setting up Bolt");
            let deviceName = document.getElementById("bolt_id").value;
            let deviceKey = document.getElementById("api_key").value;
            return boltApi.Devices.add(deviceName, deviceKey);
        }

        function read_ldr_and_control_led() {
            if (window.bolt == null) {
                window.bolt = setUpBolt();
            }

            let analogPin = document.getElementById("analog_pin").value;
            let digitalPin = document.getElementById("digital_pin").value;

            // Read analog value from LDR
            bolt.Analog.read({ pin: analogPin })
                .then((ldrValue) => {
                    console.log("LDR Value:", ldrValue);
                    
                    // Control LED based on LDR value
                    if (ldrValue > 100) {
                        LED(digitalPin, 'HIGH');
                    } else {
                        LED(digitalPin, 'LOW');
                    }
                })
                .catch((error) => {
                    console.error("Error reading LDR:", error);
                });
        }

        function LED(pin, state) {
            bolt.Digital.write({ pin: pin, state: state });
        }
    </script>

</body>

</html>

Make sure to adjust the pin numbers and threshold value based on your specific hardware and requirements and also replace the Bolt ID and API key.

could you send java script code instead of html code sir?

and also while setting up the device we should set it as input or output device sir

Hi @me23btech11058

While setting up the device you have to set it as output device since LED is an output device. You can do this in the Products tab on cloud.boltiot.com when you are creating a new product .

but we are taking input also know sir from ldr

Hi @me23btech11058

To use both LDR and LED please set it as input device during the setup as LDR is an input device. You can control the output of LED using the JavaScript code provided above.

@me23btech11058 Was your issue resolved? Do let us know if you are still facing an issue. Screenshot of your code and output will be helpful in solving the issue faster.