First, the big picture... I want to use AJAX with the web browser as client and an arduino running a webduino webserver as the server. I don't want to waste valuable memory generating web pages out of the arduino. I'd like to send either get or post variables to the arduino which the arduino can interpret as commands, carry out those commands, gather some sensor data, form that data into a response and send it back to the browser. The response could be as simple as "batteryLevel:13.2|Temperature:72|LightState:ON"
Imagine having access to your in-home robot with an IP camera. It would be a simple thing to embed the camera stream into the same web page.
If you've looked into AJAX at all, you've seen some javascript like this...
// AJAX Functions
function createRequestObject()
{
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}
var http = createRequestObject();
function sendReq(theCommands)
{
var url = "192.168.3.111/arduino.html?" + theCommands;
http.open('get', url);
http.onreadystatechange = getResponse;
http.send();
}
function getResponse()
{
if(http.readyState == 4)
{
var response = http.responseText;
...
In the above example, the variable getCommands becomes the query string. It could be something like this... light=on&move=forward. the complete url within the AJAX functions would be "192.168.3.111/arduino.html?light=on&move=forward".
Here's the Problem... If I put the above url into the address bar of my browser, the arduino responds as expected. If I have the AJAX functions attempt to contact the arduino with the same url, the AJAX response is essentially "object not found" (that page doesn't exist). I don't know if the webduino server isn't responding correctly to the AJAX request or not.
I really like the way webduino handles post & get variables and would love to stick with it as my ultimate solution. I've been trying to figure this out for a week now and I just can't google any more.
My Arduino sketch as it stands right now...
#define WEBDUINO_SERIAL_DEBUGGING 1
#include "Ethernet.h"
#include "WebServer.h"
#include "SPI.h"
#include "avr/pgmspace.h"
#include "stdlib.h";
static uint8_t mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x07, 0xBC };
static uint8_t ip[] = { 192, 168, 3, 111 };
#define PREFIX ""
#define NAMELEN 32
#define VALUELEN 32
WebServer webserver(PREFIX, 80);
// Define Pin Names Here
// #define BUZZER_PIN 3
// Game Pad State Variables
int LX = 0; int LY = 0;
int RX = 0; int RY = 0;
int LT = 0; int RT = 0;
bool AB = false; bool BB = false; bool XB = false; bool YB = false;
bool RS = false; bool LS = false;
bool DU = false; bool DD = false; bool DL = false; bool DR = false;
bool ST = false; bool BK = false;
bool LJ = false; bool RJ = false;
bool BG = false;
void changeState(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
Serial.print("made it into changeState Function\r\n\r\n");
//server.httpSuccess();
server.print("HTTP/1.1 200 OK\r\n");
server.print("Content-Type: text/html");
Serial.print("httpSuccess Header Sent\r\n\r\n");
URLPARAM_RESULT rc;
char name[NAMELEN];
int name_len;
char value[VALUELEN];
int value_len;
if (strlen(url_tail))
{
while (strlen(url_tail))
{
rc = server.nextURLparam(&url_tail, name, NAMELEN, value, VALUELEN);
Serial.print(name);
Serial.print(":");
Serial.print(value);
Serial.print("\r\n");
if (strcmp(name, "LX") == 0){LX = atoi(value);}
if (strcmp(name, "LY") == 0){LY = atoi(value);}
if (strcmp(name, "RX") == 0){RX = atoi(value);}
if (strcmp(name, "RY") == 0){RY = atoi(value);}
if (strcmp(name, "LT") == 0){LT = atoi(value);}
if (strcmp(name, "RT") == 0){RT = atoi(value);}
if (strcmp(name, "AB") == 0){AB = (value == "1") ? true : false;}
if (strcmp(name, "BB") == 0){BB = (value == "1") ? true : false;}
if (strcmp(name, "XB") == 0){XB = (value == "1") ? true : false;}
if (strcmp(name, "YB") == 0){YB = (value == "1") ? true : false;}
if (strcmp(name, "LS") == 0){LS = (value == "1") ? true : false;}
if (strcmp(name, "RS") == 0){RS = (value == "1") ? true : false;}
if (strcmp(name, "DU") == 0){DU = (value == "1") ? true : false;}
if (strcmp(name, "DD") == 0){DD = (value == "1") ? true : false;}
if (strcmp(name, "DL") == 0){DL = (value == "1") ? true : false;}
if (strcmp(name, "DR") == 0){DR = (value == "1") ? true : false;}
if (strcmp(name, "ST") == 0){ST = (value == "1") ? true : false;}
if (strcmp(name, "BK") == 0){BK = (value == "1") ? true : false;}
if (strcmp(name, "LJ") == 0){LJ = (value == "1") ? true : false;}
if (strcmp(name, "RJ") == 0){RJ = (value == "1") ? true : false;}
if (strcmp(name, "BG") == 0){BG = (value == "1") ? true : false;}
}
}
// Apply GamePad State Variables Here...
// if (AB) ? digitalWrite(BUZZER_PIN, HIGH) : digitalWrite(BUZZER_PIN, LOW);
// Gather Telemetrics and Publish them Here...
// Telemetric Variables
int LV = 0;
int RV = 0;
float PS = 14.56;
String _LT = String(LT); // Left Vibrate 0 to 255
String _RT = String(RT); // Right Vibrate 0 to 255
char buffer[10];
String _PS = dtostrf(PS, 8, 2, buffer); // Power Supply Voltage Float to fixed 2
String telemetrics = "LV:" + _LT + "|RV:" + _RT + "|PS:" + _PS;
//server.print("HTTP/1.1 200 OK");
//server.print("Content-Type: text/html");
server.print(telemetrics);
Serial.print(telemetrics);
Serial.print("\r\n\r\n");
}
void setup()
{
Serial.begin(9600);
Serial.print("Debugging\r\n\r\n");
// Set Pins Here...
// pinMode(BUZZER_PIN, OUTPUT);
Ethernet.begin(mac, ip);
webserver.setDefaultCommand(&changeState);
webserver.addCommand("arduino.html", &changeState);
webserver.begin();
}
void loop()
{
char buff[256]; // May Need to Bump these values up to accomodate full query string
int len = 256;
webserver.processConnection(buff, &len);
}
You'll notice lines commented out and several Serial.prints. I've tried everything I can think of.
Thanks for listening,
Don