So I had been researching a lot on Bolt API and i realized that throughout the training we have been taught to send sms and emails if a condition is met in python code. I’m making this post so that we can also trigger events like lighting a LED or triggering a buzzer from the code it self. I’ll assume you know the basics of python like creating a file and running some code.
Step 1:
for this we need the following things:
-
Bolt API Key
which can be found at
https://cloud.boltiot.com/api_credentials -
Device ID
which can be found at
https://cloud.boltiot.com/home/
after you have obtained the two we need to make a python file conf.py and save the data in that file. the contents of the file should look like:
API_KEY='XXXXXXXX'
DEVICE_ID='BOLTXXXXXXX'
save it as conf.py
STEP 2:
Create a new file as api_call.py
or whatever you want to name it as.
now we need to import certain modules namely boltiot and requests.
you may not have requests installed so you can do that by using pip3
sudo pip3 install requests
after installing the module write the following code
import conf
from boltiot import Bolt
import json, requests
mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
url = "https://cloud.boltiot.com/remote/"+conf.API_KEY+"/digitalWrite"
urldata = {"pin":0,
"state":'HIGH',
"deviceName":conf.DEVICE_ID
}
comm = requests.request("GET",
url,
params=urldata)
print(comm.text)
save this file now.
STEP 3:
Hardware Connections:
Connect the negative pin of the buzzer to the GND pin and the positive terminal to the 0 PIN (do not confuse with A0 that’s analog input)
Turn on the bolt module now.
Run the code api_call.py
the buzzer should buzz (xD) about in 1 second. The output of the code will be something like
{"value": "1", "success": 1}
EXPLANATION OF THE CODE
the requests function is used to make a web request
using get
method. notice that we have certain parameters defined in urldata
variable that are
- pin
- state
- deviceName (this will remain the same for one particular board )
Thepin
corresponds to the pin number of the board to which you have connect the LED or buzzer. You can change it between 0-4 to access different pins on board.
You would have noticed in url
variable we have used digitalWrite
therefore we have only 2 state
1. HIGH (turn on the device connected/ high voltage/ ON state)
2. LOW (turn off the device connected/ low voltage/ OFF state)
please remember to enclose the keywords in ’ ’ else the requests function will return an error.
PRO TIP:
You can switch digitalWrite
with analogWrite
and then use value
instead of state
ranging from 0
to 254
to set desired voltages to increase or decrease intensity.