Go Down

Topic: Arduino + Ethernet Shield Connection Status Notification Issue. (Read 507 times) previous topic - next topic

rkmalbz

Hello, good day. This is my first post in this forum. I have this project of mine which is a doorlock. Unlike other sample doorlock, this uses a 125Khz RDM6300 RF Card Reader. When a personnel wants to go in, he/she should use his/her EM4100 Card. When personnel tap on the reader, the arduino with ethernet shield would then access a remote server and then query to the database (mysql). If that card has been registered the the system would let that personnel in and etc.

My issue is that, how would I put it, that if the connection has been cut, the arduino would notify me with red blinking LED.

I tried having this:
Code: [Select]

if(client.connect("ServerIP", 80)) {
 notify me with green LED if connected.
} else {
 blink if not.
}

on top of
Code: [Select]
if((rfid.available() > 0) && (millis() - lastRead > postingInterval)) {
    rfNum = getRFNum();
    httpRequest(rfNum);
    lastRead = millis();
  }


But it just doesn't read the card and access the database. its like its doing nothing.

Heres my complete code below:
Code: [Select]
#include <Ethernet.h>
#include "rdm630.h"

//INTERNET SHIELD
byte mac[] = {0x60, 0xF8, 0x1D, 0xBA, 0x1D, 0x52};
IPAddress ip(192, 168, 20, 228);

EthernetClient client;
unsigned long lastRead = 0;
boolean lastConnected = false;
boolean isConnected = false;
const unsigned long postingInterval = 500;
int isOnline, readState = 0;
char c;

//RFID RDM630
rdm630 rfid(12, 13);
unsigned long rfNum, cardNo;

//MISC
int buzzer = 8;
int ledGreen = 24, ledRed = 22, ledBlue = 26;

//DOOR OPENNER
int doorPin = 25;
int opnDoor = 0;

void setup() {
  rfid.begin();
  Serial.begin(9600);
 
  pinMode(ledGreen, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(ledBlue, OUTPUT);
  pinMode(doorPin, OUTPUT);
 
  Ethernet.begin(mac, ip);
  Serial.print("My IP Address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
 
  rfNum = 0;
  digitalWrite(doorPin, LOW);
 
  if(client.available()) { 
    c = client.read();
    if(c == '1') {   
      accessGranted(); 
      c = 0;                                                                                                                                                                               
    } else if(c == '2'){
      accessDenied();
      c = 0;
    }
  }

  if(!client.connected()) {
    client.stop();
  }
 
  if((rfid.available() > 0) && (millis() - lastRead > postingInterval)) {
    rfNum = getRFNum();
    httpRequest(rfNum);
    lastRead = millis();
  }

  delay(1);
}

long getRFNum() {
  byte data[6];
  byte length;
 
  rfid.getData(data,length);
 
  //concatenate the bytes in the data array to one long which can be
  //rendered as a decimal number
  unsigned long result =
    ((unsigned long int)data[1]<<24) +
    ((unsigned long int)data[2]<<16) +
    ((unsigned long int)data[3]<<8) +
    data[4];
   
  return result;
}

void httpRequest(long rfCrdNo) {
  if(client.connect("192.168.20.248", 80)) {
    digitalWrite(ledGreen, HIGH);
    client.print("GET /entry.php?rfid=");
    client.print(rfCrdNo);
    client.println("HTTP/1.1");
    client.println("HOST: 192.168.20.248");
    client.println("User-Agent: arduino-ethernet");
    //client.println("Connection: Close");
    client.println();
  } else {
    disConnected();
  }
}

void accessGranted() {
  Serial.println("Access Granted");
  digitalWrite(doorPin, HIGH);
  tone(buzzer, 5000);
  delay(500);
  noTone(buzzer);
}

void accessDenied() {
  tone(buzzer, 5000);
  delay(500);
  noTone(buzzer);
  delay(500);
  tone(buzzer, 5000);
  delay(500);
  noTone(buzzer);
}

void disConnected() {
  tone(buzzer, 1000);
  for(int disLED = 0; disLED < 5; disLED++) {
    digitalWrite(ledGreen, LOW);
    digitalWrite(ledBlue, HIGH);
    digitalWrite(ledRed, LOW);
    delay(100);
    digitalWrite(ledBlue, LOW);
    digitalWrite(ledRed, HIGH);
    delay(100);
  }
  noTone(buzzer);
  client.stop();
}



rw950431

What do the logs on the php server say?  Do you see connection attempts?

One possible issue is that you will receive a complete http response from the server not just the message body that you are attempting to decode so there will be lots of spurious characters in the way. Maybe if you include "Serial.print(c)" inside that section you will see whats being sent

For the same reason you should also read the complete response using a while loop.

 
Code: [Select]
while(client.available()) {  
 c = client.read();
        serial.print(c); // for debugging
        // decode response
   }

PaulS

Quote
For the same reason you should also read the complete response using a while loop.
That loop will read all the data that has arrived. To read ALL of the data, you need an outer while loop:

Code: [Select]
   while(client.connected())
   {
      while(client.available() > 0)
      {
         c = client.read();
         serial.print(c); // for debugging
      }
   }
The art of getting good answers lies in asking good questions.

Go Up