Serial to UART POST JSON



  • Hi All,

    I have a weigh station transmitting weight data via RS232, I have connected this to UART1 pins 3 & 4 pins via TTL and I can receive ASCII string and print this using the

    print("Weight POST Successful - " + str(uart1.readall()) + " g")

    however when i use the same to print into a JSON payload it prints none.

    Here is the code

    while True:

    res = urequests.post('https://My_URL', json={'Weight': str(uart1.readall())})
    res.close()
    if (res.status_code - 200) < 100: # check that response code is in the 200 range
        print("Weight POST Successful     - " + str(uart1.readall()) + " g")
    else:
        print('there was some error')
    
    gc.collect()
    pycom.heartbeat(False)


  • @livius

    I added tmp = uart1.readall() and it works thanks again I managed to do this before your reply thanks again you are always very helpful



  • @jimpower said in Serial to UART POST JSON:

    tmp()

    without parenthesis



  • @livius

    res = urequests.post('https://My_URL', json={'Weight': str(uart1.readall())})

    sends none

    print("Weight POST Successful - " + str(uart1.readall()) + " g")

    prints as expected

    the second print is only to see what is coming through in terminal I can remove it as its not required.

    If I were to use it as you suggest would it look like this?

    while True:

    tmp = uart1.readall()

    res = urequests.post('https://My_URL', json={'Weight': str(tmp())})
    res.close()
    if (res.status_code - 200) < 100: # check that response code is in the 200 range
    print("Weight POST Successful - " + str(tmp()) + " g")
    else:
    print('there was some error')

    gc.collect()
    pycom.heartbeat(False)



  • @jimpower
    Do you mean that

    print("Weight POST Successful     - " + str(uart1.readall()) + " g")
    

    does not print anything

    or

    res = urequests.post('https://My_URL', json={'Weight': str(uart1.readall())})
    

    does not send anything? or both?

    You must know that uart1.readall() clear buffer and second uart1.readall() read nothing (or new incomming data)
    you must store data into var to use it in second print

    e.g.

    tmp = uart1.readall()
    

 

Hello World?

Pylife on Kickstarter - November 2018








Back Us On Kickstarter >

Pycom on Twitter