I'm using an ethernet shield with the w5100 chip, and an 128x64 OLED display on a Arduino Mega 2560, along with a 4 relay module. I am wanting to be able to control the relay switches from an internet browser, but also display on the OLED what the ip of the webserver is, so I don't have to look it up on the network.
If I leave the code as is below, the oled never comes on, and one of relays on the module is triggered (sometimes its the 3rd one, sometimes its the 4th one), and the web page won't show in the browser (browser says webpage cannot be displayed). If I comment out the display.println(Ethernet.localIP()); line, the webpage displays as it should in the browser and the user is able to turn each relay on or off with the html buttons.
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// 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 };
//IPAddress ip(192,168,8, 111);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
// Relay state and pin
String relay1State = "Off";
String relay2State = "Off";
String relay3State = "Off";
String relay4State = "Off";
const int relay3 = 3;
const int relay4 = 4;
const int relay5 = 5;
const int relay6 = 6;
// Client variables
char linebuf[80];
int charcount=0;
void setup() {
// Relay module prepared
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(relay5, OUTPUT);
pinMode(relay6, OUTPUT);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
digitalWrite(relay5, HIGH);
digitalWrite(relay6, HIGH);
// Open serial communication at a baud rate of 9600
//Serial.begin(9600);
Serial.begin(115200);
// start the Ethernet connection and the server:
//Ethernet.begin(mac, ip);
Ethernet.begin(mac);
server.begin();
//Serial.print("server is at ");
//Serial.println(Ethernet.localIP());
//Screen
//delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 10);
// Display static text
//display.println("Hello, world!");
display.println(Ethernet.localIP());
display.display();
}
// Display dashboard page with on/off button for each relay switch
void dashboardPage(EthernetClient &client) {
client.println("<!DOCTYPE HTML><html><head>");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head><body>");
client.println("<h3>Arduino Web Server - <a href=\"/\">Refresh</a></h3>");
// Generates buttons to control the relay
//Relay 1
client.println("<h4>Relay 1 - State: " + relay1State + "</h4>");
// If relay is off, it shows the button to turn the output on
if(relay1State == "Off"){
client.println("<a href=\"/relay1on\"><button>TURN ON</button></a>");
}
// If relay is on, it shows the button to turn the output off
else if(relay1State == "On"){
client.println("<a href=\"/relay1off\"><button>TURN OFF</button></a>");
}
//Relay 2
client.println("<h4>Relay 2 - State: " + relay2State + "</h4>");
// If relay is off, it shows the button to turn the output on
if(relay2State == "Off"){
client.println("<a href=\"/relay2on\"><button>TURN ON</button></a>");
}
// If relay is on, it shows the button to turn the output off
else if(relay2State == "On"){
client.println("<a href=\"/relay2off\"><button>TURN OFF</button></a>");
}
//Relay 3
client.println("<h4>Relay 3 - State: " + relay3State + "</h4>");
// If relay is off, it shows the button to turn the output on
if(relay3State == "Off"){
client.println("<a href=\"/relay3on\"><button>TURN ON</button></a>");
}
// If relay is on, it shows the button to turn the output off
else if(relay3State == "On"){
client.println("<a href=\"/relay3off\"><button>TURN OFF</button></a>");
}
//Relay 4
client.println("<h4>Relay 4 - State: " + relay4State + "</h4>");
// If relay is off, it shows the button to turn the output on
if(relay4State == "Off"){
client.println("<a href=\"/relay4on\"><button>TURN ON</button></a>");
}
// If relay is on, it shows the button to turn the output off
else if(relay4State == "On"){
client.println("<a href=\"/relay4off\"><button>TURN OFF</button></a>");
}
//
client.println("</body></html>");
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
memset(linebuf,0,sizeof(linebuf));
charcount=0;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
dashboardPage(client);
break;
}
if (c == '\n') {
//relay3
if (strstr(linebuf,"GET /relay1off") > 0){
digitalWrite(relay3, HIGH);
relay1State = "Off";
}
else if (strstr(linebuf,"GET /relay1on") > 0){
digitalWrite(relay3, LOW);
relay1State = "On";
}
//relay4
if (strstr(linebuf,"GET /relay2off") > 0){
digitalWrite(relay4, HIGH);
relay2State = "Off";
}
else if (strstr(linebuf,"GET /relay2on") > 0){
digitalWrite(relay4, LOW);
relay2State = "On";
}
//relay5
if (strstr(linebuf,"GET /relay3off") > 0){
digitalWrite(relay5, HIGH);
relay3State = "Off";
}
else if (strstr(linebuf,"GET /relay3on") > 0){
digitalWrite(relay5, LOW);
relay3State = "On";
}
//relay6
if (strstr(linebuf,"GET /relay4off") > 0){
digitalWrite(relay6, HIGH);
relay4State = "Off";
}
else if (strstr(linebuf,"GET /relay4on") > 0){
digitalWrite(relay6, LOW);
relay4State = "On";
}
// you're starting a new line
currentLineIsBlank = true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
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();
Serial.println("client disonnected");
}
}