Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
websockets server loop?
#1
Hello
on my de vice there is a sensor that read data all the time
I want to create a web socket , that when someone enter it he will get the data everytime it change
(I did it in the past in JAVA)


this is what I have doen:
import os
import time
import websockets
import asyncio

async def DataServer(websocket , path):
    message = bus.recv()
    # print (message)
    PID = message.arbitration_id
    dlc= message.dlc
    MyData = message.data
    MSG_Time = message.timestamp
    #print(str(MyData))
    New_Data = (bytes(MyData).hex()).upper()
    New_Data = ' '.join(New_Data[i:i+2] for i in range(0,len(New_Data),2))
    #print(New_Data)
    PID_Hex = str(hex(PID))
    print (str(MSG_Time) + '   Hex PID is - ' + PID_Hex)
    if PID_Hex == hex(0x48c):
        print('this is Door 1')
        print ('I need bytes 1 , bit 2,1')
        door1 = New_Data[0:2]
        print(door1)

    elif PID_Hex == hex(0x48d):
        print('this is Door 2')
        print ('I need bytes 1 , bit 2,1')
        door2 = New_Data[0:2]
        print(door2)
        result = getBinarybits(door2,0,2)
        print('you need this ' + result)
    else:
        print("I don't know this PID!")

    print ('done')

    SendData = (door1 + "!" + door2)
    await websocket.send(SendData)

start_server = websockets.serve(DataServer, '10.0.0.100', 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
print ('WebSocket Server Start!') # I don;t see this message 
but it doesn't read my the data all the time
it's seem that it only read the data after I open web socket to him

what did I do wrong ?
I want the main will be the reading of the sensor

Thanks ,
Reply
#2
I have also try to run this code:

import asyncio
import websockets
import time
from datetime import datetime

async def hello(websocket, path):
    await websocket.send(TS)

print('going to start')
start_server = websockets.serve(hello, "10.0.0.188", 8765)
print('Start')
asyncio.get_event_loop().run_until_complete(start_server)
print('I see you')

while True:
    TS = datetime.now().strftime('%d-%m-%y %H-%M-%S-%f')
    print('time is - ' + str(TS))
    time.sleep(0.251)
here I can see the time printing , but when I open a websocket to the server , the client is waiting for something without any reply . the server is keep printing the time

so I guess I'm close ,
but what am I missing?

Thanks,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  websockets and pymongo exception sankar2000 0 949 Oct-07-2022, 09:52 PM
Last Post: sankar2000
  How to take the tar backup files form remote server to local server sivareddy 0 1,871 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  How to print results of asyncio websockets at the same time? codingmonster 0 1,744 Jun-04-2021, 01:48 PM
Last Post: codingmonster
  Server infinite loop input from user tomislav91 1 4,168 May-23-2019, 02:18 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020