Skip to content

Instantly share code, notes, and snippets.

@elct9620
Created December 7, 2016 17:08
Show Gist options
  • Save elct9620/c177da937afa4b9b95d07967e2be225c to your computer and use it in GitHub Desktop.
Save elct9620/c177da937afa4b9b95d07967e2be225c to your computer and use it in GitHub Desktop.
require 'socket'
def open(id)
fn = "/sys/class/gpio/gpio#{id}/direction"
File.write('/sys/class/gpio/export', id) unless File.exist?(fn)
File.write(fn, "out")
end
def pin(id, value)
fn = "/sys/class/gpio/gpio#{id}/value"
File.write(fn, value) if File.exist?(fn)
end
GPIO = 0
TOKEN = "DEVIDE_ID,DEVIDE_TOKEN"
PING = TOKEN + ",0"
open(GPIO)
socket = TCPSocket.new("SERVER", 443)
socket.puts(PING)
p socket.read(PING.size)
puts "START"
buffer = ""
cmd = ""
pointer = 0
data = false
loop do
ret = socket.read(1)
buffer << ret
pos = buffer.index(TOKEN)
if pos == 0
buffer = buffer[TOKEN.size..-1]
end
if buffer.count(",") == 3
p buffer
buffer = ""
ctrl = socket.read(1)
pin(GPIO, ctrl.to_i)
end
end
socket.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment