Skip to content

Instantly share code, notes, and snippets.

@seeya
Created November 20, 2021 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seeya/ac7e9dcd6945a4b881e1a41290a559ca to your computer and use it in GitHub Desktop.
Save seeya/ac7e9dcd6945a4b881e1a41290a559ca to your computer and use it in GitHub Desktop.
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
@askdesigners
Copy link

Where do you install web_socket from? There's really no information about this library anywhere.

@seeya
Copy link
Author

seeya commented Oct 18, 2022

Where do you install web_socket from? There's really no information about this library anywhere.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment