Pages: 1 2 [3] 4 5 6   Go Down
Author Topic: Mega with wiznet shield (wire hack)+thingspeak+sd problem  (Read 3755 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 161
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

problems coming... test.txt created manually, but have errors.

Temperatures are 29 Celsius, 0 Celsius, 0 Celsius,

connecting...failed
Fail Open fail
Log failed
1
« Last Edit: November 21, 2012, 03:59:30 pm by vespapierre » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5833
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post your current loop() function. That may help me troubleshoot it.
edit: Power it down for a few seconds. Try it again.
« Last Edit: November 21, 2012, 04:11:46 pm by SurferTim » Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 161
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

powerdown not helped.
Code:

void loop()
{
  if(loopCount < 30)
  {
    // if loopCount is less than 30, just delay a second
    delay(1000);
  }
  else
  {
    // every thirty seconds this runs
    loopCount = 0;

    // Modify next line to load different page
    // or pass values to server
//get temperatures
Serial.print("Getting temperatures...\n\r");
    sensors.requestTemperatures();            //issue global request for each temp sensor on network to return a temp
       if(sensors.getAddress(tempDeviceAddress, 0))
          temp0=(sensors.getTempC(tempDeviceAddress)); //read temp from sensor 0
       if(sensors.getAddress(tempDeviceAddress, 1))
          temp1=(sensors.getTempC(tempDeviceAddress)); //read temp from sensor 1
       if(sensors.getAddress(tempDeviceAddress, 2))
         temp2=(sensors.getTempC(tempDeviceAddress));  //read temp from sensor 2  
  
    Serial.print("Temperatures are ");
    Serial.print(temp0);
    Serial.print(" Celsius, ");
    Serial.print(temp1);
    Serial.print(" Celsius, ");
    Serial.print(temp2);
    Serial.print(" Celsius, ");
    Serial.print("\n\r");
    
   sprintf(pageAdd,"/update?key=XXXXXXXXXXXXXXXXXXXX&field1=%u&field2=%u&field3=%u",temp0,temp1,temp2);
 // sprintf(pageAdd,"/",totalCount);

    // sprintf(pageAdd,"/arduino.php?test=%u",totalCount);

    if(!getPage(server,pageAdd)) Serial.print("Fail ");
    else Serial.print("Pass ");
    if(!logFile(temp0,temp1,temp2)) Serial.println("Log failed");
    else Serial.println("Log ok");
    
    totalCount++;
    Serial.println(totalCount,DEC);
  }    

  loopCount++;
}
int logFile(int temp0, int temp1, int temp2) {
  File fh = SD.open("test.txt",FILE_WRITE);

  if(!fh) {
    Serial.println("Open fail");
    return 0;
  }

  // Use the same character array here to send stuff to the file.
  // pageAdd array is global
  sprintf(pageAdd,"Field1=%u Field2=%u Field3=%u",temp0,temp1,temp2);
  fh.println(pageAdd);
  fh.close();
  return 1;
}





byte getPage(IPAddress ipBuf,char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print("connecting...");

  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");

    sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
    client.write(outBuf);
  }
  else
  {
    Serial.println("failed");
    return 0;
  }

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println("Timeout");
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

  Serial.println("disconnecting.");
  // close client end
  client.stop();

  return 1;
}

UPDATE: strange...without the logfile modification, it is not connecting...that was working fine before...maybe thingspeak issue...
« Last Edit: November 21, 2012, 04:26:48 pm by vespapierre » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5833
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Back up then. Remove the call to logFile and the logFile function. Does it work then?
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 161
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

backed up... connection failed...tried to ping the server..got back answer after ping
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5833
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Thingspeak should not affect whether the SD writes or not. If the connection failed, and the SD write failed, something is not right. Are you certain that pageAdd array is big enough to handle all that?
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 161
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

after router restart :

Found device 1 with address: 286C81C5030000FA
Found device 2 with address: 2811E8E803000027
Getting temperatures...

Temperatures are 16 Celsius, 8 Celsius, 6 Celsius,

connecting...connected
HTTP/1.1 200 OK
Server: nginx/0.8.53
Date: Wed, 21 Nov 2012 21:49:10 GMT
Content-Type: text/html; charset=utf-8
Connection: close
Vary: Accept-Encoding
Status: 200
ETag: "1ff1de774005f8da13f42943881c655f"
Cache-Control: max-age=0, private, must-revalidate

24
disconnecting.
Pass Open fail
Log failed
1

this is with the SD version

without SD it is fine.

UPDATE: in the txt I can find the values!!! but failed message appears on serial monitor
« Last Edit: November 21, 2012, 04:56:54 pm by vespapierre » Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 161
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

let's have a rest... smiley-red
again thanks for your help/support Tim!
I'll back to here tomorrow...(meanwhile trying to figure out, how to put date,time and ";" values to the log.it was working alone in a previous sketch what can be found in Reply #23)
If you could help me in that too I would be more than happy!:) I have learned a lot today.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 161
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Strange that I have only found 1 line in the test.txt when the program started...I have checked this after a day...(????)
Now I tried to put to the code this one:
Code:
#include <Rtc_Pcf8563.h>

//init the real time clock
Rtc_Pcf8563 rtc;


loop()

char sdate[11] = "";
char stime[9] ="";

strcpy(stime, rtc.formatTime());
strcpy(sdate, rtc.formatDate(RTCC_DATE_ASIA));

Serial.print(stime);
Serial.print(" ");
Serial.print(sdate);
Serial.println();

//***************log cuccos

if(!logFile(sdate,stime,temp0,temp1,temp2)) Serial.println("Log failed");
    else Serial.println("Log ok");

}
int logFile(int sdate, int stime, int temp0, int temp1, int temp2) {
  File fh = SD.open("test.txt",FILE_WRITE);

  if(!fh) {
    Serial.println("Open fail");
    return 0;
  }

  // Use the same character array here to send stuff to the file.
  // pageAdd array is global
  sprintf(pageAdd,"Date=%u Time=%u Field1=%u Field2=%u Field3=%u",sdate,stime,temp0,temp1,temp2);
  fh.println(pageAdd);
  fh.close();
  return 1;
}

but seems this is not a good idea...problems having converting sdate and stime... those are char and it should be int

UPDATE: the first writing attempt to log file is failing....than the second one is fine! at this moment it is running with usb cable and serial monitor. The previous test was from external supply with 12V..._(when I have found only 1 line in the log)

Tim have you got any idea?




« Last Edit: November 24, 2012, 06:13:11 am by vespapierre » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5833
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That last code has problems. It is not complete. You should post all the code.

edit: This is wrong in the code you posted:
Code:
// sdate and stime are character arrays here
char sdate[11] = "";
char stime[9] ="";

// then later you pass the character array pointer to logFile
if(!logFile(sdate,stime,temp0,temp1,temp2)) Serial.println("Log failed");
    else Serial.println("Log ok");

