Noob needs help.

The EtherTen combines an Uno-equivalent Arduino-compatible board and Wiznet-based Ethernet support, along with a microSD card slot and Power-over-Ethernet support. [Product page]
Post Reply
Belleye
Posts: 3
Joined: Sun Oct 07, 2012 10:47 am

Noob needs help.

Post by Belleye » Sun Oct 07, 2012 11:02 am

Hi All,

So I've done all the boring copy and paste projects now I'm trying to mash one together; I'm not sure what I'm doing wrong but I think I'm slowly starting to make things more complicated...

The aim of this code is to write values to twitter every hour and values to the SD card every 5 minutes later I want to add Prowl notifications.

I've tried to break everything down into sub routines but I think I'm getting a memory issue right after I fetch the seconds since 1900.

Any help would be greatly appreciated :)

Code: Select all

#include <SPI.h> 
#include <Ethernet.h> 
#include <EthernetUdp.h> 
#include <Twitter.h> 
#include <SD.h> 


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
unsigned int localPort = 8888;
IPAddress timeServer(192, 43, 244, 18);
const int NTP_PACKET_SIZE= 48; 
byte packetBuffer[ NTP_PACKET_SIZE];
EthernetUDP Udp;

long Myhour = 25;
Twitter twitter("Secret Stuff"); // twitter
String dataString;

void setup()
{  
  Serial.begin(9600);
  Serial.println("Connecting Nets");
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH); //disable SD card
  pinMode(10, OUTPUT);
  if (Ethernet.begin(mac) == 0) 
  {
    Serial.println("Failed to configure Ethernet using DHCP");
    return;
  }
  Udp.begin(localPort);

  // data logger
  digitalWrite(10, HIGH); //disable Nets
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) 
  {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  else
  {
    Serial.println("card initialized.");
  }

  pinMode(0, INPUT);
  pinMode(1, INPUT);
}

void loop()
{
  Serial.println("Getting time");
  long secsSince1900 = getSecs();
  if (secsSince1900 > 1)
  {         
    const unsigned long seventyYears = 2208988800UL;     
    dataString = "Secsince1900,";
    dataString += secsSince1900;
    dataString += (",Pin 0,");
    dataString += ((((analogRead(0) / 1023) * 5) * 100) - 273);
    dataString += (",Pin 1,");
    dataString += ((((analogRead(1) / 1023) * 5) * 100) - 273);

    if (Myhour != (((secsSince1900 - seventyYears)  % 86400L) / 3600))
    { 
      postTweet(dataString);
    }
    Myhour = (((secsSince1900 - seventyYears)  % 86400L) / 3600); // print the hour (86400 equals secs per day)
    Serial.println(Myhour);
  }
  Write2SD(dataString);
  Serial.println("wait....");
  delay(5900);  // add a zero later
}

long getSecs ()
{
  digitalWrite(4, HIGH); //disable SD card
  sendNTPpacket(timeServer);
  delay(1000);  
  Serial.println("Got time");
  unsigned long secsSince1900 = 0;
  if ( Udp.parsePacket() ) 
  {  
    Udp.read(packetBuffer,NTP_PACKET_SIZE);
    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);  
    secsSince1900 = highWord << 16 | lowWord;
  }
  else
  {
    unsigned long secsSince1900 = 0;
  }
  return (long) &secsSince1900;
}

void Write2SD(String dataString)
{
  //data logger
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH); //disable Nets
  File dataFile = SD.open("datalog.csv", FILE_WRITE);
  if (dataFile) // if the file is available, write to it:
  {
    dataFile.println(dataString);
    dataFile.close();
  }  
  else 
  {
    Serial.println("error opening datalog.csv");
  }
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH); //disable SD card
}
void postTweet(String tweet)
{
  char msg[125];
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH); //disable SD card
  String termTweet = tweet + "\0" ;// Terminate the tweet with a null
  Serial.print(termTweet);
  Serial.println(" - Terminated tweet");
  int twtlen = (termTweet.length()+3); // count the characters, add 3 just in case
  Serial.print(twtlen);
  Serial.println(" - Tweet length");
  termTweet.toCharArray(msg,twtlen); // Convert it to an array called msg
  Serial.print("Attempted tweet ");
  Serial.println(msg);
  //  tweet.toCharArray(msg,twtlen);
  Serial.println("connecting ...");
  if (twitter.post(msg)) {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200) {
      Serial.println("OK.");
      delay (120000); // Wait two minutes before doing anything after a tweet (don't spam!) 
    } 
    else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  } 
  else {
    Serial.println("connection failed.");
  }
}
unsigned long sendNTPpacket(IPAddress& address)
{
  memset(packetBuffer, 0, NTP_PACKET_SIZE); 
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  packetBuffer[12]  = 49; 
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;		   
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket(); 
}


Belleye
Posts: 3
Joined: Sun Oct 07, 2012 10:47 am

Re: Noob needs help.

Post by Belleye » Mon Oct 08, 2012 12:37 am

Maybe i can simplify my last post, what I'm trying to do is:

Code: Select all

Get time
*turn off internet & turn on SD*
Write to SD
*turn on internet & turn off SD*
maybe post twitter
loop
I can do one or the other but not the two together. I think the issue lies in pin 4 but I can't be sure.

Cheers.

ngp99
Posts: 49
Joined: Sat Jul 14, 2012 1:53 am
Location: Dee Why NSW

Re: Noob needs help.

Post by ngp99 » Tue Oct 09, 2012 12:15 am

Belleye wrote:Maybe i can simplify my last post,I can do one or the other but not the two together. I think the issue lies in pin 4 but I can't be sure.
I understand this is correct, and I'm having similar problems. The SD is slected by pin 4 and the ethernet by pin 10. They both use pins 11,12,13 but not at the same time. Indeed, I don't think it is strictly correct to say they share the pins for, when they actually use the pins, they require exclusive use of them.

viewtopic.php?f=4&t=1921

Belleye
Posts: 3
Joined: Sun Oct 07, 2012 10:47 am

Re: Noob needs help.

Post by Belleye » Tue Oct 09, 2012 12:02 pm

I have tried every possible combination of digitalWrite(pin, HIGH/LOW), still not progressing, it seems its ok to change which device (SD/Ethernet) accesses pin once but try again and things get corrupt?

I've run out of time I'll be able to have a play in about a week.

Post Reply