Go Down

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

SurferTim

#45
Nov 24, 2012, 02:51 pm Last Edit: Nov 24, 2012, 03:04 pm by SurferTim Reason: 1
Good! In your code now, add only this:
Code: [Select]
   // after this in loop...
   if(!getPage(server,pageAdd)) Serial.print("Fail ");
   else Serial.print("Pass ");

   // add these two lines
   if(!logFile(1,2,3)) Serial.println("Log fail");
   else Serial.println("Log ok");

Does it still work?

edit: The log results should be in file "test.txt". You can change that later. I wanted to insure nothing was wrong with that file, so I used a new name.

vespapierre

so adding those lines to the code looks like this:
Code: [Select]

#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=RNO8YHD647QKX5P2&field1=200&field2=200&field3=200");

    if(!getPage(server,pageAdd)) Serial.print("Fail ");
    else Serial.print("Pass ");
    if(!logFile(1,2,3)) Serial.println("Log fail");
    else Serial.println("Log ok");
   
    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;
}


at first: pass open fail log fail!  at the second time in the log : pass log ok.

vespapierre

and logfile looks like:

Field1=1,Field2=2,Field3=3

Field1=1,Field2=2,Field3=3
Field1=1,Field2=2,Field3=3
Field1=1,Field2=2,Field3=3



SurferTim

Better! Add this at the top:
Code: [Select]
#include <OneWire.h>
#include <DallasTemperature.h>

Does it still run?

vespapierre

ok I am adding it, but there were empty lines in the logg as you can see. ok just a few seconds..

PaulS

Just adding the include files, without actually instancing (and using the instance) the class won't tell you anything. The compiler will optimize them away as useless.

vespapierre

#51
Nov 24, 2012, 03:19 pm Last Edit: Nov 24, 2012, 03:21 pm by vespapierre Reason: 1
ok it is not running...log is:
Starting ethernet...
192.168.9.108
Starting SD...ok Ready
connecting...connected
HTTP/1.1 200 OK
Server: nginx/0.8.53
Date: Sat, 24 Nov 2012 14:18:58 GMT
Content-Type: text/html; charset=utf-8
Connection: close
Vary: Accept-Encoding
Status: 200
ETag: "cc06a6150b92e17dd3076a0f0f9d2af4"
Cache-Control: max-age=0, private, must-revalidate

6978
disconnecting.
Pass Open fail
Log fail
1



return code is 200 so it is sent to the server but after it is failed

SurferTim

Remove the new includes you just added. Upload the code and then power down the Arduino for a few seconds. Try it again. Does it fail?


vespapierre

no, it does not failed after removing the two includes. Pass log ok.
logfile:
"Field1=1,Field2=2,Field3=3
Field1=1,Field2=2,Field3=3

"

SurferTim

OK. Add the new includes one at a time. Try it after each add. Which include causes the fail?

vespapierre

after adding onewire.h

it is failed.

after adding just dallastemp  there was an error during compile...seems dallas needs onewire

wildbill

If you are indeed short of SRAM, you can benefit from the usual recommendation to use the F macro to wrap all your constant strings - you have rather a lot of them, particularly in your serial.prints

SurferTim

Which pin is the OneWire pin? That is the one with the Dallas temp sensor, correct?


vespapierre

wildbill, i should learn a lot I know but I don't know much about F macros. I gues mega should not run out of SRAM with this...

Tim,
yes,

this is pin 3.(digital)

SurferTim

OK, add just this. We'll try initializing the OneWire.
Code: [Select]
#include <OneWire.h>

// DS Temperature chip i/o
OneWire ds(3);  // on pin 3

Does that fail also?

Go Up