// but logFile has sdate and stime as int type. ??
int logFile(int sdate, int stime, int temp0, int temp1, int temp2) {

This is how logFile should look:
Code:
int logFile(char* sdate, char* stime, int temp0, int temp1, int temp2) {
  File fh = SD.open("test.txt",FILE_WRITE);

  if(!fh) {
    Serial.println("Open fail");
    return 0;
  }

  // Use the same character array here to send stuff to the file.
  // pageAdd array is global
  sprintf(pageAdd,"Date=%s Time=%s Field1=%u Field2=%u Field3=%u",sdate,stime,temp0,temp1,temp2);
  fh.println(pageAdd);
  fh.close();
  return 1;
}

Insure you have enough memory allocated for pageAdd. If it is still 32 characters, you are overflowing the pageAdd array.
« Last Edit: November 24, 2012, 07:52:59 am by SurferTim » Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 161
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Tim,

this is the full code, without the date and time...sometimes it is put empty lines to log, or stop the log, however serial sais log ok.

Code:
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>

byte mac[] = { 
  0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };

// change to your network settings
IPAddress ip(192,168,9,110);
IPAddress gateway(192, 168, 9, 1);
IPAddress subnet(255, 255, 255, 0);

// change to your server
IPAddress server(184,106,153,149);

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

//Variables

EthernetClient client;
int totalCount = 0;
int loopCount = 0;
char pageAdd[140];
int temp0;                                   //temperature 1
int temp1;                                  //temperature 2
int temp2;                                  //temperature 3
int numberOfDevices;                          // Number of temperature devices found
DeviceAddress tempDeviceAddress;             // We'll use this variable to store a found device address


// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

//**********SETUP***************
void setup() {
  Serial.begin(9600);

  // disable SD SPI
  pinMode(8,OUTPUT);
  digitalWrite(8,HIGH);

  // Start ethernet
  Serial.println("Starting ethernet...");
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  // If using dhcp, comment out the line above
  // and uncomment the next 2 lines

   //if(!Ethernet.begin(mac)) Serial.println("failed");
   //else Serial.println("ok");

  digitalWrite(10,HIGH);
  Serial.println(Ethernet.localIP());
  //start SD
  Serial.print("Starting SD...");
  if(!SD.begin(8)) Serial.println("failed");
  else Serial.print("ok ");
  delay(2000);
  Serial.println("Ready");
  //Start sensors
  sensors.begin();
  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();

  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  Serial.print(numberOfDevices, DEC);
  Serial.println(" devices.");

  // report parasite power requirements
  Serial.print("Parasite power is: ");
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");

  // Loop through each device, print out address
  for(int i=0;i<numberOfDevices; i++)
  {

    // Search the wire for address
    if(sensors.getAddress(tempDeviceAddress, i))
    {
      Serial.print("Found device ");
      Serial.print(i, DEC);
      Serial.print(" with address: ");
      printAddress(tempDeviceAddress);
      Serial.println();
      delay(100);
      // Get inital sensor readings to avoid 85 degrees
      sensors.requestTemperatures();            //issue global request for each temp sensor on network to return a temp
      if(sensors.getAddress(tempDeviceAddress, 0))
        temp0=(sensors.getTempC(tempDeviceAddress)); //read temp from sensor 0
      if(sensors.getAddress(tempDeviceAddress, 1))
        temp1=(sensors.getTempC(tempDeviceAddress)); //read temp from sensor 1
      if(sensors.getAddress(tempDeviceAddress, 2))
        temp2=(sensors.getTempC(tempDeviceAddress));  //read temp from sensor 2
}
  }
}

//****************LOOP****************

void loop()
{
  if(loopCount < 30)
  {
    // if loopCount is less than 30, just delay a second
    delay(1000);
  }
  else
  {
    // every thirty seconds this runs
    loopCount = 0;

    // Modify next line to load different page
    // or pass values to server
    //get temperatures
    Serial.print("Getting temperatures...\n\r");
    sensors.requestTemperatures();            //issue global request for each temp sensor on network to return a temp
    if(sensors.getAddress(tempDeviceAddress, 0))
      temp0=(sensors.getTempC(tempDeviceAddress)); //read temp from sensor 0
    if(sensors.getAddress(tempDeviceAddress, 1))
      temp1=(sensors.getTempC(tempDeviceAddress)); //read temp from sensor 1
    if(sensors.getAddress(tempDeviceAddress, 2))
      temp2=(sensors.getTempC(tempDeviceAddress));  //read temp from sensor 2   

    Serial.print("Temperatures are ");
    Serial.print(temp0);
    Serial.print(" Celsius, ");
    Serial.print(temp1);
    Serial.print(" Celsius, ");
    Serial.print(temp2);
    Serial.print(" Celsius, ");
    Serial.print("\n\r");

    sprintf(pageAdd,"/update?key=XXXXXXXXXXXXXX&field1=%u&field2=%u&field3=%u",temp0,temp1,temp2);
    // sprintf(pageAdd,"/",totalCount);

    // sprintf(pageAdd,"/arduino.php?test=%u",totalCount);

    if(!getPage(server,pageAdd)) Serial.print("Fail ");
    else Serial.print("Pass ");
    delay(500);
    if(!logFile(temp0,temp1,temp2)) Serial.println("Log failed");
    else Serial.println("Log ok");

    totalCount++;
    Serial.println(totalCount,DEC);
  }   

  loopCount++;
}
int logFile(int temp0, int temp1, int temp2) {
  File fh = SD.open("test.txt",FILE_WRITE);
  if(!fh) {
    Serial.println("Open fail");
    return 0;
  }

  // Use the same character array here to send stuff to the file.
  // pageAdd array is global
  sprintf(pageAdd,"Field1=%u,Field2=%u,Field3=%u",temp0,temp1,temp2);
  fh.println(pageAdd);
  fh.close();
  return 1;
}

byte getPage(IPAddress ipBuf,char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print("connecting...");

  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");

    sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
    client.write(outBuf);
  }
  else
  {
    Serial.println("failed");
    return 0;
  }

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println("Timeout");
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

  Serial.println("disconnecting.");
  // close client end
  client.stop();

  return 1;
}
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5833
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You may be running out of SRAM. The SD open function requires a lot of memory. If you run out of SRAM, the open function may return success, but if you try to read or write, that is when it seems to fail. I'll take a quick guess that it does not check if the memory is available, just if it can open the file on the SD card.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 161
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley-sad what can I do in this case? Should I take out some from the code? I have just followed your advices.

