Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It looks like there is a bug with parsing the throttle topic. #135

Open
correiaal opened this issue Sep 18, 2021 · 0 comments
Open

It looks like there is a bug with parsing the throttle topic. #135

correiaal opened this issue Sep 18, 2021 · 0 comments

Comments

@correiaal
Copy link

I posted this first in the forum and brubell asked to post an issue here.

I am using Python Adafruit_IO MQTT library and everything looks fine, except for that I am not succeeding in subscribing to throttle and errors topic.
I realize, by looking at the code on Github, that the platform requires different string formations depending on the type of subscription you need. And the functions or modules are specifically coded to form that kind of string. For regular feeds the subscribe module will get the feed Id and combine with username to create a string like " "{username}/feeds/{feed_id}". The subscribe_group requires "{username}/groups/{group_id}". The subscribe_time module will do some logic to form the string like "time/{type of time}. And so on.
As the errors and throttle topics string are {username}/errors and {username}/throttle, respectively. I guess an specific module or function is missing.
I intentionally went above publishing maximum threshold to test using another client I have running where I would like to add throttle control based on the warnings. I see the throttle warning on monitor, but no message, if I subscribe using the regular feed module.
I tried to create function in another client to accomplish with the subscription, copying and adapting the subscribe_time module, but it gives errors when a message should be coming. Maybe this is not possible or I did it wrong.

  • Platform/operating system (i.e. Raspberry Pi with Raspbian operating system,
    Windows 32-bit, Windows 64-bit, Mac OSX 64-bit, etc.): Raspberry Pi with Raspbian and Windows 64-bit

  • Python version (run python -version or python3 -version): python3 3.7.3 on Raspberry Pi and pytho3 3.9.7 on Windows

  • Error message you are receiving, including any Python exception traces:
    When the throttle warnings start to appear I get the errors below running on Windows:


Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9__...\lib\threading.py", line 973, in bootstrap_inner
self.run()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9
...\lib\threading.py", line 910, in run
self.target(*self.args, **self.kwargs)
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9
...\LocalCache\local-packages\Python39\site-packages\paho\mqtt\client.py", line 3452, in thread_main
self.loop_forever(retry_first_connection=True)
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9
...\LocalCache\local-packages\Python39\site-packages\paho\mqtt\client.py", line 1779, in loop_forever
rc = self.loop(timeout, max_packets)
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9__...\LocalCache\local-packages\Python39\site-packages\paho\mqtt\client.py", line 1181, in loop
rc = self.loop_read(max_packets)
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9__...\LocalCache\local-packages\Python39\site-packages\paho\mqtt\client.py", line 1572, in loop_read
rc = self.packet_read()
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9
_...\LocalCache\local-packages\Python39\site-packages\paho\mqtt\client.py", line 2310, in packet_read
rc = self.packet_handle()
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9
...\LocalCache\local-packages\Python39\site-packages\paho\mqtt\client.py", line 2936, in packet_handle
return self.handle_publish()
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9
...\LocalCache\local-packages\Python39\site-packages\paho\mqtt\client.py", line 3216, in handle_publish
self.handle_on_message(message)
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9
...\LocalCache\local-packages\Python39\site-packages\paho\mqtt\client.py", line 3444, in handle_on_message
self.on_message(self, self.userdata, message)
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9
...\LocalCache\local-packages\Python39\site-packages\Adafruit_IO\mqtt_client.py", line 121, in _mqtt_message
elif parsed_topic[2] == 'weather':
IndexError: list index out of range

I get no errors on Raspberry Pi, but no message is coming from the throttle topic, even if I get the throttle warnings on my profile page and in the journal in my dashboard.

  • List the steps to reproduce the problem below (if possible attach code or commands
    to run): LIST REPRO STEPS BELOW
    Here is my code:

import sys
import time

from Adafruit_IO import MQTTClient, Client, MQTTError, ThrottlingError
from Adafruit_IO.errors import MQTT_ERRORS

ADAFRUIT_IO_KEY = ''
ADAFRUIT_IO_USERNAME = '
**'
myFeeds = {'water-level':'40.0',
'command':'no command',
'operations':'ON',
'pump-time':'30.0',
'errors':'',
'throttle':''}
myPubDict = {'feedId':'feedname', 'feedvalue':'0.0'}
myPubList = []
countMsg =0
connSubscr=0
rateStat = {'countPub':0, 'lastPub': time.time(),'currRate':0, 'lastRate':0, 'maxRate':0,'wait':False}
rateLimit =7
countPub =0

def connected(client):
print('Connected to Adafruit IO! Listening for and changes...')
for f in myFeeds:
if f not in ['throttle','errors']:
client.subscribe(f)

def subscribed(client, userdata, mid, granted_qos):
global connSubscr
print('Subscribed to {0} with QoS {1} for {2}'.format(mid, granted_qos[0],userdata))
connSubscr += 1

def disconnected(client):
print('Disconnected from Adafruit IO!')
sys.exit(1)

def message(client, feed_id, payload):
global countMsg, myFeeds
countMsg +=1
print('Feed {0} received new {2} value: {1}'.format(feed_id, payload,countMsg))
myFeeds[feed_id]=payload

def updateRate(rateStat,limit=20):
if rateStat['wait']: num =0
else: num =1
rateStat['countPub']+=num
dif = time.time() - rateStat['lastPub']
if dif <= 60:
rateStat['currRate']+=num
else:
rateStat['lastRate']=rateStat['currRate']
rateStat['currRate']=num
rateStat['lastPub']= time.time()
if rateStat['lastRate']> rateStat['maxRate']:
rateStat['maxRate']= rateStat['lastRate']
rateStat['wait'] = rateStat['currRate']>limit
print (rateStat)

def adaSend (client, sendList, rateCtrl, get=''):
if len(sendList)>0:
print('Publishing {0}{1} to {2} = {3}.'.format(sendList[0]['feedValue'], get,sendList[0]['feedId'],not rateStat['wait']))
if not rateCtrl['wait']:
client.publish('{0}{1}'.format(sendList[0]['feedId'],get),sendList[0]['feedValue'])
sendList.pop(0)
updateRate(rateCtrl,rateLimit)

def subscribe_throttle(self):
"""Subscribe to throttle topic on the Adafruit IO."""
self._client.subscribe('{0}/errors'.format(self._username))
print (self._username)
return

client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
client.on_connect = connected
client.on_disconnect = disconnected
client.on_message = message
client.on_subscribe = subscribed

client.connect()

lastadd=time.time()
lastpub=lastadd
client.loop_background()
value = 'Teste'
while connSubscr < 4:
pass
for f in myFeeds:
if f not in ['command']:
client.publish(f+'/get')
wait = updateRate(rateStat,len(myFeeds))
subscribe_throttle(client)
while countMsg < 30:
if (time.time() - lastadd) >= 10.0:
countPub +=1
value = 'Teste'+str(countPub)
myPubList.append({'feedId':'command', 'feedValue':value})
print (myPubList)
lastadd = time.time()
print('time is {0}'.format(lastadd))
if (time.time() - lastpub) >= 5.0:
adaSend (client, myPubList, rateStat)
lastpub = time.time()
client.loop_background(stop=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant