Skip to content

Instantly share code, notes, and snippets.

@af12066
Created July 17, 2016 08:18
Show Gist options
  • Save af12066/19b795cc45acd16b59bf68864a4b7b5f to your computer and use it in GitHub Desktop.
Save af12066/19b795cc45acd16b59bf68864a4b7b5f to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <Ethernet.h>
int val[10];
int i;
const int iPin = A0;
const int oPin = 13;
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x00, 0x00};
byte ip[] = {172, 28, 192, 100};
byte gateway[] = {172, 28, 192, 1};
byte subnet[] = {255, 255, 255, 0};
EthernetServer server(80);;
void setup() {
for(i=0; i<10; i++){
val[i] = 0;
}
Serial.begin(9600);
pinMode(oPin, OUTPUT);
while(!Serial){
;
}
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n' && currentLineIsBlank) {
val[0] = analogRead(iPin);
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
// HTML code
client.println("<!DOCTYPE HTML>");
client.println("<html lang='ja'>");
client.print("<head><meta charset='utf-8'><title>hogehoge</title>");
client.println("<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css' integrity='sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7' crossorigin='anonymous'>");
client.println("<style>body{font-family:'Avenir Next',Helvetica,'Hiragino Sans',Arial,sans-serif;} .axis path,.axis line{fill:none;stroke:black;} .tick text{font-size:14px;} .line{fill:none;stroke:#1abc9c;stroke-width:2px;}</style>");
client.println("</head><body><script src='http://d3js.org/d3.v3.js'></script>");
// D3
client.println("<script>");
client.println("d3.select('body').style('background-color','#ecf0f1');");
client.println("var margin={top:20,right:100,bottom:30,left:100},width=700-margin.left-margin.right,height=400-margin.top-margin.bottom;");
client.print("var dataset=[{x:0,y:");
client.print(val[9]);
client.print("},{x:1,y:");
client.print(val[8]);
client.print("},{x:2,y:");
client.print(val[7]);
client.print("},{x:3,y:");
client.print(val[6]);
client.print("},{x:4,y:");
client.print(val[5]);
client.print("},{x:5,y:");
client.print(val[4]);
client.print("},{x:6,y:");
client.print(val[3]);
client.print("},{x:7,y:");
client.print(val[2]);
client.print("},{x:8,y:");
client.print(val[1]);
client.print("},{x:9,y:");
client.print(val[0]);
client.println("},];");
client.println("var xScale=d3.scale.linear().domain([0,d3.max(dataset, function(d){return d.x;})]).range([0,width]);");
client.println("var yScale=d3.scale.linear().domain([0,800]).range([height, 0]);");
client.println("var xAxis=d3.svg.axis().scale(xScale).orient('bottom');");
client.println("var yAxis=d3.svg.axis().scale(yScale).orient('left');");
client.println("var line=d3.svg.line().x(function(d){return xScale(d.x);}).y(function(d){return yScale(d.y);});");
client.println("var svg=d3.select('body').append('svg').attr('width',width+margin.left+margin.right).attr('height',height+margin.top+margin.bottom).append('g').attr('transform','translate('+margin.left+','+margin.top+')');");
client.println("svg.append('g').attr('class','x axis').attr('transform','translate(0,'+height+')').call(xAxis)");
client.println("svg.append('g').attr('class','y axis').call(yAxis)");
client.println("svg.append('path').datum(dataset).attr('class','line').attr('d',line(dataset));");
client.println("</script>");
// output the value of each analog input pin
int sum = 0;
for(i=0; i<10; i++){
sum += val[i];
}
client.print("<div class='text-center'><p>Average: ");
client.print(sum/10);
client.println("</p></div>");
client.println("<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js' integrity='sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS' crossorigin='anonymous'></script>");
client.println("</body></html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
for(i=9; i>0; i--){
val[i] = val[i-1];
}
Serial.println("client disconnected");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment