Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Convert Hex to exact Dec image on: March 01, 2013, 03:51:52 am
Quote
No, what you did was multiply the value by 16.

I read more about bitWise operators and found this "in fact, left shifting is the equivalent of multiplying by a power of two."

You were right. Moving 4 bits to the left is also equivalent to multiplying the value by 16.. smiley-lol

Anyway, what should I do if I need to change a hex value of 12 into a dec value of 12...
Or maybe a different approach..a char 1 and a char 2 into one single decimal variable of 12...

The algorithm I can only think about is doing it manually like
if(heading == 0x12){
decheading=12;
}

But I've got 0-360 HEX...and that means 360 if statements =.=
2  Using Arduino / Programming Questions / Re: Convert Hex to exact Dec image on: March 01, 2013, 03:31:10 am
Quote
the "<<" is multiplying by 16 (hexadecimal, remember), but you want decimal, so what value do you think you might want to multiply by?

I'm sorry sir AWOL but what do you mean by this?....what I did there is I moved heading 4 bits to the left so that I can use OR operations to it for the next HEX value to be recieved...like if the 1st data is 1,HEX which is equal to 0001 then I move it 4 bits to he left, it will become 0001 000 then OR it with the next value for example 2,HEX which is equal to 0010. Using OR operation on 0001 0000 and 0010. It will be 0001 0010 which is equal to 12 in HEX.

But what I want is instead of having 12 in HEX..I want to have a 12 in DEC but I can't think of an algorithm for doing so...
3  Using Arduino / Programming Questions / Convert Hex to exact Dec image on: March 01, 2013, 02:04:08 am
Good Day guys!

I've got an electronic compass that sends char data through serial connection.
For example if the degrees read is 250 it will send a char 2, then a char 5, then a char 0.
I was able to use this data by converting it to HEX with this code

Code:
int asc2dec (char c){
  if (c >= '0' && c <= '9')
    return c - '0' ;
  if (c >= 'A' && c <= 'F')
    return c - 'A' + 10 ;
  if (c >= 'a' && c <= 'f')
    return c - 'a' + 10 ;
}

and then combining it all together into one variable by moving 4 bits to the left and using OR logic like this
Code:
for(byte r= 1; r < 4; r++){
          heading <<= 4 ;
          heading |= asc2dec(EC_Data[r]);

so this code returns a hexadecimal value of the read degrees from the compass

Now what I want is instead of having a hex value, I want to a have a decimal value...Like if the compass sends a char of 250. I also want to have a decimal value of 250?

Any ideas on how to do this? THX!
4  Using Arduino / Programming Questions / Re: Wiegand RFID (attachinterrupt) + new functions on: February 20, 2013, 09:26:16 am
I already made it work! Thx PaulS for teaching me the debugging part using LEDs....i forgot doing that..lol
But anyway...how does a simple Serial.println messes up the value of my flagdone?
 
Here is the code that worked...I removed the wiegand counter option which changes the flagdone value
I hope by doing so I dont get wrong values in the long run xD

Code:
#define MAX_BITS 100                 // max number of bits

unsigned char databits[MAX_BITS];    // stores all of the data bits
unsigned char bitCount;              // number of bits currently captured

unsigned long facilityCode=0;        // decoded facility code
unsigned long cardCode=0;            // decoded card code

// interrupt that happens when INTO goes low (0 bit)
void ISR_INT0()
{
  digitalWrite(13,HIGH);
  bitCount++;
 
}

// interrupt that happens when INT1 goes low (1 bit)
void ISR_INT1()
{
  digitalWrite(13,LOW);
  databits[bitCount] = 1;
  bitCount++;
}

void setup()
{
  pinMode(13, OUTPUT);  // LED
  pinMode(2, INPUT);     // DATA0 (INT0)
  pinMode(3, INPUT);     // DATA1 (INT1)
 
  Serial.begin(9600);
  Serial.println("RFID Readers");
 
  // binds the ISR functions to the falling edge of INTO and INT1
  attachInterrupt(0, ISR_INT0, FALLING); 
  attachInterrupt(1, ISR_INT1, FALLING);
 

}

void loop(){
  Serial.println("aw");
  rfid();
}

void rfid()
{
 
  // if we have bits and we the weigand counter went out
  if (bitCount > 25) {
    unsigned char i;
    digitalWrite(13,LOW); 
    // we will decode the bits differently depending on how many bits we have
    // see www.pagemac.com/azure/data_formats.php for mor info
    if (bitCount == 35)
    {
      digitalWrite(13,LOW);
      // 35 bit HID Corporate 1000 format
      // facility code = bits 2 to 14
      for (i=2; i<14; i++)
      {
         facilityCode <<=1;
         facilityCode |= databits[i];
      }
     
      // card code = bits 15 to 34
      for (i=14; i<34; i++)
      {
         cardCode <<=1;
         cardCode |= databits[i];
      }
     
      printBits();
    }
    else if (bitCount == 26)
    {
      digitalWrite(13,LOW);
      // standard 26 bit format
      // facility code = bits 2 to 9
      for (i=1; i<9; i++)
      {
         facilityCode <<=1;
         facilityCode |= databits[i];
      }
     
      // card code = bits 10 to 23
      for (i=9; i<25; i++)
      {
         cardCode <<=1;
         cardCode |= databits[i];
      }
     
      printBits(); 
    }
    else {
      digitalWrite(13,LOW);
      delay(200);
      digitalWrite(13,HIGH);
     Serial.println("Unable to decode.");
    }

     // cleanup and get ready for the next card
     bitCount = 0;
     facilityCode = 0;
     cardCode = 0;
     for (i=0; i<MAX_BITS; i++)
     {
       databits[i] = 0;
     }
  }
}

void printBits()
{
      // I really hope you can figure out what this function does
      Serial.print("FC = ");
      Serial.print(facilityCode);
      Serial.print(", CC = ");
      Serial.println(cardCode);
}
5  Using Arduino / Programming Questions / Re: Wiegand RFID (attachinterrupt) + new functions on: February 20, 2013, 09:13:00 am
I found the problem..it seems that I can't even enter if (bitCount > 0 && flagDone)
because flagdone won't get 1.

What seems to be causing this?
6  Using Arduino / Programming Questions / Re: Wiegand RFID (attachinterrupt) + new functions on: February 20, 2013, 09:03:34 am
I also added more Serial.prints on rfid() an it prints them all out

I tried adding a digitalWrite LOW inside the if( bitcount==26) and also if(bitcount==35)
and the LED doesnt turn off?
So i guess these mean i'm not getting all the bits I need?

what can be a solution for this?


**EDIT

I found the problem..it seems that I can't even enter if (bitCount > 0 && flagDone)
because flagdone won't get 1.

What seems to be causing this?
7  Using Arduino / Programming Questions / Re: Wiegand RFID (attachinterrupt) + new functions on: February 20, 2013, 08:59:22 am
Quote
You could add some LEDs, and toggle them in the ISRs, to see if ISRs are being activated. You could add more Serial.print()s in rfid(), to see where it really gets to.

I tried what you said..I included a digitalwrite HIGH on isrint0 and a LOW on isrint1 and noticed that when I swipe the card 1st time the LED lit up and the 2nd time the LED blinks a little..so it seems the ISR are getting activated but still it doesn't print the card value in the serial monitor
8  Using Arduino / Programming Questions / Re: Wiegand RFID (attachinterrupt) + new functions on: February 20, 2013, 08:43:30 am
Quote
You're changing variables in an interrupt handler (like databits[], bitCount, flagDone, etc.) without declaring them as volatile. If you don't do that results may be unpredictable as the compiler is free to optimize in a way that may change the functionality.

I haven't notice any unpredictable results before...but I guess no harm will be done by declaring them as volatile so I'll add them anyway...thx!

Quote
Those Serial.print()s, even though they are commented out, do not belong in the interrupt service routines. Get rid of them.

What have you added to loop()? What happened when you did that? What serial output do you see?

I simply add a Serial.println("test"); on the loop just to test If the reading of RFID will work with other functions.
( If this worked, I'm going to add additional functions such as reading compasses with software serial..and also some codes for ethernet)

By adding it the output was purely

test
test
test
test

even though i swiped a card already.
My expected output was

test
test
test
FC=99999, CC=000000  (this will appear if i swipe a card)
test
test
test

something like this
9  Using Arduino / Programming Questions / Wiegand RFID (attachinterrupt) + new functions on: February 20, 2013, 08:26:50 am
Good Day Guys!

I am trying to read a wiegand RFID with the use of this code
Code:
#define MAX_BITS 100                 // max number of bits
#define WEIGAND_WAIT_TIME  3000      // time to wait for another weigand pulse. 

unsigned char databits[MAX_BITS];    // stores all of the data bits
unsigned char bitCount;              // number of bits currently captured
unsigned char flagDone;              // goes low when data is currently being captured
unsigned int weigand_counter;        // countdown until we assume there are no more bits

unsigned long facilityCode=0;        // decoded facility code
unsigned long cardCode=0;            // decoded card code

// interrupt that happens when INTO goes low (0 bit)
void ISR_INT0()
{
  //Serial.print("0");
  bitCount++;
  flagDone = 0;
  weigand_counter = WEIGAND_WAIT_TIME; 
 
}

// interrupt that happens when INT1 goes low (1 bit)
void ISR_INT1()
{
  //Serial.print("1");
  databits[bitCount] = 1;
  bitCount++;
  flagDone = 0;
  weigand_counter = WEIGAND_WAIT_TIME; 
}

void setup()
{
  pinMode(13, OUTPUT);  // LED
  pinMode(2, INPUT);     // DATA0 (INT0)
  pinMode(3, INPUT);     // DATA1 (INT1)
 
  Serial.begin(9600);
  Serial.println("RFID Readers");
 
  // binds the ISR functions to the falling edge of INTO and INT1
  attachInterrupt(0, ISR_INT0, FALLING); 
  attachInterrupt(1, ISR_INT1, FALLING);
 

  weigand_counter = WEIGAND_WAIT_TIME;
}

void loop(){
  rfid();
}

void rfid()
{
  // This waits to make sure that there have been no more data pulses before processing data
  if (!flagDone) {
    if (--weigand_counter == 0)
      flagDone = 1;
  }
 
  // if we have bits and we the weigand counter went out
  if (bitCount > 0 && flagDone) {
    unsigned char i;
   
    Serial.print("Read ");
    Serial.print(bitCount);
    Serial.print(" bits. ");
   
    // we will decode the bits differently depending on how many bits we have
    // see www.pagemac.com/azure/data_formats.php for mor info
    if (bitCount == 35)
    {
      // 35 bit HID Corporate 1000 format
      // facility code = bits 2 to 14
      for (i=2; i<14; i++)
      {
         facilityCode <<=1;
         facilityCode |= databits[i];
      }
     
      // card code = bits 15 to 34
      for (i=14; i<34; i++)
      {
         cardCode <<=1;
         cardCode |= databits[i];
      }
     
      printBits();
    }
    else if (bitCount == 26)
    {
      // standard 26 bit format
      // facility code = bits 2 to 9
      for (i=1; i<9; i++)
      {
         facilityCode <<=1;
         facilityCode |= databits[i];
      }
     
      // card code = bits 10 to 23
      for (i=9; i<25; i++)
      {
         cardCode <<=1;
         cardCode |= databits[i];
      }
     
      printBits(); 
    }
    else {
      // you can add other formats if you want!
     Serial.println("Unable to decode.");
    }

     // cleanup and get ready for the next card
     bitCount = 0;
     facilityCode = 0;
     cardCode = 0;
     for (i=0; i<MAX_BITS; i++)
     {
       databits[i] = 0;
     }
  }
}

void printBits()
{
      // I really hope you can figure out what this function does
      Serial.print("FC = ");
      Serial.print(facilityCode);
      Serial.print(", CC = ");
      Serial.println(cardCode);
}

This code actually works fine...instead of the content of void RFID() being inside void loop(), I seperatd it like the one shown in the code so that I can add additional functions...
The problem is that when I add additional functions on void loop, the code no longer functions properly.
Like when I add Serial.println("Test"); on void loop(); I cannot read tags anymore?

So what seems to be the problem on this? Thx Guys!
10  Using Arduino / Project Guidance / ACM08Y RFID reader HELP on: February 15, 2013, 11:52:42 pm
Good day guys! First off, I'm not really sure if this is the right section to post this problem , but since it is related to my project I guess it is somehow ok smiley-grin

Anyway, I recently bought this RFID reader

http://www.soselectronic.com/a_info/resource/c/ACM08Y_ID_Card_reader.pdf

I have experienced programming RFID reader like the RDM630 before so i thought I will have no problem in using this one..
I've tried reading the data sent by the RFID using an McU but all I get is random characters of øøøøøøÿÿÿÿÿÿÿÿ and I have no idea why. I haven't experience having problems like this on the RDM630 before. Baud rate is set at 9600

Anyone has any idea on how to solve this problem? Should I use a hyper terminal or the McU way of serial monitor is just fine?
11  Using Arduino / Programming Questions / Anyone familiar with EtherCard.h or ENC28J60 Ethernet shield? on: February 04, 2013, 07:43:30 am
Good day guys! I'm currently working on a web server switch for my project and here is the code. So far it is working fine, its just having some timeouts after it has been open for more than 30 mins.


#include <EtherCard.h>

// Ethernet Part declaration
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
#define BUFFER_SIZE 800
BufferFiller bfill;
byte Ethernet::buffer[BUFFER_SIZE];
static byte myip[] = {192,168,1,15};
// Ethernet Part ends here


const int ledPin = 6;



void setup () {
 
  Serial.begin(9600);
 
//Ethernet Debugging
 
ether.begin(BUFFER_SIZE, mymac, 10);

ether.staticSetup(myip);

//Ethernet Debugging Ends Here

  pinMode(10, OUTPUT);                       
 digitalWrite(10, HIGH);
 



  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}


void homepage(){
      bfill.emit_p(PSTR(
"<!DOCTYPE html><html><title>Ethernet Controller</title>"
"<body><div id='content' style='background-color:lightgreen;width:1100px;height:900px;'><hr><h1><p><center><font size='28'><br>IP Based Automated <br>Load Carrier with RFID Security<br>"
"</p><hr><br><table bgcolor='#58D3F7' border='1'><tr><th colspan='4' >Room Destination</th></tr>"
"<tr><td><INPUT Type='BUTTON' VALUE='GV208' ONCLICK='window.location.href=\"/?cmd=GV208\"'></td><td>"
"<INPUT Type='BUTTON' VALUE='GV209' ONCLICK='window.location.href=\"/?cmd=GV209\"'></td>"
"<td><INPUT Type='BUTTON' VALUE='GV210' ONCLICK='window.location.href=\"/?cmd=GV210\"'></td>"
"<td><INPUT Type='BUTTON' VALUE='GV211' ONCLICK='window.location.href=\"/?cmd=GV211\"'></td></tr>"
"</table><br><hr>Mobot is</font><br><hr></div></body></html>"
   ));
}
void http_Unauthorized(){
      bfill.emit_p(PSTR("HTTP/1.0 401 Unauthorized\r\n"
    "Content-Type: text/html\r\n\r\n"
    "<h1>401 Unauthorized</h1>"   
      ));
}

void loop() {
  //checking for requesting client
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
 

 
  if(pos) {
    bfill = ether.tcpOffset();
   
    //storing buffer on data variable
    char *data = (char *) Ethernet::buffer + pos;
   
    if (strncmp("GET /", data, 5) != 0) {
            // Unsupported HTTP request
            // 304 or 501 response would be more appropriate
             http_Unauthorized();
        }
    else {
        data += 5;
           
            if (data[0] == ' ') {
                // Return home page
                homepage();
            }
            else if (strncmp("?cmd=GV208 ", data, 10) == 0) {
                homepage();
                Serial.println("Recieve ON");
               digitalWrite(ledPin, HIGH);


            }
            else if (strncmp("?cmd=GV209 ", data, 10) == 0) {
              homepage();
                Serial.println("Recieve OFF");
                digitalWrite(ledPin, LOW);
            }
            else if (strncmp("?cmd=GV210 ", data, 10) == 0) {
                homepage();
                Serial.println("Recieve HALF");
                analogWrite(ledPin,150);
            }
            else if (strncmp("?cmd=GV211 ", data, 10) == 0) {
                homepage();
                Serial.println("Recieve HALF");
                analogWrite(ledPin,10);
            }
            else {
                // Page not found
                http_Unauthorized();
            }
        }
    ether.httpServerReply(bfill.position());
  }
}

My problem is that when I want to create a better looking webpage, everything stops loading. I guess this is the limited RAM issues? (I'm thing of buying ATMEGA1280 to have more RAM, currently using ATMGE328)
While I still dont have a new mcu, is it possible to send the html on multiple packets instead of one so I can host a bigger html file? or is buying a new MCU the only solution for now? or is there any other way I can use a bigger HTML?

Also about the timeouts, is there a problem in my code which is causing the timeouts? Can i do anything to avoid these timeouts and high latency issues?

Another thing how is it possible to use an SD card with this library? I tried using the ETHER_28J60.h library and it is working fine with its ether.print command.
Im having problem using the data I read on my SD on bfill.emit_p command.
What I do is
while (sd.available()){
c= sd.read();
bfillemit_p(c);
}
kinda like this.
this code works fine on other libraries( changing bfill.emit_p with the print command of the  other library) but it is not working on ethercard.h library. How do I use my SD right on this library?

THX GUYS and more power to the community!
12  Using Arduino / Programming Questions / Re: Load full html on SD ( Ethernet Shield) on: January 30, 2013, 12:51:14 pm
Thx for the reply sir!

Here is the code that I tried

Code:
#include <EtherCard.h>
#include <SdFat.h>
#include <SdFatUtil.h>


// Ethernet Part declaration
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
#define BUFFER_SIZE 700
BufferFiller bfill;
byte Ethernet::buffer[BUFFER_SIZE];
static byte myip[] = {192,168,1,15};
// Ethernet Part ends here

// SD card declaration
const uint8_t SdChipSelect = 4;
Sd2Card card;
SdVolume volume;
SdFile myfile;
SdFile root;
// SD card declaration ends here

const int ledPin = 6;

// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
 
void error_P(const char* str) {
  PgmPrint("error: ");
  SerialPrintln_P(str);
  if (card.errorCode()) {
    PgmPrint("SD error: ");
    Serial.print(card.errorCode(), HEX);
    Serial.print(',');
    Serial.println(card.errorData(), HEX);
  }
  while(1);
}


void setup () {
 
  Serial.begin(9600);
 
   //SD card debugging

  PgmPrint("Free RAM: ");
  Serial.println(FreeRam());
 
  if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");
 
  // initialize a FAT volume
  if (!volume.init(&card)) error("vol.init failed!");

  PgmPrint("Volume is FAT");
  Serial.println(volume.fatType(),DEC);
  Serial.println();
 
  if (!root.openRoot(&volume)) error("openRoot failed");


  Serial.println();
  PgmPrintln("Done");
//SD card debugging ends here
 

//Ethernet Debugging
  Serial.println("Control WebPage");
 
  if (ether.begin(BUFFER_SIZE, mymac, 10)==0)
    Serial.println( "Failed to access Ethernet controller");
 else
   Serial.println("Ethernet controller initialized");
 
  if (!ether.staticSetup(myip))
    Serial.println("Failed to set IP address");
  Serial.println();
//Ethernet Debugging Ends Here

  pinMode(10, OUTPUT);                       
 digitalWrite(10, HIGH);
 



  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}

const char http_Unauthorized[] PROGMEM =
    "HTTP/1.0 401 Unauthorized\r\n"
    "Content-Type: text/html\r\n\r\n"
    "<h1>401 Unauthorized</h1>";

void homepage(){
            myfile.open("test.txt", O_READ);
            char d;
            while ((d = myfile.read()) > 0) {
              bfill.emit_p((char)d);
          }
              myfile.close();
}

void loop() {
  //checking for requesting client
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
 

 
  if(pos) {
    bfill = ether.tcpOffset();
   
    //storing buffer on data variable
    char *data = (char *) Ethernet::buffer + pos;
   
    if (strncmp("GET /", data, 5) != 0) {
            // Unsupported HTTP request
            // 304 or 501 response would be more appropriate
            bfill.emit_p(http_Unauthorized);
        }
    else {
        data += 5;
           
            if (data[0] == ' ') {
                // Return home page
                homepage();
            }
            else if (strncmp("?cmd=ON ", data, 8) == 0) {
                homepage();
                Serial.println("Recieve ON");
               digitalWrite(ledPin, HIGH);


            }
            else if (strncmp("?cmd=OFF ", data, 9) == 0) {
                homepage();
                Serial.println("Recieve OFF");
                digitalWrite(ledPin, LOW);
            }
            else if (strncmp("?cmd=HALF ", data, 10) == 0) {
                homepage();
                Serial.println("Recieve HALF");
                analogWrite(ledPin,150);
            }
            else {
                // Page not found
                bfill.emit_p(http_Unauthorized);
            }
        }
    ether.httpServerReply(bfill.position());
  }
}

so basically instead of void homepage loading the html on arduino..it tries to retrieve the html code on a test.txt file on my SD.
upon using this code an error shows.

_7_WebLedsss.ino: In function 'void homepage()':
_7_WebLedsss:109: error: invalid conversion from 'char' to 'const prog_char*'
_7_WebLedsss:109: error: initializing argument 1 of 'void BufferFiller::emit_p(const prog_char*, ...)'

what does this mean sir? maybe my approach is wrong? or is it possible that instead of retrieving it on a txt file I can retrieve it on a htm file?
13  Using Arduino / Programming Questions / Re: Basic problem on: January 30, 2013, 12:35:35 pm
Quote
instead of 1 analog input I want to detect 7.

Then you have to declare 7 sensorpins variable and 7 sensorvalues to read your data like
Code:
int sensorPin7 = A8;  // select the input pin A8
int sensorPin6 = A7;
int sensorPin5 = A6;

int sensorValue1 = 0;  // variable to store the value coming from the sensor
int sensorValue2 = 0;
int sensorValue3= 0;

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
}

void loop() {
  // read the value from the sensor:
  sensorValue1 = analogRead(sensorPin7);  
sensorValue2 = analogRead(sensorPin6);  
sensorValue3 = analogRead(sensorPin5);  
                
}

Of course this reads 3 analog pins...just declare more if you want more...is this what you mean?
14  Using Arduino / Programming Questions / Load full html on SD ( Ethernet Shield) on: January 30, 2013, 12:24:54 pm
Good day guys! For my project im using an ENC26j80 Ethernet shield with sd card.

I am trying to make a web switch. for testing purposes I tried 1st with a simple led web switch.
Here is my code modified from the tutorials.

Code:
#include <EtherCard.h>
#include <SdFat.h>
#include <SdFatUtil.h>


// Ethernet Part declaration
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
#define BUFFER_SIZE 700
BufferFiller bfill;
byte Ethernet::buffer[BUFFER_SIZE];
static byte myip[] = {192,168,1,15};
// Ethernet Part ends here

// SD card declaration
const uint8_t SdChipSelect = 4;
Sd2Card card;
SdVolume volume;
SdFile myfile;
SdFile root;
// SD card declaration ends here

const int ledPin = 6;

// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
 
void error_P(const char* str) {
  PgmPrint("error: ");
  SerialPrintln_P(str);
  if (card.errorCode()) {
    PgmPrint("SD error: ");
    Serial.print(card.errorCode(), HEX);
    Serial.print(',');
    Serial.println(card.errorData(), HEX);
  }
  while(1);
}


void setup () {
 
  Serial.begin(9600);
 
   //SD card debugging

  PgmPrint("Free RAM: ");
  Serial.println(FreeRam());
 
  if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");
 
  // initialize a FAT volume
  if (!volume.init(&card)) error("vol.init failed!");

  PgmPrint("Volume is FAT");
  Serial.println(volume.fatType(),DEC);
  Serial.println();
 
  if (!root.openRoot(&volume)) error("openRoot failed");
 
  Serial.println();
  PgmPrintln("Done");
//SD card debugging ends here
 

//Ethernet Debugging
  Serial.println("Control WebPage");
 
  if (ether.begin(BUFFER_SIZE, mymac, 10)==0)
    Serial.println( "Failed to access Ethernet controller");
 else
   Serial.println("Ethernet controller initialized");
 
  if (!ether.staticSetup(myip))
    Serial.println("Failed to set IP address");
  Serial.println();
//Ethernet Debugging Ends Here

 pinMode(10, OUTPUT);                       
 digitalWrite(10, HIGH);
 



  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}

const char http_Unauthorized[] PROGMEM =
    "HTTP/1.0 401 Unauthorized\r\n"
    "Content-Type: text/html\r\n\r\n"
    "<h1>401 Unauthorized</h1>";

void homepage(){
      bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
      "<html><head><title>WebLed</title></head>"
      "<body><center>Web Remote<br>"
      "<a href=\"/?cmd=ON\"><input type=\"button\" value=\"ON\"></a><br><a href=\"/?cmd=OFF\"><input type=\"button\" value=\"OFF\"></a><br>"
      "<a href=\"/?cmd=HALF\"><input type=\"button\" value=\"HALF\"></a></center>"
      "</body></html>"     
      ));
}

void loop() {
  //checking for requesting client
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
 

 
  if(pos) {
    bfill = ether.tcpOffset();
   
    //storing buffer on data variable
    char *data = (char *) Ethernet::buffer + pos;
   
    if (strncmp("GET /", data, 5) != 0) {
            // Unsupported HTTP request
            // 304 or 501 response would be more appropriate
            bfill.emit_p(http_Unauthorized);
        }
    else {
        data += 5;
           
            if (data[0] == ' ') {
                // Return home page
                homepage();
            }
            else if (strncmp("?cmd=ON ", data, 8) == 0) {
                homepage();
                Serial.println("Recieve ON");
                digitalWrite(ledPin, HIGH);
     
            }
            else if (strncmp("?cmd=OFF ", data, 9) == 0) {
                homepage();
                Serial.println("Recieve OFF");
                digitalWrite(ledPin, LOW);
            }
            else if (strncmp("?cmd=HALF ", data, 10) == 0) {
                homepage();
                Serial.println("Recieve HALF");
                analogWrite(ledPin,150);
            }
            else {
                // Page not found
                bfill.emit_p(http_Unauthorized);
            }
        }
    ether.httpServerReply(bfill.position());
  }
}


So far this program is working fine but without fetching anything from the SD card..
As you can see I am able to initialize the SD and web with no problems.
The problem is I can't load my html from the sd.

