My Ethernet server keeps crashing after pulling the RGB values from the string. It remains connected if I'm just toggling a pin, but whenever the string has the rgb values in it, the RGB values are pulled, the LED changes but then the server crashes and won't take any more requests. Do I need to restart the server after it plucks the RGB values for another read? If so, how would I go about doing this?
Thanks, James :)
Example HTTP request that causes the crash: /?LED=B233+G011+R133&t=T HTTP/1.1
#include <SPI.h>
#include <Ethernet.h>
#include <string.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {192, 168, 0, 102 };
byte gateway[] = {192, 168, 0, 1 };
byte subnet[] = {255, 255, 255, 0 };
EthernetServer server(84);
;
char* redstring={0};
String blue1;
String green1;
String red1;
String testString = "0123456789";
int blue;
int green;
int red;
int been=0;
int b=0;
int g=0;
int r=0;
//////////////////////
void setup(){
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//enable serial data print
Serial.begin(9600);
Serial.println("SERVER RUNNING HTTP / GET");
}
void loop(){
////////////////////////////////////////
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (sizeof(redstring) < 100) {
//store characters to string
redstring += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
///////////////////////////////////////////
Serial.println(redstring);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<link rel=\"shortcut icon\" href=\"data:image/x-icon;,\" type=\"image/x-icon\">");
client.println("<HEAD>");
client.println("<TITLE>Arduino GET</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>HTML GET</H1>");
//change ip back if not local
client.println("<FORM ACTION=\"http://192.168.0.102:84\" method=get >");
client.println("JQUERY: <INPUT TYPE=TEXT NAME=\"LED\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\"><BR>");
client.println("<INPUT TYPE=SUBMIT NAME=\"t\" VALUE=\"T\">");
client.println("</FORM>");
client.println("<BR>");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
/////////////////////
//readString = Character.toUpperCase(readString);
for (int i = 1; i < sizeof(redstring); i++) {
if (strchr(redstring, 'B') && && redstring[i+1] > -1) {
blue=redstring[i+1, i+4];
analogWrite(11, blue);
delay(1000);
}
if (strchr(redstring, 'G') && redstring[i+1] > -1) {
green=redstring[i+1, i+4];
analogWrite(9, green);
delay(1000);
}
if (strchr(redstring, 'R')&& redstring[i+1] > -1) {
red=redstring[i+1, i+4];
analogWrite(7, red);
delay(1000);
}
}
//clearing string for next read
redstring="";
(redstring, "0", sizeof(redstring));
}
}
}
}
}
String
as it is a source of this kind of crashes, due to memory heap fragmentation.