Merge Webduino to Current Sketch

im trying to merge Webduino with my current project so i can control it over http

after much messing around i have managed to get the webduino examples working how i wanted to get them

the 2nd sketch is just a pinger, it pings an ip address, when it fails x amount of times it turns a relay off, back on and tries again in x minitues.

now when i try to add the pair together the pinger runs as it should but the webduino dose not seem to work ?

ive attached my sketch, but i cant work it out, ive been looking at this for the past 2 hrs and i cant find anything wrong with it ?

i am a novis to programming but id like to think im getting better,

Regards & thanks to anyone who replys to the post.

PingV4.2.ino (5.93 KB)

Post your code per #7 below:

http://forum.arduino.cc/index.php/topic,148850.0.html

#include <SPI.h>
#include <Ethernet.h>  // call ethernet module
#include <ICMPPing.h> // call ping module
#include <LiquidCrystal.h> // call lcd moduletv
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//    LiquidCrystal lcd(22,24,26,28,30,32);      // tell lcd what pins are being used to control lcd
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xF0, 0xED}; // set mac address for ethernet port
byte ip[] = {192, 168, 1, 187}; // set desired ip address ethernet port
// byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
// byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
IPAddress pingAddr(8, 8, 8, 8); // ip address to ping (google.com)
//    IPAddress pingAddr(192,168,1,199); // ip address to ping (google.com)
const int plugRelay = 8;
boolean pingSuccess = false;
SOCKET pingSocket = 0;
const int maxNumberOfTimesToTryToPing = 10;
char buffer [256];
ICMPPing ping(pingSocket, (uint16_t)random(0, 255));
EthernetServer server(80);                             //server port
String readString;
void setup() {
  // START THE WEBSERVER //
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  //  pinMode(led, OUTPUT);
  // start the Ethernet connection and the server:
  //  Ethernet.begin(mac, ip, gateway, subnet);
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  // END THE WEBSERVER CODE //

  //      digitalWrite(plugRelay, LOW);
  pinMode(plugRelay, OUTPUT);
  Ethernet.begin(mac, ip);  // start Ethernet
  lcd.begin(16, 2); // start lcd
  Serial.begin(9600);  //
  lcd.setCursor(0, 0); // set lcd to print to line 1
  Serial.println("POWER UP // DISPLAY WELCOME MESSAGE");  // print to serial monitor
  lcd.print("   Power-Ping   ");  // message to print
  lcd.setCursor(0, 1); // set lcd to print to line 2
  lcd.print("  Mathew  Buer  ");  // message to print
  //  delay(5000);  // delay for message
  lcd.clear();  // clear lcd
  lcd.print("Power         ON"); // print

}

void checkping() {
  pingSuccess = false;
  for (int p = 0; p < maxNumberOfTimesToTryToPing; p++)
  {
    ICMPEchoReply echoReply = ping(pingAddr, 4);  // start ping
    if (echoReply.status == SUCCESS)   // advise of success
    {
      Serial.println("PING SUCCESSFULL / WAIT 2 SECONDS BEFORE NEXT ATTEMPT");  // print to serial monitor
      pingSuccess = true;
      // Do whatever you need to with the reply data
      lcd.setCursor(0, 1); // let lcd to print to line 2
      lcd.print("Ping          OK"); // print
      digitalWrite(plugRelay, HIGH);
      delay(2000);
      break; // skip the rest of the iterations of the for loop
      break;
    }
  }

  if (!pingSuccess)
  {
    Serial.println("PING FAILED / WILL ATTEMPT AGAIN * 5 THEN RESTART");  // print to serial monitor
    lcd.setCursor(0, 1);
    lcd.print("Ping      FAILED");   // Failed to ping the device in maxNumberOfTimesToTryToPing tries
    digitalWrite(plugRelay, LOW);
    lcd.setCursor(0, 0); // set lcd to print to line 1
    lcd.print("Power        OFF"); // print
    delay(5000);
    digitalWrite(plugRelay, HIGH);
    lcd.setCursor(0, 0); // set lcd to print to line 1
    lcd.print("Power         ON"); // print
    lcd.setCursor(0, 1); // set lcd to print to line 2
    lcd.print("Re-Test in 5Mins"); // print
    delay(300000);
  }
}

void createhtml() {
  // 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;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {
          Serial.println(readString); //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("<link rel='stylesheet' type='text/css' href='http://www.autodoor.org.uk/powerpinger/css/button.css' />");
          client.println("<TITLE>Power Pinger | Web Control</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>Power Pinger Web Control</H1>");
          client.println("<hr />");
          client.println("
");
          client.println("<a href=\"/?power-on-relay\"\">Turn On Relay</a>");
          client.println("<a href=\"/?power-off-relay\"\">Turn Off Relay</a>
");
          client.println("
");
          client.println("
");
          client.println("<p>Created by Mathew Buer!</p>");
          client.println("
");
          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();
          //controls the Arduino if you press the buttons
          if (readString.indexOf("?power-on-relay") > 0) {
            digitalWrite(plugRelay, HIGH);
          }
          if (readString.indexOf("?power-off-relay") > 0) {
            digitalWrite(plugRelay, LOW);
          }

          //clearing string for next read
          readString = "";

        }
      }
    }
  }
}
void loop() {
  createhtml(); // creates webduino html page
  // checkping();
}

You might be having low memory issues. Put all your static lines of text in your code inside F() macros similar to below.

          client.println(F("HTTP/1.1 200 OK")); //send new page
          client.println(F("Content-Type: text/html"));
          client.println();

          client.println(F("<HTML>"));
          client.println(F("<HEAD>"));
          client.println(F("<TITLE>Arduino GET test page</TITLE>"));
          client.println(F("</HEAD>"));
          client.println(F("<BODY>"));
Serial.println(F("POWER UP // DISPLAY WELCOME MESSAGE"));  // print to serial monitor

can i ask what the F() Macro Dose ?

the arduino should have no problem doing this little bit of code surley ?

mathewbuer:
can i ask what the F() Macro Dose ?

I will use this call as an example...

client.println( "Content-Type: text/html" );

"Content-Type: text/html" is a string constant. String constants are always placed in Flash. Normally, the Libc initialization code copies string constants from Flash to SRAM. The call above uses the string constant from SRAM.

The F-macro prevents the Libc initialization code from copying the string constant to SRAM; the string constant only ever resides in Flash. The following call uses the string constant from Flash saving 24 bytes of SRAM.

client.println( F( "Content-Type: text/html" ) );

i did google it but got a little lost with it,

however, since adding the F() macro my project still dose not work, but they work separately.

i have even tried removing the delay() and replacing with millis() because i thought the delay between ping's would cause the webduino library to stop working.

can anyone help im really stuck and this is bugging the hell outta me now!!! thanks in advance.

Ok, first things first.

The F() macro haven't anything to do with any problem you might have, but it's a MUST, you NEED to use it to print string constants, by that I mean anything that you print that isn't a variable, or everything you print inside quotes. That will prevent you from running out of RAM too quickly, even when you run a "simple" code.

Second, and mostly important, I have a webServer running on my own Arduino Mega, and when I started it I was looking to do exactly the same thing as you are. Trying to merge something I already got working with the Webduino library. I'm not a programmer, I took some (like 3 or 4, kkkk) C++ classes 5 years ago, and only used it a while ago when I discovered the amazing Arduino world.

And my advice is: forget about that library. From my little experience, "mostly" of that Arduino custom libraries are really not needed (except Ethernet, SDfat, Time, etc.), since it only masks the inner workings, almost always spending alot of unnecessary resources.

You should try to understand, and do it on your own, the knowledge you will acquire will help alot on future maintances that you will "probably" need, if you are planning to make it up and running 24/7.

So, a really nice start is the WebServerST code from SurferTim, I took alot of ideas from it, and always return to it when I have some doubt or some problem while trying new implements.

Another good references besides SurferTim posts / codes, are zoomkat's and CatweazleNZ's.
These three are the best when researching about webservers.

For the future CatweazleNZ's - 2wg code it's a bit tricky and really complex, but it's a nice research for new functions, once you start to understand it.

Thanks for the input!!!! i shall go away and have a look at them bits of code!!

TBH my finished product dose what it needs to do, i just wanted to add more and more functions to it, and i came up with the idea of having a web GUI where users could change the ping address, the server address etc.