Created
December 7, 2016 17:08
-
-
Save elct9620/c177da937afa4b9b95d07967e2be225c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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