Re: [Dorset] Python Problem in MQTT subscriber program

2021-08-26 Thread Patrick Wigmore
On Thu, 26 Aug 2021 14:10:31 +0100, PeterMerchant wrote:
> Problem sorted, though I don't know why.
> I took out the hyphens in the Pot-Left & Right strings, and made
> them Pleft and Pright. and it does not crash.
> 
> Peter.

I don't know whether you are still interested in getting to the bottom 
of this now that you've got it working.

I've just had a look at your original message with the code excerpts 
and I could not see anything that stood out to me as obviously wrong. 
But I am missing a lot of context.

Here are some questions and suppositions I might put to myself if I 
was trying to get to the root of why this was happening:

When does it crash? i.e. what part of the program is it executing when 
it crashes. I'm guessing it crashes when the subscriber receives a new 
message, rather than as soon as you try to run the program, but does 
it crash for _any_ message, or only messages with particular payloads?

What (if any) traceback do you get when it crashes? That's probably 
useful information.

I don't know what kind of object 'message' is, or what calls these 
functions you've posted. That might be relevant.

It could be that whatever code in the subscriber is creating the 
message object is not handling the hyphens properly, or it is not 
encoding the strings into exactly the format you expected.

Maybe the hyphens are being converted into some other character 
*before* getting sent over the message bus, or perhaps the byte 
encoding is coming directly from the publisher and is not 100% 
compatible with the encoding that the subscriber is expecting.

Are both the publisher and the subscriber written in Python? Does the 
underlying CPU architecture affect the binary format for either one, 
or is that all abstracted away for us?

Maybe more messages than expected are being sent and received. Perhaps 
motorleft(), motorright(), etc, can't handle being called too 
frequently.

No worries if you are not going to dig any further into this.

Patrick



-- 
  Next meeting: Online, Jitsi, Tuesday, 2021-09-07 20:00
  Check to whom you are replying
  Meetings, mailing list, IRC, ...  http://dorset.lug.org.uk
  New thread, don't hijack:  mailto:dorset@mailman.lug.org.uk


Re: [Dorset] Python Problem in MQTT subscriber program

2021-08-26 Thread PeterMerchant

Problem sorted, though I don't know why.
I took out the hyphens in the Pot-Left & Right strings, and made them Pleft and 
Pright. and it does not crash.

Peter.

On 23/08/2021 18:29, PeterMerchant wrote:

I have three versions of this program, and two work, but the third with the 
differences put together crashes.   Can anybody advise me what is happening.

Here are the relevant bits.

1. Working


def on_message(client, userdata, message):
   if message.payload  == b'forward':
  up_15()
   elif message.payload  == b'slow':
  down_15()
   elif message.payload  == b'stop':
  on()
   elif message.payload  == b'motorleft':
  motorleft()
   elif message.payload  == b'motorright':
  motorright()
   elif message.payload  == b'backwards':
  mbackwards()
   else:
  print(message.payload)

the stuff in quotes 'forward' etc is the message payload that I act on.

2 Working

def on_message(client, userdata, message):
   if message.payload  == b'forward':
  up_15()
   elif message.payload  == b'slow':
  down_15()
   elif message.payload  == b'stop':
  on()
   elif message.payload  == b'Pot-left':
  motorleft()
   elif message.payload  == b'Pot-rt':
  motorright()
   elif message.payload  == b'backwards':
  mbackwards()
   else:
  print(message.payload)

the left and right have been changed to Potentiometer values

and 3, not working


def on_message(client, userdata, message):
   if message.payload  == b'forward':
  up_15()
   elif message.payload  == b'slow':
  down_15()
   elif message.payload  == b'stop':
  on()
   elif message.payload  == b'motorleft':
  motorleft()
   elif message.payload  == b'motorright':
  motorright()
   elif message.payload  == b'Pot-left':
  motorleft()
   elif message.payload  == b'Pot-rt':
  motorright()
   elif message.payload  == b'backwards':
  mbackwards()
   else:
  print(message.payload)

Actually the Pot-rt lines have been commented out. It did work with both the 
Pot-* bits commented out.


Thanks for any thoughts.





--
 Next meeting: Online, Jitsi, Tuesday, 2021-09-07 20:00
 Check to whom you are replying
 Meetings, mailing list, IRC, ...  http://dorset.lug.org.uk
 New thread, don't hijack:  mailto:dorset@mailman.lug.org.uk