Why does uploading data from Pi block UART Communication?

Viewed 30

I have a waveshare TOF Laser Range Sensor (B) that I am using with my Raspberry Pi 3 Model B+. My main goal is to receive distance from the sensor and upload that data to ThingSpeak cloud platform. The first part of the code works well: I am receiving the distance and timestamp, along with signal status and data check from the sensor. However, when I try to upload the timestamp and distance values to the cloud platform, the sensor's data is is incorrect and will be values that have only 5-7 cm of variation, even though the object is very close to it. I have tried using async requests using the aiohhtp and ayncio libraries, to no avail.

Here is the demo code from the manufacturers of the sensor that I have modified to send async requests.

#coding: UTF-8
import RPi.GPIO as GPIO
import serial 
import time
import chardet
import sys
import aiohttp
import asyncio

#ThingSpeak Cloud Write Definitions:
channel_id = "1853890"
write_api_key = "G22BQASFVWJT6T"


TOF_length = 16
TOF_header=(87,0,255)
TOF_system_time = 0
TOF_distance = 0
TOF_status = 0
TOF_signal = 0
TOF_check = 0
    
ser = serial.Serial('/dev/serial0',921600)
ser.flushInput()

#Async Function to upload Data
async def upload(timer, dist):
    async with aiohttp.ClientSession() as session:
        upload_url = "https://api.thingspeak.com/update?api_key=G22BQASFVWJT6TOH&field1=" + str(timer) + "&field2=" + str(dist)
        async with session.get(upload_url) as res:
            print('ok')
        

def verifyCheckSum(data, len):
    #print(data)
    TOF_check = 0
    for  k in range(0,len-1):
        TOF_check += data[k]
    TOF_check=TOF_check%256
    
    if(TOF_check == data[len-1]):
        print("TOF data is ok!")
        return 1    
    else:
        print("TOF data is error!")
        return 0  
  

while True:
    TOF_data=()
    timer=0
    dist=0
    time.sleep(0.01)
    if ser.inWaiting() >=32:
        for i in range(0,16):
            TOF_data=TOF_data+(ord(ser.read(1)),ord(ser.read(1)))
        #print(TOF_data)
        for j in range(0,16):
            if( (TOF_data[j]==TOF_header[0] and TOF_data[j+1]==TOF_header[1] and TOF_data[j+2]==TOF_header[2]) and (verifyCheckSum(TOF_data[j:TOF_length],TOF_length))):
                if(((TOF_data[j+12]) | (TOF_data[j+13]<<8) )==0):
                    print("Out of range!")
                else :
                    print("TOF id is: "+ str(TOF_data[j+3]))
                

                    TOF_system_time = TOF_data[j+4] | TOF_data[j+5]<<8 | TOF_data[j+6]<<16 | TOF_data[j+7]<<24
                    print("TOF system time is: "+str(TOF_system_time)+'ms')
                    timer = TOF_system_time

                    TOF_distance = (TOF_data[j+8]) | (TOF_data[j+9]<<8) | (TOF_data[j+10]<<16)
                    print("TOF distance is: "+str(TOF_distance)+'mm')
                    dist=TOF_distance

                    TOF_status = TOF_data[j+11]
                    print("TOF status is: "+str(TOF_status))
                    TOF_signal = TOF_data[j+12] | TOF_data[j+13]<<8
                    print("TOF signal is: "+str(TOF_signal))

                    #Calling async function to upload data:
                    asyncio.run(upload(timer, dist))
                break

Here is the output when calling upload method:

Sensor output with upload

Here is the output when not calling the upload method:

Sensor output without upload

Can someone please explain what I am doing wrong and correct me?

Thanks!

0 Answers
Related