-
-
Save seeya/ac7e9dcd6945a4b881e1a41290a559ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net | |
import http | |
import web_socket | |
import bytes | |
import gpio | |
IS_CONNECTED := false | |
SWITCH ::= gpio.Pin 16 --output | |
HOST ::= "your-endpoint" | |
PORT ::= 80 | |
main: | |
print "Device Online" | |
s := connect_server | |
while true: | |
if IS_CONNECTED == false: | |
s = connect_server | |
if s != null: | |
IS_CONNECTED = true | |
else: | |
IS_CONNECTED = false | |
sleep --ms=5000 | |
else if IS_CONNECTED == true: | |
try: | |
print "Waiting data" | |
dc := catch: | |
payload := s.read | |
if payload == "ON": | |
SWITCH.set 1 | |
else if payload == "OFF": | |
SWITCH.set 0 | |
print "$payload" | |
if dc: | |
IS_CONNECTED = false | |
SWITCH.set 0 | |
finally: | |
connect_server: | |
network_interface := net.open | |
socket := network_interface.tcp_connect HOST PORT | |
http_connection := http.Connection socket HOST | |
err := catch: | |
request := http_connection.new_request "GET" "/ws" | |
client := web_socket.WebSocketClient http_connection request | |
return client | |
if err: | |
print "Server down, retry in 5s" | |
return null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's in their standard library. The documentation isn't the best so you'll have to try things out.
https://libs.toit.io/web_socket/library-summary