What I tried is putting the html code from void homepage() to a text file on the sd
then I did something like this inside void homepage();
Code:
myfile.open("test.txt", O_READ);
char* d;
 while ((*d = myfile.read()) > 0) {
   bfill.emit_p((char*)d);
 }
but its not working...I also tried following from the tutorial
Code:
              int16_t c;
          while ((c = file.read()) > 0) {
             bfill.emit_p((char)c);

the 1st one doesn't load the web page at all and upon looking at the serial monitor, it stops at "Free RAM:"
while the 2nd one always return an error of "wrong conversion of char...something like that"

So where have I gone wrong? I hope someone can hel me! thx to all! smiley-grin
15  Using Arduino / Programming Questions / Re: Ethernet Shield ENC26J80 + SD CARD on: January 28, 2013, 11:29:50 am
Hi guys after some reading and modifying the code for my convenience
and here it is

Code:
#include <EtherCard.h>

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
#define BUFFER_SIZE 700
byte Ethernet::buffer[BUFFER_SIZE];
static byte myip[] = {192,168,1,2};

const int ledPin = 6;

void setup () {
 
  Serial.begin(9600);
  Serial.println("Control WebPage");
 
  if (ether.begin(BUFFER_SIZE, mymac, 10)==0)
    Serial.println( "Failed to access Ethernet controller");
 else
   Serial.println("Ethernet controller initialized");
 
  if (!ether.staticSetup(myip))
    Serial.println("Failed to set IP address");

  Serial.println();
 
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}
 
void loop() {
 
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
 
  if(pos) {

    if(strstr((char *)Ethernet::buffer + pos, "GET /?cmd=ON") != 0) {
      Serial.println("Recieve ON");
      digitalWrite(ledPin, HIGH);
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?cmd=OFF") != 0) {
      Serial.println("Recieve OFF");
      digitalWrite(ledPin,LOW);
    }
   
     if(strstr((char *)Ethernet::buffer + pos, "GET /?cmd=HALF") != 0) {
       Serial.println("Recieve HALF");
      analogWrite(ledPin,150);
    }
     
     BufferFiller bfill = ether.tcpOffset();
    bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
      "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
      "<html><head><title>WebLed</title></head>"
      "<body>Web Remote<br>"
      "<a href=\"/?cmd=ON\"><input type=\"button\" value=\"ON\"></a>"
      "<a href=\"/?cmd=OFF\"><input type=\"button\" value=\"OFF\"></a>"
      "<a href=\"/?cmd=HALF\"><input type=\"button\" value=\"HALF\"></a>"
      "</body></html>"     
      ));
   
    ether.httpServerReply(bfill.position());
  }
}


basically what I want next is to store this html on the embedded  sd card on my shield and also instead of using buttons as the link
I want to use images on the SD.
I found some tutorials on doing these like this one http://arduino.cc/forum/index.php?topic=100360.0
but its kinda hard to understand and the format is different since it uses a W1500 chip with Ethernet.h library.
What I'm using is a ENC26J80 shield with EtherCard.h library.
A little guide, sample codes would be a great help just to give me an idea on how to start migrating the sample from the
 above link to something suitable for my code.

THx!
Pages: [1] 2