i cannot figure out why the serial connection quits and server hangs after writing to a pin based on a specific price.
#include <SPI.h>
#include <Ethernet.h>
#include <String.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,4 };
String readString;
boolean RELAY1ON = true;
boolean RELAY2ON = true;
boolean RELAY3ON = true;
boolean RELAY4ON = true;
boolean RELAY5ON = true;
boolean RELAY6ON = true;
String price;
float fString;
float tmp = -1.0;
float tmp2 = -1.0;
String price1="";
int counter;
Server server(80);
float price_signal()
{
if(Serial.available())
{
price="";
while (Serial.available()) {
delay(10); //small delay to allow input buffer to fill
char c = Serial.read(); //gets one byte from serial buffer
price += c;
} //makes the string readString
}
//return price;
char* p=&price[0];
fString = atof(p);
return fString;
}
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
Serial.begin(9600);
}
int val=1;
void loop()
{
delay(10);
// listen for incoming clients
Client client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
delay(10); // i've added and tried taking this delay out
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (readString.length() < 30){
readString.concat(c);
}
if (c == '\n' && currentLineIsBlank) {
fString=price_signal();
if(fString != tmp){
tmp = fString;
counter++;
}
if (readString.indexOf("R1=1")>-1){
digitalWrite(2,LOW);
RELAY1ON = true;
}
else if (readString.indexOf("R1=2")>-1){
RELAY1ON = false;
}
else if (readString.indexOf("R2=1")>-1){
digitalWrite(3,LOW);
RELAY2ON = true;
}
else if (readString.indexOf("R2=2")>-1){
RELAY2ON = false;
}
else if (readString.indexOf("R3=1")>-1){
digitalWrite(4,LOW);
RELAY3ON = true;
}
else if (readString.indexOf("R3=2")>-1){
RELAY3ON = false;
}
else if (readString.indexOf("R4=1")>-1){
digitalWrite(5,LOW);
RELAY4ON = true;
}
else if (readString.indexOf("R4=2")>-1){
RELAY4ON = false;
}
else if (readString.indexOf("R5=1")>-1){
digitalWrite(6,LOW);
RELAY5ON = true;
}
else if (readString.indexOf("R5=2")>-1){
RELAY5ON = false;
}
else if (readString.indexOf("R6=1")>-1){
digitalWrite(7,LOW);
RELAY6ON = true;
}
else if (readString.indexOf("R6=2")>-1){
RELAY6ON = false;
}
if(RELAY1ON && fString >=.18){
digitalWrite(2,HIGH);
}
if(RELAY2ON && fString >=.22){
digitalWrite(3,HIGH);
}
if(RELAY3ON && fString >=.10){
digitalWrite(4,HIGH);
}
if(RELAY4ON && fString >=.15){
digitalWrite(5,HIGH);
}
if(RELAY5ON && fString >=.13){
digitalWrite(5,HIGH);
}
if(RELAY6ON && fString >=.20){
digitalWrite(5,HIGH);
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
//meta-refresh every 15 seconds
client.print("<head>");
client.print("<meta http-equiv=\"refresh\" content=\"15\">");
client.print("</head>");
client.println("<body bgcolor=FFFFCC>");
client.println("<center>");
client.println("<h3> Current price: $/KWH ");
client.println(fString);
client.println("</h3>");
client.println(counter);
client.println("<table border=1>");
client.println("<tr align=center>");
client.println("<td>");
client.println("control 1
");
client.println("<form method=get name=RELAY><input type=radio name=R1 value=1>On
<input type=radio name=R1 value=2>Off
<input type=submit value=submit></form>");
client.print("Auto-Control: ");
if (RELAY1ON)
client.println("<font color=green>ON");
else{
client.println("<font color=grey>OFF");
}
client.println("</td>");
client.println("<td>");
client.println("control 2
");
client.println("<form method=get name=RELAY><input type=radio name=R2 value=1>On
<input type=radio name=R2 value=2>Off
<input type=submit value=submit></form>");
client.print("Auto-Control: ");
if (RELAY2ON)
client.println("<font color=green>ON");
else{
client.println("<font color=grey>OFF");
}
client.println("</td>");
client.println("</tr>");
client.println("<tr align=center>");
client.println("<td>");
client.println("control 3
");
client.println("<form method=get name=RELAY><input type=radio name=R3 value=1>On
<input type=radio name=R3 value=2>Off
<input type=submit value=submit></form>");
client.print("Auto-Control: ");
if (RELAY3ON)
client.println("<font color=green>ON");
else{
client.println("<font color=grey>OFF");
}
client.println("</td>");
client.println("<td>");
client.println("control 4
");
client.println("<form method=get name=RELAY><input type=radio name=R4 value=1>On
<input type=radio name=R4 value=2>Off
<input type=submit value=submit></form>");
client.print("Auto-Control: ");
if (RELAY4ON)
client.println("<font color=green>ON");
else{
client.println("<font color=grey>OFF");
}
client.println("</td>");
client.println("</tr>");
client.println("<tr align=center>");
client.println("<td>");
client.println("control 5
");
client.println("<form method=get name=RELAY><input type=radio name=R5 value=1>On
<input type=radio name=R5 value=2>Off
<input type=submit value=submit></form>");
client.print("Auto-Control: ");
if (RELAY5ON)
client.println("<font color=green>ON");
else{
client.println("<font color=grey>OFF");
}
client.println("</td>");
client.println("<td>");
client.println("control 6
");
client.println("<form method=get name=RELAY><input type=radio name=R6 value=1>On
<input type=radio name=R6 value=2>Off
<input type=submit value=submit></form>");
client.print("Auto-Control: ");
if (RELAY6ON)
client.println("<font color=green>ON");
else{
client.println("<font color=grey>OFF");
}
client.println("</td>");
client.println("</tr>");
client.println("</table>");
if(RELAY1ON && tmp2!=fString){
tmp2=fString;
price1.concat("1");
//price1.concat('|');
}
else if(digitalRead(2)==HIGH) {
price1.concat("0");
// price1.concat('|');
}
client.println(price1);
client.println("</center>");
client.println("</body>");
readString="";
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(10);
// close the connection:
client.stop();
}
}