Created
June 20, 2014 16:10
-
-
Save macbury/25df0a6ede4b8e97004d to your computer and use it in GitHub Desktop.
Arduino code for stupid guage
This file contains 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
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <Servo.h> | |
Servo guageServo; | |
byte mac[] = { | |
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; | |
IPAddress ip(192,168,1,177); | |
int port = 3000; | |
IPAddress server(192,168,1,106); | |
float MAX_ROTATION = 180.0f; | |
int SERVO_PIN = 9; | |
EthernetClient client; | |
String clientMsg = ""; | |
void setup() { | |
Serial.begin(9600); | |
Ethernet.begin(mac, ip); | |
Serial.println("Conecting."); | |
client.connect(server, port); | |
rotate(0); | |
delay(2000); | |
rotate(1); | |
delay(2000); | |
rotate(0); | |
} | |
float lastRotation = 0; | |
void rotate(float progress) { | |
int rotation = MAX_ROTATION - progress * MAX_ROTATION; | |
if (lastRotation == rotation) { | |
guageServo.attach(SERVO_PIN); | |
} else { | |
guageServo.attach(SERVO_PIN); | |
Serial.println("Rotation: "); | |
Serial.println(rotation); | |
Serial.println("Progress: "); | |
Serial.println(progress); | |
guageServo.write(rotation); | |
delay(1000); | |
guageServo.detach(); | |
lastRotation = rotation; | |
} | |
} | |
void loop() { | |
if (client.available()) { | |
char c = client.read(); | |
if (c == '\n') { | |
Serial.println("Message from Client:"+clientMsg); | |
char buf[clientMsg.length()]; | |
clientMsg.toCharArray(buf,clientMsg.length()); | |
float progress = atof(buf); | |
rotate(progress); | |
clientMsg = ""; | |
} else { | |
clientMsg += c; | |
} | |
} | |
// if the server's disconnected, stop the client: | |
if (!client.connected()) { | |
Serial.println(); | |
Serial.println("disconnecting."); | |
client.stop(); | |
delay(1000); | |
Serial.println("Conecting."); | |
client.connect(server, port); | |
} | |
} |
This file contains 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' | |
maximum_load = 8.0 | |
puts "Starting" | |
server = TCPServer.new(3000) | |
puts "Listening" | |
loop do | |
client = server.accept | |
puts "Accepted client" | |
while true | |
sleep 1 | |
begin | |
current_load = (`cat /proc/loadavg`.split(" ")[0].to_f * 100.0).round / 100.0 | |
total_load = (current_load / maximum_load).to_s | |
puts "[#{Time.now.to_s}] Sending: " + total_load | |
client.puts total_load + "\n" | |
rescue Errno::EPIPE, Errno::ECONNRESET | |
break | |
end | |
end | |
puts "Closing connection" | |
client.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment