i'm trying to read a ds1307 rtc breakout board (sparkfun) through a PoEthernet shield (sparkfun), connected to an arduino uno.
however its not working, and i'm confused why. if i load a sketch which does nothing but read the ds1307, it works fine. once i add the ethernet code into the mix i cannot read it anymore, i get 165/165/165. the ethernet shield is connected all the time and the rtc breakout board passes through on analog pins 4 and 5
i'll note that i make no hardware changes to my setup between these tests. if i run an i2c scanner sketch, it does indeed find the ds1307 at 0x68, but i can't read the time for some reason. anyone have any thoughts on what i might be doing wrong?
here's my sketch
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#define tmp102_ADDRESS 0x48
#define DS1307_ADDRESS 0x68
// 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(10,12,1,177);
IPAddress gateway(10,12,1,1);
IPAddress subnet(255,255,255,0);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// Disable SD SPI
// Is this needed ???
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
// start the Ethernet connection:
Serial.println("Trying to get an IP address using DHCP");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// initialize the ethernet device not using DHCP:
Ethernet.begin(mac, ip, gateway, subnet);
}
// print your local IP address:
Serial.print("My IP address: ");
ip = Ethernet.localIP();
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(ip[thisByte], DEC);
Serial.print(".");
}
Serial.println();
// start listening for clients
server.begin();
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// 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) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
printDate(client);
printTemp(client);
// output the value of each analog input pin
// for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
// int sensorReading = analogRead(analogChannel);
// client.print("analog input ");
// client.print(analogChannel);
// client.print(" is ");
// client.print(sensorReading);
// client.println("
");
// }
client.println("</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();
Serial.println("client disconnected");
}
}
float getTemperature() {
Wire.requestFrom(tmp102_ADDRESS,2);
byte MSB = Wire.read();
byte LSB = Wire.read();
//it's a 12bit int, using two's compliment for negative
int TemperatureSum = ((MSB << 8) | LSB) >> 4;
float celsius = TemperatureSum*0.0625;
return celsius;
}
void printTemp(EthernetClient client) {
float celsius = getTemperature();
float fahrenheit = (1.8 * celsius) + 32;
client.print("Celsius: ");
client.print(celsius);
client.print("\tFahrenheit: ");
client.println(fahrenheit);
client.println("
");
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
String getDate() {
byte zero = 0x00;
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
String now;
now += year;
now += "-";
now += month;
now += "-";
now += monthDay;
now += " ";
now += hour;
now += ":";
now += minute;
now += ":";
now += second;
return now;
}
void printDate(EthernetClient client) {
String now = getDate();
client.print(now);
client.print("
");
}