I have been working on this web server issue for a week and cant get ajax to work. the XML is ajax inputs. when i have the "if string contains" for ajax in the loop after "HTTP/1.1 200 OK" is freezes. if i put it at the end of the loop it works but never updates. Not sure if i have the set up proper.
and advice will help.
ARDUINO
#include "HX711.h"
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#define PIN_ETH_SPI 10
#define REQ_BUF_SZ 60
#define DOUT 3
#define CLK 2
float calibration_factor = 80; //-7050 worked for my 440lb max scale setup
float units;
float pounds;
float ounces;
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 50); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile; // the web page file on the SD card
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
boolean LED_state[4] = {0}; // stores the states of the LEDs
HX711 scale(DOUT, CLK);// LEDs
#define FILE_NAME_LEN 20
// HTTP request type
#define HTTP_invalid 0
#define HTTP_GET 1
#define HTTP_POST 2
#define FT_HTML 0
#define FT_ICON 1
#define FT_CSS 2
#define FT_JAVASCRIPT 3
#define FT_XML 4
#define FT_PNG 5
#define FT_GIF 6
#define FT_TEXT 7
#define FT_INVALID 8
void setup()
{
pinMode(PIN_ETH_SPI, OUTPUT);
digitalWrite(PIN_ETH_SPI, HIGH);
digitalWrite(7, HIGH);
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
Serial.begin(115200); // for debugging
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
// switches on pins 2, 3 and 5
pinMode(3, INPUT);
pinMode(2, OUTPUT);
scale.set_scale();
scale.tare(); //Reset the scale to 0
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
}
void loop() {
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (ServiceClient(&client)) {
break;
}
}
delay(1);
client.stop();
}
}
bool ServiceClient(EthernetClient *client)
{
static boolean currentLineIsBlank = true;
char cl_char;
File webFile;
char file_name[FILE_NAME_LEN + 1] = {0};
char http_req_type = 0;
char req_file_type = FT_INVALID;
const char *file_types[] = {"text/html", "image/x-icon", "text/css", "application/javascript", "text/xml", "image/png", "image/gif", "text/plain"};
static char req_line_1[40] = {0};
static unsigned char req_line_index = 0;
static bool got_line_1 = false;
if (client->available()) {
cl_char = client->read();
if ((req_line_index < 39) && (got_line_1 == false)) {
if ((cl_char != '\r') && (cl_char != '\n')) {
req_line_1[req_line_index] = cl_char;
req_line_index++;
}
else {
got_line_1 = true;
req_line_1[39] = 0;
}
}
if ((cl_char == '\n') && currentLineIsBlank) {
http_req_type = GetRequestedHttpResource(req_line_1, file_name, &req_file_type);
if (http_req_type == HTTP_GET) {
if (req_file_type < FT_INVALID) {
webFile = SD.open(file_name);
if (webFile) {
client->println(F("HTTP/1.1 200 OK"));
client->print(F("Content-Type: "));
client->println(file_types[req_file_type]);
client->println(F("Connection: close"));
client->println();
while(webFile.available()) {
int num_bytes_read;
char byte_buffer[64];
num_bytes_read = webFile.read(byte_buffer, 64);
client->write(byte_buffer, num_bytes_read);
}
webFile.close();
}
else {
}
}
else {
}
}
else if (http_req_type == HTTP_POST) {
}
else {
}
req_line_1[0] = 0;
req_line_index = 0;
got_line_1 = false;
return 1;
}
if (cl_char == '\n') {
currentLineIsBlank = true;
}
else if (cl_char != '\r') {
currentLineIsBlank = false;
}
}
return 0;
if (StrContains(HTTP_req, "ajax_inputs")) {
// send rest of HTTP header
client->println("Content-Type: text/xml");
client->println("Connection: keep-alive");
client->println();
XML_response(client);
}
}
char GetRequestedHttpResource(char *req_line, char *file_name, char *file_type)
{
char request_type = HTTP_invalid;
char *str_token;
*file_type = FT_INVALID;
str_token = strtok(req_line, " ");
if (strcmp(str_token, "GET") == 0) {
request_type = HTTP_GET;
str_token = strtok(NULL, " ");
if (strcmp(str_token, "/") == 0) {
strcpy(file_name, "index.htm");
*file_type = FT_HTML;
}
else if (strlen(str_token) <= FILE_NAME_LEN) {
strcpy(file_name, str_token);
str_token = strtok(str_token, ".");
str_token = strtok(NULL, ".");
if (strcmp(str_token, "htm") == 0) {*file_type = 0;}
else if (strcmp(str_token, "ico") == 0) {*file_type = 1;}
else if (strcmp(str_token, "css") == 0) {*file_type = 2;}
else if (strcmp(str_token, "js") == 0) {*file_type = 3;}
else if (strcmp(str_token, "xml") == 0) {*file_type = 4;}
else if (strcmp(str_token, "png") == 0) {*file_type = 5;}
else if (strcmp(str_token, "gif") == 0) {*file_type = 6;}
else if (strcmp(str_token, "txt") == 0) {*file_type = 7;}
else {*file_type = 8;}
}
else {
}
}
else if (strcmp(str_token, "POST") == 0) {
request_type = HTTP_POST;
}
return request_type;
}
void XML_response(EthernetClient cl)
{
int dogwater;
int pin_val2;
int pin_val3;
cl.print("<?xml version = \"1.0\" ?>");
cl.print("");
cl.print("");
if (digitalRead(7)) {
cl.print("ON");
}
else {
cl.print("OFF");
}
cl.print("");
pin_val2 = dogwater;
dogwater = random(2,99);
Serial.print(dogwater);
cl.print("");
cl.print(pin_val2);
cl.print("");
cl.print("");
pin_val3 = pounds;
cl.print("");
cl.print(pin_val3);
cl.print("");
cl.print("");
}
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str = 0;
-
}*
}
char StrContains(char *str, char *sfind)
{ -
char found = 0;*
-
char index = 0;*
-
char len;*
-
len = strlen(str);*
-
if (strlen(sfind) > len) {*
-
return 0;*
-
}*
-
while (index < len) {*
-
if (str[index] == sfind[found]) {*
-
found++;*
-
if (strlen(sfind) == found) {*
-
return 1;*
-
}*
-
}*
-
else {*
-
found = 0;*
-
}*
-
index++;*
-
}*
-
return 0;*
}