RFID assitance monitoring

Hello,

I know it might be a C/C++ standard question but I am working on a firmware that will help me monitor the assistance of personal in a remote location. I am using examples that I have found in the forum to implement my solution.
I am using a SparkFun RFID 125kHz card reader and a Photon. The RFID reader communicates with my Photon using the serial port and when its online its working really good. No problems at all. When its offline, I would like to read as much as 100 (As many as possible honestly) RFID cards and store this IDs within the Photon, every card ID is 12 characters long followed by ‘\r’’\n’. The problem I’m having is that whenever I am offline instead of sending the RFID tags that I am storing its sending “null”. I believe my photon is not really storing anything and I am not sure which datatype would work better to store this tags so later whenever it connects to WiFi again it can send them.
I also get a SOS and a hard fault. Not sure why.

/* Libraries */
#include "Particle.h"
/* Begining WiFi communication status variables */
#define disconnectDelay 15000 // After 3 seconds of no WiFi, disconnect
#define checkInterval 45000 //Every 30 seconds it attempts to connect to WiFi
unsigned int lastAttempt, lastDisconnect;
bool connected = true;
SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
/* End WiFi communication status variables */
/* Beginning Serial communication variables  */
const unsigned long SEND_INTERVAL_MS = 2000;
const size_t READ_BUF_SIZE = 64;
void processBuffer();
int counter = 0;
unsigned long lastSend = 0;
char readBuf[READ_BUF_SIZE];
size_t readBufOffset = 0;
/* End Serial communication variables  */
/* System variables */
int OutputLED1 = D7;
retained char offlineBuffer[20][16];
String auxString = "";
retained int offlineBufferPos = 0;
retained int whileOffline = 0;
retained int whileOnline = 0;
void setup() {
	Serial1.begin(9600);
	pinMode(OutputLED1, OUTPUT);
	Particle.connect();
}
void checkConnection(){
        if (Particle.connected() == false && millis() - lastDisconnect >= disconnectDelay){
            //statusControl(1);
            lastDisconnect = millis();
            WiFi.off();
            connected = false;
        }
        if (millis() - lastAttempt >= checkInterval){
            //statusControl(3);
            WiFi.on();
            Particle.connect();
            lastAttempt = millis();
        }
        if (millis() - lastAttempt >= disconnectDelay){
            //statusControl(0);
            Particle.connect();
            connected = Particle.connected();
        }
}
void statusControl(int rfidState){
/*
This will control the output LED and flashing varies depending on buffer status
0 - Connected
1 - Not connected
2 - Not connected and buffer at 25%
3 - Not connected and buffer at 50%
4 - Not connected and buffer at 75%
5 - Not connected and buffer at 100%
*/
    switch (rfidState){
        case 0:
            digitalWrite(OutputLED1, LOW);
            delay(50);
            break;
        case 1:
            digitalWrite(OutputLED1, HIGH);
            delay(50);
            digitalWrite(OutputLED1, LOW);
            delay(450);
            digitalWrite(OutputLED1, HIGH);
            delay(50);
            digitalWrite(OutputLED1, LOW);
            delay(450);
            break;
        case 2:
            digitalWrite(OutputLED1, HIGH);
            delay(450);
            digitalWrite(OutputLED1, LOW);
            delay(50);
            digitalWrite(OutputLED1, HIGH);
            delay(450);
            digitalWrite(OutputLED1, LOW);
            delay(50);
            break;
        default:
            digitalWrite(OutputLED1, HIGH);
            delay(250);
            digitalWrite(OutputLED1, LOW);
            delay(250);
            break;
    }
}
void loop() {
    checkConnection();
    if (connected == false){ // This code will run while the Photon is disconnected from Cloud and WiFi
        //statusControl(1);
        while(Serial1.available()) {
    		if (readBufOffset < READ_BUF_SIZE) {
    			char c = Serial1.read();
    			if(c != '\n'){
    			    if((c != '') && (c != '')){
    				    readBuf[readBufOffset++] = c;// Add character to buffer
    				   offlineBuffer[offlineBufferPos][readBufOffset++] = c;
    			    }
    			}
    			else 
    			{
    				// End of line character found, process line
    				readBuf[readBufOffset] = '\n';
    				offlineBuffer[offlineBufferPos][readBufOffset] = '\n';
    				readBufOffset = 0;
    				offlineBufferPos++;
    			}
    		}
    		else 
    		{
    			Particle.publish("readBuf overflow, emptying buffer");
    			readBufOffset = 0;
    			offlineBufferPos = 30405;;
    		}
    	}
    }
    else
    {
        //statusControl(0);
    	// Read data from serial
    	while(Serial1.available()) {
    		if (readBufOffset < READ_BUF_SIZE) {
    			char c = Serial1.read();
    			if(c != '\n'){
    			    if((c != '') && (c != '')){
    				    readBuf[readBufOffset++] = c;// Add character to buffer
    			    }
    			}
    			else {
    				// End of line character found, process line
    				readBuf[readBufOffset] = '\n';
    				processBuffer();
    				readBufOffset = 0;
    			}
    		}
    		else {
    			Particle.publish("readBuf overflow, emptying buffer");
    			readBufOffset = 0;
    		}
    		if(offlineBufferPos > 0){
        	    for(int i = 0; i < offlineBufferPos; i++){
        	         Particle.publish("offlineRFID:", String(offlineBuffer[i]), PRIVATE);
        	         statusControl(2);
        	    }
    	    offlineBufferPos = 0;
    	    }
    	}
    }
}
void processBuffer() {
	 Particle.publish("P_RFIDAssistance:", String(readBuf), PRIVATE);
	 Particle.publish("offlinePos:", String(offlineBufferPos));
}

I have reformatted your code block and the code highlighting does reveal some missing characters (which should cause build errors if this was the real code).
Could you correct the code so that it matches your actual test code?

Especially this portion

    			    if((c != '') && (c != '')){

(the double check for c beeing not equal to the same thing)

Thanks, I tried but was not able to re-format my code.
The conditions inside that IF did not copied from my code. This is what I mean:

Thats my real code, just this characters were not moved from my code to the post.

@estebitfan, what exactly are those special characters? What is their HEX value in the ASCII table?

1 Like

Usually you can’t just copy/paste special characters from one application to another since they may use different encoding schemes. Special characters usually want to be escaped.

The fact that two different special characters came up as one and the same symbol in your code should raised some questions :wink:

1 Like

Here are the two codes of the ASCII codes that I get: ASCII%20code%202ASCII%20code%201

With the IF instruction I managed to get rid of them and I get the following code for my RFID card:

I can change this and thank you for pointing out it is weird, could this be the reason of the SOS?
Still my main question is: How can I store the codes offline and send all the data that was stored offline whenever the photon is online again?

You can write if (c != '\x02' && c != '\x03')

Once your code runs, I wouldn't see why your retained char offlineBuffer[][] wouldn't hold the data - as long you also enable the Backup RAM feature as pointed out in the docs

See here
https://docs.particle.io/reference/firmware/photon/#enabling-backup-ram-sram-