WappAutomation 2 : - WITH RESET - WITHOUT Switch Cable Tested on TEL50

Dependencies:   MFRC522

Committer:
dalbanf
Date:
Mon Apr 16 15:07:19 2018 +0000
Revision:
9:cfa9a379dbd3
Parent:
7:64d536d218c3
Child:
10:4eebb90e8e06
Add a timeout during tag reading

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dalbanf 0:8814d243dfec 1 #include "mbed.h"
dalbanf 0:8814d243dfec 2 #include "MFRC522.h"
dalbanf 0:8814d243dfec 3 #include "EthernetInterface.h"
dalbanf 0:8814d243dfec 4
dalbanf 0:8814d243dfec 5
dalbanf 0:8814d243dfec 6 //***************************** Change this for each new prober installation ********************************
dalbanf 9:cfa9a379dbd3 7 #define PROBER_NAME "tl37"
dalbanf 9:cfa9a379dbd3 8 #define IP "10.129.97.108"
dalbanf 0:8814d243dfec 9 /*Please update the list below:
dalbanf 0:8814d243dfec 10 TEL12 : 10.129.97.100
dalbanf 0:8814d243dfec 11 TEL20 : 10.129.97.101
dalbanf 0:8814d243dfec 12 TEL23 : 10.129.97.102
dalbanf 0:8814d243dfec 13 TEL36 : 10.129.97.103
dalbanf 1:24db8b26b816 14 TEL15 : 10.129.97.104
dalbanf 5:2b7c8f43efd9 15 TEL01 : 10.129.97.105
dalbanf 5:2b7c8f43efd9 16 TEL19 : 10.129.97.106
dalbanf 5:2b7c8f43efd9 17 TEL27 : 10.129.97.107
dalbanf 6:f7c355fcb302 18 TEL37 : 10.129.97.108
dalbanf 6:f7c355fcb302 19 TEL38 : 10.129.97.109
dalbanf 6:f7c355fcb302 20 TEL39 : 10.129.97.110
dalbanf 6:f7c355fcb302 21 TEL40 : 10.129.97.111
dalbanf 6:f7c355fcb302 22 TEL16 : 10.129.97.112
dalbanf 6:f7c355fcb302 23 TEL21 : 10.129.97.113
dalbanf 6:f7c355fcb302 24 TEL22 : 10.129.97.114
dalbanf 6:f7c355fcb302 25 TEL34 : 10.129.97.115
dalbanf 7:64d536d218c3 26 TEL30 : 10.129.97.116
dalbanf 7:64d536d218c3 27 TEL32 : 10.129.97.117
dalbanf 7:64d536d218c3 28 TEL26 : 10.129.97.118
dalbanf 7:64d536d218c3 29 TEL25 : 10.129.97.119
dalbanf 7:64d536d218c3 30 TEL28 : 10.129.97.120
dalbanf 7:64d536d218c3 31 TEL29 : 10.129.97.121
dalbanf 7:64d536d218c3 32 TEL31 : 10.129.97.122
dalbanf 7:64d536d218c3 33 TEL33 : 10.129.97.123
dalbanf 7:64d536d218c3 34 TEL42 : 10.129.97.124
dalbanf 7:64d536d218c3 35 TEL43 : 10.129.97.125
dalbanf 7:64d536d218c3 36 TEL44 : 10.129.97.126
dalbanf 0:8814d243dfec 37 ************************************************************************************************************/
dalbanf 0:8814d243dfec 38
dalbanf 0:8814d243dfec 39
dalbanf 0:8814d243dfec 40 //******************************************* RFID Antenna **************************************************
dalbanf 0:8814d243dfec 41 //Cheap 13.56 Mhz RFID-RC522 module (This code is based on Martin Olejar's MFRC522 library. Minimal changes)
dalbanf 1:24db8b26b816 42 //On Nucleo-F429ZI, SPI1 and SPI2 could not be used in the same time with EthernetInterface -> SPI4 used
dalbanf 0:8814d243dfec 43 //RFID IRQ=pin5 -> Not used. Leave open
dalbanf 0:8814d243dfec 44 //RFID MISO=pin4 -> Nucleo SPI_MISO -> White -> PE_5
dalbanf 0:8814d243dfec 45 //RFID MOSI=pin3 -> Nucleo SPI_MOSI -> Green -> PE_6
dalbanf 0:8814d243dfec 46 //RFID SCK=pin2 -> Nucleo SPI_SCK -> Yellow -> PE_2
dalbanf 0:8814d243dfec 47 //RFID SDA=pin1 -> Nucleo SPI_CS -> Blue -> PE_4
dalbanf 0:8814d243dfec 48 //RFID RST=pin7 -> Nucleo DigitalOut -> Brown -> D8
dalbanf 0:8814d243dfec 49 //***********************************************************************************************************
dalbanf 0:8814d243dfec 50
dalbanf 0:8814d243dfec 51
dalbanf 0:8814d243dfec 52 //********************************************* Antenna LED *************************************************
dalbanf 0:8814d243dfec 53 //Anode -> +3.3v pin
dalbanf 0:8814d243dfec 54 //Cathode -> D3 pin
dalbanf 0:8814d243dfec 55 //Use LED_ON and LED_OFF to use the LED
dalbanf 0:8814d243dfec 56 //***********************************************************************************************************
dalbanf 0:8814d243dfec 57
dalbanf 0:8814d243dfec 58
dalbanf 0:8814d243dfec 59 //****************************************** Interlock Relay ************************************************
dalbanf 0:8814d243dfec 60 //Power + -> +5v pin
dalbanf 0:8814d243dfec 61 //Power - -> GND pin
dalbanf 0:8814d243dfec 62 //Command -> D4 pin
dalbanf 0:8814d243dfec 63 //Use RELAY_OPEN and RELAY_CLOSE to use the relay
dalbanf 0:8814d243dfec 64 //***********************************************************************************************************
dalbanf 0:8814d243dfec 65
dalbanf 0:8814d243dfec 66
dalbanf 0:8814d243dfec 67 #define MF_RESET D8 // Nucleo Pin for MFRC522 reset
dalbanf 0:8814d243dfec 68 #define LED_ON 0
dalbanf 0:8814d243dfec 69 #define LED_OFF 1
dalbanf 1:24db8b26b816 70 #define RELAY_CLOSED 0
dalbanf 1:24db8b26b816 71 #define RELAY_OPEN 1
dalbanf 0:8814d243dfec 72
dalbanf 0:8814d243dfec 73 #define INTERLOCK_DELAY 5000 // Delay (in milliseconds) between alarm received and relay off to simulate open interlock
dalbanf 0:8814d243dfec 74 #define APCD_READ_DELAY 500 // Delay (in milliseconds) before read the APCD data
dalbanf 0:8814d243dfec 75 #define SOCKETCONNECTION_ATTEMPT 5 // Number of attempts for socket connection
dalbanf 0:8814d243dfec 76 #define TIMEOUT_WAITTAG 20 // Timeout in case of New Tag Present detected
dalbanf 0:8814d243dfec 77
dalbanf 4:caf61234018c 78 #define GATEWAY "10.129.97.254" //The gateway to go out the prober VLAN
dalbanf 0:8814d243dfec 79 #define MASK "255.255.255.0"
dalbanf 7:64d536d218c3 80 #define SERVER "10.18.47.90" //Automation server IP Address - { 164, 129, 103, 88 } for Dev Automation server
dalbanf 4:caf61234018c 81 #define PORT 7778 //Automation port
dalbanf 0:8814d243dfec 82
dalbanf 4:caf61234018c 83 #define MBX_OPEN "MBX_OPEN srvpat" //"MBX_OPEN srv_pat"
dalbanf 0:8814d243dfec 84 #define MBX_CLOSE "MBX_DISCONNECT"
dalbanf 0:8814d243dfec 85 #define MBX_COUNT "MBX_COUNT STM32Insrv_" PROBER_NAME
dalbanf 0:8814d243dfec 86 #define MBX_GET "MBX_GET 5,STM32Insrv_" PROBER_NAME
dalbanf 0:8814d243dfec 87 #define MBX_PUT_BEGIN "MBX_PUT STM32Outsrv, CMD/A=\"WAPP\" MID/A=\"gnb3t" PROBER_NAME "\" MTY/A=\"E\" WAPPID/A=\""
dalbanf 0:8814d243dfec 88 #define MBX_PUT_END "\""
dalbanf 0:8814d243dfec 89
dalbanf 0:8814d243dfec 90
dalbanf 0:8814d243dfec 91 Serial PC(USBTX, USBRX);
dalbanf 0:8814d243dfec 92 MFRC522 Antenna(PE_6, PE_5, PE_2, PE_4, MF_RESET);
dalbanf 0:8814d243dfec 93 EthernetInterface eth;
dalbanf 0:8814d243dfec 94 TCPSocket apcd;
dalbanf 0:8814d243dfec 95 DigitalIn WappExchangeSwitch(D2);
dalbanf 0:8814d243dfec 96 DigitalOut TagReadLED(D3);
dalbanf 0:8814d243dfec 97 DigitalOut Interlock(D4);
dalbanf 0:8814d243dfec 98 DigitalOut red(LED3);
dalbanf 0:8814d243dfec 99 DigitalOut blue(LED2);
dalbanf 0:8814d243dfec 100 DigitalOut green(LED1);
dalbanf 0:8814d243dfec 101
dalbanf 0:8814d243dfec 102 char bufTagID[256];
dalbanf 0:8814d243dfec 103 int APCDsendData (char *tcpCmd);
dalbanf 0:8814d243dfec 104 char *APCDgetData ();
dalbanf 0:8814d243dfec 105
dalbanf 0:8814d243dfec 106
dalbanf 0:8814d243dfec 107 //IsTagPresent(): function to read the Tag ID
dalbanf 0:8814d243dfec 108 // Return false if no Tag present
dalbanf 0:8814d243dfec 109 // Return true if a Tag ID was read (Tag ID is saved int the bufTagID buffer)
dalbanf 0:8814d243dfec 110 bool IsTagPresent(){
dalbanf 9:cfa9a379dbd3 111 /*
dalbanf 0:8814d243dfec 112 if (Antenna.PICC_IsNewCardPresent() && WappExchangeSwitch == 1){
dalbanf 0:8814d243dfec 113 PC.printf("TAG detected ... ");
dalbanf 0:8814d243dfec 114
dalbanf 0:8814d243dfec 115 //Select one of the cards
dalbanf 0:8814d243dfec 116 int count = 0;
dalbanf 0:8814d243dfec 117 while (!Antenna.PICC_ReadCardSerial() && WappExchangeSwitch == 1 && count < TIMEOUT_WAITTAG){
dalbanf 0:8814d243dfec 118 count++;
dalbanf 0:8814d243dfec 119 wait_ms(100);
dalbanf 0:8814d243dfec 120 }
dalbanf 0:8814d243dfec 121 if (count == TIMEOUT_WAITTAG){
dalbanf 0:8814d243dfec 122 PC.printf("Timeout occured -> no Tag present\r\n");
dalbanf 0:8814d243dfec 123 return false;
dalbanf 0:8814d243dfec 124 }
dalbanf 0:8814d243dfec 125 if (WappExchangeSwitch == 0) return false;
dalbanf 0:8814d243dfec 126
dalbanf 0:8814d243dfec 127 //Light during 1s Tag LED to indicate that a tag was read
dalbanf 0:8814d243dfec 128 TagReadLED = LED_ON;
dalbanf 0:8814d243dfec 129 wait_ms(500);
dalbanf 0:8814d243dfec 130 TagReadLED = LED_OFF;
dalbanf 0:8814d243dfec 131
dalbanf 0:8814d243dfec 132 //Save Tag ID into buffer
dalbanf 0:8814d243dfec 133 PC.printf("ID = ");
dalbanf 0:8814d243dfec 134 int pos = 0;
dalbanf 0:8814d243dfec 135 char hexbuf[2];
dalbanf 0:8814d243dfec 136 for (int i = 0; i < 256; i++) bufTagID[i] = '\0';
dalbanf 0:8814d243dfec 137 for (uint8_t i = 0; i < Antenna.uid.size; i++) {
dalbanf 0:8814d243dfec 138 sprintf(hexbuf, "%02x", Antenna.uid.uidByte[i]);
dalbanf 0:8814d243dfec 139 PC.putc(hexbuf[0]);
dalbanf 0:8814d243dfec 140 PC.putc(hexbuf[1]);
dalbanf 0:8814d243dfec 141 bufTagID[pos] = hexbuf[0];
dalbanf 0:8814d243dfec 142 pos++;
dalbanf 0:8814d243dfec 143 bufTagID[pos] = hexbuf[1];
dalbanf 0:8814d243dfec 144 pos++;
dalbanf 0:8814d243dfec 145 }
dalbanf 0:8814d243dfec 146 PC.printf("\r\n");
dalbanf 0:8814d243dfec 147 return true;
dalbanf 0:8814d243dfec 148 }
dalbanf 0:8814d243dfec 149 else return false;
dalbanf 9:cfa9a379dbd3 150 */
dalbanf 9:cfa9a379dbd3 151
dalbanf 9:cfa9a379dbd3 152 //Look for new cards during 5s
dalbanf 9:cfa9a379dbd3 153 int timeout = 0;
dalbanf 9:cfa9a379dbd3 154 while ( ! Antenna.PICC_IsNewCardPresent() && WappExchangeSwitch == 1 && timeout < TIMEOUT_WAITTAG) {
dalbanf 9:cfa9a379dbd3 155 wait_ms(100);
dalbanf 9:cfa9a379dbd3 156 timeout++;
dalbanf 9:cfa9a379dbd3 157 }
dalbanf 9:cfa9a379dbd3 158 if (timeout >= TIMEOUT_WAITTAG) {
dalbanf 9:cfa9a379dbd3 159 PC.printf("Timeout occured -> no Tag present\r\n");
dalbanf 9:cfa9a379dbd3 160 return false;
dalbanf 9:cfa9a379dbd3 161 }
dalbanf 9:cfa9a379dbd3 162 if (WappExchangeSwitch == 0) return false;
dalbanf 9:cfa9a379dbd3 163 PC.printf("TAG detected ... ");
dalbanf 9:cfa9a379dbd3 164
dalbanf 9:cfa9a379dbd3 165 //pc.printf("Yes, tag is present... Try to read Tag ID\r\n");
dalbanf 9:cfa9a379dbd3 166
dalbanf 9:cfa9a379dbd3 167 //Select one of the cards
dalbanf 9:cfa9a379dbd3 168 timeout = 0;
dalbanf 9:cfa9a379dbd3 169 while ( ! Antenna.PICC_ReadCardSerial() && WappExchangeSwitch == 1 && timeout < TIMEOUT_WAITTAG) {
dalbanf 9:cfa9a379dbd3 170 wait_ms(100);
dalbanf 9:cfa9a379dbd3 171 timeout++;
dalbanf 9:cfa9a379dbd3 172 }
dalbanf 9:cfa9a379dbd3 173 if (timeout >= TIMEOUT_WAITTAG) {
dalbanf 9:cfa9a379dbd3 174 PC.printf("Timeout occured during ReadCardSerial\r\n");
dalbanf 9:cfa9a379dbd3 175 return false;
dalbanf 9:cfa9a379dbd3 176 }
dalbanf 9:cfa9a379dbd3 177 if (WappExchangeSwitch == 0) return false;
dalbanf 9:cfa9a379dbd3 178
dalbanf 9:cfa9a379dbd3 179 //Light during 1s Tag LED to indicate that a tag was read
dalbanf 9:cfa9a379dbd3 180 TagReadLED = LED_ON;
dalbanf 9:cfa9a379dbd3 181 wait_ms(500);
dalbanf 9:cfa9a379dbd3 182 TagReadLED = LED_OFF;
dalbanf 9:cfa9a379dbd3 183
dalbanf 9:cfa9a379dbd3 184 //Save Tag ID into buffer
dalbanf 9:cfa9a379dbd3 185 PC.printf("ID = ");
dalbanf 9:cfa9a379dbd3 186 int pos = 0;
dalbanf 9:cfa9a379dbd3 187 char hexbuf[2];
dalbanf 9:cfa9a379dbd3 188 for (int i = 0; i < 256; i++) bufTagID[i] = '\0';
dalbanf 9:cfa9a379dbd3 189 for (uint8_t i = 0; i < Antenna.uid.size; i++) {
dalbanf 9:cfa9a379dbd3 190 sprintf(hexbuf, "%02x", Antenna.uid.uidByte[i]);
dalbanf 9:cfa9a379dbd3 191 PC.putc(hexbuf[0]);
dalbanf 9:cfa9a379dbd3 192 PC.putc(hexbuf[1]);
dalbanf 9:cfa9a379dbd3 193 bufTagID[pos] = hexbuf[0];
dalbanf 9:cfa9a379dbd3 194 pos++;
dalbanf 9:cfa9a379dbd3 195 bufTagID[pos] = hexbuf[1];
dalbanf 9:cfa9a379dbd3 196 pos++;
dalbanf 9:cfa9a379dbd3 197 }
dalbanf 9:cfa9a379dbd3 198 PC.printf("\r\n");
dalbanf 9:cfa9a379dbd3 199
dalbanf 9:cfa9a379dbd3 200 return true;
dalbanf 0:8814d243dfec 201 }
dalbanf 0:8814d243dfec 202
dalbanf 0:8814d243dfec 203 //APCDGetWappAlarm: function to check from APCD mailbox if a wapp alarm was sent by automation
dalbanf 0:8814d243dfec 204 // return 1 in case of alarm
dalbanf 0:8814d243dfec 205 // return 2 if apcd.connect failed
dalbanf 0:8814d243dfec 206 // return 0 if there is no alarm
dalbanf 0:8814d243dfec 207 int APCDGetWappAlarm(){
dalbanf 0:8814d243dfec 208 int result = 2;
dalbanf 0:8814d243dfec 209 int rtrn = 2;
dalbanf 0:8814d243dfec 210 int attempt = 0;
dalbanf 0:8814d243dfec 211
dalbanf 0:8814d243dfec 212 //Open a TCP socket
dalbanf 0:8814d243dfec 213 rtrn = apcd.open(&eth);
dalbanf 0:8814d243dfec 214 PC.printf("\r\nAPCDGetWappAlarm:\r\n");
dalbanf 0:8814d243dfec 215 PC.printf("\tOpen socket -> %d\r\n", rtrn);
dalbanf 0:8814d243dfec 216 blue = 1;
dalbanf 0:8814d243dfec 217
dalbanf 0:8814d243dfec 218 //Try APCD connection
dalbanf 0:8814d243dfec 219 rtrn = apcd.connect(SERVER, PORT);
dalbanf 0:8814d243dfec 220 while (rtrn < 0 && attempt < SOCKETCONNECTION_ATTEMPT){
dalbanf 0:8814d243dfec 221 wait_ms(500);
dalbanf 0:8814d243dfec 222 rtrn = apcd.connect(SERVER, PORT);
dalbanf 0:8814d243dfec 223 attempt++;
dalbanf 0:8814d243dfec 224 }
dalbanf 0:8814d243dfec 225 PC.printf("\tConnection -> %d\r\n", rtrn);
dalbanf 0:8814d243dfec 226
dalbanf 0:8814d243dfec 227 if (rtrn == 0){ //Automation server connection successfully
dalbanf 0:8814d243dfec 228 APCDsendData(MBX_OPEN); //Open the APCD bus
dalbanf 0:8814d243dfec 229 APCDgetData();
dalbanf 0:8814d243dfec 230
dalbanf 0:8814d243dfec 231 //Get message's number in the mailbox
dalbanf 0:8814d243dfec 232 PC.printf("\tAPCD -> %s\r\n", MBX_COUNT);
dalbanf 0:8814d243dfec 233 APCDsendData(MBX_COUNT);
dalbanf 0:8814d243dfec 234
dalbanf 0:8814d243dfec 235 //Extract the data from the string received
dalbanf 0:8814d243dfec 236 char buf[256];
dalbanf 0:8814d243dfec 237 sprintf(buf,"%s", APCDgetData());
dalbanf 0:8814d243dfec 238 unsigned short int answerLen = strlen(buf);
dalbanf 0:8814d243dfec 239 PC.printf("\tAnswer -> %s\r\n", buf);
dalbanf 0:8814d243dfec 240 if (answerLen > 10){ //Answer will be MBX_COUNT,xx where xx is the number of message
dalbanf 0:8814d243dfec 241 int nbMsg = 0;
dalbanf 0:8814d243dfec 242 char nb[10];
dalbanf 0:8814d243dfec 243 int pos = 10;
dalbanf 0:8814d243dfec 244 while (buf[pos] != '\0' && pos < 20){
dalbanf 0:8814d243dfec 245 nb[pos-10] = buf[pos];
dalbanf 0:8814d243dfec 246 pos++;
dalbanf 0:8814d243dfec 247 }
dalbanf 0:8814d243dfec 248 nb[pos-10] = '\0';
dalbanf 0:8814d243dfec 249 nbMsg = atoi(nb);
dalbanf 0:8814d243dfec 250 PC.printf("\tMsg count -> %d\r\n", nbMsg);
dalbanf 0:8814d243dfec 251 //If there is a message or more, get all message (=> mailbox will be empty)
dalbanf 0:8814d243dfec 252 if (nbMsg > 0){
dalbanf 0:8814d243dfec 253 result = 1;
dalbanf 0:8814d243dfec 254 while (nbMsg > 0){ //Get all VFEI message in the mailbox
dalbanf 0:8814d243dfec 255 PC.printf("\tAPCD -> %s\r\n", MBX_GET);
dalbanf 0:8814d243dfec 256 APCDsendData(MBX_GET);
dalbanf 0:8814d243dfec 257 APCDgetData();
dalbanf 0:8814d243dfec 258 nbMsg--;
dalbanf 0:8814d243dfec 259 }
dalbanf 0:8814d243dfec 260 }
dalbanf 0:8814d243dfec 261 else result = 0;
dalbanf 0:8814d243dfec 262 }
dalbanf 0:8814d243dfec 263
dalbanf 0:8814d243dfec 264 //Close the APCD bus
dalbanf 0:8814d243dfec 265 APCDsendData(MBX_CLOSE);
dalbanf 0:8814d243dfec 266 PC.printf("\tAPCD -> %s\r\n", MBX_CLOSE);
dalbanf 0:8814d243dfec 267 APCDgetData();
dalbanf 0:8814d243dfec 268 }
dalbanf 0:8814d243dfec 269 else result = 2;
dalbanf 0:8814d243dfec 270
dalbanf 0:8814d243dfec 271 apcd.close();
dalbanf 0:8814d243dfec 272 blue = 0;
dalbanf 0:8814d243dfec 273 return result;
dalbanf 0:8814d243dfec 274 }
dalbanf 0:8814d243dfec 275
dalbanf 0:8814d243dfec 276 //APCDSendTagID: function to send the VFEI message with TagID to the automation
dalbanf 0:8814d243dfec 277 // return 1 if data sent correctly
dalbanf 0:8814d243dfec 278 // return 2 if apcd.connect failed
dalbanf 0:8814d243dfec 279 // return 0 if sent data lenght doesn't match with message lenght
dalbanf 0:8814d243dfec 280 int APCDSendTagID(){
dalbanf 0:8814d243dfec 281 int attempt = 0;
dalbanf 0:8814d243dfec 282 int result = 2;
dalbanf 0:8814d243dfec 283 int rtrn = 2;
dalbanf 0:8814d243dfec 284 PC.printf("\r\nAPCDSendTagID:\r\n");
dalbanf 0:8814d243dfec 285
dalbanf 0:8814d243dfec 286 //Build VFEI message
dalbanf 0:8814d243dfec 287 char concatBuffer[256];
dalbanf 0:8814d243dfec 288 sprintf(concatBuffer, "%s%s%s\0", MBX_PUT_BEGIN, bufTagID, MBX_PUT_END);
dalbanf 0:8814d243dfec 289 PC.printf("\tVFEI message : %s\r\n", concatBuffer);
dalbanf 0:8814d243dfec 290
dalbanf 0:8814d243dfec 291 //Open a TCP socket
dalbanf 0:8814d243dfec 292 rtrn = apcd.open(&eth);
dalbanf 0:8814d243dfec 293 PC.printf("\tOpen socket -> %d\r\n", rtrn);
dalbanf 0:8814d243dfec 294 blue = 1;
dalbanf 0:8814d243dfec 295
dalbanf 0:8814d243dfec 296 //Try APCD connection
dalbanf 0:8814d243dfec 297 rtrn = apcd.connect(SERVER, PORT);
dalbanf 0:8814d243dfec 298 while (rtrn < 0 && attempt < SOCKETCONNECTION_ATTEMPT){
dalbanf 0:8814d243dfec 299 wait_ms(500);
dalbanf 0:8814d243dfec 300 rtrn = apcd.connect(SERVER, PORT);
dalbanf 0:8814d243dfec 301 attempt++;
dalbanf 0:8814d243dfec 302 }
dalbanf 0:8814d243dfec 303 PC.printf("\tConnection -> %d\r\n", rtrn);
dalbanf 0:8814d243dfec 304
dalbanf 0:8814d243dfec 305 //Send VFEI message if possible
dalbanf 0:8814d243dfec 306 if (rtrn == 0){
dalbanf 0:8814d243dfec 307 PC.printf("\tAPCD -> %s\r\n", MBX_OPEN);
dalbanf 0:8814d243dfec 308 APCDsendData(MBX_OPEN); //Open the APCD bus
dalbanf 0:8814d243dfec 309 APCDgetData();
dalbanf 0:8814d243dfec 310 rtrn = APCDsendData(concatBuffer); //Send the VFEI message
dalbanf 0:8814d243dfec 311 APCDgetData();
dalbanf 0:8814d243dfec 312 PC.printf("\tAPCD -> %s\r\n", MBX_CLOSE);
dalbanf 0:8814d243dfec 313 APCDsendData(MBX_CLOSE); //Close the APCD bus
dalbanf 0:8814d243dfec 314 APCDgetData();
dalbanf 0:8814d243dfec 315 result = 1;
dalbanf 0:8814d243dfec 316 }
dalbanf 0:8814d243dfec 317 else result = 2;
dalbanf 0:8814d243dfec 318
dalbanf 0:8814d243dfec 319 apcd.close();
dalbanf 0:8814d243dfec 320 blue = 0;
dalbanf 0:8814d243dfec 321 return result;
dalbanf 0:8814d243dfec 322 }
dalbanf 0:8814d243dfec 323
dalbanf 0:8814d243dfec 324 //APCDsendData: function to send data to APCD bus
dalbanf 0:8814d243dfec 325 // Return 1 if data sent correctly, 0 in other case
dalbanf 0:8814d243dfec 326 // Author: F. Frezot from Arduino code
dalbanf 0:8814d243dfec 327 int APCDsendData (char *tcpCmd){
dalbanf 0:8814d243dfec 328 unsigned short int dataLength = strlen(tcpCmd);
dalbanf 0:8814d243dfec 329 unsigned char len[2];
dalbanf 0:8814d243dfec 330
dalbanf 0:8814d243dfec 331 len[0] = ((dataLength>>8)&0xFF); //MSB
dalbanf 0:8814d243dfec 332 len[1] = ((dataLength>>0)&0xFF); //LSB
dalbanf 0:8814d243dfec 333
dalbanf 0:8814d243dfec 334 int rtrnLenght = apcd.send(len, sizeof(len));
dalbanf 0:8814d243dfec 335 rtrnLenght = apcd.send(tcpCmd, dataLength);
dalbanf 0:8814d243dfec 336 if (rtrnLenght == dataLength) return 1;
dalbanf 0:8814d243dfec 337 else return 0;
dalbanf 0:8814d243dfec 338 }
dalbanf 0:8814d243dfec 339
dalbanf 0:8814d243dfec 340
dalbanf 0:8814d243dfec 341 //APCDgetData: function to read data from APCD bus
dalbanf 0:8814d243dfec 342 // Return a char array with data read
dalbanf 0:8814d243dfec 343 // Author: F. Frezot from Arduino code
dalbanf 0:8814d243dfec 344 char *APCDgetData (){
dalbanf 0:8814d243dfec 345 char MSB[1];
dalbanf 0:8814d243dfec 346 char LSB[1];
dalbanf 0:8814d243dfec 347 unsigned short int dataLength;
dalbanf 0:8814d243dfec 348 char readBuf[256] = "\0";
dalbanf 0:8814d243dfec 349
dalbanf 0:8814d243dfec 350 wait_ms(APCD_READ_DELAY);
dalbanf 0:8814d243dfec 351
dalbanf 0:8814d243dfec 352 apcd.recv(MSB, 1);
dalbanf 0:8814d243dfec 353 apcd.recv(LSB, 1);
dalbanf 0:8814d243dfec 354 dataLength = ((MSB[0]<<8)&0xFF00)|(LSB[0]<<0)&0x00FF;
dalbanf 0:8814d243dfec 355 apcd.recv(readBuf, dataLength);
dalbanf 0:8814d243dfec 356
dalbanf 0:8814d243dfec 357 return (readBuf);
dalbanf 0:8814d243dfec 358 }
dalbanf 0:8814d243dfec 359
dalbanf 0:8814d243dfec 360 int main() {
dalbanf 0:8814d243dfec 361 // Init Serial and nucleo board LED (off)
dalbanf 0:8814d243dfec 362 PC.baud(9600);
dalbanf 0:8814d243dfec 363 PC.printf("Running ...\r\n");
dalbanf 0:8814d243dfec 364 green = 0;
dalbanf 0:8814d243dfec 365 red = 0;
dalbanf 0:8814d243dfec 366 blue = 0;
dalbanf 0:8814d243dfec 367
dalbanf 0:8814d243dfec 368 // Init RC522 Chip
dalbanf 0:8814d243dfec 369 PC.printf("\tInit RFID...");
dalbanf 0:8814d243dfec 370 wait_ms(500);
dalbanf 0:8814d243dfec 371 Antenna.PCD_Init();
dalbanf 0:8814d243dfec 372 wait_ms(100);
dalbanf 0:8814d243dfec 373 Antenna.PCD_AntennaOff(); //Turn off the RFID antenna.
dalbanf 0:8814d243dfec 374 TagReadLED = LED_OFF; //Turn off the LED who indicate if a TAG was read
dalbanf 0:8814d243dfec 375 PC.printf("DONE\r\n");
dalbanf 0:8814d243dfec 376
dalbanf 1:24db8b26b816 377 //PC.printf("\tClose relay for prober Interlock\r\n");
dalbanf 1:24db8b26b816 378 //Interlock = RELAY_CLOSED; //Close relay for prober interlock
dalbanf 0:8814d243dfec 379
dalbanf 0:8814d243dfec 380 //Ethernet Init
dalbanf 0:8814d243dfec 381 PC.printf("\tInit Ethernet...");
dalbanf 0:8814d243dfec 382 eth.set_network(IP, MASK, GATEWAY);
dalbanf 0:8814d243dfec 383 int rtrn = eth.connect();
dalbanf 0:8814d243dfec 384 if (rtrn == 0){
dalbanf 0:8814d243dfec 385 PC.printf("DONE\r\n");
dalbanf 0:8814d243dfec 386 const char *ip = eth.get_ip_address();
dalbanf 0:8814d243dfec 387 const char *mac = eth.get_mac_address();
dalbanf 0:8814d243dfec 388 PC.printf("\tIP address = %s\n\r", ip ? ip : "No IP");
dalbanf 0:8814d243dfec 389 PC.printf("\tMAC address = %s\n\r", mac ? mac : "No MAC");
dalbanf 0:8814d243dfec 390 PC.printf("Running DONE\r\n\r\n");
dalbanf 0:8814d243dfec 391 }
dalbanf 0:8814d243dfec 392 else{
dalbanf 0:8814d243dfec 393 PC.printf("FAIL -> Check if Network cable is plug-in then reset the board\r\n");
dalbanf 0:8814d243dfec 394 red = 0;
dalbanf 0:8814d243dfec 395 green = 0;
dalbanf 0:8814d243dfec 396 PC.printf("Running FAIL -> Program aborted\r\n\r\n");
dalbanf 0:8814d243dfec 397 while (true){
dalbanf 0:8814d243dfec 398 blue = !blue;
dalbanf 0:8814d243dfec 399 TagReadLED = !TagReadLED;
dalbanf 0:8814d243dfec 400 wait_ms(200);
dalbanf 0:8814d243dfec 401 }
dalbanf 0:8814d243dfec 402 }
dalbanf 0:8814d243dfec 403
dalbanf 0:8814d243dfec 404 //Waiting Tag and WAPP alarm
dalbanf 0:8814d243dfec 405 int count = 0;
dalbanf 0:8814d243dfec 406 while (true) {
dalbanf 0:8814d243dfec 407 //Check if Tag read
dalbanf 0:8814d243dfec 408 if (WappExchangeSwitch == 1){
dalbanf 0:8814d243dfec 409 //Turn on the antenna
dalbanf 0:8814d243dfec 410 Antenna.PCD_AntennaOn();
dalbanf 0:8814d243dfec 411 wait_ms(100);
dalbanf 0:8814d243dfec 412
dalbanf 0:8814d243dfec 413 //Waiting Tag ID
dalbanf 0:8814d243dfec 414 if (IsTagPresent()){
dalbanf 0:8814d243dfec 415 green = 0;
dalbanf 0:8814d243dfec 416 int rtrn = APCDSendTagID();
dalbanf 0:8814d243dfec 417 if (rtrn == 1){
dalbanf 0:8814d243dfec 418 PC.printf("\tTag ID sent correctly (Code=%d)\r\n", rtrn);
dalbanf 0:8814d243dfec 419 red = 0;
dalbanf 0:8814d243dfec 420 //Blink Tag LED during 3s
dalbanf 0:8814d243dfec 421 for (int i = 0; i < 10; i++){
dalbanf 0:8814d243dfec 422 TagReadLED = !TagReadLED;
dalbanf 0:8814d243dfec 423 wait_ms(500);
dalbanf 0:8814d243dfec 424 }
dalbanf 0:8814d243dfec 425 //Tag LED continuous ON during 1s
dalbanf 0:8814d243dfec 426 TagReadLED = LED_ON;
dalbanf 0:8814d243dfec 427 wait_ms(2000);
dalbanf 0:8814d243dfec 428
dalbanf 0:8814d243dfec 429 //Turn off Tag LED
dalbanf 0:8814d243dfec 430 TagReadLED = LED_OFF;
dalbanf 0:8814d243dfec 431 }
dalbanf 0:8814d243dfec 432 else {
dalbanf 0:8814d243dfec 433 PC.printf("\tWARNING: Fail to send Tag ID (Code=%d)\r\n", rtrn);
dalbanf 0:8814d243dfec 434 red = 1;
dalbanf 0:8814d243dfec 435 }
dalbanf 0:8814d243dfec 436 }
dalbanf 0:8814d243dfec 437 Antenna.PCD_AntennaOff();
dalbanf 0:8814d243dfec 438 }
dalbanf 0:8814d243dfec 439
dalbanf 4:caf61234018c 440 //Check alarm each 4s
dalbanf 4:caf61234018c 441 if (count >= 8){ // Count value = each xx seconds * 2. Example: Count >= 4 for check alarm each 2s
dalbanf 0:8814d243dfec 442 green = 0;
dalbanf 0:8814d243dfec 443 count = -1;
dalbanf 0:8814d243dfec 444 int rtrn = APCDGetWappAlarm();
dalbanf 0:8814d243dfec 445 if (rtrn == 1){
dalbanf 0:8814d243dfec 446 red = 0;
dalbanf 0:8814d243dfec 447 PC.printf("Wapp alarm received -> Open interlock relay during 1s\r\n");
dalbanf 0:8814d243dfec 448 wait_ms(INTERLOCK_DELAY);
dalbanf 0:8814d243dfec 449 Interlock = RELAY_OPEN; //Activate the relay on prober interlock
dalbanf 0:8814d243dfec 450 wait_ms(1000);
dalbanf 0:8814d243dfec 451 Interlock = RELAY_CLOSED;
dalbanf 0:8814d243dfec 452 PC.printf(" -> Close interlock relay\r\n");
dalbanf 0:8814d243dfec 453 }
dalbanf 0:8814d243dfec 454 else if (rtrn == 2){
dalbanf 0:8814d243dfec 455 PC.printf("WARNING: Fail to check if alarm is present (Code=%d)\r\n", rtrn);
dalbanf 0:8814d243dfec 456 red = 1;
dalbanf 0:8814d243dfec 457 }
dalbanf 0:8814d243dfec 458 else if (rtrn == 0){
dalbanf 0:8814d243dfec 459 red = 0;
dalbanf 0:8814d243dfec 460 }
dalbanf 0:8814d243dfec 461 }
dalbanf 0:8814d243dfec 462
dalbanf 0:8814d243dfec 463 green = !green;
dalbanf 0:8814d243dfec 464 count++;
dalbanf 0:8814d243dfec 465 wait_ms(500);
dalbanf 0:8814d243dfec 466 }
dalbanf 0:8814d243dfec 467 }