Hello everyone,
I hope you are doing well. I’m currently working on a project using the Bolt IoT platform, and I need some help with my code. I am trying to collect sensor data from multiple sensors and transmit it to the Bolt Cloud for further analysis and visualization.
I have written the following code, but I believe there might be some issues that I need assistance with:
import requests
import json
import time
# Bolt Cloud API Configuration
API_KEY = "MY_API_KEY"
DEVICE_ID = "MY_DEVICE_ID"
# Analog Pin
analog_pin = "A0"
# Multiplexing Digital Pins
Temp = "D0"
Mos = "D1"
Lig = "D2"
# Bolt Cloud API Endpoint
base_url = "https://cloud.boltiot.com"
def select_sensor_type(sensor_type):
# Function to select a specific sensor by setting the corresponding digital pin to HIGH
url = "https://cloud.boltiot.com/remote/MY_API_KEY/digitalWrite"
if sensor_type == "Temp":
# Select Temperature Sensor
querystring = {"pin": "0", "state": "HIGH", "deviceName": "MY_DEVICE_ID"}
headers = {
'Cache-Control': "no-cache"
}
requests.request("GET", url, headers=headers, params=querystring)
if sensor_type == "Mos":
# Select Moisture Sensor
querystring = {"pin": "1", "state": "HIGH", "deviceName": "MY_DEVICE_ID"}
headers = {
'Cache-Control': "no-cache"
}
requests.request("GET", url, headers=headers, params=querystring)
if sensor_type == "Lig":
# Select Light Sensor
querystring = {"pin": "2", "state": "HIGH", "deviceName": "MY_DEVICE_ID"}
headers = {
'Cache-Control': "no-cache"
}
requests.request("GET", url, headers=headers, params=querystring)
def deselect_all_sensors():
# Function to deselect all sensors by setting all digital pins to LOW
url = "https://cloud.boltiot.com/remote/MY_API_KEY/digitalWrite"
# Deselect Temperature Sensor
querystring = {"pin": "0", "state": "LOW", "deviceName": "MY_DEVICE_ID"}
headers = {
'Cache-Control': "no-cache"
}
requests.request("GET", url, headers=headers, params=querystring)
# Deselect Moisture Sensor
querystring = {"pin": "1", "state": "LOW", "deviceName": "MY_DEVICE_ID"}
headers = {
'Cache-Control': "no-cache"
}
requests.request("GET", url, headers=headers, params=querystring)
# Deselect Light Sensor
querystring = {"pin": "2", "state": "LOW", "deviceName": "MY_DEVICE_ID"}
headers = {
'Cache-Control': "no-cache"
}
requests.request("GET", url, headers=headers, params=querystring)
def get_sensor_data(pin, stype):
# Function to read sensor data from the specified pin
select_sensor_type(stype)
url = f"{base_url}/remote/{API_KEY}/analogRead?deviceName={DEVICE_ID}&pin={pin}"
response = requests.get(url)
data = json.loads(response.text)
deselect_all_sensors()
sensor_value = int(data["value"])
return sensor_value
def send_sensor_data(data):
# Function to send sensor data to the Bolt Cloud and create charts
url = f"{base_url}/remote/{API_KEY}/{DEVICE_ID}/sensor_data"
headers = {"Content-Type": "application/json"}
payload = json.dumps(data)
response = requests.post(url, headers=headers, data=payload)
print("Data sent to Bolt Cloud:", response.text)
# Create charts
chart_url = f"{base_url}/remote/{API_KEY}/chart?deviceName={DEVICE_ID}"
chart_data = {
"yAxis": {
"temperature": data["temperature"],
"moisture": data["moisture"],
"light": data["light"]
}
}
response = requests.post(chart_url, headers=headers, data=json.dumps(chart_data))
print("Chart created in Bolt Cloud:", response.text)
def collect_and_transmit_data():
# Function to collect sensor data and transmit it to the Bolt Cloud
temperature = get_sensor_data(analog_pin, "Temp")
time.sleep(10) # Delay for stability
moisture = get_sensor_data(analog_pin, "Mos")
time.sleep(10) # Delay for stability
light = get_sensor_data(analog_pin, "Lig")
# Prepare data in the desired format
sensor_data = {
"temperature": temperature,
"moisture": moisture,
"light": light
}
# Send data to the Bolt Cloud
send_sensor_data(sensor_data)
# Main program loop
while True:
try:
collect_and_transmit_data()
time.sleep(300) # Delay between data collection and transmission
except KeyboardInterrupt:
print("Program terminated by user.")
break
except Exception as e:
print("An error occurred:", str(e))
In this code, I have defined the necessary API configurations, pins for multiplexing sensors, and the base URL for the Bolt Cloud API. However, I’m not sure if the implementation for reading data from different sensors and transmitting it to the Bolt Cloud is correct.
I would greatly appreciate it if someone could review my code and provide guidance on any corrections or improvements that I should make. Specifically, I would like to understand how to properly select and read data from different sensors using the Bolt IoT platform and ensure its accurate transmission to the Bolt Cloud.
note: check if my URls for data transmission are correct or not.
Thank you in advance for your help and guidance. I’m looking forward to learning from your expertise and suggestions.
Best regards,
Zameel.