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("℃"); // °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("℃"); // °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