-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll_server.rb
61 lines (48 loc) · 1.34 KB
/
poll_server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'serialport'
require 'pry'
require 'net/http'
require 'json'
class Arduino
# Params for serial port
PORT = "/dev/tty.usbmodemfd121"
DATA_BITS = 8
STOP_BITS = 1
BAUD_RATE = 9600
PARITY = SerialPort::NONE
def initialize
@sp = SerialPort.new(PORT, BAUD_RATE, DATA_BITS, STOP_BITS, PARITY)
end
def write_to_pin(number, high = true)
@sp.write(?P + ("A".ord + number - 1).chr + (high ? ?1 : ?0 ) + "\r\n")
end
def led_on
write_to_pin(13, true)
end
def led_off
write_to_pin(13, false)
end
def turn_servo(angle)
sign = angle < 0 ? ?- : ?+
turn_char = angle.abs
@sp.write(?S + turn_char.chr + sign + "\r\n")
end
end
arduino = Arduino.new
uri = URI.parse("http://ec2-50-19-60-252.compute-1.amazonaws.com/query.json")
current_state = {:led => false, :servo => 0}
while true do
response = Net::HTTP.get_response(uri)
result = JSON.parse(response.body)
if result
if result["led"] != current_state[:led]
current_state[:led] = result["led"]
puts "Change detected! LED is now #{current_state[:led] ? "on" : "off"}"
current_state[:led] ? arduino.led_on : arduino.led_off
end
if result["servo"] != current_state[:servo]
current_state[:servo] = result["servo"]
arduino.turn_servo(current_state[:servo])
end
end
sleep 2
end