Go Down

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

vespapierre

Hi,

I'm using Arduino 1.0.1 with mega 2560 and wiznet shield(not official). I wrote the sketch to upload temp data to thingspeak and beside I would like to log the data to the SD card. And have problems... :)
1. I cannot set the date and time with the example sketch, serial monitor said nothing...
2. Sketch is running, but sending strange values to the serial monitor
3. Message appears cannot open datalog.txt ( it seems data are going into the textfile
4. Uploading is working however i cannot see the messega in the serial monitor
you can find the scetch attached.
string in datalog like : 19@5-25-45;45:85:85;

I'm new in programming Arduino, please help me. Thanks a lot!


PaulS

#1
Nov 15, 2012, 10:59 pm Last Edit: Nov 15, 2012, 11:02 pm by PaulS Reason: 1
Quote
1. I cannot set the date and time with the example sketch, serial monitor said nothing...

Nor did you say anything about what example you are referring to or what you are trying to set the date and time on.

Quote
2. Sketch is running, but sending strange values to the serial monitor

But, your not going to show us the serial monitor output or confirm that the baud rate setting is correct.

Quote
3. Message appears cannot open datalog.txt ( it seems data are going into the textfile

If it can't open the file, it can't write to it, so you shouldn't even be trying.

Virtually none of the stuff you are doing with the String class is necessary. Get rid of the String class.

Tools + Auto Format needs to be used before you post code again.

vespapierre

#2
Nov 16, 2012, 07:26 am Last Edit: Nov 16, 2012, 08:29 am by vespapierre Reason: 1
Hi Paul,
sorry I will attach the codes! I have just found out that I2C pins are not on the same position on mega and duemilanove...first I have to wire out it from wiznet to mega in the right position.
I will update soon.
Thanks

update: back a few steps...attached the two sketch (temp monitor with not hard coded ds18b20 -s to thingspeak, and the sketch for sd writing). Noted that:
- I have to make the wire out for I2C
- Have to take care when I'm using SD and when I'm using wiznet
I would like to use these codes as one...

vespapierre

I have checked the code! At the start it is wokring fine, sending the first value to thingspeak and starting to write the sd...
Later(maybe 1-2 minutes) I get back the error code: cannot open datalog.txt, and havent received the second values at thingspeak... and seems in datalog.txt  like loosing the first character at the string:
"4;16;13;2012-11-17;10:22:35;
4;16;13;2012-11-17;10:22:41;
4;16;13;2012-11-17;10:22:47;"

serial at the start: "Getting temperatures...

Temperatures are 14 Celsius, 14 Celsius, 13 Celsius,

10:20:34 2012-11-17
14;14;13;2012-11-17;10:20:34;
"

later:
"SGetting temperatures...

Temperatures are 14 Celsius, 18 Celsius, 15 Celsius,

10:25:29 2012-11-17
cannot open datalog.txt
aGetting temperatures..."

Help me please to find the problem!
Thanks!

wildbill

You're using String a lot. It has issues with a bug in the underlying memory allocation routines it uses. The symptom you have that your sketch works for a while and then fails suggests that this may be your problem. Try using C strings (arrays of char) instead.

vespapierre

I have changed the strings...
now I haven't got error message, but there is only 10 rows in the log file...all the temperature values are doubled, and there is only one sending data to thingspeak...:(
new sketch in the txt

PaulS

Code: [Select]
  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("Program start: ");
    Serial.println(message);
  } 

Why? Are you scared of writing to the file? Quit screwing around with the data and just write it!

Code: [Select]
  if (logFile)
  {
    logFile.println("Program start: ");
    logFile.print(rtc.formatDate(RTCC_DATE_ASIA));
    logFile.print(" ");
    logFile.println(rtc.formatTime());
    logFile.close();
  }


There is no need to format a pretty string. It gets written one byte at a time, anyway.

The same thing applies later in your code where you formulate a string with the date and time to write to the file. Quit wasting resources.

Create some functions. All that code in setup() is ridiculous. Ditto for loop().

vespapierre

I have made the changes regarding the logfile writing. I have commented out the pinmode commands...
seems something mistakes still the result is the same. Just one sending to thingspeak, and the errorlog seems:
Program start:
2012-11-18 18:38:33
Program start:
2012-11-18 18:38:47
Program start:
2012-11-18 18:47:42

Paul, thanks for helping, I1m new to programming Arduino as I wrote at the beginning, that's why the code as you said ridiculous.

vespapierre

I have tried to create another sketch using pachube and SD card...Problem is still alive...I mean I have empty rows in tghe datalog file, and it seems there are more progstart -s in the errorlog file.
sketch can be found here:
http://www.retrobogo.hu/temp/ts_sd_problem.txt

Any help would be appreciated. Thanks

vespapierre

I am a bit confused about the pin modes...
wiznet is 10
sd is 8
DS18 onewire sensors is 3
I have read because of bug, after Ethernet.begin()  10 should pe put to high.
regarding the last code, to where ( and which ) should I insert the pinmode()-s and digitalwrite()-s?

Thanks

SurferTim

I don't know about the code you are using. It seems it is running out of memory or crashing because of some variable array overflow.

I have proven code that works well for me. Here is my client code in the playground.
http://arduino.cc/playground/Code/WebClient

Modify that code with this to start the SD.
Code: [Select]
// In setup, change these to pin 8
// disable SD SPI
pinMode(8,OUTPUT);
digitalWrite(8,HIGH);

// And after this
Serial.println(Ethernet.localIP());

// put this to start the SD
Serial.print("Starting SD...");
if(!SD.begin(8)) Serial.println("failed");
else Serial.print("ok");

You should be able to modify that code to suit your purpose.

vespapierre

Tim,

thank you! I will put this ( with modifications) to my sketch to send data to thingspeak and inform you!

SurferTim

I would work on the sending data to the server and read a response until it works flawlessly. Then add the SD code to save the data.

NickPyner

I am in the throes of converting to Mega 2560 at the moment and modifying my proto boards. The cosm feed I have at the moment does not use the SD or a clock since it only needs them for backup when in a remote location.

Here is what I'm working on - untested. This is for Uno and is simply the cosm stuff I had added to a previous sketch that worked fine. 

Note that I don't use strings for the serial and SD feeds, hence the slightly bloated code, but it works.

Mega pins are different but it should not affect the code.

I have had serial print stuff looking like yours and it was down to sending it too fast but the code you sent me had the serial at 9600 so maybe you need a couple of delays in it. The 85 could be the DS18B20s being read too soon.

Code: [Select]
/*
From cosm library example and lifts from a lot of others
particularly from Stanley in Kuala Lumpur.
Use your own DS18B20 addresses, keys etc.

*/
//#include <LiquidCrystal.h>
#include <SD.h>
#include <string.h>
#include "RTClib.h"

RTC_DS1307 RTC;
File myFile;
char filename[] = "00000000.CSV";
#include "Wire.h"
#define DS1307_ADDRESS 0x68

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>

byte InThermo[8] =  {
  0x28, 0x69, 0xC2, 0xB0, 0x03, 0x00, 0x00, 0X9F};
byte OutThermo[8] = {
  0x28, 0x7A, 0x8B, 0xC0, 0x03, 0x00, 0x00, 0x2F};
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

char cosmKey[] = "l6absmJ.................U0t0Zz0g";

int sensorPin = 3;
int  second, minute, hour, weekDay, monthDay, month, year;
float InTemp, OutTemp;

// Define the strings for our datastream IDs
char sensorId0[] = "InThermo";
char sensorId1[] = "OutThermo";
//char sensorId2{} = "DrainThermo"

const int bufferSize = 100;
char bufferValue[bufferSize]; // enough space to store the string we're going to send

CosmDatastream datastreams[] = {
  CosmDatastream(sensorId0, strlen(sensorId0), DATASTREAM_FLOAT),
  CosmDatastream(sensorId1, strlen(sensorId1), DATASTREAM_FLOAT),
  //    CosmDatastream(sensorId2, strlen(sensorId2), DATASTREAM_FLOAT),
};

// Finally, wrap the datastreams into a feed
CosmFeed feed(83153, datastreams, 2 /*put your number here */);

EthernetClient client;
CosmClient cosmclient(client);

//LiquidCrystal lcd(8,9,56,5,6,7);

void setup() {

  Wire.begin();
  Serial.begin(9600);
  delay(300);//Wait for newly restarted system to stabilize
  Serial.print("Temperature measurement");
  Serial.print("Initializing SD card...");

  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }

  Serial.println("initialization done.");

  sensors.setResolution(InThermo, 12);
  sensors.setResolution(OutThermo, 12);

  Serial.println("Starting multiple datastream upload to Cosm...");
  Serial.println();

  while (Ethernet.begin(mac) != 1)
  {
    Serial.println("Error getting IP address via DHCP, trying again...");
    delay(10000);
  }
}

