Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Landrash/79138bcf05a9266021bbc6e4eac22ce8 to your computer and use it in GitHub Desktop.
Save Landrash/79138bcf05a9266021bbc6e4eac22ce8 to your computer and use it in GitHub Desktop.
Combined IR Roomba controll with ethernet webserver code from W.A. Smith, http://startingelectronics.com Extended script to send command 2 times, since the first command is most of the times not received by the Roomba. The short delay overwrites the first command if this is received, resulting in a total of 1 command excution of the Roomba, as d…
/*--------------------------------------------------------------
Program: eth_websrv_LED
Description: Arduino web server that serves up a web page
allowing the user to control an LED
Hardware: - Arduino Uno and official Arduino Ethernet
shield. Should work with other Arduinos and
compatible Ethernet shields.
- LED and resistor in series connected between
Arduino pin 2 and GND
Software: Developed using Arduino 1.0.3 software
Should be compatible with Arduino 1.0 +
References: - WebServer example by David A. Mellis and
modified by Tom Igoe
- Ethernet library documentation:
http://arduino.cc/en/Reference/Ethernet
Date: 11 January 2013
Author: W.A. Smith, http://startingelectronics.com
--------------------------------------------------------------*/
#include <SPI.h>
#include <Ethernet.h>
#include <IRremote.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 134, 177); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
String HTTP_req; // stores the HTTP request
boolean LED_status = 0; // state of LED, off by default
//int ledPin = 13;
/*
Send infrared commands from the Arduino to the iRobot Roomba
by probono
2013-03-17 Initial release
Copyright (c) 2013 by probono
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IR Remote Control
129 Left
130 Forward
131 Right
132 Spot //werkt
133 Max
134 Small
135 Medium
136 Large / Clean
137 Stop
138 Power
139 Arc Left
140 Arc Right
141 Stop
Scheduling Remote
142 Download
143 Seek Dock
Roomba Discovery Driveon Charger
240 Reserved
248 Red Buoy
244 Green Buoy
242 Force Field
252 Red Buoy and Green Buoy
250 Red Buoy and Force Field
246 Green Buoy and Force Field
254 Red Buoy, Green Buoy and Force
Roomba 500 Drive-on Charger
160 Reserved
161 Force Field
164 Green Buoy
165 Green Buoy and Force Field
168 Red Buoy
169 Red Buoy and Force Field
172 Red Buoy and Green Buoy
173 Red Buoy, Green Buoy and Force Field
Roomba 500 Virtual Wall
162 Virtual Wall
Roomba 500 Virtual Wall Lighthouse ###### (FIXME: not yet supported here)
0LLLL0BB
LLLL = Virtual Wall Lighthouse ID
(assigned automatically by Roomba
560 and 570 robot130s)
1-10: Valid ID
11: Unbound
12-15: Reserved
BB = Which Beam
00 = Fence
01 = Force Field
10 = Green Buoy
11 = Red Buoy
*/
IRsend irsend;
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for diagnostics
// pinMode(ledPin, OUTPUT); // LED on pin 2
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
HTTP_req += c; // save the HTTP request 1 char at a time
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino Roomba Remote Controll</title>");
client.println("</head>");
client.println("<body>");
client.println("<h1>LED</h1>");
client.println("<p>Let Robby clean the house!</p>");
client.println("<form method=\"get\">");
client.println("<button name=\"clean\" value=\"2\" onclick=\"submit();\">clean</button>");
ProcessCheckbox(client);
client.println("</form>");
client.println("</body>");
client.println("</html>");
Serial.print(HTTP_req);
HTTP_req = ""; // finished with request, empty string
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
// switch LED and send back HTML for LED checkbox
void ProcessCheckbox(EthernetClient cl)
{
if (HTTP_req.indexOf("clean=2") > -1) { // see if checkbox was clicked
// the checkbox was clicked, toggle the LED
if (LED_status) {
LED_status = 0;
}
else {
LED_status = 1;
// start Roomba
roomba_send(136);
delay(500);
roomba_send(136);
}
}
if (LED_status) { // switch LED on
// digitalWrite(ledPin, HIGH);
// checkbox is checked
cl.println("<p>Robby is cleaning</p>");
}else{
cl.println("<p>Robby is not cleaning</p>");
}
}
void roomba_send(int code)
{
Serial.print("Sending Roomba code ");
Serial.print(code);
int length = 8;
unsigned int raw[length*2];
unsigned int one_pulse = 3000;
unsigned int one_break = 1000;
unsigned int zero_pulse = one_break;
unsigned int zero_break = one_pulse;
int arrayposition = 0;
// Serial.println("");
for (int counter = length-1; counter >= 0; --counter) {
if(code & (1<<counter)) {
// Serial.print("1");
raw[arrayposition] = one_pulse;
raw[arrayposition+1] = one_break;
}
else {
// Serial.print("0");
raw[arrayposition] = zero_pulse;
raw[arrayposition+1] = zero_break;
}
arrayposition = arrayposition + 2;
}
for (int i = 0; i < 3; i++) {
irsend.sendRaw(raw, 15, 38);
delay(50);
}
Serial.println("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment