Arduino 1.0 and EthernetServer Issue

Hi everyone,

I just upgraded to Arduino 1.0 and now have troubles with one of my projects.
What my script does is, it opens a web server and in case of a request answers with the current temperature.
This was working fine for a couple of weeks now until I installed said upgrade.

Here is my script:

#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,222 };

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
Server server(80);

// Thermometer hängt an Digitalport 2
#define ONE_WIRE_BUS 2
// Setup oneWire Instanz um mit dem Thermometer zu kommunizieren
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  
  // Start Sensor Library
  sensors.begin();
 
}

void loop()
{
  String entireRequest = "";
  String requestedFile = "";
  int line = 0;
  // listen for incoming clients
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if(line == 0)
          entireRequest += c;
        
        // Find out which file was requested (for the future)
        if (c == '\n' && currentLineIsBlank == false) {
          line++;
          if (line == 1) {
            requestedFile = entireRequest.substring(5);
            requestedFile = requestedFile.substring(0,requestedFile.indexOf(" "));
          }
        }
        
        
        if (c == '\n' && currentLineIsBlank) {
          if(requestedFile == "")
            requestedFile = "index";
          // Get temperature
          sensors.requestTemperatures();
          
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          client.println("<html><head><title>Wohnzimmer Arduino: "+requestedFile+"</title></head><body>");
          
          client.println("The temperature is: ");
          client.println(sensors.getTempCByIndex(0)); // Die Temperatur
          client.println("&#8451;"); // °C Zeichen als HTML Entity

          client.println("</body></html>");

          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(1);
    // close the connection:
    client.stop();
  }
}

As you can tell I am heavily relying on the example on the Arduino website. They only changes I made was adding the temperature sensor and finding out which page was requested.

Now the first error that I got was:

As of Arduino 1.0, the Server class in the Ethernet library has been renamed to EthernetServer.

This one was easy to fix, I just replaced Server by EthernetServer and got this:

#include <SPI.h>
#include <EthernetServer.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,222 };

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

// Thermometer hängt an Digitalport 2
#define ONE_WIRE_BUS 2
// Setup oneWire Instanz um mit dem Thermometer zu kommunizieren
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup()
{
  // start the Ethernet connection and the server:
  EthernetServer.begin(mac, ip);
  server.begin();
  
  // Start Sensor Library
  sensors.begin();
 
}

void loop()
{
  String entireRequest = "";
  String requestedFile = "";
  int line = 0;
  // listen for incoming clients
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if(line == 0)
          entireRequest += c;
        
        // Find out which file was requested (for the future)
        if (c == '\n' && currentLineIsBlank == false) {
          line++;
          if (line == 1) {
            requestedFile = entireRequest.substring(5);
            requestedFile = requestedFile.substring(0,requestedFile.indexOf(" "));
          }
        }
        
        
        if (c == '\n' && currentLineIsBlank) {
          if(requestedFile == "")
            requestedFile = "index";
          // Get temperature
          sensors.requestTemperatures();
          
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          client.println("<html><head><title>Wohnzimmer Arduino: "+requestedFile+"</title></head><body>");
          
          client.println("The temperature is: ");
          client.println(sensors.getTempCByIndex(0)); // Die Temperatur
          client.println("&#8451;"); // °C Zeichen als HTML Entity

          client.println("</body></html>");

          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(1);
    // close the connection:
    client.stop();
  }
}

The error now is:

WohnzimmerWebServer.cpp: In function 'void setup()':
WohnzimmerWebServer:24: error: expected unqualified-id before '.' token
WohnzimmerWebServer.cpp: In function 'void loop()':
WohnzimmerWebServer:38: error: 'Client' was not declared in this scope
WohnzimmerWebServer:38: error: expected `;' before 'client'
WohnzimmerWebServer:39: error: 'client' was not declared in this scope

Any ideas? I am completely stuck as the setup() functions looks perfectly fine to me and the line "EthernetServer.begin(mac, ip);" should also work.

Thanks!
Christoph

Did you look at the EthernetServer.h or EthernetServer.cpp file? The Ethernet.begin() method has two versions. One takes a port number. The other takes no arguments. You are trying to call a non-existent static method here:

  EthernetServer.begin(mac, ip);

The begin method needs to be called on an instance, as is done on the next line.

The Client class was renamed, too. To EthernetClient.

If you look at the server examples in the 1.0 IDE you will see some differences, like the ones below. Bottom link has some of the new changes that one needs to be aware of.

IPAddress ip(192,168,1,177);

EthernetClient client = server.available();

Thanks guys!

@PaulS: We were both wrong here. While the method begin is indeed called on the class, the correct class would have been Ethernet:

Ethernet.begin(mac, ip);
server.begin();

@zoomkat: Thanks for the link, this helped me quite a lot.

I got everything back to working except for the temperature sensor, I keep getting this error and I cannot figure out what thats supposed to tell me (and I also added #include <Arduino.h> to my sketch):

/Volumes/christoph/Documents/Arduino/libraries/DallasTemperature_371Beta/DallasTemperature.cpp:9:26: error: WConstants.h: No such file or directory

Thanks,
Christoph

the script now is:

#include <Arduino.h>
#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>



byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,222 };

EthernetServer server(80);

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup()
{
  
  Ethernet.begin(mac, ip);
  server.begin();
  
  // Start Sensor Library
  sensors.begin();
}

void loop()
{
  String entireRequest = "";
  String requestedFile = "";
  int line = 0;
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if(line == 0)
          entireRequest += c;
        
        // Figure out which file was requested
        if (c == '\n' && currentLineIsBlank == false) {
          line++;
          if (line == 1) {
            requestedFile = entireRequest.substring(5); 
            requestedFile = requestedFile.substring(0,requestedFile.indexOf(" "));
          }
        }
        
        
        if (c == '\n' && currentLineIsBlank) {
          if(requestedFile == "")
            requestedFile = "index";
          // Get the temperature
          sensors.requestTemperatures();
          
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          client.println("<html><head><title>Living Room Arduino: "+requestedFile+"</title></head><body>");
          
          client.println("The temperature is: ");
          client.println(sensors.getTempCByIndex(0)); // The Temperature
          client.println("&#8451;"); // °C Sign as HTML Entity
         

          client.println("</body></html>");

          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(1);
    // close the connection:
    client.stop();
  }
}

zoomkat:
Projects – Make: DIY Projects and Ideas for Makers

This should be a sticky somewhere if its not already. I was having problems with wprogram.h missing after the upgrade - the above link tell me why.

Hi,

so the problem was not in my code but rather in the dallastemperature.cpp file.

What you have to do is open this file, and replace

extern "C" {
  #include "WConstants.h"
}

with this

extern "C" {
  #include "Arduino.h"
}

Thanks for your support everyone!
Christoph