Skip to content

Instantly share code, notes, and snippets.

@ipa
Created December 7, 2011 16:53
Show Gist options
  • Save ipa/1443563 to your computer and use it in GitHub Desktop.
Save ipa/1443563 to your computer and use it in GitHub Desktop.
telnet access to a coffeemachine using arduino
#include <Servo.h>
#include <SPI.h>
#include <Ethernet.h>
// enter mac, ip, gateway and subnet mask
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2, 177);
IPAddress gateway(192,168,2, 1);
IPAddress subnet(255, 255, 255, 0);
// telnet server on port 23
EthernetServer server(23);
// whenever a message is received
boolean gotAMessage = false;
// the request
String request = "";
void setup()
{
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
// open the serial port
Serial.begin(9600);
}
void loop() {
// wait for a new client
EthernetClient client = server.available();
boolean currentLineIsBlank = true;
if (client) {
// when the clients opens the telnet connection ask him for coffee
if (!gotAMessage) {
Serial.println("New coffee client");
client.println("Do you want some coffe?");
gotAMessage = true;
}
char c = client.read();
Serial.println(request);
// handle the request
handleRequest(request, client);
if (c == '\n') {
// a new line was started
Serial.println("newline");
currentLineIsBlank = true;
request = "";
}
else if (c != '\r') {
// received a character on the line
Serial.println(c);
currentLineIsBlank = false;
request += c;
}
delay(1);
}
}
void handleRequest(String command, EthernetClient c)
{
if(request.endsWith("exit"))
{
c.println("bye");
c.stop();
request = "";
}
if(request.endsWith("yes"))
{
c.println("making some coffee");
// servo to 160° and back to 10°
// this will push the button ond the coffeemachine
Servo servo;
servo.attach(8);
servo.write(160);
delay(1000);
servo.write(10);
// wait some seconds
for(int t = 0; t < 12; t++)
{
c.print("#");
delay(1000);
}
c.println("");
c.println("coffe is finished");
delay(100);
c.stop();
request = "";
}
if(request.endsWith("no"))
{
c.println("bye");
c.stop();
request = "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment