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:
I get messages/requests from my browser and with the line "self.connection.write(...)" I am able to send an answer to my browser.
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 if cmd == "Hello": self.connection.write(cmd + " World") print("Hello World") except ClientClosedError: self.connection.close()
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