0

I have a function call as follow:

send_at('AT+CMQPUB=0,"sensor",1,0,0,34',"hex_numbers_here",'OK',10)

I want the "34" and the "hex_numbers_here" to be variables instead of literal inputs, but my attempts to "escape" out of the quotes are failing miserably...

Please help!

I tried:

send_at('AT+CMQPUB=0,"sensor",1,0,0,'+myval+',"'+my_hex+'",'OK',10)

where myval and my_hex are the variables I created earlier in my code.

update:

I tried the following as well now:

mqtt_msg = b'testing mqtt message from sim7020' 
output = binascii.b2a_hex(mqtt_msg) 
length = len(output) 
print(output) 
output_text = str(output) 
print(output_text) 
f1 = 'AT+CMQPUB=0,"sensor",1,0,0,'+str(length)+',"'+output_text+'"' 
f2 = 'OK' 
f3 = '10' 
print(f1)
 AT+CMQPUB=0,"sensor",1,0,0,66,"b'74657374696e67206d717474206d6573736167652066726f6d2073696d37303230'"

The only thing noiw is that the output_text variable should not contain the b'' - making me think that the casting to str() of the hex/bit value is now doing what I think it should be doing...

BTW - I HAVE to cast the text to binary/hex - this is what the modem wants...

UPDATE:

OK, I found a way around this that is working now:

mqtt_msg = b'testing mqtt message from sim7020 - and again!'
output = binascii.b2a_hex(mqtt_msg)
length = len(output)
print(output)
output_text = str(output)
print(output_text)
output_text = output_text[2:length+2]
new_len = len(output_text)
f1 = 'AT+CMQPUB=0,"sensor",1,0,0,'+str(new_len)+',"'+output_text+'"'
f2 = 'OK'
f3 = '10'
print(f1)

I'm basically just stripping out the first 2 and last chars from the string.

Monty
  • 1,161
  • 2
  • 15
  • 26
  • What do you mean by "want to be variables"? Doesn't `func(var1, var2, ...)` work? – iBug Jan 19 '21 at 06:48
  • You use f-variables in Python instead of concatenating variables using `+` but you don't even need that for `hex_numbers_here`. Try `send_at(f'AT+CMQPUB=0,"sensor",1,0,0,{myval}', my_hex, 'OK', 10)`. – Selcuk Jan 19 '21 at 06:54

1 Answers1

0

What about this?

def send_at(p1, p2, p3, p4):
    print(p1, p2, p3, p4)

send_at('AT+CMQPUB=0,"sensor",1,0,0,{}'.format(34), '{}'.format('FC0012'), 'ok', 10)

Output:

AT+CMQPUB=0,"sensor",1,0,0,34 FC0012 ok 10
Alireza Saremi
  • 401
  • 1
  • 7
  • 13
  • Why `'{}'.format('FC0012')`? Isn't it the same thing as `'FC0012'`? – Selcuk Jan 19 '21 at 06:55
  • OK, your suggestion seems to give me best results so far, mqtt_msg = b'testing mqtt message from sim7020' output = binascii.b2a_hex(mqtt_msg) length = len(output) print(output) output_text = str(output) print(output_text) > f1 = 'AT+CMQPUB=0,"sensor",1,0,0,'+str(length)+',"'+output_text+'"' f2 = 'OK' f3 = '10' print(f1) AT+CMQPUB=0,"sensor",1,0,0,66,"b'74657374696e67206d717474206d6573736167652066726f6d2073696d37303230'" I just need to get rid of the b' ____ ' part in the result - seems that casting the bits to string doesn't work? – Monty Jan 19 '21 at 08:23