Problem in coding

  1. How to take input from pins
  2. How to write conditional statements and loops in bolt module

@alamhbk420 as far as the python code for the same is concerned, you can follow the below approaches:

Note: Before running the following python code, make sure that you have imported the boltiot module by running the following commands:
sudo apt-get -y update
sudo apt install python3-pip
sudo pip3 install boltiot

Also make a seperate conf.py file in which you should have the DEVICE_ID and the API_KEY of your bolt module so that the requests from the Cloud and Bolt module can be established.

Content of conf.py should be like this:
api_key = “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”
device_id = “BOLTXXXXX”

Note: Replace the strings with your personal DEVICE_ID and API_KEY You can find your API_KEY in https://cloud.boltiot.com/api_credentials

1. How to take input from pins: The input from the A0 pin can be fetched using the analogRead or digitalRead functions. analogRead function gives the accurate reading fetched from the input device whereas the digitalRead just gives the response in terms of 0 or 1. You can visit the documentation to see the same: https://docs.boltiot.com/docs/gpio-commands.

For the sake of making you understand let us assume that we have to make the project using LDR sensor
Before writing and running the code, do the hardware connections for the LDR which are provided in the Section: Project 1: Building your first IoT Sensor Project
Connect the one leg of LDR into the A0 pin and other end to the 3V pin. Use a 10k ohm resistor and insert one end of it into the Ground pin and the other end to the A0 pin.

Step 1: Make a file say light.py by running the command sudo nano light.py
Step 2: write the following code in the light.py file

import conf, json
from boltiot import Bolt

mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)

response = mybolt.analogRead(‘A0’)
data = json.loads(response)
print("Sensor value is: " + str(data[‘value’]))

Note: In the conf.py file, make sure that you name the variables correctly so that while using them in the light.py you don’t do any spelling error.
Step 3: Save the file and run the file using the command sudo python3 light.py

You can also visit the documentatio npage to understand the same: https://docs.boltiot.com/docs/python-library

2. 1. How to write conditional statements and loops in bolt module:
The loop and the conditiional statements can be used in the following manner.

While loop:

Result

Similarly you can go through the https://www.w3schools.com/python/python_for_loops.asp and others to have a firm grip on the concepts.

You can also change the code in the first part to run continuously through an infinite while loop. The code will look like this:

import conf, json, time
from boltiot import Bolt

mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)

while True:
response = mybolt.analogRead(‘A0’)
data = json.loads(response)
print("Sensor value is: " + str(data[‘value’]))
time.sleep(5)

Note: This loop wil continosly fetch the LDR values every after 10 seconds, use Ctrl+C to exit teh loop

1 Like

@alamhbk420 if I have solved your query then please mark the previous post by clicking on solution button :slight_smile: