Im attempting to add ethernet control to an old RS232 sketch that you guys helped me with tremendously. Old sketch located here: map 4 pins so each one has a position in a string received via serial port? - #26 by n8huntsman - Programming Questions - Arduino Forum
That will explain why Im using the arrays that Ive chosen.
I've got a basic ethernet server up on the Arduino Mega with ethernet shield. Using the arrays that everyone helped me with many years ago I wrote some simple If/Then statements that should work with the existing code. For now, I started basic and don't have the RS232 control but left the arrays intact so that I can add it as soon as I get my logic working.
The intent is for the server to have discrete ON/OFF command buttons such as 0x31ON, and also a status indicator (not yet implemented) while the RS232 code will be simple toggles with no feedback.
Im pretty sure the reason it's not working is because of data types not matching in my statements, integers, bytes, decimal, hex, etc. Can someone help me out with the following code and understanding what types of data im receiving and how to compare it to the data in the existing arrays?
Thanks
#include <SPI.h>
#include <Ethernet.h>
// following 3 includes from older code. might be needed eventually for serial control
//#include <Server.h>
//#include <Client.h>
//#include <Udp.h>
//macro for calculating the length of arrays
#define ARY_LEN(a) (sizeof(a)/sizeof(a[0]))
//array called "state"
boolean state [] = {HIGH, LOW, LOW, LOW};
//array called "zone" that defines the zone being toggled by the remote or changed via ethernet
int zone [] = {0x31, 0x32, 0x33, 0x34};
//int eth_command [] = {1, 2, 3, 4}; //might need to use this to check the ethernet command instead of zone[] since those are hex values?
//get the length of the "zone" array and call it "num_zones"
const int NUM_ZONES = ARY_LEN(zone);
// array called "pins" that defines which pins are used
int pins [] = {8, 9, 10, 11};
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 199 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
void setup()
{
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin(); //this was missing in previous version (P4)
//enable serial data print
Serial.begin(9600);
//sets pins defined in "pins" array as outputs
for (int i = 0; i < NUM_ZONES; i++) digitalWrite(pins [i], state [i]);
for (int i = 0; i < NUM_ZONES; i++) pinMode(pins [i], OUTPUT);
}
void loop()
{
// Create a client connection
EthernetClient client = server.available();
if (client)
{
while (client.connected())
{
if (client.available())
{
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100)
{
//store characters to string
readString += c; //replaces readString.append(c);
//Serial.print(c); //prints GG
}
//if HTTP request has ended
if (c == '\n')
{
//removed the following per zoomkat example
//Serial.println(readString); //see what was captured
//dirty skip of "GET /favicon.ico HTTP/1.1"
//if (readString.indexOf("?") < 0)
//{
//skip everything
//Serial.print("skipping everything");
//}
//else
// now output HTML data starting with standard header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
//controlling led via checkbox
client.println("<HTML>");
client.println("<HEAD>");
client.print("<font size='5'>Speaker Selector ");
client.println("</HEAD>");
client.println("<BODY>");
client.println("
");
//only created two buttons for now. more to come
client.println("<input type=submit value=Speaker 1 - ON style=width:120px;height:45px onclick=location.href='/?0x31HIGH;'>");
client.println("
");
client.println("<input type=submit value=Speaker 1 - OFF style=width:120px;height:45px onclick=location.href='/?0x31LOW;'>");
client.println("
");
//future use for getting status - printing LED status - ignore for now
//client.print("<font size='5'>Speaker 1 Status: ");
//if ()
//client.println("<font color='green' size='5'>ON");
//else
//client.println("<font color='grey' size='5'>OFF");
//clearing string for next read
client.println("</FORM>");
client.println("
");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
for (int i = 0; i < NUM_ZONES; i++)
//lets check if LED should be lighted
{
Serial.print(readString); //check to see what was received
//i dont thing the following if will work because its comparing two different data types
if (readString.indexOf(zone[i]) > 0) //if zone[i] (any command from array) is found anywhere in the readstring
{
Serial.print(readString.indexOf(zone[i])); //check to see what position zone[i] is in in the readstring if at all. >0 means it exists
//check to see if we need to toggle the state of ith zone
//i dont thing the following if will work because its comparing two different data types again
if (readString.indexOf(state[i]) == 0) //if state[i] is not found in readstring
{
state[i] = !state[i];//state in readstring was different from state[i] so we need to invert state
digitalWrite(pins [i], state [i]);
}
}
}
readString = "";
}
}
}
}
}