Onderstaande code doet wat het moet doen, maar netjes is het zeker niet. Zijn er mensen die mogelijke verbeteringen of meer fool-proof ideeen hebben hiervoor?
Het is de betaal automaat voor een frisdrankautomaat (gewoon een kleintje voor een groep vrienden). Mensen betalen vooraf en krijgen dan credits op een webserver (in een mysql database).
Dit programma leest een aangeboden tag, stuurt de tag naar een webpagina en leest de return code. Afhankelijk van de code krijgen ze of een groen lampje en schakelt een relais kort in. Of ze krijgen bij geen saldo een rood lampje, of indien de tag niet herkent is een blauw lampje.
#include <SoftwareSerial.h>
#include <EtherCard.h>
#include <TextFinder.h>
int length = 0;
static uint32_t timer;
// Patterns and pattern buffer pointer
char *searchPattern[] = {
"<code>"
};
char *searchPatternProgressPtr;
// Output bugger and pointer to the buffer
char displayBuff[64];
char testBuf[64];
char *outputBufferPtr;
int foundPatternState;// Initialsed in loop()
// Utility functions
void removeSubstring(char *s,const char *toremove)
{
while( s=strstr(s,toremove) )
{
memmove(s,s+strlen(toremove),1+strlen(s+strlen(toremove)));
}
}
// Function to find a string in a buffer, and return a pointer to the first char after the end of the match
// Returns pointer to NULL if a pattern is not found
// This function is designed to fing the search string across multiple buffers, and uses the patternInProgress pinter to store partial pattern matches
// The calling code needs to maintain patternInProgress between subsequent calls to the function.
char *multiBufferFindPattern(char *buffer,char *searchString,char *patternInProgress)
{
while (*buffer && *patternInProgress)
{
if (*buffer == *patternInProgress)
{
patternInProgress++;
}
else
{
patternInProgress=searchString;// reset to start of the pattern
}
buffer++;
}
if (!*patternInProgress)
{
return buffer;
}
return NULL;
}
int getData(char *inputBuffer, char *outputBuffPtr, char endMarker)
{
while(*inputBuffer && *inputBuffer!=endMarker && *outputBuffPtr)
{
*outputBuffPtr=*inputBuffer;
outputBuffPtr++;
inputBuffer++;
}
if (*inputBuffer==endMarker && *outputBuffPtr!=0)
{
*outputBuffPtr=0;
// end character found
return 1;
}
else
{
return 0;
}
}
//Ethernet needs this
#define REQUEST_RATE 1000 // milliseconds
static byte mymac[] = {
0x74,0x69,0x69,0x2D,0x30,0x31 };
char website[] PROGMEM = "frizzbar.nl";
byte Ethernet::buffer[400];
// request 2 remove other
static void browseUrlCallback (byte status, word off, word len)
{
char *pos;// used for buffer searching
pos=(char *)(Ethernet::buffer+off);
Ethernet::buffer[off+len] = 0;// set the byte after the end of the buffer to zero to act as an end marker (also handy for displaying the buffer as a string)
Serial.println((const char*) Ethernet::buffer + off);
//Serial.println(pos);
if (foundPatternState==0)
{
// initialise pattern search pointers
searchPatternProgressPtr=searchPattern[0];
foundPatternState=1;
}
if (foundPatternState==1)
{
pos = multiBufferFindPattern(pos,searchPattern[0],searchPatternProgressPtr);
if (pos)
{
foundPatternState=2;
outputBufferPtr=displayBuff;
memset(displayBuff,'0',sizeof(displayBuff));// clear the output display buffer
displayBuff[sizeof(displayBuff)-1]=0;//end of buffer marker
}
else
{
return;// Need to wait for next buffer, so just return to save processing the other if states
}
}
if (foundPatternState==2)
{
//Serial.println("Found HR start");
if (getData(pos,outputBufferPtr,'<'))
{
Serial.print("Code ");
Serial.println(displayBuff);
long displayInt = atol(displayBuff);
long codeInt = atol("9812");
long faultInt = atol("6767");
if (displayInt == codeInt)
{
Serial.print("Equal.");
Consumption();
}
if (displayInt == faultInt)
{
Serial.print("Equal.");
GeenSaldo();
}
String DisplayTAG = "";
ether.persistTcpConnection(false); //Move to next state (not used in this demo)
}
else
{
Beep(100);
Beep(100);
Beep(100);
Beep(100);
// end marker is not found, stay in same findPatternState and when the callback is called with the next packet of data, outputBufferPtr will continue where it left off
}
}
Pauze();
}
// called when the client request is complete
static void my_result_cb (byte status, word off, word len) {
Serial.print("<<< reply ");
Serial.print(millis() - timer);
Serial.println(" ms");
Serial.println((const char*) Ethernet::buffer + off);
}
//Parallax RFID Reader
#define RFIDSerialRate 9600 //Parallax RFID Reader Serial Port Speed
//Using SoftwareSerial Library to locate the serial pins off the default set
//This allows the Arduino to be updated via USB with no conflict
#define RxPin 5 //Pin to read data from Reader
#define TxPin 4 //Pin to write data to the Reader NOTE: The reader doesn't get written to, don't connect this line.
SoftwareSerial RFIDReader(RxPin,TxPin);
String RFIDTAG=""; //Holds the RFID Code read from a tag
String DisplayTAG = ""; //Holds the last displayed RFID Tag
void setup()
{
pinMode(3, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
// RFID reader SOUT pin connected to Serial RX pin at 9600bps
RFIDReader.begin(RFIDSerialRate);
// Set Enable pin as OUTPUT to connect it to the RFID /ENABLE pin
// Activate the RFID reader
// Setting the RFIDEnablePin HIGH will deactivate the reader
// which could be usefull if you wanted to save battery life for
// example.
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("\n[getDHCPandDNS]");
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
Beep(500);
Blue(100);
Beep(500);
Blue(100);
Blue(100);
Blue(100);
Blue(100);
if (!ether.dhcpSetup())
Serial.println("DHCP failed");
ether.printIp("My IP: ", ether.myip);
// ether.printIp("Netmask: ", ether.mymask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
ether.printIp("Server: ", ether.hisip);
timer = - REQUEST_RATE; // start timing out right away
}