Thanks to everyone that came out last night – we’re all looking forward to these types of meetings in the future. Posting this for anyone that saw the simple HTTP / Serial port demo.
First, the Arduino code (adapted from an example that I can’t remember where at the moment.) I setup 4 LEDs (blue, red, green, yellow) off pins 3-6 and set the pin mode to OUTPUT. I also setup the serial port for a 57600 baud connection.
void setup() {
// initialize serial communication:
Serial.begin(57600);
// initialize the LED pins:
for (int thisPin = 3; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// read the sensor:
if (Serial.available() > 0) {
int inByte = Serial.read();
// do something different depending on the character received.
switch (inByte) {
case 'y':
digitalWrite(3, HIGH);
break;
case 'g':
digitalWrite(4, HIGH);
break;
case 'r':
digitalWrite(5, HIGH);
break;
case 'b':
digitalWrite(6, HIGH);
break;
case 'o':
// turn all the LEDs off:
for (int thisPin = 3; thisPin < 7; thisPin++) {
digitalWrite(thisPin, LOW);
}
break;
default:
break;
}
}
}
The main part of this code is the switch statement that reads a single byte off the serial connection. If the character is a 'b', 'r', 'g', or 'y', the corresponding LED turns on. An 'o' turns off all the LEDs and any other character is ignored. Setting up this code and running it with the serial monitor will work exactly as expected. You can also type multiple entries at once and turn on multiple LEDs at once.
The 2nd part of the code is a Python script using PySerial and BaseHTTPServer.
import BaseHTTPServer
import cgi, random, sys, socket, serial
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
ser.write(self.path)
try:
# redirect stdout to client
stdout = sys.stdout
sys.stdout = self.wfile
self.makepage()
finally:
sys.stdout = stdout # restore
def makepage(self):
print "<html>"
print "<body>"
print "<a href='/r'>Turn on Red</a><br>"
print "<a href='/b'>Turn on Blue</a><br>"
print "<a href='/g'>Turn on Green</a><br>"
print "<a href='/y'>Turn on Yellow</a><br>"
print "<a href='/o'>Turn off</a><br>"
print "</body>"
print "</html>"
PORT = 8000
ser = serial.Serial('/dev/cu.usbserial-A4001sM1', 57600)
httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
ser.close()
The do_GET method handles most of the work - when a GET request is received by the server, it takes the path element (basically http://<address>/<path>) and feeds it to the serial port where the arduino is connected. For example, it will take http://localhost/rgb and send "/rgb" to the serial port, in effect turning on the red, green, and blue LEDs. The makepage method actually generates the HTML to make it easy for the requests. The remainder of the code simply sets up the serial port and the web server handling.
I used python due to simplicity, but this can be done using any application capable of interacting with a given serial port. Let me know if you have questions!