analogMultiWrite function not working for boltiot python library

I am using the boltiot python library but the analogMultiWrite isn’t working…

THE CODE:

import os
import time
import json
import tkinter as tk
from boltiot import Bolt
import sys # Import sys module for sys.exit

Fetch API key and device ID from environment variables

bolt_api_key = os.getenv(‘BOLT_API_KEY’)
bolt_device = os.getenv(‘BOLT_DEVICE’)

Initialize Bolt device

mybolt = Bolt(bolt_api_key, bolt_device)

light_on = False

def initial_check():
status_response = mybolt.isOnline()
status_response = json.loads(status_response)

if status_response['success'] == 1:
    if status_response['value'] == 'online':
        print("DEVICE " + bolt_device + " IS ONLINE ;-)")
    elif status_response['value'] == 'offline':
        print("DEVICE " + bolt_device + " IS OFFLINE x-(")
        sys.exit(1)
    else:
        print("<FAILED TO FETCH RESPONSE> response value: ", status_response['value'])
        sys.exit(1)
else:
    print("UNABLE TO CHECK DEVICE STATUS. Error: ", status_response['value'], status_response['success'], bolt_device)
    sys.exit(1)

def buzzer_alert():
buzz_state = mybolt.analogMultiWrite(‘1’, ‘2’, ‘250’, ‘200’)
return buzz_state

def buzzer_close():
buzz_close = mybolt.analogMultiWrite(‘1’, ‘2’, ‘0’, ‘0’)
return buzz_close

def light_ON():
state_change = mybolt.digitalWrite(‘0’, ‘HIGH’)
buzzer_alert()
time.sleep(1)
buzzer_close()
return state_change

def light_OFF():
state_shift = mybolt.digitalWrite(‘0’, ‘LOW’)
buzzer_alert()
time.sleep(1)
buzzer_close()
return state_shift

def toggle_light():
global light_on
if light_on:
light_OFF()
else:
light_ON()
light_on = not light_on

def sense_env():
env_read = mybolt.analogRead(‘A0’)
env_read = json.loads(env_read)
if int(env_read[‘value’]) < 1000:
state_transfer = mybolt.digitalWrite(‘0’, ‘HIGH’)
buzzer_alert()
time.sleep(1)
buzzer_close()
return state_transfer
elif int(env_read[‘value’]) >= 1000:
state_transfer = mybolt.digitalWrite(‘0’, ‘LOW’)
buzzer_alert()
time.sleep(1)
buzzer_close()
return state_transfer
else:
print(env_read[‘value’])

def change_intensity():
value = spinbox.get()
intensity_change = mybolt.analogWrite(‘0’, value)
return intensity_change

def main():
initial_check()
root = tk.Tk()
root.title(“LIGHT CONTROLLER”)

global spinbox
spinbox = tk.Spinbox(root, values=(0, 50, 100, 150, 200, 255))
spinbox.pack(pady=10)

toggle_button = tk.Button(root, text="LIGHT SWITCH", command=toggle_light)
toggle_button.pack(pady=10)

sense_button = tk.Button(root, text="SMART SENSE", command=sense_env)
sense_button.pack(pady=10)

intensity_button = tk.Button(root, text="REGULATOR", command=change_intensity)
intensity_button.pack(pady=10)

root.mainloop()

if name == ‘main’:
main()

THE ERROR:

Exception in Tkinter callback
Traceback (most recent call last):
File “/usr/lib/python3.10/tkinter/init.py”, line 1921, in call
return self.func(*args)
File “/home/danlappisto22/bolt_projects/light_control.py”, line 61, in toggle_light
light_ON()
File “/home/danlappisto22/bolt_projects/light_control.py”, line 44, in light_ON
buzzer_alert()
File “/home/danlappisto22/bolt_projects/light_control.py”, line 35, in buzzer_alert
buzz_state = mybolt.analogMultiWrite(‘1’, ‘2’, ‘250’, ‘200’)
AttributeError: ‘Bolt’ object has no attribute ‘analogMultiWrite’

Hi @dan.lappisto22

The Bolt Python library does not have the MultiWrite function. You can instead use the digitalWrite function. It will give you similar output.

Do try this and let us know. If you still face any issue, please feel free to get back to us.

Hi @dan.lappisto22
You can use loop for passing these values to the desired GPIO pins.
Given below is the sample code.
def buzzer_alert():
values = [(1, 250), (2, 200), (1, 150), (2, 100)] # Example analog values
for pin, value in values:
buzz_state = mybolt.analogWrite(pin, value)
time.sleep(0.5) # Delay to simulate the alert pattern
return buzz_state

def buzzer_close():
values = [(1, 0), (2, 0), (1, 50), (2, 50)] # Example analog values for closing
for pin, value in values:
buzz_close = mybolt.analogWrite(pin, value)
time.sleep(0.5) # Delay to simulate the closing pattern
return buzz_close