i understand how it works but something is still not clear so anyone if possible could you explain by taking some example like take temperature alert project. what happens when i run the python script lets say temp_alert.py in command prompt(since iam using CMD to run python scripts) i want to know how that project works by simply running python script
how bolt works on python i mean i want to know what is the role of Bolt cloud in this whole process
@kondruabhay
We control our Bolt module by sending API requests to the Bolt Cloud server. You can do this by heading to the API section in the documentation. https://docs.boltiot.com/docs/api-request-syntax The cloud helps us to have a database online, so that we don’t need to locally connect with our wifi module.
What boltiot’s python package has, are the predefined functions like digitaWrite/Read, analogWrite/Read, etc which includes parameters and the function to make an HTTP request to the server.
You can perform this from a webpage as well. You can use AJAX to use the custom functions present for javascript in https://cloud.boltiot.com/static/js/boltCommands.js
The python, javascript functions all of them does the same job, i.e. making an HTTP request GET with the API URL.
Let me know if you need any other information.
ok for example when i run the python script temp_alert.py the example i mentioned it sends api request to bolt cloud to api request telegram to send message!!!.
correct me if im wrong!!
Half right. It surely sends an API request to bolt cloud. But only the part where bolt module is included, like reading your pin.
Telegram API request is sent to telegram website. The URL provided in the function.
response = requests.request(“POST”,url,params=data)
what does the above line do in python script
Using HTTP request to perform some http actions, like GET, PUT, POST, DELETE. You’ll understand more about this from some random youtube video explaining about http methods.
So, these are the statements that are responsible for connecting you with the internet.
response will be in HTTP format so we convert it into JSON correct me if im wrong!!
I thought it would be better just to add an example to @akshayan.sinha answer to give you a more clear understanding that Request is a python library which provides users with different kind of mechanism to fetch or push data to server depending on the requirement of user. Most common ones are:
- Get
- Post.
Get is used for requesting data from server while post is used to submit data to server. Here you asked about post so have attached example related to it.
Typically, you want to send some form-encoded data — much like an HTML form. To do this, simply pass a dictionary to the data argument. Your dictionary of data will automatically be form-encoded when the request is made:
payload = {‘key1’: ‘value1’, ‘key2’: ‘value2’}
r = requests.post(“https://httpbin.org/post”, data=payload)
print(r.text)
{
…
“form”: {
“key2”: “value2”,
“key1”: “value1”
},
…
}
The data argument can also have multiple values for each key. This can be done by making data either a list of tuples or a dictionary with lists as values. This is particularly useful when the form has multiple elements that use the same key:
payload_tuples = [(‘key1’, ‘value1’), (‘key1’, ‘value2’)]
r1 = requests.post(‘https://httpbin.org/post’, data=payload_tuples)
payload_dict = {‘key1’: [‘value1’, ‘value2’]}
r2 = requests.post(‘https://httpbin.org/post’, data=payload_dict)
print(r1.text)
{
…
“form”: {
“key1”: [
“value1”,
“value2”
]
},
…
}r1.text == r2.text
True
I hope you find it helpful.
yes its definitely helpful thanks but i have a small doubt what will print(r.text) will print?
No no wait. The responses of HTTP and JSON are different.
HTTP response will return us with 200, 404 503 which means OK, Error Not found and Service Unavailable respectively. There are few more responses which have their unique reason cited across the reason. It will return you, with whether or not the URL you’re accessing is OK / forbidden / unviable etc. So, when you see https status being 200, it means the website is accessible and its working fine. And so, you’re allowed to access it.
On the other hand, JSON is simply object notation found in the form of key and value. Like username and password. Most of the APIs return in the JSON format, set by the cloud manager or architect. The need for this, is to have a simple way of conveying a message. So, if you run our Bolt API on the browser, you’ll see different fields in which form you can view it. In JSON format, it is easy to read as key and value. When you switch to RAW format, you’ll see what the message conveyed by the API URL really looks like. So, need for JSON in python is to have the ability to differentiate between key and value and being able to use that data to manipulate out program.
Hope this clears out the difference.
Yes.
You can also, add this data = json.loads(r)
and print(data)
telegram_data = json.loads(response.text)`
dont we use above code to convert it into JSON format if it is alreaady in json format then why
according to above lines first we are printing response in text format and then converting it into JSON that means
JSON ->text ->JSON
By default it is present in RAW format, i.e.
The reason behind this is to return the value of a particular key that cannot be read as text.
The above scripts store the data of the URL response in JSON format. So, when you need the value of any key, you can return it directly by print(telegram_data[“key”])
i got this doubt by observing this
this is part of my code
this is out put of that part of code
both are same either in text format or in JSON so since we cant access or return data in text format we are converting it into json and telegram response is in RAW format thats why we convert it into text
correct me if im wrong
It generally returns the encoding of the response obtained from Server. For further assistance you can go through the documentation of request library which will make it more clear.