Hi Kevin,
It's been a few months since I've used this code... However, it worked fine back then:
Init:
#include <Arduino.h>
#include <include/twi.h>
//#include <Wire.h>
#include <SPI.h> // Required for Ethernet
#include <Ethernet.h>
// printing to the ethernet output
#define ETH_STR_BUFF 200
String Eth_Str = "";
char Outs[ETH_STR_BUFF] = "";
const int SERVER_PORT = 6789;
//unsigned char mac[] = {0xde, 0xad, 0xbe, 0xef, 0xfd, 0xec};
//unsigned char ip[] = {129, 0, 0, 178};
// ETHERNET
const char Use_DHCP = 0; //set to 0 to use a fixed IP address, specified below.
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// Otherwise, make one up...
// Fake MAC addresses should have a 2, 6, A, or E as the 2nd digit, in the first
// group of numbers. The rest of the digits can be random.
// The last digit is a function of dio pin-state (see below, and setup()).
// NER LAN expects acs like: {0x02, 0x00, 0x00, 0x00, 0x00, 0x00}, where the last
// group can be 0x00 to 0x0f.
byte mac[] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x00};
#define pin_mac0 22
#define pin_mac1 23
#define pin_mac2 24
#define pin_mac3 25
//When this gadget is on an 192.168 net, start at this address (like 192.168.0.100)
// Modified in Setup()
// Use this IP address if DHCP fails, or is disabled.
#define PRIVATE_IP_OFFSET 50
byte ip[] = {192, 168, 0, PRIVATE_IP_OFFSET};
// If all pin_mac's are left floating, then id will = 15, and it is assumed that we're in
// "development mode" on NER's LAN.
#define DEVELOPMENT_IP {129, 0, 0, 180}
byte subnet[] = {255, 255, 255, 0};
//This board:
EthernetServer server = EthernetServer(6789);
//Who's talking to us:
EthernetClient client;
Setup:
// ------------------------------------ ------------------------------------
// SETUP
// ------------------------------------ ------------------------------------
void setup() {
//these pins assign this pcb it's id, all high = default
pinMode(pin_mac0, INPUT_PULLUP);
pinMode(pin_mac1, INPUT_PULLUP);
pinMode(pin_mac2, INPUT_PULLUP);
pinMode(pin_mac3, INPUT_PULLUP);
// Init GP DIO as all inputs
for (i = 0; i <=2; i++){
for (j = 0; j <=7; j++) {
pinMode(Port_pins[i][j], INPUT_PULLUP);
}
}
// Init Output String
i = Eth_Str.reserve(ETH_STR_BUFF);
if (i < 1) {
Serial.println(" ERROR! Not eneough memory (Eth_Str).");
while(true){}
}
//---------------------
//ETHERNET -----------
if (digitalRead(pin_mac0)==HIGH) {id+=1;}
if (digitalRead(pin_mac1)==HIGH) {id+=2;}
if (digitalRead(pin_mac2)==HIGH) {id+=4;}
if (digitalRead(pin_mac3)==HIGH) {id+=8;}
if (Debug_Serial) {
Serial.print("My ID# (from Pins ");
Serial.print(pin_mac3);
Serial.print("..");
Serial.print(pin_mac0);
Serial.print("): ");
Serial.println(id,DEC);
}
if (id > 15) {
if (Debug_Serial) {
Serial.print("Board id is mangled (got: ");
Serial.print(id, DEC);
Serial.println("). Seting board id to 15.");
}
id = 15;
}
mac[5] = id;
if (Debug_Serial) {
Serial.print("Assigning MAC to: ");
Serial.print(mac[0],HEX);
for (i=1;i<6;i++){
Serial.print(" : ");
Serial.print(mac[i],HEX);
}
Serial.println(" ");
}
if (Use_DHCP == 1) {
DBG_PRINTLN("Waiting for response from DHCP server...");
dhcp_ok = Ethernet.begin(mac);
if ((dhcp_ok == 1)&&(Debug_Serial)) {
Serial.println("Network address received from DHCP server.");
} else {
Serial.println("Tried, and failed to configure Ethernet using DHCP.");
}
}
if ((Use_DHCP == 0) || (dhcp_ok == 0)) {
DBG_PRINTLN("Using fixed network settings.");
if (id <15) {
ip[3] = PRIVATE_IP_OFFSET+id; // last ip is an indication if location/id
} else {
ip = DEVELOPMENT_IP;
}
Ethernet.begin(mac, ip);
}
// print the local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
Serial.print("Port: ");
Serial.println(SERVER_PORT, DEC);
// End ETHERNET Setup
delay(1000); // 1 second.
server.begin();
Main:
// ------------------------------------ ------------------------------------
// MAIN LOOP
// ------------------------------------ ------------------------------------
void loop() {
while(1) {
client = server.available();
if(!client) {
continue;
}
while(client.connected()) {
toggle_led();
service_client();
}
}
}
Ethernet service:
//******************************************************
// If an Ethernet client is talking to us, process its input
//******************************************************
void service_client(){
int i;
Error_Code = 0;
Numeric_Error = 0;
if(!client.available())
return;
if(fetch_input() < 1) {
//DBG_PRINTLN("Client was detected, but no commands were found.");
return;
}
if (Error_Code > 0) {
DBG_PRINT("service_client: Warning, data lost. Error Code: ");
DBG_PRINTLN(Error_Code);
return;
}
//Start processing the input commands
Cmd = Params[0][0];
//upcase cmd for ease of use...
if ((Cmd >= 97) && (Cmd <=122)) {
Cmd = Cmd - 32;
}
DBG_PRINTLN(Cmd);
Eth_Str = "{'cmd': '";
Eth_Str += Params[0];
switch (Cmd) {
//case 'A' : Get_Onboard_ADC();
// break;
case 'C' : Conf_DIO(); //set each port to either input or output.
break;
case 'D' : Update_Onboard_DAC();
break;
case 'F' : Fetch_I2C_Values(); //read-only, start reading from current location
break;
case 'I' : Read_Onboard_DIO();
break;
case 'O' : Set_Onboard_DIO();
break;
case 'Q' : I2CBus_SoftwareReset();
break;
case 'R' : Read_I2C_Values(); //Transmitt with no Stop bit, restart & read from starting register
break;
case 'S' : Scan_I2C_Bus(); // Scan from <beg> <end> for known interface chips.
break;
case 'T' : Send_I2C_Command(); //Transmitt data to chip at address
break;
case 'V' : Version();
break;
case 'W' : WaitFor_mS();
break;
case 'Z' : Z_Reset_Processor();
break;
default : Error_Code = 1;
Eth_Str += "', 'err': " ;
Eth_Str += Error_Code;
Eth_Str += ", }\n";
DBG_PRINTLN("service_client(): Invalid input command. Received: ");
DBG_PRINT(Cmd);
DBG_PRINT(" (in decimal: ");
DBG_PRINT(Cmd, DEC);
DBG_PRINTLN(").");
}
for (i = 0; i<Eth_Str.length(); i++) {
Outs[i] = Eth_Str[i];
}
Outs[i] = 0;
server.print(Outs);
if (I2C_Reset_Request == 1) {
I2CBus_SoftwareReset();
I2C_Reset_Request = 0;
}
}