Created
November 24, 2013 06:00
-
-
Save willywg/7623877 to your computer and use it in GitHub Desktop.
Ping Web. Connect Ruby with Arduino by Serial Port
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 'serialport' | |
require 'net/ping' | |
website = 'http://turismoi.pek' | |
#this *will* be different for you | |
#You need to find out what port your arduino is on | |
#and also what the corresponding file is on /dev | |
#You can do this by looking at the bottom right of the Arduino | |
#environment which tells you what the path. | |
port_file = '/dev/tty.usbmodem1411' | |
#this must be same as the baud rate set on the Arduino | |
#with Serial.begin | |
baud_rate = 9600 | |
data_bits = 8 | |
stop_bits = 1 | |
parity = SerialPort::NONE | |
#create a SerialPort object using each of the bits of information | |
port = SerialPort.new(port_file, baud_rate, data_bits, stop_bits, parity) | |
wait_time = 4 | |
#for an infinite amount of time | |
loop do | |
#lets us know that we've checked the unread messages | |
puts "Ping #{website}" | |
pingomatic = Net::Ping::HTTP.new(website) | |
unless pingomatic.ping? | |
port.write "b" | |
end | |
#wait before we make another request to the Gmail servers | |
sleep wait_time | |
end |
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
void setup() { | |
Serial.begin(9600); | |
//Set pin 11 to be output pin (connect LED here) | |
pinMode(11, OUTPUT); | |
//set pin 11 to be high initially; LED is usually on, but, not blinking | |
digitalWrite(11, HIGH); | |
} | |
//this procedure is called if we need to blink the LED | |
void blink_led() { | |
//we can't just blink it once, because that wouldn't be noticeable | |
//so, we blink it three times | |
for(int i = 0; i<10; i++) { | |
digitalWrite(11, HIGH); | |
delay(100); | |
digitalWrite(11, LOW); | |
delay(100); | |
} | |
//reset the LED back to just on, not blinking | |
digitalWrite(11, HIGH); | |
} | |
//this function is called when we get a character | |
//over USB/Serial | |
void got_char(char x) { | |
//if we get b over Serial | |
if(x == 'b') { | |
//... blink the LED | |
blink_led(); | |
} | |
} | |
void loop() { | |
//check if there's any data available on serial | |
if(Serial.available() > 0) { | |
//if there is, we read it | |
byte byte_read = Serial.read(); | |
//and call "got_char" | |
got_char((char)byte_read); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment