Go Down

Topic: Mega with wiznet shield (wire hack)+thingspeak+sd problem (Read 5 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..

Go Up