system
August 8, 2013, 8:44am
1
Hi,
I'm developing a web server class that includes the Ethernet library but have a problem when I declare the object in my class header.
In myWebServerClass.h :-
EthernetServer myServer(80); // doesn't work because its a function/method
EthernetServer myServer; // doesn't work because instantiating the object requires a port number
EthernetServer myServer = EthernetServer(80); // Isn't allowed in a header?
How do I create a "placeholder" object in the header then instantiate it in the class constructor? Or is there a way of declaring and instantiating the object in the c++ file without an entry in the .h file?
I'm using Atmel Studio with the Arduino add on.
Thanks in advance,
Tb.
system
August 8, 2013, 9:44am
2
You create the object in the .cpp file
EthernetServer myServer(80);
Then in the .h file you create an "extern" reference to it:
extern EthernetServer myServer;
system
August 8, 2013, 10:50am
3
Thanks for your help Majenko.
I've done what you suggested but now get a 'storage class specified for myServer' compile error?
system
August 8, 2013, 11:26am
5
Hi,
The header file:
/*
webServerClass.h - Libs for XML and HTML
Created by Tb 08-08-20131.
*/
#ifndef webServerClass_h
#define webServerClass_h
#include "Arduino.h"
#include "C:\Users\tb\Documents\Arduino\libraries\SPI\SPI.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\util.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\EthernetUdp.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\EthernetServer.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\EthernetClient.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\Ethernet.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\Dns.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\Dhcp.h"
class webServer
{
public:
extern EthernetServer myServer;
byte serverMac[];
IPAddress serverIp;
String pcId;
webServer(void);
void start(void);
void decodeRequest(void);
void readClientRequests(void);
void makeXmlHeader(void);
void makeXmlFooter(void);
void makeHtmlHeader(void);
void makeHtmlFooter(void);
void getCommandString(void);
void getCommandParameters(void);
void getCommand(void);
String xmlHeader;
String xmlFooter;
String htmlHeader;
String htmlFooter;
String httpRequest;
String httpCommandString;
String httpCommand;
String httpCommandParameter;
String httpResponse;
protected:
private:
};
#endif
The cpp:
/*
webServerClass.cpp - Libs for serving XML and HTML put and get via requests.
Created by Tb 08-08-20131.
*/
#include "Arduino.h"
#include "webServerClass.h"
#include "C:\Users\tb\Documents\Arduino\libraries\SPI\SPI.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\util.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\EthernetUdp.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\EthernetServer.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\EthernetClient.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\Ethernet.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\Dns.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\Dhcp.h"
webServer::webServer()
{
// Instantiate object code
}
void webServer::start()
{
// start the Ethernet connection and server:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,1,180);
Ethernet.begin(mac, ip);
myServer.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
system
August 8, 2013, 11:30am
6
I guess I'm a bit confused about where in the two files to put the:
extern EthernetServer myServer;
and
EthernetServer myServer(80);
When I move extern EthernetServer myServer; outside the class definition I don't get the error: "storage class specified for 'myServer'"
but I then can't access the object and get this error: "'class webServer' has no member named 'myServer'" instead
Thanks
Tb.
system
August 8, 2013, 11:32am
7
#include "C:\Users\tb\Documents\Arduino\libraries\SPI\SPI.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\util.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\EthernetUdp.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\EthernetServer.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\EthernetClient.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\Ethernet.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\Dns.h"
#include "C:\Users\tb\Documents\Arduino\libraries\Ethernet\Dhcp.h"
You should not have to be using fully qualified paths for these libraries.
You should not have to include them in the header file AND the source file.
Why is everything in your class public?
Where are you creating the myServer instance?