Go Down

Topic: Mega with wiznet shield (wire hack)+thingspeak+sd problem (Read 5923 times) previous topic - next topic


Are these correct?

No. Once all the SPI slave selects are set HIGH in setup, they no longer need to be manipulated. The libraries do that.

Code: [Select]
// in setup()

//in loop() for example. Only the w5100 SPI will be selected.
// all other slave selects are HIGH (disabled)

if(!getPage(server,pageAdd)) Serial.print("Fail ");
    else Serial.print("Pass ");


is that working for older types of Ethernet shields without the ICSP connection too?


is that working for older types of Ethernet shields without the ICSP connection too?

The SPI works like that with all shields, but the SD is a different pin than the new cards. On the new shields, the SD slave select is D4.

The ICSP connector in the new shields handle only SCLK, MISO, and MOSI lines. The slave selects for those devices remain on D10 and D4.


my last idea was, maybe SD and Ethernet tried to communicate at the same time...tha's why I thought they should be switched enabled and disabled...:(


That is not it. The ethernet and SD libraries use the same type code. Both use something like this to transfer:
Code: [Select]

Both leave the slave select HIGH (disabled).

It is adding the OneWire.h file that causes the crash, correct?


Nov 26, 2012, 07:19 pm Last Edit: Nov 26, 2012, 08:22 pm by vespapierre Reason: 1
yes, but before adding onewire library it was unstable. for the first attempt log write failed, and then the second was ok.
I have found on the forum that w5100 has some reset bug...maybe this can be a problem...just trying to get more information...

update: currently what I can't understand...there is 3 ds18b20  one is on the wiznet board, other 2 is on wire...after a while i have only get values in every 30 minutes from the first sensor! other in every 30 seconds...???

update: if wiznet reset pin is output and high in the setup and in the beginning of the loop, the log file is much bigger...unfortunately there are emtpy lines too in the log...


Nov 28, 2012, 06:38 pm Last Edit: Nov 28, 2012, 06:42 pm by vespapierre Reason: 1
I have tried another code:
Code: [Select]

#include <Wire.h>
#include <OneWire.h>
#include <Rtc_Pcf8563.h>
#include <SD.h>
#define loByte (byte)lowByte
#define hiByte (byte)highByte

#define ERROR_LED 4
#define ERROR_RTC_GET 2 // Unable to get RTC time and date

//init the real time clock
Rtc_Pcf8563 rtc;
OneWire  ds(3);  // Thermometer on pin 3

// Global variables
byte result;
const int period = 20000;
void SetError(int);
const int chipSelect = 8;
//char sWholetempbuf[5];
//char sFracttempbuf[3];

void setup()
 char message[45]="";
 pinMode(ERROR_LED, OUTPUT); // Set error LED
pinMode(8, OUTPUT);
  if (!SD.begin(chipSelect))
   Serial.println(F("Card failed, or not present"));
  // don't do anything more:
 Serial.println(F("Card initialized."));
 digitalWrite(ERROR_LED, HIGH);
 digitalWrite(ERROR_LED, LOW);
 digitalWrite(ERROR_LED, HIGH);
 digitalWrite(ERROR_LED, LOW);
 File logFile = SD.open("errorlog.txt", FILE_WRITE);
 if (logFile)
   strcpy(message, rtc.formatDate(RTCC_DATE_ASIA));
   strcat(message, " ");
   strcat(message, rtc.formatTime());
   logFile.println("Program start: ");
   Serial.println(F("Program started: "));
 // if the file isn't open, pop up an error:
   Serial.println(F("error opening errorlog.txt"));

void loop()

  // make a string for assembling the data to log:
 char dataString[30] = "";
// char sWhole[5] = "";
// char sFract[3] = "";
 char sDate[11] = "";
 char sTime[9] ="";
 int SignBit, Tc_100, Whole, Fract;

 int Temperature = getTemperature();

 SignBit = Temperature & 0x8000;  // test most sig bit
 if (SignBit) // negative
   Temperature = (Temperature ^ 0xffff) + 1; // 2's comp
 Tc_100 = (Temperature*100/2);  
 Whole = Tc_100 / 100;  // separate off the whole and fractional portions
 Fract = Tc_100 % 100;
 if (SignBit) // If its negative
   if (Fract < 10)

//   sWhole=String(Whole);    
//   sFract=String(Fract);
   strcpy(sTime, rtc.formatTime());               //read formatted time from i2C RTC
   strcpy(sDate, rtc.formatDate(RTCC_DATE_ASIA)); //read formatted date from 12C RTC
//   strcpy(sWhole, Whole);
//   strcpy(sFract, Fract);

 Serial.print(F(" "));
strcpy(dataString, itoa(Whole,sWholetempbuf,10));  
strcat(dataString, ".");
strcat(dataString, itoa(Fract,sFracttempbuf,10));
strcat(dataString, ";");
strcat(dataString, sDate);
strcat(dataString, ";");
strcat(dataString, sTime);
strcat(dataString, ";");

//dataString += String(sWhole + "." + sFract + ";" + sDate + ";" + sTime + ";");
 File dataFile = SD.open("datalog.txt", FILE_WRITE);
 if (dataFile)
    digitalWrite(ERROR_LED, HIGH);
      digitalWrite(ERROR_LED, LOW);
   // print to the serial port too:
 // if the file isn't open, pop up an error:
   Serial.println(F("error opening datalog.txt"));

void SetError(int error) // Blinks forever the error led a number of times corresponding to error number
 while(1) // Forever
   for (byte index = 0; index < error; index++)
     digitalWrite(ERROR_LED, HIGH);
     digitalWrite(ERROR_LED, LOW);

int getTemperature() //gets the actual Temperature from 1-wire interface
 byte i;
 byte present = 0;
 byte data[12];
 byte addr[8];
 int HighByte, LowByte, TReading;
//  if ( !ds.search(addr))
//  {
////  Serial.print(F("No more addresses.\n"));
//    ds.reset_search();
//    delay(250);
//    return 0;
//  }

 ds.write(0x44,1);         // start conversion, with parasite power on at the end
 delay(900);     // maybe 750ms is enough, maybe not
 // we might do a ds.depower() here, but the reset will take care of it.
 present = ds.reset();
 ds.write(0xBE);         // Read Scratchpad
 for ( i = 0; i < 9; i++)
  {           // we need 9 bytes
   data[i] = ds.read();
//    Serial.print(data[i], HEX);
//    Serial.print(" ");
 LowByte = data[0];
 HighByte = data[1];
 return TReading = (HighByte << 8) + LowByte;

Now with one ds18b20 sd writing is working fine. I would like to solve to put 2 or 3 more ds18b20 to onewire and put the values to the logfile.
Can you help me how to do this?
After can come the upload to the web...

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131