void loop() {

  GetClock();
  PrintDate;

  sensors.requestTemperatures();
  Serial.println("Read sensor value ");

  InTemp = (sensorValue(InThermo));
  OutTemp = (sensorValue(OutThermo)); 
  // float Drain = (sensorValue(DrainThermo)); 

  datastreams[0].setFloat(InTemp);
  datastreams[1].setFloat(OutTemp);

  Serial.println(datastreams[0]);
  Serial.println(datastreams[1]);
  /*
  datastreams[2].setFloat(DrainTemp);
   Serial.println(datastreams[2].getFloat(DrainTemp));
   */
  // LCD stuff will go here

  if (second == 0 || second == 10 || second == 20 || second == 30 || second == 40 || second == 50)
  {
    myFile = SD.open("data.csv", FILE_WRITE);//<<<<<<<<<<<<< OPEN
    WriteSD;
    int ret=0;
    delay(200);// Delay... must not be too short.
    Serial.println("Uploading it to Cosm");
    ret = cosmclient.put(feed, cosmKey);    // SEND FEED TO COSM
    Serial.print("cosmclient.put returned "); // tell us about it
    Serial.println(ret);                      // pray to God it says 200
    myFile.println();
    myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE
  }
    delay(200);// Delay... must not be too short.
    Serial.println();
    delay(800);
}

//sensorValue function
float sensorValue (byte deviceAddress[])
{
  float tempC = sensors.getTempC (deviceAddress);
  return tempC;
}


byte bcdToDec(byte val)  {
  // Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void GetClock(){
  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 7);

  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  monthDay = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
}

void PrintDate () {
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(month);
  Serial.print("/");
  Serial.print(year);
  Serial.print("     ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");

  if (second < 10)
  {
    Serial.print("0");
  };

  Serial.print(second);
  Serial.print("         ");
}

void WriteSD () {

  myFile.print(monthDay);
  myFile.print("/");
  myFile.print(month);
  myFile.print("/");
  myFile.print(year);
  myFile.print(",  ");
  myFile.print(hour);
  myFile.print(":");
  myFile.print(minute);
  myFile.print(":");
  myFile.print(second);
  myFile.print(",         ");

  myFile.print(InTemp);
  myFile.print(",    ");
  myFile.print(OutTemp);
  myFile.print(",    ");
}

vespapierre

Thanks for your replies, I will take a deep breath and create the new sketches.

Go Up