hi,
I am attempting to get a program that will take a call from an ip address such as "192.168.1.86:50/?ledOn!" to print out on to Serial or to run a function.
My desired outcome would be to the same effect as:
Serial.println("ledOn");
or
if (input == 'ledOn') {
ledON();
}
[code/]
Here is my full code:
[code]
#include <SPI.h>
#include <Ethernet2.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10-71, 0x8E }; //physical mac address
byte ip[] = { 192, 168, 1, 86}; // ip in lan
byte gateway[] = { 192, 168, 1, 254 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(50); //server port
//String readString;
const byte maxNum = 100;
char charRead[maxNum];
char inNum = 0;
bool anyMore = false;
const byte numChars = maxNum;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
char endMark = '!';
char startMark = '?';
bool comingIn = false;
void setup(){
Serial.begin(9600);
// Serial.println("Serial Up");
//pinMode(5, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, gateway, subnet);
//Serial.println("ethernet begin done");
pinMode(4, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(13,HIGH);
digitalWrite(4, HIGH);
//Serial.println("pins done");
delay(2000);
server.begin();
Serial.println("server done");
Serial.println(Ethernet.localIP());
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
//Serial.print("server available");
if (client) {
//Serial.println("allOK");
if (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
while (c != endMark && inNum < maxNum && comingIn == true) {
char c = client.read();
charRead[inNum] += c;
//store characters to string
//Serial.print(c);
inNum ++;
c = NULL;
Serial.println(charRead);
//delay(2);
client.flush();
}
comingIn = false;
if (c == startMark){
comingIn = true;
}
//if HTTP request has ended
if (c == endMark || inNum >= maxNum) {
///////////////
//Serial.println(charRead); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE> Box of tricks... </TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Arduino button's</H1>");
client.println("<a href=\"/?on!\">ON</a>");
client.println("<a href=\"/?off!\">OFF</a>");
client.println("<a href=\"/?red!\">RED</a>");
client.println("<a href=\"/?green!\">GREEN</a>");
client.println("<a href=\"/?blue!\">BLUE</a>");
client.println("<a href=\"/?cyan!\">CYAN</a>");
client.println("<a href=\"/?purple!\">PURPLE</a>");
client.println("<a href=\"/?yellow!\">YELLOW</a>");
client.println("<a href=\"/?curtainClose!\">CURTAINCLOSE</a>");
client.println("<a href=\"/?curtainOpen!\">CURTAINOPEN</a>");
client.println("<a href=\"/?brightness!\">BRIGHTNESS</a>");
client.println("<a href=\"/?l!\">LEDON</a>");
client.println("<a href=\"/?f!\">LEDOFF</a>");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
/*if(readString.indexOf("on") == -1) {
} else {
Serial.println(readString.indexOf("on"));
Serial.println("on");
}
if(readString.indexOf("off") != 1)//checks for off
{
Serial.println(readString.indexOf("off"));
Serial.println("off");
}
if(readString.indexOf("red") != 1)//checks for off
{
Serial.println(readString.indexOf("red"));
Serial.println("red");
}
if(readString.indexOf("green") != 1)//checks for off
{
Serial.println(readString.indexOf("green"));
Serial.println("green");
}
if(readString.indexOf("blue") != 1)//checks for off
{
Serial.println(readString.indexOf("blue"));
Serial.println("blue");
}
if(readString.indexOf("cyan") != 1)//checks for off
{
Serial.println(readString.indexOf("cyan"));
Serial.println("cyan");
}
if(readString.indexOf("purple") != 1)//checks for off
{
Serial.println(readString.indexOf("purple"));
Serial.println("purple");
}
if(readString.indexOf("yellow") != 1)//checks for off
{
Serial.println(readString.indexOf("yellow"));
Serial.println("yellow");
}
if(readString.indexOf("curtainOpen") != 1)//checks for off
{
Serial.println(readString.indexOf("curtainOpen"));
Serial.println("curtainOpen");
}
if(readString.indexOf("curtainClose") != 1)//checks for off
{
Serial.println(readString.indexOf("curtainClose"));
Serial.println("curtainClose");
}
if(readString.indexOf("brightness") != 1)//checks for off
{
Serial.println(readString.indexOf("brightness"));
Serial.println("brightness");
}
if(readString.indexOf("ledon") != -1) {
Serial.println(readString.indexOf("ledon"));
digitalWrite(13, HIGH);
delay(2000);
Serial.println("ledon");
}
if(readString.indexOf("ledoff") == -1){
} else {
Serial.println(readString.indexOf("ledoff"));
digitalWrite(13, LOW);
Serial.println("ledoff");
}
if (readString.indexOf("l") < -1) {
digitalWrite(13,HIGH);
Serial.println("ledon");
} else if (readString.indexOf("f") < -1) {
digitalWrite(13,LOW);
}
//clearing string for next read
readString="";
*/
Serial.println(charRead[maxNum]);
if (charRead == "?l") {
digitalWrite(13,HIGH);
} else if (charRead == "?f") {
digitalWrite(13,LOW);
} else if (charRead == "?on") {
Serial.println("on");
}
inNum = 0;
charRead[maxNum] = NULL;
}
}
}
}
inNum = 0;
charRead[inNum] ="";
}
void input() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
// if (Serial.available() > 0) {
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
//Serial.print("This just in ... ");
Serial.println(receivedChars);
server.println(receivedChars);
newData = false;
}
}
after running this for the call above i keep receiving this in serial monitor:
@an! HTTP/1.1
Host: 192.168.1.86:50
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Accept: d
Any suggestions to help me make this code work properly or more efficient would be appreciated.
Thank you.