edit: maybe I should not use the function to find the onewire adresses automatically? but that is good function in case of a sensor change...
« Last Edit: November 24, 2012, 08:15:24 am by vespapierre » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5833
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are using a Mega, you should not be running out of SRAM unless something has gone wrong. It has 8K, compared to the Uno's 2K. I suspect maybe a buffer overflow is the cause.

Go back to the playground and upload that code again. Get it running. You had it running before. Change the network settings, the array size for pageAdd, and add "#include <SD.h>" at the top (edit: Change the pin for your SD card SS to the correct pin number in SD.begin). That's all!

Then add this to it:
Code:
int logFile(int temp0, int temp1, int temp2) {
  File fh = SD.open("test.txt",FILE_WRITE);
  if(!fh) {
    Serial.println("Open fail");
    return 0;
  }

  // Use the same character array here to send stuff to the file.
  // pageAdd array is global
  sprintf(pageAdd,"Field1=%u,Field2=%u,Field3=%u",temp0,temp1,temp2);
  fh.println(pageAdd);
  fh.close();
  return 1;
}
Don't call the the logFile routine in loop(). See if everything runs ok before you do.

Upload the code, then power down the Arduino for a few seconds, then power it up and open the serial monitor.
« Last Edit: November 24, 2012, 08:17:25 am by SurferTim » Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 161
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok. with this as you said running all ok:

Code:
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>

// this must be unique
byte mac[] = {  0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };

// change to your network settings
IPAddress ip(192,168,9,108);
IPAddress gateway(192, 168, 9, 1);
IPAddress subnet(255, 255, 255, 0);

// change to your server
IPAddress server(184,106,153,149);

EthernetClient client;
int totalCount = 0;
int loopCount = 0;
char pageAdd[200];

//char pageAdd[140];

void setup() {
  Serial.begin(9600);

  // disable SD SPI
  pinMode(8,OUTPUT);
  digitalWrite(8,HIGH);

  // Start ethernet
  Serial.println("Starting ethernet...");
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  // If using dhcp, comment out the line above
  // and uncomment the next 2 lines

  // if(!Ethernet.begin(mac)) Serial.println("failed");
  // else Serial.println("ok");
  digitalWrite(10,HIGH);

  Serial.println(Ethernet.localIP());
//start SD
Serial.print("Starting SD...");
if(!SD.begin(8)) Serial.println("failed");
else Serial.print("ok ");
  delay(2000);
  Serial.println("Ready");
  
}

void loop()
{
  if(loopCount < 30)
  {
    // if loopCount is less than 30, just delay a second
    delay(1000);
  }
  else
  {
    // every thirty seconds this runs
    loopCount = 0;

    // Modify next line to load different page
    // or pass values to server
    
  
   sprintf(pageAdd,"/update?key=XXXXXXXXXXXXXXX&field1=200&field2=200&field3=200");

    if(!getPage(server,pageAdd)) Serial.print("Fail ");
    else Serial.print("Pass ");
    totalCount++;
    Serial.println(totalCount,DEC);
  }    

  loopCount++;
}

byte getPage(IPAddress ipBuf,char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print("connecting...");

  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");

    sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
    client.write(outBuf);
  }
  else
  {
    Serial.println("failed");
    return 0;
  }

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println("Timeout");
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

  Serial.println("disconnecting.");
  // close client end
  client.stop();

  return 1;
}

int logFile(int temp0, int temp1, int temp2) {
  File fh = SD.open("test.txt",FILE_WRITE);
  if(!fh) {
    Serial.println("Open fail");
    return 0;
  }

  // Use the same character array here to send stuff to the file.
  // pageAdd array is global
  sprintf(pageAdd,"Field1=%u,Field2=%u,Field3=%u",temp0,temp1,temp2);
  fh.println(pageAdd);
  fh.close();
  return 1;
}


here there was mannually added temp values as 200. no temp reading from the sensors. and no writing to the logfile
Logged

Pages: 1 2 [3] 4 5 6   Go Up
Jump to: