Using Ajax with Arduino Ethernet Shield
hello, beginner here, i'm working on oil valve remote control uses optical sensor detect number drops on given time, i'm using ethernet shield send number of degrees turn servo. want use ajax sensor info , show on page, purpose of simulation, i'm sending degrees input arduino , return it.
i still don't have proper idea on how make work, welcome, thank time.
code: [select]
#include <servo.h>
#include <spi.h>
#include <ethernet.h>
#include <wire.h>
#include <i2c_anything.h>
string newstring;
string readstring;
servo myservo;
byte mac[] = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed };
byte ip[] = { 169, 254, 152, 120};
ethernetserver server(80);
void setup(){
wire.begin();
pinmode(5, output);
ethernet.begin(mac, ip);
server.begin();
serial.begin(9600);
serial.println(ethernet.localip());
myservo.attach(9);
}
void loop(){
ethernetclient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readstring.length() < 100) {
readstring += c;
}
if (c == '\n') {
serial.println(readstring);
client.println("http/1.1 200 ok");
client.println("content-type: text/html");
client.println();
client.println("<html>");
client.println("<head>");
client.println("<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'>");
client.print("<script src='https://code.jquery.com/jquery-3.1.0.min.js' integrity='sha256-ccuebr6csya4/9szppfrx3s49m9vuu5bgtijj06wt/s=' crossorigin='anonymous'></script>");
client.println("<title>arduino test page</title>");
client.println("</head>");
client.println("<body>");
client.print("<div class='container'>");
client.println("<h1>control de servo</h1>");
client.print(" <div class='form-group'>");
client.println("<form action='/' method=get >");
client.println("ingrese gotas por minuto: <input type=text name='led' id='gpm' value='' size='25' maxlength='50'><br>");
client.println("<button type='button' id='submit' class='btn btn-default'>ingresar</button>");
client.println("</form>");
client.print(" </div>");
client.print("<script>");
client.print(" $('#submit').click(function(){");
client.print(" var gpm = $('#gpm').val();");
client.print(" $.get('/',{gpm: gpm, x:1}, function(){})");
client.print(" });");
client.print("</script>");
client.println("<br>");
client.print(" </div>");
client.println("</body>");
client.println("</html>");
delay(1);
client.stop();
if (readstring.length() >0) {
int pos1 = readstring.indexof('=');
int pos2 = readstring.indexof('&');
serial.println(pos1);
serial.println(pos2);
if (pos1>0 && pos2>0) {//si se envia dato de giro
newstring = readstring.substring(pos1+1, pos2);
serial.print("newstring is: ");
serial.println(newstring);
float val = newstring.tofloat();
serial.print("the value sent is: ");
serial.println(val);
myservo.write((val*3));
/*wire.begintransmission(8);
float y = (60/val);
float z = (y*1000);
float = (z-10);
i2c_writeanything (a);
serial.println(a);
wire.endtransmission();
delay(500);*/
readstring="";
newstring="";
}
}
}
}
}
}
}
i still don't have proper idea on how make work, welcome, thank time.
Arduino Forum > Using Arduino > Programming Questions > Using Ajax with Arduino Ethernet Shield
arduino
Comments
Post a Comment