Skip to content

Instantly share code, notes, and snippets.

@sergiobuj
Last active June 4, 2018 01:02
Show Gist options
  • Save sergiobuj/5479792 to your computer and use it in GitHub Desktop.
Save sergiobuj/5479792 to your computer and use it in GitHub Desktop.
Arduino + Webduino + LED
Requires:
Makefile include: https://github.com/mjoldfield/Arduino-Makefile
Webduino: https://github.com/sirleech/Webduino
<!DOCTYPE HTML>
<head>
<link href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css' rel=stylesheet />
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'></script>
<link rel='stylesheet' type='text/css' href='http://show_docs.cloudfoundry.com/css/arduinito.css'>
<!--<link rel='stylesheet' type='text/css' href='arduinito.css'>-->
<script>
function state(obj) { $.post('/lamp_action', { state: this.id } ); }
$(document).ready(function(){ $('.button').click(state) });
</script>
</head>
<html>
<button class='button' id='on'>
<font>ON</font>
</button>
<button class='button' id='off'>
<font>OFF</font>
</button>
</html>
ARDUINO_SKETCHBOOK = $(HOME)/Documents/Arduino/SKETCHBOOK
ARDUINO_DIR = /Applications/Arduino.app/Contents/Resources/Java
ARDMK_DIR = $(HOME)/Documents/Arduino/Arduino-Makefile
BOARD_TAG = uno
ARDUINO_PORT = /dev/cu.usb*
ARDUINO_LIBS = Ethernet Ethernet/utility SPI WebServer
include $(ARDMK_DIR)/arduino-mk/Arduino.mk
#include "SPI.h"
#include "Ethernet.h"
#include "WebServer.h"
P(ControlPage) = "<!DOCTYPE HTML>"
"<head>"
"<link href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css' rel=stylesheet />"
"<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>"
"<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'></script>"
"<link rel='stylesheet' type='text/css' href='http://show_docs.cloudfoundry.com/css/arduinito.css'>"
"<script>"
"function state(obj) { $.post('/lamp_action', { state: this.id } ); }"
"$(document).ready(function(){ $('.button').click(state) });"
"</script>"
"</head>"
"<html>"
"<button class='button' id='on'>"
"<font>ON</font>"
"</button>"
"<button class='button' id='off'>"
"<font>OFF</font>"
"</button>"
"</html>";
P(Error) = "<!DOCTYPE HTML>"
"<head></head>"
"<body><h1>Error Page</h1></body>"
"</html>";
static uint8_t mac[6] = { 0x02, 0xAA, 0xBB, 0xCC, 0x00, 0x22 };
static uint8_t ip[4] = { 192, 168, 0, 210 };
#define PREFIX ""
WebServer webserver(PREFIX, 80);
#define LAMP_PIN 8
#define NAMELEN 32
#define VALUELEN 32
void stateCmd(WebServer &s, WebServer::ConnectionType t, char *url_tail, bool tail_complete){
URLPARAM_RESULT rc;
char name[NAMELEN];
char value[VALUELEN];
if (t == WebServer::POST){
while (s.readPOSTparam(name, NAMELEN, value, VALUELEN)){
if(strcmp(name, "state") == 0){
if (strcmp(value, "on") == 0){
digitalWrite(LAMP_PIN, HIGH);
}else{
digitalWrite(LAMP_PIN, LOW);
}
}
}
s.httpSeeOther(PREFIX);
return;
}
s.httpSuccess();
if (t == WebServer::GET)
s.printP(ControlPage);
}
void failCmd(WebServer &s, WebServer::ConnectionType t, char *url_tail, bool tail_complete)
{
s.httpFail();
if (t == WebServer::HEAD)
return;
s.printP(Error);
}
void setup()
{
pinMode(LAMP_PIN, OUTPUT);
Ethernet.begin(mac, ip);
webserver.setDefaultCommand(&stateCmd);
webserver.setFailureCommand(&failCmd);
webserver.addCommand("lamp_action", &stateCmd);
webserver.begin();
}
void loop()
{
char buff[64];
int len = 64;
webserver.processConnection(buff, &len);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment