upy-websocket-server from Beta_Ravener

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
sf-ing
Posts: 2
Joined: Sun Feb 03, 2019 8:55 am

upy-websocket-server from Beta_Ravener

Post by sf-ing » Sun Feb 03, 2019 9:14 am

Hello,

I'm new to this forum and I like to say thank you for this huge information base in micropython programming.
I use an ESP8266 as an webserver with websocket function. I have implemented the upy-websocket-server module from here https://github.com/BetaRavener/upy-websocket-server. I can connect from my browser to the ESP8266 and send information between them. But at the moment I can only react on request from the browser, but I like to send information without requesting.
In my program I like to set a timer and read out temperature from a sensor every second. If the temperature is different from last read, then I like to send the information to the browser. So the ESP8266 is starting the websocket communication and not the browser.
I hope it is clear what I like to do.
Here is the code basis I use:

Code: Select all

 
 class TestClient(WebSocketClient):
    def __init__(self, conn):
        super().__init__(conn)

    def process(self):
        try:
            msg = self.connection.read()
            if not msg:
                return
            msg = msg.decode("utf-8")
            items = msg.split(" ")
            cmd = items[0]
            if cmd == "Hello":
                self.connection.write(cmd + " World")
                print("Hello World")
        except ClientClosedError:
            self.connection.close()
I get messages/requests from my browser and with the line "self.connection.write(...)" I am able to send an answer to my browser.
But I like to send information outside of this class. I will be send information without getting requests from the browser. How can I do that? I think it is more a python question than ESP8266, but I hope somebody can help me.


Thank you in advance

Best regards Sebastian

sf-ing
Posts: 2
Joined: Sun Feb 03, 2019 8:55 am

Re: upy-websocket-server from Beta_Ravener

Post by sf-ing » Fri Feb 08, 2019 4:25 pm

Hello,

I have found a solution with this extract of code:

Code: Select all

        for client in server._clients:
            print(static_var.ambient_temp)
            client.connection.write("Send from server to webbrowser")
With this code all connected clients get this message. Sometimes there is a long delay (seconds to ten of seconds) between calling this on the ESP and getting reaction in the browser. Some messages where never delivered to the browser.
Perhaps somebody has understand the Beta_Ravener code fully, this person can help me to understand, too.

Best Regards Sebastian

Post Reply