Hi I have an arduino mega with ethernet2 shield when it reaches the Ethernet.begin it doesn't seem to go any further I have tried this on another arduino mega and the same issue happens. I have loaded the Ethernet webclient example and this works fine.
I have 3 mux's wired in as well on my test arduino it was just the Ethernet Shield attached.
here is my code I am quite puzzled.
#define NUM_ZONES (63)
#include <SPI.h>
#include <Ethernet2.h>
#include <SD.h>
#include <IniFile.h>
#include "MUX74HC4067.h"
// Creates a MUX74HC4067 instance
// 1st argument is the Arduino PIN to which the EN pin connects
// 2nd-5th arguments are the Arduino PINs to which the S0-S3 pins connect
MUX74HC4067 mux1(22,23,24,25,26);
MUX74HC4067 mux2(28,29,30,31,32);
MUX74HC4067 mux3(48,49,50,51,52);
//check the mac address
// spare arduino mac addres
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// arduino mac addres
//byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x3D, 0xFB };
byte ip[] = {192,168,0,177};
EthernetClient client ;
// The select pin used for the SD card
#define SD_SELECT 4
#define ETHERNET_SELECT 10
//arrays to check if button has changed
//int previousState[NUM_ZONES]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int previousState[NUM_ZONES]= {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
//Command to pass to homeseer
char* myStrings[64];
//SubRoutine For Error Messages Reading SD card
void printErrorMessage(uint8_t e, bool eol = true)
{
switch (e) {
case IniFile::errorNoError:
Serial.print("no error");
break;
case IniFile::errorFileNotFound:
Serial.print("file not found");
break;
case IniFile::errorFileNotOpen:
Serial.print("file not open");
break;
case IniFile::errorBufferTooSmall:
Serial.print("buffer too small");
break;
case IniFile::errorSeekError:
Serial.print("seek error");
break;
case IniFile::errorSectionNotFound:
Serial.print("section not found");
break;
case IniFile::errorKeyNotFound:
Serial.print("key not found");
break;
case IniFile::errorEndOfFile:
Serial.print("end of file");
break;
case IniFile::errorUnknownError:
Serial.print("unknown error");
break;
default:
Serial.print("unknown error value");
break;
}
if (eol)
Serial.println();
}
void setup()
{
//Config and read SD card
// Configure all of the SPI select pins as outputs and make SPI
// devices inactive, otherwise the earlier init routines may fail
// for devices which have not yet been configured.
digitalWrite(ETHERNET_SELECT, HIGH); // disable Ethernet
const size_t bufferLen = 80;
char buffer[bufferLen];
const char *filename = "/config.ini";
Serial.begin(9600);
SPI.begin();
if (!SD.begin(SD_SELECT))
while (1)
Serial.println("SD.begin() failed");
IniFile ini(filename);
if (!ini.open()) {
Serial.print("Ini file ");
Serial.print(filename);
Serial.println(" does not exist");
// Cannot do anything else
while (1)
;
}
Serial.println("Ini file exists");
// Check the file is valid. This can be used to warn if any lines
// are longer than the buffer.
if (!ini.validate(buffer, bufferLen)) {
Serial.print("ini file ");
Serial.print(ini.getFilename());
Serial.print(" not valid: ");
printErrorMessage(ini.getError());
// Cannot do anything else
while (1)
;
}
// Fetch a value from a key which is present
// get data from sd card and write to array
Serial.println("getting Homesser commands");
for (int j=0; j<=63; j=j+1) {
char mpass[ ] = "command" ;
int num = j;
char cstr[16];
itoa(num, cstr, 10);
strcat(mpass,cstr);
if (ini.getValue("HSCOMMANDS",mpass, buffer, bufferLen)) {
myStrings[j] = buffer;
}
}
// Configures how the SIG pin will be interfaced e.g. The SIG pin connects to PIN 3 on the Arduino and PIN 3 is a digital input
mux1.signalPin(27, INPUT, DIGITAL);
mux2.signalPin(33, INPUT, DIGITAL);
mux3.signalPin(53, INPUT, DIGITAL);
Serial.println("loaded ini");
//Network card setup
digitalWrite(ETHERNET_SELECT, LOW); // enable Ethernet this is turned on after SD reader finished with SPI pins
Serial.println("Turning Ethernet on");
digitalWrite(SD_SELECT, HIGH); // Disable SD card
Serial.println("Disabled SD card");
Ethernet.begin(mac, ip);
delay(1000);
Serial.println("ready");
Serial.println("waiting for button press");
}
void loop()
{
byte data;
String hsCommand;
String mypass;
for (byte i = 0; i < 48; ++i)// was 16
{
// Reads from channel i and returns HIGH or LOW
if (i < 16){
data = mux1.read(i);
//Serial.print("MUX1 Push button at channel ");
}
else if (i >15 and i <32){
data = mux2.read(i-16);
//Serial.print("MUX2 Push button at channel ");
}
else if (i >31){
data = mux3.read(i-16);
//Serial.print("MUX3 Push button at channel ");
}
//tmp not to include in final version
Serial.print("posistion ");
Serial.print(i);
Serial.print("value ");
Serial.println(data);
//
if ((data == HIGH) && (previousState[i] == LOW )) {
//call homeseerpost turning on
Serial.println("in on if statement");
mypass = String(myStrings[i]) + "&label=off";
//mypass = "347&label=off";
Serial.println(mypass);
sendHomeSeerCommand(mypass);
}
else if ((data == LOW) && (previousState[i] == HIGH )) {
// call homeseerpost turning off
Serial.println("in Off if statement");
mypass = String(myStrings[i]) + "&label=on";
//mypass = "347&label=on";
sendHomeSeerCommand(mypass);
}
previousState[i] = data;
}
// this to remove
delay(500);
}
void sendHomeSeerCommand(String HScommand)
{
byte server[] = { 192,168,0,2 };
String url = "/JSON?request=controldevicebylabel&ref=" + HScommand ;
int ret = client.connect(server, 80) ;//was 8080
if (ret==1)
{
Serial.println("Sending " + url);
client.print("GET " + url + " HTTP/1.1\r\n" +
"Host: 192.168.0.2 " + "\r\n" +
"Connection: close\r\n\r\n");
client.stop();
}
else {
Serial.println("connection failed");
}
}