Go Down

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

SurferTim

Quote
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()
pinmode(8,OUTPUT);
pinmode(10,OUTPUT);
pinmode(3,OUTPUT);
digitalwrite(10,HIGH);
digitalwrite(8,HIGH);
digitalwrite(3,HIGH);

//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 ");


vespapierre

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

SurferTim


Tim,
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.

vespapierre

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...:(

SurferTim

That is not it. The ethernet and SD libraries use the same type code. Both use something like this to transfer:
Code: [Select]
digitalWrite(SS,LOW);
SPI.transfer(0x00);
SPI.transfer(0x00);
digitalWrite(SS,HIGH);

Both leave the slave select HIGH (disabled).

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

vespapierre

#80
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...

vespapierre

#81
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
 
 Serial.begin(9600);
 Wire.begin();
 
pinMode(8, OUTPUT);
  if (!SD.begin(chipSelect))
   {
   Serial.println(F("Card failed, or not present"));
  // don't do anything more:
   return;
   }
   
 Serial.println(F("Card initialized."));
 digitalWrite(ERROR_LED, HIGH);
 delay(150);
 digitalWrite(ERROR_LED, LOW);
 delay(150);
 digitalWrite(ERROR_LED, HIGH);
 delay(150);
 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: ");
   logFile.println(message);
   logFile.close();
   Serial.println(F("Program started: "));
   Serial.println(message);
 }  
 // if the file isn't open, pop up an error:
 else
 {
   Serial.println(F("error opening errorlog.txt"));
 }
}


void loop()
{

 delay(period);  
 
  // 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
 {
    Serial.print(F("-"));
      }
 
 Serial.print(Whole);
   
 Serial.print(F("."));
   if (Fract < 10)
 {
    Serial.print(F("0"));
      }
 Serial.print(Fract);
 Serial.print(F("Celsius"));
 Serial.print(F("\n"));
 
 
 
//-------------------------------------------------------------  


//   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(sTime);
 Serial.print(F(" "));
 Serial.print(sDate);
 Serial.println();
   
 
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 + ";");
   
   delay(100);
   
 File dataFile = SD.open("datalog.txt", FILE_WRITE);
 if (dataFile)
 {
   dataFile.println(dataString);
   dataFile.close();
    digitalWrite(ERROR_LED, HIGH);
     delay(500);
      digitalWrite(ERROR_LED, LOW);
     //delay(500);
   // print to the serial port too:
   Serial.println(dataString);
 }  
 // if the file isn't open, pop up an error:
 else
 {
   Serial.println(F("error opening datalog.txt"));
 }
} // END LOOP
//**************************************************************************************

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);
     delay(500);
     digitalWrite(ERROR_LED, LOW);
     delay(500);
   }
   delay(1000);
 }
}

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.search(addr);
 ds.reset();
 ds.select(addr);
 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.select(addr);    
 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